make crafting grid work
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package storagecraft.container;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.Slot;
|
||||
@@ -10,6 +12,8 @@ public abstract class ContainerBase extends Container
|
||||
{
|
||||
private EntityPlayer player;
|
||||
|
||||
private List<Slot> playerInventorySlots = new ArrayList<Slot>();
|
||||
|
||||
public ContainerBase(EntityPlayer player)
|
||||
{
|
||||
this.player = player;
|
||||
@@ -26,7 +30,11 @@ public abstract class ContainerBase extends Container
|
||||
|
||||
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++;
|
||||
}
|
||||
@@ -35,7 +43,11 @@ public abstract class ContainerBase extends Container
|
||||
{
|
||||
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++;
|
||||
}
|
||||
@@ -75,4 +87,9 @@ public abstract class ContainerBase extends Container
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Slot> getPlayerInventorySlots()
|
||||
{
|
||||
return playerInventorySlots;
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,8 @@ public class ContainerGrid extends ContainerBase
|
||||
{
|
||||
super(player);
|
||||
|
||||
addPlayerInventory(8, grid.isCrafting() ? 174 : 108);
|
||||
|
||||
if (grid.isCrafting())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@@ -3,11 +3,37 @@ package storagecraft.container.slot;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.SlotCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import storagecraft.tile.TileGrid;
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@@ -284,10 +284,8 @@ public class GuiGrid extends GuiBase
|
||||
}
|
||||
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 (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -74,7 +74,6 @@ public class MessageStoragePush implements IMessage, IMessageHandler<MessageStor
|
||||
}
|
||||
else
|
||||
{
|
||||
// @TODO: This crashes sometimes on shift??
|
||||
stack = player.inventory.getStackInSlot(message.slot);
|
||||
}
|
||||
|
||||
|
@@ -15,6 +15,7 @@ import storagecraft.item.ItemBlockGrid;
|
||||
import storagecraft.network.MessageCompareUpdate;
|
||||
import storagecraft.network.MessageDetectorAmountUpdate;
|
||||
import storagecraft.network.MessageDetectorModeUpdate;
|
||||
import storagecraft.network.MessageGridCraftingUpdate;
|
||||
import storagecraft.network.MessageImporterModeUpdate;
|
||||
import storagecraft.network.MessageRedstoneModeUpdate;
|
||||
import storagecraft.network.MessageStoragePull;
|
||||
@@ -41,6 +42,7 @@ public class CommonProxy
|
||||
StorageCraft.NETWORK.registerMessage(MessageImporterModeUpdate.class, MessageImporterModeUpdate.class, 5, 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(MessageGridCraftingUpdate.class, MessageGridCraftingUpdate.class, 8, Side.CLIENT);
|
||||
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler(StorageCraft.INSTANCE, new GuiHandler());
|
||||
|
||||
|
@@ -55,20 +55,17 @@ public class TileExporter extends TileMachine implements IInventory, ISidedInven
|
||||
|
||||
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()))
|
||||
{ // @TODO: make more compact
|
||||
if (InventoryUtils.canPushToInventorySlot(connectedInventory, si, took))
|
||||
if (sided.canInsertItem(sidedSlot, took, getDirection().getOpposite().ordinal()) && InventoryUtils.canPushToInventorySlot(connectedInventory, sidedSlot, took))
|
||||
{
|
||||
InventoryUtils.pushToInventorySlot(connectedInventory, si, took);
|
||||
InventoryUtils.pushToInventorySlot(connectedInventory, sidedSlot, took);
|
||||
|
||||
pushedAny = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pushedAny)
|
||||
{
|
||||
|
@@ -1,15 +1,20 @@
|
||||
package storagecraft.tile;
|
||||
|
||||
import net.minecraft.inventory.InventoryCrafting;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import storagecraft.StorageCraft;
|
||||
import storagecraft.container.ContainerGridCrafting;
|
||||
import storagecraft.inventory.InventorySimple;
|
||||
import storagecraft.network.MessageGridCraftingUpdate;
|
||||
import storagecraft.storage.StorageItem;
|
||||
import storagecraft.util.InventoryUtils;
|
||||
|
||||
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);
|
||||
|
||||
@Override
|
||||
@@ -43,6 +48,35 @@ public class TileGrid extends TileMachine
|
||||
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()
|
||||
{
|
||||
return craftingResult;
|
||||
|
Reference in New Issue
Block a user