make crafting grid work

This commit is contained in:
Raoul Van den Berge
2015-12-22 16:01:52 +01:00
parent f502595758
commit 1dfe250ddc
9 changed files with 170 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
package storagecraft.container; package storagecraft.container;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container; import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot; import net.minecraft.inventory.Slot;
@@ -10,6 +12,8 @@ public abstract class ContainerBase extends Container
{ {
private EntityPlayer player; private EntityPlayer player;
private List<Slot> playerInventorySlots = new ArrayList<Slot>();
public ContainerBase(EntityPlayer player) public ContainerBase(EntityPlayer player)
{ {
this.player = player; this.player = player;
@@ -26,7 +30,11 @@ public abstract class ContainerBase extends Container
for (int i = 0; i < 9; i++) for (int i = 0; i < 9; i++)
{ {
addSlotToContainer(new Slot(player.inventory, id, xInventory + i * 18, yInventory + 4 + (3 * 18))); Slot slot = new Slot(player.inventory, id, xInventory + i * 18, yInventory + 4 + (3 * 18));
playerInventorySlots.add(slot);
addSlotToContainer(slot);
id++; id++;
} }
@@ -35,7 +43,11 @@ public abstract class ContainerBase extends Container
{ {
for (int x = 0; x < 9; x++) for (int x = 0; x < 9; x++)
{ {
addSlotToContainer(new Slot(player.inventory, id, xInventory + x * 18, yInventory + y * 18)); Slot slot = new Slot(player.inventory, id, xInventory + x * 18, yInventory + y * 18);
playerInventorySlots.add(slot);
addSlotToContainer(slot);
id++; id++;
} }
@@ -75,4 +87,9 @@ public abstract class ContainerBase extends Container
{ {
return true; return true;
} }
public List<Slot> getPlayerInventorySlots()
{
return playerInventorySlots;
}
} }

View File

@@ -11,6 +11,8 @@ public class ContainerGrid extends ContainerBase
{ {
super(player); super(player);
addPlayerInventory(8, grid.isCrafting() ? 174 : 108);
if (grid.isCrafting()) if (grid.isCrafting())
{ {
int x = 44; int x = 44;
@@ -29,9 +31,7 @@ public class ContainerGrid extends ContainerBase
} }
} }
addSlotToContainer(new SlotGridCraftingResult(player, grid.getCraftingMatrix(), grid.getCraftingResult(), 0, 125, 124)); addSlotToContainer(new SlotGridCraftingResult(player, grid.getCraftingMatrix(), grid.getCraftingResult(), grid, 0, 125, 124));
} }
addPlayerInventory(8, grid.isCrafting() ? 174 : 108);
} }
} }

View File

@@ -3,11 +3,37 @@ package storagecraft.container.slot;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.SlotCrafting; import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import storagecraft.tile.TileGrid;
public class SlotGridCraftingResult extends SlotCrafting public class SlotGridCraftingResult extends SlotCrafting
{ {
public SlotGridCraftingResult(EntityPlayer player, IInventory craftingMatrix, IInventory craftingResult, int id, int x, int y) private IInventory craftingMatrix;
private TileGrid grid;
public SlotGridCraftingResult(EntityPlayer player, IInventory craftingMatrix, IInventory craftingResult, TileGrid grid, int id, int x, int y)
{ {
super(player, craftingMatrix, craftingResult, id, x, y); super(player, craftingMatrix, craftingResult, id, x, y);
this.craftingMatrix = craftingMatrix;
this.grid = grid;
}
@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack stack)
{
ItemStack[] matrixSlots = new ItemStack[craftingMatrix.getSizeInventory()];
for (int i = 0; i < craftingMatrix.getSizeInventory(); ++i)
{
if (craftingMatrix.getStackInSlot(i) != null)
{
matrixSlots[i] = craftingMatrix.getStackInSlot(i).copy();
}
}
super.onPickupFromSlot(player, stack);
grid.onCrafted(matrixSlots);
} }
} }

View File

@@ -284,10 +284,8 @@ public class GuiGrid extends GuiBase
} }
else else
{ {
for (int i = 0; i < container.inventorySlots.size(); ++i) for (Slot slot : container.getPlayerInventorySlots())
{ {
Slot slot = (Slot) container.inventorySlots.get(i);
if (inBounds(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX - guiLeft, mouseY - guiTop)) if (inBounds(slot.xDisplayPosition, slot.yDisplayPosition, 16, 16, mouseX - guiLeft, mouseY - guiTop))
{ {
if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))

View File

@@ -0,0 +1,77 @@
package storagecraft.network;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import storagecraft.tile.TileGrid;
public class MessageGridCraftingUpdate implements IMessage, IMessageHandler<MessageGridCraftingUpdate, IMessage>
{
private int x;
private int y;
private int z;
private ItemStack[] craftingMatrix = new ItemStack[9];
public MessageGridCraftingUpdate()
{
}
public MessageGridCraftingUpdate(TileGrid grid)
{
this.x = grid.xCoord;
this.y = grid.yCoord;
this.z = grid.zCoord;
for (int i = 0; i < 9; ++i)
{
craftingMatrix[i] = grid.getCraftingMatrix().getStackInSlot(i);
}
}
@Override
public void fromBytes(ByteBuf buf)
{
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
for (int i = 0; i < 9; ++i)
{
craftingMatrix[i] = ByteBufUtils.readItemStack(buf);
}
}
@Override
public void toBytes(ByteBuf buf)
{
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
for (ItemStack stack : craftingMatrix)
{
ByteBufUtils.writeItemStack(buf, stack);
}
}
@Override
public IMessage onMessage(MessageGridCraftingUpdate message, MessageContext context)
{
TileEntity tile = Minecraft.getMinecraft().theWorld.getTileEntity(message.x, message.y, message.z);
if (tile instanceof TileGrid)
{
for (int i = 0; i < 9; ++i)
{
((TileGrid) tile).getCraftingMatrix().setInventorySlotContents(i, message.craftingMatrix[i]);
}
}
return null;
}
}

View File

@@ -74,7 +74,6 @@ public class MessageStoragePush implements IMessage, IMessageHandler<MessageStor
} }
else else
{ {
// @TODO: This crashes sometimes on shift??
stack = player.inventory.getStackInSlot(message.slot); stack = player.inventory.getStackInSlot(message.slot);
} }

View File

@@ -15,6 +15,7 @@ import storagecraft.item.ItemBlockGrid;
import storagecraft.network.MessageCompareUpdate; import storagecraft.network.MessageCompareUpdate;
import storagecraft.network.MessageDetectorAmountUpdate; import storagecraft.network.MessageDetectorAmountUpdate;
import storagecraft.network.MessageDetectorModeUpdate; import storagecraft.network.MessageDetectorModeUpdate;
import storagecraft.network.MessageGridCraftingUpdate;
import storagecraft.network.MessageImporterModeUpdate; import storagecraft.network.MessageImporterModeUpdate;
import storagecraft.network.MessageRedstoneModeUpdate; import storagecraft.network.MessageRedstoneModeUpdate;
import storagecraft.network.MessageStoragePull; import storagecraft.network.MessageStoragePull;
@@ -41,6 +42,7 @@ public class CommonProxy
StorageCraft.NETWORK.registerMessage(MessageImporterModeUpdate.class, MessageImporterModeUpdate.class, 5, Side.SERVER); StorageCraft.NETWORK.registerMessage(MessageImporterModeUpdate.class, MessageImporterModeUpdate.class, 5, Side.SERVER);
StorageCraft.NETWORK.registerMessage(MessageDetectorModeUpdate.class, MessageDetectorModeUpdate.class, 6, Side.SERVER); StorageCraft.NETWORK.registerMessage(MessageDetectorModeUpdate.class, MessageDetectorModeUpdate.class, 6, Side.SERVER);
StorageCraft.NETWORK.registerMessage(MessageDetectorAmountUpdate.class, MessageDetectorAmountUpdate.class, 7, Side.SERVER); StorageCraft.NETWORK.registerMessage(MessageDetectorAmountUpdate.class, MessageDetectorAmountUpdate.class, 7, Side.SERVER);
StorageCraft.NETWORK.registerMessage(MessageGridCraftingUpdate.class, MessageGridCraftingUpdate.class, 8, Side.CLIENT);
NetworkRegistry.INSTANCE.registerGuiHandler(StorageCraft.INSTANCE, new GuiHandler()); NetworkRegistry.INSTANCE.registerGuiHandler(StorageCraft.INSTANCE, new GuiHandler());

View File

@@ -55,20 +55,17 @@ public class TileExporter extends TileMachine implements IInventory, ISidedInven
boolean pushedAny = false; boolean pushedAny = false;
for (int si = 0; si < connectedInventory.getSizeInventory(); ++si) for (int sidedSlot = 0; sidedSlot < connectedInventory.getSizeInventory(); ++sidedSlot)
{ {
if (sided.canInsertItem(si, took, getDirection().getOpposite().ordinal())) if (sided.canInsertItem(sidedSlot, took, getDirection().getOpposite().ordinal()) && InventoryUtils.canPushToInventorySlot(connectedInventory, sidedSlot, took))
{ // @TODO: make more compact
if (InventoryUtils.canPushToInventorySlot(connectedInventory, si, took))
{ {
InventoryUtils.pushToInventorySlot(connectedInventory, si, took); InventoryUtils.pushToInventorySlot(connectedInventory, sidedSlot, took);
pushedAny = true; pushedAny = true;
break; break;
} }
} }
}
if (!pushedAny) if (!pushedAny)
{ {

View File

@@ -1,15 +1,20 @@
package storagecraft.tile; package storagecraft.tile;
import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import storagecraft.StorageCraft;
import storagecraft.container.ContainerGridCrafting; import storagecraft.container.ContainerGridCrafting;
import storagecraft.inventory.InventorySimple; import storagecraft.inventory.InventorySimple;
import storagecraft.network.MessageGridCraftingUpdate;
import storagecraft.storage.StorageItem;
import storagecraft.util.InventoryUtils; import storagecraft.util.InventoryUtils;
public class TileGrid extends TileMachine public class TileGrid extends TileMachine
{ {
private InventoryCrafting craftingMatrix = new InventoryCrafting(new ContainerGridCrafting(this), 3, 3); private ContainerGridCrafting craftingMatrixContainer = new ContainerGridCrafting(this);
private InventoryCrafting craftingMatrix = new InventoryCrafting(craftingMatrixContainer, 3, 3);
private InventorySimple craftingResult = new InventorySimple("craftingResult", 1); private InventorySimple craftingResult = new InventorySimple("craftingResult", 1);
@Override @Override
@@ -43,6 +48,35 @@ public class TileGrid extends TileMachine
craftingResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(craftingMatrix, worldObj)); craftingResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(craftingMatrix, worldObj));
} }
public void onCrafted(ItemStack[] matrixSlots)
{
if (isConnected() && !worldObj.isRemote)
{
for (int i = 0; i < craftingMatrix.getSizeInventory(); ++i)
{
ItemStack slot = craftingMatrix.getStackInSlot(i);
if (slot == null && matrixSlots[i] != null)
{
for (StorageItem item : getController().getItems())
{
if (item.compareNoQuantity(matrixSlots[i].copy()))
{
craftingMatrix.setInventorySlotContents(i, getController().take(matrixSlots[i].copy()));
break;
}
}
}
}
onCraftingMatrixChanged();
// @TODO: HACK!
StorageCraft.NETWORK.sendToAll(new MessageGridCraftingUpdate(this));
}
}
public InventorySimple getCraftingResult() public InventorySimple getCraftingResult()
{ {
return craftingResult; return craftingResult;