Skip to content

Commit e89694c

Browse files
committed
Extract common container and gui code.
Fix some parts of the GUIs being off-center. Also fix minor desync on craft and reduce packet spam.
1 parent d1e2219 commit e89694c

18 files changed

Lines changed: 268 additions & 461 deletions
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.blakebr0.extendedcrafting.client.container;
2+
3+
import com.blakebr0.extendedcrafting.crafting.table.TableCraftResult;
4+
import com.blakebr0.extendedcrafting.crafting.table.TableCrafting;
5+
import com.blakebr0.extendedcrafting.crafting.table.TableRecipeManager;
6+
import com.blakebr0.extendedcrafting.crafting.table.TableResultHandler;
7+
import com.blakebr0.extendedcrafting.tile.AbstractExtendedTable;
8+
import net.minecraft.entity.player.EntityPlayer;
9+
import net.minecraft.entity.player.InventoryPlayer;
10+
import net.minecraft.inventory.Container;
11+
import net.minecraft.inventory.IInventory;
12+
import net.minecraft.inventory.InventoryCrafting;
13+
import net.minecraft.inventory.Slot;
14+
import net.minecraft.item.ItemStack;
15+
16+
public abstract class AbstractTableContainer extends Container {
17+
public static final int SLOT_SIZE = 18;
18+
public static final int PLAYER_INV_WIDTH = 9;
19+
public static final int PLAYER_INV_ROWS = 3;
20+
public static final int GRID_START_Y = 18;
21+
public final InventoryCrafting matrix;
22+
public final IInventory result;
23+
public final AbstractExtendedTable tile;
24+
25+
public AbstractTableContainer(InventoryPlayer player, AbstractExtendedTable tile, int guiWidth, int guiHeight, int gridStartX, int outputSlotX) {
26+
this.matrix = new TableCrafting(this, tile);
27+
this.result = new TableCraftResult(tile);
28+
this.tile = tile;
29+
int lineSize = tile.getLineSize();
30+
31+
int outputSlotY = GRID_START_Y + ((lineSize - 1) * SLOT_SIZE) / 2;
32+
this.addSlotToContainer(new TableResultHandler(this.matrix, this.result, tile.getWorld(), 0, outputSlotX, outputSlotY));
33+
34+
int y;
35+
int x;
36+
for (y = 0; y < lineSize; y++) {
37+
for (x = 0; x < lineSize; x++) {
38+
this.addSlotToContainer(new Slot(this.matrix, x + y * lineSize, gridStartX + x * SLOT_SIZE, GRID_START_Y + y * SLOT_SIZE));
39+
}
40+
}
41+
42+
// player inventory
43+
44+
int hotbarStartY = guiHeight - SLOT_SIZE - 6;
45+
int playerInvStartX = getPlayerInvStartX(guiWidth);
46+
int playerInvStartY = hotbarStartY - SLOT_SIZE * PLAYER_INV_ROWS - 4;
47+
48+
for (y = 0; y < PLAYER_INV_ROWS; y++) {
49+
for (x = 0; x < PLAYER_INV_WIDTH; x++) {
50+
this.addSlotToContainer(new Slot(player, x + (y + 1) * PLAYER_INV_WIDTH, playerInvStartX + x * SLOT_SIZE, playerInvStartY + y * SLOT_SIZE));
51+
}
52+
}
53+
54+
for (x = 0; x < PLAYER_INV_WIDTH; x++) {
55+
this.addSlotToContainer(new Slot(player, x, playerInvStartX + x * SLOT_SIZE, hotbarStartY));
56+
}
57+
}
58+
59+
public static int getPlayerInvStartX(int guiWidth) {
60+
return (guiWidth - PLAYER_INV_WIDTH * SLOT_SIZE) / 2 + 1;
61+
}
62+
63+
@Override
64+
public void onCraftMatrixChanged(IInventory matrix) {
65+
this.result.setInventorySlotContents(0, TableRecipeManager.getInstance().findMatchingRecipe(this.matrix, this.tile.getWorld()));
66+
}
67+
68+
@Override
69+
public boolean canInteractWith(EntityPlayer player) {
70+
return this.tile.isUsableByPlayer(player);
71+
}
72+
73+
@Override
74+
public ItemStack transferStackInSlot(EntityPlayer player, int slotNumber) {
75+
ItemStack itemstack = ItemStack.EMPTY;
76+
Slot slot = this.inventorySlots.get(slotNumber);
77+
78+
int size = tile.getSizeInventory();
79+
80+
if (slot != null && slot.getHasStack()) {
81+
ItemStack itemstack1 = slot.getStack();
82+
itemstack = itemstack1.copy();
83+
84+
int gridEnd = size + 1;
85+
int maxSlots = inventorySlots.size();
86+
if (slotNumber == 0) {
87+
if (!this.mergeItemStack(itemstack1, gridEnd, maxSlots, true)) {
88+
return ItemStack.EMPTY;
89+
}
90+
91+
slot.onSlotChange(itemstack1, itemstack);
92+
} else if (slotNumber >= gridEnd && slotNumber < maxSlots) {
93+
if (!this.mergeItemStack(itemstack1, 1, gridEnd, false)) {
94+
return ItemStack.EMPTY;
95+
}
96+
} else if (!this.mergeItemStack(itemstack1, gridEnd, maxSlots, false)) {
97+
return ItemStack.EMPTY;
98+
}
99+
100+
if (itemstack1.isEmpty()) {
101+
slot.putStack(ItemStack.EMPTY);
102+
} else {
103+
slot.onSlotChanged();
104+
}
105+
106+
if (itemstack1.getCount() == itemstack.getCount()) {
107+
return ItemStack.EMPTY;
108+
}
109+
110+
slot.onTake(player, itemstack1);
111+
this.onCraftMatrixChanged(this.matrix);
112+
}
113+
114+
return itemstack;
115+
}
116+
117+
}
Lines changed: 11 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,22 @@
11
package com.blakebr0.extendedcrafting.client.container;
22

3-
import com.blakebr0.extendedcrafting.crafting.table.TableCraftResult;
4-
import com.blakebr0.extendedcrafting.crafting.table.TableCrafting;
5-
import com.blakebr0.extendedcrafting.crafting.table.TableRecipeManager;
6-
import com.blakebr0.extendedcrafting.crafting.table.TableResultHandler;
73
import com.blakebr0.extendedcrafting.tile.TileAdvancedCraftingTable;
8-
import net.minecraft.entity.player.EntityPlayer;
94
import net.minecraft.entity.player.InventoryPlayer;
10-
import net.minecraft.inventory.Container;
11-
import net.minecraft.inventory.IInventory;
12-
import net.minecraft.inventory.InventoryCrafting;
13-
import net.minecraft.inventory.Slot;
14-
import net.minecraft.item.ItemStack;
155

16-
public class ContainerAdvancedTable extends Container {
6+
public class ContainerAdvancedTable extends AbstractTableContainer {
177

18-
public final InventoryCrafting matrix;
19-
public final IInventory result;
20-
public final TileAdvancedCraftingTable tile;
8+
public static final int X_SIZE = 176;
9+
public static final int Y_SIZE = 206;
10+
public static final int GRID_START_X = 14;
11+
public static final int OUTPUT_SLOT_X = 142;
2112

2213
public ContainerAdvancedTable(InventoryPlayer player, TileAdvancedCraftingTable tile) {
23-
this.tile = tile;
24-
this.matrix = new TableCrafting(this, tile);
25-
this.result = new TableCraftResult(tile);
26-
27-
this.addSlotToContainer(new TableResultHandler(this.matrix, this.result, tile.getWorld(), 0, 142, 53));
28-
29-
int wy, ex;
30-
for (wy = 0; wy < 5; wy++) {
31-
for (ex = 0; ex < 5; ex++) {
32-
this.addSlotToContainer(new Slot(this.matrix, ex + wy * 5, 14 + ex * 18, 18 + wy * 18));
33-
}
34-
}
35-
36-
for (wy = 0; wy < 3; wy++) {
37-
for (ex = 0; ex < 9; ex++) {
38-
this.addSlotToContainer(new Slot(player, ex + wy * 9 + 9, 8 + ex * 18, 124 + wy * 18));
39-
}
40-
}
41-
42-
for (ex = 0; ex < 9; ex++) {
43-
this.addSlotToContainer(new Slot(player, ex, 8 + ex * 18, 182));
44-
}
45-
}
46-
47-
@Override
48-
public void onCraftMatrixChanged(IInventory matrix) {
49-
this.result.setInventorySlotContents(0, TableRecipeManager.getInstance().findMatchingRecipe(this.matrix, this.tile.getWorld()));
50-
}
51-
52-
@Override
53-
public boolean canInteractWith(EntityPlayer player) {
54-
return this.tile.isUsableByPlayer(player);
14+
super(player,
15+
tile,
16+
X_SIZE,
17+
Y_SIZE,
18+
GRID_START_X,
19+
OUTPUT_SLOT_X);
5520
}
5621

57-
@Override
58-
public ItemStack transferStackInSlot(EntityPlayer player, int slotNumber) {
59-
ItemStack itemstack = ItemStack.EMPTY;
60-
Slot slot = this.inventorySlots.get(slotNumber);
61-
62-
if (slot != null && slot.getHasStack()) {
63-
ItemStack itemstack1 = slot.getStack();
64-
itemstack = itemstack1.copy();
65-
66-
if (slotNumber == 0) {
67-
if (!this.mergeItemStack(itemstack1, 26, 62, true)) {
68-
return ItemStack.EMPTY;
69-
}
70-
71-
slot.onSlotChange(itemstack1, itemstack);
72-
} else if (slotNumber >= 26 && slotNumber < 62) {
73-
if (!this.mergeItemStack(itemstack1, 1, 26, false)) {
74-
return ItemStack.EMPTY;
75-
}
76-
} else if (!this.mergeItemStack(itemstack1, 26, 62, false)) {
77-
return ItemStack.EMPTY;
78-
}
79-
80-
if (itemstack1.isEmpty()) {
81-
slot.putStack(ItemStack.EMPTY);
82-
} else {
83-
slot.onSlotChanged();
84-
}
85-
86-
if (itemstack1.getCount() == itemstack.getCount()) {
87-
return ItemStack.EMPTY;
88-
}
89-
90-
slot.onTake(player, itemstack1);
91-
this.onCraftMatrixChanged(this.matrix);
92-
}
93-
94-
return itemstack;
95-
}
9622
}
Lines changed: 11 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,22 @@
11
package com.blakebr0.extendedcrafting.client.container;
22

3-
import com.blakebr0.extendedcrafting.crafting.table.TableCraftResult;
4-
import com.blakebr0.extendedcrafting.crafting.table.TableCrafting;
5-
import com.blakebr0.extendedcrafting.crafting.table.TableRecipeManager;
6-
import com.blakebr0.extendedcrafting.crafting.table.TableResultHandler;
73
import com.blakebr0.extendedcrafting.tile.TileBasicCraftingTable;
8-
import net.minecraft.entity.player.EntityPlayer;
94
import net.minecraft.entity.player.InventoryPlayer;
10-
import net.minecraft.inventory.Container;
11-
import net.minecraft.inventory.IInventory;
12-
import net.minecraft.inventory.InventoryCrafting;
13-
import net.minecraft.inventory.Slot;
14-
import net.minecraft.item.ItemStack;
155

16-
public class ContainerBasicTable extends Container {
6+
public class ContainerBasicTable extends AbstractTableContainer {
177

18-
public final InventoryCrafting matrix;
19-
public final IInventory result;
20-
public final TileBasicCraftingTable tile;
8+
public static final int X_SIZE = 176;
9+
public static final int Y_SIZE = 170;
10+
public static final int GRID_START_X = 32;
11+
public static final int OUTPUT_SLOT_X = 124;
2112

2213
public ContainerBasicTable(InventoryPlayer player, TileBasicCraftingTable tile) {
23-
this.tile = tile;
24-
this.matrix = new TableCrafting(this, tile);
25-
this.result = new TableCraftResult(tile);
26-
27-
this.addSlotToContainer(new TableResultHandler(this.matrix, this.result, tile.getWorld(), 0, 124, 36));
28-
29-
int wy, ex;
30-
for (wy = 0; wy < 3; wy++) {
31-
for (ex = 0; ex < 3; ex++) {
32-
this.addSlotToContainer(new Slot(this.matrix, ex + wy * 3, 32 + ex * 18, 18 + wy * 18));
33-
}
34-
}
35-
36-
for (wy = 0; wy < 3; wy++) {
37-
for (ex = 0; ex < 9; ex++) {
38-
this.addSlotToContainer(new Slot(player, ex + wy * 9 + 9, 8 + ex * 18, 88 + wy * 18));
39-
}
40-
}
41-
42-
for (ex = 0; ex < 9; ex++) {
43-
this.addSlotToContainer(new Slot(player, ex, 8 + ex * 18, 146));
44-
}
45-
}
46-
47-
@Override
48-
public void onCraftMatrixChanged(IInventory matrix) {
49-
this.result.setInventorySlotContents(0, TableRecipeManager.getInstance().findMatchingRecipe(this.matrix, this.tile.getWorld()));
14+
super(player,
15+
tile,
16+
X_SIZE,
17+
Y_SIZE,
18+
GRID_START_X,
19+
OUTPUT_SLOT_X);
5020
}
5121

52-
@Override
53-
public boolean canInteractWith(EntityPlayer player) {
54-
return this.tile.isUsableByPlayer(player);
55-
}
56-
57-
@Override
58-
public ItemStack transferStackInSlot(EntityPlayer player, int slotNumber) {
59-
ItemStack itemstack = ItemStack.EMPTY;
60-
Slot slot = this.inventorySlots.get(slotNumber);
61-
62-
if (slot != null && slot.getHasStack()) {
63-
ItemStack itemstack1 = slot.getStack();
64-
itemstack = itemstack1.copy();
65-
66-
if (slotNumber == 0) {
67-
if (!this.mergeItemStack(itemstack1, 10, 46, true)) {
68-
return ItemStack.EMPTY;
69-
}
70-
71-
slot.onSlotChange(itemstack1, itemstack);
72-
} else if (slotNumber >= 10 && slotNumber < 46) {
73-
if (!this.mergeItemStack(itemstack1, 1, 10, false)) {
74-
return ItemStack.EMPTY;
75-
}
76-
} else if (!this.mergeItemStack(itemstack1, 10, 46, false)) {
77-
return ItemStack.EMPTY;
78-
}
79-
80-
if (itemstack1.isEmpty()) {
81-
slot.putStack(ItemStack.EMPTY);
82-
} else {
83-
slot.onSlotChanged();
84-
}
85-
86-
if (itemstack1.getCount() == itemstack.getCount()) {
87-
return ItemStack.EMPTY;
88-
}
89-
90-
slot.onTake(player, itemstack1);
91-
this.onCraftMatrixChanged(this.matrix);
92-
}
93-
94-
return itemstack;
95-
}
9622
}

0 commit comments

Comments
 (0)