Added packet handlers, guis, etc required to handle the routing nodes. Added the ability to have a different filter for each direction.

This commit is contained in:
WayofTime 2016-01-14 11:06:50 -05:00
parent ac919c7882
commit a895809274
13 changed files with 508 additions and 78 deletions

View file

@ -93,6 +93,8 @@ public class Constants
public static final String ROUTING_MASTER_OUTPUT = "outputList";
public static final String GHOST_STACK_SIZE = "stackSize";
public static final String ITEM_INVENTORY = "itemInventory";
}
public static class Mod

View file

@ -1,7 +1,5 @@
package WayofTime.bloodmagic.block;
import java.util.LinkedList;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
@ -12,10 +10,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.routing.IMasterRoutingNode;
import WayofTime.bloodmagic.routing.IRoutingNode;
import WayofTime.bloodmagic.tile.routing.TileInputRoutingNode;
import WayofTime.bloodmagic.util.ChatUtil;
public class BlockInputRoutingNode extends BlockContainer
{
@ -43,12 +38,14 @@ public class BlockInputRoutingNode extends BlockContainer
}
@Override
//TODO: Combine BlockOutputRoutingNode and BlockInputRoutingNode so they have the same superclass
public void breakBlock(World world, BlockPos pos, IBlockState state)
{
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof IRoutingNode)
if (tile instanceof TileInputRoutingNode)
{
((IRoutingNode) tile).removeAllConnections();
((TileInputRoutingNode) tile).removeAllConnections();
((TileInputRoutingNode) tile).dropItems();
}
super.breakBlock(world, pos, state);
}
@ -56,30 +53,11 @@ public class BlockInputRoutingNode extends BlockContainer
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (world.isRemote)
if (world.getTileEntity(pos) instanceof TileInputRoutingNode)
{
return false;
player.openGui(BloodMagic.instance, Constants.Gui.ROUTING_NODE_GUI, world, pos.getX(), pos.getY(), pos.getZ());
}
TileEntity tile = world.getTileEntity(pos);
IRoutingNode node = (IRoutingNode) tile;
ChatUtil.sendChat(player, "Master: " + node.getMasterPos().toString());
for (BlockPos connPos : node.getConnected())
{
ChatUtil.sendChat(player, "Connected to: " + connPos.toString());
}
BlockPos masterPos = node.getMasterPos();
TileEntity testTile = world.getTileEntity(masterPos);
if (testTile instanceof IMasterRoutingNode)
{
IMasterRoutingNode master = (IMasterRoutingNode) testTile;
if (master.isConnected(new LinkedList<BlockPos>(), pos))
{
ChatUtil.sendChat(player, "Can find the path to the master");
}
}
return false;
return true;
}
}

View file

@ -10,7 +10,6 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import WayofTime.bloodmagic.BloodMagic;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.routing.IRoutingNode;
import WayofTime.bloodmagic.tile.routing.TileOutputRoutingNode;
public class BlockOutputRoutingNode extends BlockContainer
@ -43,9 +42,10 @@ public class BlockOutputRoutingNode extends BlockContainer
public void breakBlock(World world, BlockPos pos, IBlockState state)
{
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof IRoutingNode)
if (tile instanceof TileOutputRoutingNode)
{
((IRoutingNode) tile).removeAllConnections();
((TileOutputRoutingNode) tile).removeAllConnections();
((TileOutputRoutingNode) tile).dropItems();
}
super.breakBlock(world, pos, state);
}
@ -59,31 +59,5 @@ public class BlockOutputRoutingNode extends BlockContainer
}
return true;
// if (world.isRemote)
// {
// return false;
// }
//
// TileEntity tile = world.getTileEntity(pos);
// IRoutingNode node = (IRoutingNode) tile;
// ChatUtil.sendChat(player, "Master: " + node.getMasterPos().toString());
// for (BlockPos connPos : node.getConnected())
// {
// ChatUtil.sendChat(player, "Connected to: " + connPos.toString());
// }
//
// BlockPos masterPos = node.getMasterPos();
// TileEntity testTile = world.getTileEntity(masterPos);
// if (testTile instanceof IMasterRoutingNode)
// {
// IMasterRoutingNode master = (IMasterRoutingNode) testTile;
// if (master.isConnected(new LinkedList<BlockPos>(), pos))
// {
// ChatUtil.sendChat(player, "Can find the path to the master");
// }
// }
//
// return false;
}
}

View file

@ -11,7 +11,7 @@ import WayofTime.bloodmagic.tile.TileTeleposer;
import WayofTime.bloodmagic.tile.container.ContainerItemRoutingNode;
import WayofTime.bloodmagic.tile.container.ContainerSoulForge;
import WayofTime.bloodmagic.tile.container.ContainerTeleposer;
import WayofTime.bloodmagic.tile.routing.TileOutputRoutingNode;
import WayofTime.bloodmagic.tile.routing.TileFilteredRoutingNode;
public class GuiHandler implements IGuiHandler
{
@ -27,7 +27,7 @@ public class GuiHandler implements IGuiHandler
case Constants.Gui.SOUL_FORGE_GUI:
return new ContainerSoulForge(player.inventory, (TileSoulForge) world.getTileEntity(pos));
case Constants.Gui.ROUTING_NODE_GUI:
return new ContainerItemRoutingNode(player.inventory, (TileOutputRoutingNode) world.getTileEntity(pos));
return new ContainerItemRoutingNode(player.inventory, (TileFilteredRoutingNode) world.getTileEntity(pos));
}
return null;
@ -47,7 +47,7 @@ public class GuiHandler implements IGuiHandler
case Constants.Gui.SOUL_FORGE_GUI:
return new GuiSoulForge(player.inventory, (TileSoulForge) world.getTileEntity(pos));
case Constants.Gui.ROUTING_NODE_GUI:
return new GuiItemRoutingNode(player.inventory, (TileOutputRoutingNode) world.getTileEntity(pos));
return new GuiItemRoutingNode(player.inventory, (TileFilteredRoutingNode) world.getTileEntity(pos));
}
}

View file

@ -1,23 +1,65 @@
package WayofTime.bloodmagic.client.gui;
import java.io.IOException;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.network.BloodMagicPacketHandler;
import WayofTime.bloodmagic.network.ItemRouterButtonPacketProcessor;
import WayofTime.bloodmagic.tile.container.ContainerItemRoutingNode;
@SideOnly(Side.CLIENT)
public class GuiItemRoutingNode extends GuiContainer
{
private GuiButton downButton;
private GuiButton upButton;
private GuiButton northButton;
private GuiButton southButton;
private GuiButton westButton;
private GuiButton eastButton;
private TileEntity inventory;
public GuiItemRoutingNode(InventoryPlayer playerInventory, IInventory tileRoutingNode)
{
super(new ContainerItemRoutingNode(playerInventory, tileRoutingNode));
this.xSize = 176;
this.ySize = 169;
inventory = (TileEntity) tileRoutingNode;
}
@Override
public void initGui()
{
super.initGui();
this.buttonList.clear();
this.buttonList.add(this.downButton = new GuiButton(0, (this.width - this.xSize) / 2 + 133, (this.height - this.ySize) / 2 + 50, 18, 18, "D"));
this.buttonList.add(this.upButton = new GuiButton(1, (this.width - this.xSize) / 2 + 133, (this.height - this.ySize) / 2 + 14, 18, 18, "U"));
this.buttonList.add(this.northButton = new GuiButton(2, (this.width - this.xSize) / 2 + 151, (this.height - this.ySize) / 2 + 14, 18, 18, "N"));
this.buttonList.add(this.southButton = new GuiButton(3, (this.width - this.xSize) / 2 + 151, (this.height - this.ySize) / 2 + 50, 18, 18, "S"));
this.buttonList.add(this.westButton = new GuiButton(4, (this.width - this.xSize) / 2 + 133, (this.height - this.ySize) / 2 + 32, 18, 18, "W"));
this.buttonList.add(this.eastButton = new GuiButton(5, (this.width - this.xSize) / 2 + 151, (this.height - this.ySize) / 2 + 32, 18, 18, "E"));
}
/**
* Called by the controls from the buttonList when activated. (Mouse pressed
* for buttons)
*/
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.enabled)
{
BloodMagicPacketHandler.INSTANCE.sendToServer(new ItemRouterButtonPacketProcessor(button.id, inventory.getPos(), inventory.getWorld()));
}
}
@Override

View file

@ -0,0 +1,258 @@
package WayofTime.bloodmagic.item.inventory;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.util.helper.NBTHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
public class ItemInventory implements IInventory
{
protected int[] syncedSlots = new int[0];
private ItemStack[] inventory;
private int size;
private String name;
protected ItemStack masterStack;
public ItemInventory(ItemStack masterStack, int size, String name)
{
this.inventory = new ItemStack[size];
this.size = size;
this.name = name;
this.masterStack = masterStack;
if (masterStack != null)
this.readFromStack(masterStack);
}
public void initializeInventory(ItemStack masterStack)
{
this.masterStack = masterStack;
this.clear();
this.readFromStack(masterStack);
}
private boolean isSyncedSlot(int slot)
{
for (int s : this.syncedSlots)
{
if (s == slot)
{
return true;
}
}
return false;
}
public void readFromNBT(NBTTagCompound tagCompound)
{
NBTTagList tags = tagCompound.getTagList("Items", 10);
inventory = new ItemStack[getSizeInventory()];
for (int i = 0; i < tags.tagCount(); i++)
{
if (!isSyncedSlot(i))
{
NBTTagCompound data = tags.getCompoundTagAt(i);
byte j = data.getByte("Slot");
if (j >= 0 && j < inventory.length)
{
inventory[j] = ItemStack.loadItemStackFromNBT(data);
}
}
}
}
public void writeToNBT(NBTTagCompound tagCompound)
{
NBTTagList tags = new NBTTagList();
for (int i = 0; i < inventory.length; i++)
{
if ((inventory[i] != null) && !isSyncedSlot(i))
{
NBTTagCompound data = new NBTTagCompound();
data.setByte("Slot", (byte) i);
inventory[i].writeToNBT(data);
tags.appendTag(data);
}
}
tagCompound.setTag("Items", tags);
}
public void readFromStack(ItemStack masterStack)
{
if (masterStack != null)
{
NBTHelper.checkNBT(masterStack);
NBTTagCompound tag = masterStack.getTagCompound();
readFromNBT(tag.getCompoundTag(Constants.NBT.ITEM_INVENTORY));
}
}
public void writeToStack(ItemStack masterStack)
{
if (masterStack != null)
{
NBTHelper.checkNBT(masterStack);
NBTTagCompound tag = masterStack.getTagCompound();
NBTTagCompound invTag = new NBTTagCompound();
writeToNBT(invTag);
tag.setTag(Constants.NBT.ITEM_INVENTORY, invTag);
}
}
@Override
public int getSizeInventory()
{
return size;
}
@Override
public ItemStack getStackInSlot(int index)
{
return inventory[index];
}
@Override
public ItemStack decrStackSize(int index, int count)
{
if (inventory[index] != null)
{
// if (!worldObj.isRemote)
// worldObj.markBlockForUpdate(this.pos);
if (inventory[index].stackSize <= count)
{
ItemStack itemStack = inventory[index];
inventory[index] = null;
markDirty();
return itemStack;
}
ItemStack itemStack = inventory[index].splitStack(count);
if (inventory[index].stackSize == 0)
inventory[index] = null;
markDirty();
return itemStack;
}
return null;
}
@Override
public ItemStack removeStackFromSlot(int slot)
{
if (inventory[slot] != null)
{
ItemStack itemStack = inventory[slot];
setInventorySlotContents(slot, null);
return itemStack;
}
return null;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack)
{
inventory[slot] = stack;
if (stack != null && stack.stackSize > getInventoryStackLimit())
stack.stackSize = getInventoryStackLimit();
markDirty();
// if (!worldObj.isRemote)
// worldObj.markBlockForUpdate(this.pos);
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
return true;
}
@Override
public void openInventory(EntityPlayer player)
{
}
@Override
public void closeInventory(EntityPlayer player)
{
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack)
{
return true;
}
@Override
public int getField(int id)
{
return 0;
}
@Override
public void setField(int id, int value)
{
}
@Override
public int getFieldCount()
{
return 0;
}
@Override
public void clear()
{
this.inventory = new ItemStack[size];
}
@Override
public String getName()
{
return name;
}
@Override
public boolean hasCustomName()
{
return false;
}
@Override
public IChatComponent getDisplayName()
{
return new ChatComponentText(getName());
}
@Override
public void markDirty()
{
if (masterStack != null)
{
this.writeToStack(masterStack);
}
}
public boolean canInventoryBeManipulated()
{
return masterStack != null;
}
}

View file

@ -16,6 +16,7 @@ public class BloodMagicPacketHandler
public static void init()
{
INSTANCE.registerMessage(ChatUtil.PacketNoSpamChat.Handler.class, ChatUtil.PacketNoSpamChat.class, 0, Side.CLIENT);
INSTANCE.registerMessage(ItemRouterButtonPacketProcessor.class, ItemRouterButtonPacketProcessor.class, 1, Side.SERVER);
}
public static void sendToAllAround(IMessage message, TileEntity te, int range)

View file

@ -0,0 +1,73 @@
package WayofTime.bloodmagic.network;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import WayofTime.bloodmagic.tile.routing.TileFilteredRoutingNode;
public class ItemRouterButtonPacketProcessor implements IMessage, IMessageHandler<ItemRouterButtonPacketProcessor, IMessage>
{
private int buttonPress;
private int dimension;
private BlockPos pos;
public ItemRouterButtonPacketProcessor()
{
}
public ItemRouterButtonPacketProcessor(int buttonPress, BlockPos pos, World world)
{
this.buttonPress = buttonPress;
this.pos = pos;
this.dimension = world.provider.getDimensionId();
}
@Override
public void fromBytes(ByteBuf buffer)
{
PacketBuffer buff = new PacketBuffer(buffer);
dimension = buff.readInt();
pos = buff.readBlockPos();
buttonPress = buff.readInt();
}
@Override
public void toBytes(ByteBuf buffer)
{
PacketBuffer buff = new PacketBuffer(buffer);
buff.writeInt(dimension);
buff.writeBlockPos(pos);
buff.writeInt(buttonPress);
}
@Override
public IMessage onMessage(ItemRouterButtonPacketProcessor message, MessageContext ctx)
{
if (ctx.side == Side.SERVER)
{
message.onMessageFromClient();
}
return null;
}
public void onMessageFromClient()
{
World world = DimensionManager.getWorld(dimension);
if (world != null)
{
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileFilteredRoutingNode)
{
((TileFilteredRoutingNode) tile).swapFilters(buttonPress);
}
}
}
}

View file

@ -1,28 +1,37 @@
package WayofTime.bloodmagic.tile.container;
import WayofTime.bloodmagic.util.GhostItemHelper;
import WayofTime.bloodmagic.util.Utils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import WayofTime.bloodmagic.item.inventory.ItemInventory;
import WayofTime.bloodmagic.util.GhostItemHelper;
import WayofTime.bloodmagic.util.Utils;
public class ContainerItemRoutingNode extends Container
{
private final IInventory tileItemRoutingNode;
private final ItemInventory itemInventory;
private int slotsOccupied;
public ContainerItemRoutingNode(InventoryPlayer inventoryPlayer, IInventory tileItemRoutingNode)
{
this.tileItemRoutingNode = tileItemRoutingNode;
// this.addSlotToContainer(new Slot(tileItemRoutingNode, 0, 8, 15));
// this.addSlotToContainer(new Slot(tileItemRoutingNode, 1, 80, 15));
// this.addSlotToContainer(new Slot(tileItemRoutingNode, 2, 80, 87));
// this.addSlotToContainer(new Slot(tileItemRoutingNode, 3, 8, 87));
this.addSlotToContainer(new SlotGhostItem(tileItemRoutingNode, 0, 8, 33));
slotsOccupied = 1;
this.addSlotToContainer(new SlotItemFilter(this, tileItemRoutingNode, 0, 8, 33));
ItemStack masterStack = tileItemRoutingNode.getStackInSlot(0);
itemInventory = new ItemInventory(masterStack, 9, "");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
addSlotToContainer(new SlotGhostItem(itemInventory, j + i * 3, 26 + j * 18, 15 + i * 18));
}
}
slotsOccupied = 10;
// this.addSlotToContainer(new SlotOutput(tileItemRoutingNode, TileSoulForge.outputSlot, 44, 51));
for (int i = 0; i < 3; i++)
@ -39,6 +48,11 @@ public class ContainerItemRoutingNode extends Container
}
}
public void resetItemInventory(ItemStack masterStack)
{
itemInventory.initializeInventory(masterStack);
}
/**
* Overridden in order to handle ghost item slots.
*/
@ -46,7 +60,7 @@ public class ContainerItemRoutingNode extends Container
public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer player)
{
InventoryPlayer inventoryPlayer = player.inventory;
if (!player.worldObj.isRemote)
// if (!player.worldObj.isRemote)
{
if (slotId >= 0)
{
@ -56,8 +70,6 @@ public class ContainerItemRoutingNode extends Container
{
if ((mode == 0 || mode == 1) && (clickedButton == 0 || clickedButton == 1))
{
System.out.println("Clicked button: " + clickedButton + ", mode: " + mode);
ItemStack slotStack = slot.getStack();
ItemStack heldStack = inventoryPlayer.getItemStack();
@ -71,6 +83,10 @@ public class ContainerItemRoutingNode extends Container
slot.putStack(slotStack);
} else if (heldStack != null)
{
if (!((SlotGhostItem) slot).canBeAccessed())
{
return super.slotClick(slotId, clickedButton, mode, player);
}
if (slotStack != null && Utils.canCombine(slotStack, heldStack))
{
GhostItemHelper.incrementGhostAmout(slotStack, heldStack.stackSize);
@ -192,9 +208,12 @@ public class ContainerItemRoutingNode extends Container
private class SlotItemFilter extends Slot
{
public SlotItemFilter(IInventory inventory, int slotIndex, int x, int y)
public ContainerItemRoutingNode container;
public SlotItemFilter(ContainerItemRoutingNode container, IInventory inventory, int slotIndex, int x, int y)
{
super(inventory, slotIndex, x, y);
this.container = container;
}
@Override
@ -202,13 +221,28 @@ public class ContainerItemRoutingNode extends Container
{
return true; //TODO: Create a new Item that holds the filter.
}
@Override
public void onSlotChanged()
{
super.onSlotChanged();
container.resetItemInventory(getStack());
for (int i = 1; i <= 9; i++)
{
Slot slot = container.getSlot(i);
slot.onSlotChanged();
}
}
}
private class SlotGhostItem extends Slot
{
public SlotGhostItem(IInventory inventory, int slotIndex, int x, int y)
private ItemInventory itemInv;
public SlotGhostItem(ItemInventory inventory, int slotIndex, int x, int y)
{
super(inventory, slotIndex, x, y);
itemInv = inventory;
}
@Override
@ -222,5 +256,16 @@ public class ContainerItemRoutingNode extends Container
{
return false;
}
// @Override
// public boolean isHere(IInventory inv, int slotIn)
// {
// return itemInv.canInventoryBeManipulated() && super.isHere(inv, slotIn);
// }
public boolean canBeAccessed()
{
return itemInv.canInventoryBeManipulated();
}
}
}

View file

@ -0,0 +1,57 @@
package WayofTime.bloodmagic.tile.routing;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
public class TileFilteredRoutingNode extends TileRoutingNode implements ISidedInventory
{
public int currentActiveSlot = 0;
public TileFilteredRoutingNode(int size, String name)
{
super(size, name);
}
@Override
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT(tag);
currentActiveSlot = tag.getInteger("currentSlot");
}
@Override
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT(tag);
tag.setInteger("currentSlot", currentActiveSlot);
}
public void swapFilters(int requestedSlot)
{
this.setInventorySlotContents(currentActiveSlot + 1, this.getStackInSlot(0));
this.setInventorySlotContents(0, this.getStackInSlot(requestedSlot + 1));
this.setInventorySlotContents(requestedSlot + 1, null);
currentActiveSlot = requestedSlot;
this.markDirty();
}
@Override
public int[] getSlotsForFace(EnumFacing side)
{
return new int[0];
}
@Override
public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction)
{
return false;
}
@Override
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
{
return false;
}
}

View file

@ -1,9 +1,9 @@
package WayofTime.bloodmagic.tile.routing;
public class TileInputRoutingNode extends TileRoutingNode
public class TileInputRoutingNode extends TileFilteredRoutingNode
{
public TileInputRoutingNode()
{
super(1, "inputNode");
super(7, "inputNode");
}
}

View file

@ -1,9 +1,9 @@
package WayofTime.bloodmagic.tile.routing;
public class TileOutputRoutingNode extends TileRoutingNode
public class TileOutputRoutingNode extends TileFilteredRoutingNode
{
public TileOutputRoutingNode()
{
super(1, "outputNode");
super(7, "outputNode");
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB