diff --git a/src/main/scala/extracells/container/ContainerFluidInterface.java b/src/main/scala/extracells/container/ContainerFluidInterface.java index 1788be3..1b300cb 100644 --- a/src/main/scala/extracells/container/ContainerFluidInterface.java +++ b/src/main/scala/extracells/container/ContainerFluidInterface.java @@ -19,151 +19,142 @@ import net.minecraftforge.fluids.FluidStack; public class ContainerFluidInterface extends Container implements - IContainerListener { - public IFluidInterface fluidInterface; - @SideOnly(Side.CLIENT) - public GuiFluidInterface gui; - EntityPlayer player; - - public ContainerFluidInterface(EntityPlayer player, - IFluidInterface fluidInterface) { - this.player = player; - this.fluidInterface = fluidInterface; - for (int j = 0; j < 9; j++) { - addSlotToContainer(new SlotRespective( - fluidInterface.getPatternInventory(), j, 8 + j * 18, 115)); - } - bindPlayerInventory(player.inventory); - if (fluidInterface instanceof TileEntityFluidInterface) { - ((TileEntityFluidInterface) fluidInterface).registerListener(this); - } else if (fluidInterface instanceof PartFluidInterface) { - ((PartFluidInterface) fluidInterface).registerListener(this); - } - if (fluidInterface instanceof TileEntityFluidInterface) { - ((TileEntityFluidInterface) fluidInterface).doNextUpdate = true; - } else if (fluidInterface instanceof PartFluidInterface) { - ((PartFluidInterface) fluidInterface).doNextUpdate = true; - } - } - - protected void bindPlayerInventory(IInventory inventoryPlayer) { - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 9; j++) { - addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, - 8 + j * 18, i * 18 + 149)); - } - } - - for (int i = 0; i < 9; i++) { - addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 207));// 173 - } - } - - @Override - public boolean canInteractWith(EntityPlayer entityplayer) { - return true; - } - - private int getFluidID(ForgeDirection side) { - Fluid fluid = this.fluidInterface.getFilter(side); - if (fluid == null) - return -1; - return fluid.getID(); - } - - private String getFluidName(ForgeDirection side) { - Fluid fluid = this.fluidInterface.getFilter(side); - if (fluid == null) { - return ""; - } - return fluid.getName(); - } - - @Override - public void onContainerClosed(EntityPlayer player) { - super.onContainerClosed(player); - if (this.fluidInterface instanceof TileEntityFluidInterface) { - ((TileEntityFluidInterface) this.fluidInterface) - .removeListener(this); - } else if (this.fluidInterface instanceof PartFluidInterface) { - ((PartFluidInterface) this.fluidInterface).removeListener(this); - } - } - - @Override - protected void retrySlotClick(int p_75133_1_, int p_75133_2_, - boolean p_75133_3_, EntityPlayer p_75133_4_) { - - } - - @Override - public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) { - ItemStack itemstack = null; - Slot slot = (Slot) this.inventorySlots.get(slotnumber); - - if (slot != null && slot.getHasStack()) { - ItemStack itemstack1 = slot.getStack(); - itemstack = itemstack1.copy(); - - if (itemstack.getItem() instanceof ICraftingPatternItem) { - if (slotnumber < 9) { - if (!mergeItemStack(itemstack1, - this.inventorySlots.size() - 9, - this.inventorySlots.size(), false)) { - if (!mergeItemStack(itemstack1, 9, - this.inventorySlots.size() - 9, false)) - return null; - } - } else if (!mergeItemStack(itemstack1, 0, 9, false)) { - return null; - } - if (itemstack1.stackSize == 0) { - slot.putStack(null); - } else { - slot.onSlotChanged(); - } - return itemstack; - } - - if (slotnumber < this.inventorySlots.size() - 9) { - if (!mergeItemStack(itemstack1, this.inventorySlots.size() - 9, - this.inventorySlots.size(), true)) { - return null; - } - } else if (!mergeItemStack(itemstack1, 9, - this.inventorySlots.size() - 9, false)) { - return null; - } - - if (itemstack1.stackSize == 0) { - slot.putStack(null); - } else { - slot.onSlotChanged(); - } - } - - return itemstack; - } - - @Override - public void updateContainer() { - new PacketFluidInterface(new FluidStack[]{ - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(0)).getFluid(), - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(1)).getFluid(), - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(2)).getFluid(), - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(3)).getFluid(), - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(4)).getFluid(), - this.fluidInterface.getFluidTank(ForgeDirection.getOrientation(5)).getFluid() - }, - new String[]{ - getFluidName(ForgeDirection.getOrientation(0)), - getFluidName(ForgeDirection.getOrientation(1)), - getFluidName(ForgeDirection.getOrientation(2)), - getFluidName(ForgeDirection.getOrientation(3)), - getFluidName(ForgeDirection.getOrientation(4)), - getFluidName(ForgeDirection.getOrientation(5)) - }, - this.player).sendPacketToPlayer(this.player); - } + IContainerListener { + public IFluidInterface fluidInterface; + @SideOnly(Side.CLIENT) + public GuiFluidInterface gui; + EntityPlayer player; + + public ContainerFluidInterface(EntityPlayer player, + IFluidInterface fluidInterface) { + this.player = player; + this.fluidInterface = fluidInterface; + for (int j = 0; j < 9; j++) { + addSlotToContainer(new SlotRespective( + fluidInterface.getPatternInventory(), j, 8 + j * 18, 115)); + } + bindPlayerInventory(player.inventory); + if (fluidInterface instanceof TileEntityFluidInterface) { + ((TileEntityFluidInterface) fluidInterface).registerListener(this); + } else if (fluidInterface instanceof PartFluidInterface) { + ((PartFluidInterface) fluidInterface).registerListener(this); + } + if (fluidInterface instanceof TileEntityFluidInterface) { + ((TileEntityFluidInterface) fluidInterface).doNextUpdate = true; + } else if (fluidInterface instanceof PartFluidInterface) { + ((PartFluidInterface) fluidInterface).doNextUpdate = true; + } + } + + protected void bindPlayerInventory(IInventory inventoryPlayer) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 9; j++) { + addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, + 8 + j * 18, i * 18 + 149)); + } + } + + for (int i = 0; i < 9; i++) { + addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 207));// 173 + } + } + + @Override + public boolean canInteractWith(EntityPlayer entityplayer) { + return true; + } + + private int getFluidID(ForgeDirection side) { + Fluid fluid = this.fluidInterface.getFilter(side); + if (fluid == null) + return -1; + return fluid.getID(); + } + + private String getFluidName(ForgeDirection side) { + Fluid fluid = this.fluidInterface.getFilter(side); + if (fluid == null) { + return ""; + } + return fluid.getName(); + } + + @Override + public void onContainerClosed(EntityPlayer player) { + super.onContainerClosed(player); + if (this.fluidInterface instanceof TileEntityFluidInterface) { + ((TileEntityFluidInterface) this.fluidInterface) + .removeListener(this); + } else if (this.fluidInterface instanceof PartFluidInterface) { + ((PartFluidInterface) this.fluidInterface).removeListener(this); + } + } + + @Override + protected void retrySlotClick(int p_75133_1_, int p_75133_2_, + boolean p_75133_3_, EntityPlayer p_75133_4_) { + + } + + @Override + public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) { + ItemStack itemstack = null; + Slot slot = (Slot) this.inventorySlots.get(slotnumber); + + if (slot != null && slot.getHasStack()) { + ItemStack itemstack1 = slot.getStack(); + itemstack = itemstack1.copy(); + + if (itemstack.getItem() instanceof ICraftingPatternItem) { + if (slotnumber < 9) { + if (!mergeItemStack(itemstack1, + this.inventorySlots.size() - 9, + this.inventorySlots.size(), false)) { + if (!mergeItemStack(itemstack1, 9, + this.inventorySlots.size() - 9, false)) + return null; + } + } else if (!mergeItemStack(itemstack1, 0, 9, false)) { + return null; + } + if (itemstack1.stackSize == 0) { + slot.putStack(null); + } else { + slot.onSlotChanged(); + } + return itemstack; + } + + if (slotnumber < this.inventorySlots.size() - 9) { + if (!mergeItemStack(itemstack1, this.inventorySlots.size() - 9, + this.inventorySlots.size(), true)) { + return null; + } + } else if (!mergeItemStack(itemstack1, 9, + this.inventorySlots.size() - 9, false)) { + return null; + } + + if (itemstack1.stackSize == 0) { + slot.putStack(null); + } else { + slot.onSlotChanged(); + } + } + + return itemstack; + } + + @Override + public void updateContainer() { + FluidStack[] fluidStacks = new FluidStack[6]; + String[] fluidNames = new String[6]; + for (int i = 0; i < 6; i++) { + ForgeDirection location = ForgeDirection.getOrientation(i); + fluidStacks[i] = fluidInterface.getFluidTank(location).getFluid(); + fluidNames[i] = getFluidName(location); + } + new PacketFluidInterface(fluidStacks, fluidNames, this.player).sendPacketToPlayer(this.player); + } } diff --git a/src/main/scala/extracells/network/packet/part/PacketFluidInterface.java b/src/main/scala/extracells/network/packet/part/PacketFluidInterface.java index fbb979e..16e576d 100644 --- a/src/main/scala/extracells/network/packet/part/PacketFluidInterface.java +++ b/src/main/scala/extracells/network/packet/part/PacketFluidInterface.java @@ -16,116 +16,131 @@ public class PacketFluidInterface extends AbstractPacket { - FluidStack[] tank; - String[] filter; - String fluidName; - int filterSlot; + FluidStack[] tank; + String[] filter; + int fluidID; + int filterSlot; + + public PacketFluidInterface() {} + + public PacketFluidInterface(FluidStack[] _tank, String[] _filter, + EntityPlayer _player) { + super(_player); + this.mode = 0; + this.tank = _tank; + this.filter = _filter; + } + + public PacketFluidInterface(int _fluidID, int _filterSlot, + EntityPlayer _player) { + super(_player); + this.mode = 1; + this.fluidID = _fluidID; + this.filterSlot = _filterSlot; + } + + @Override + public void execute() { + switch (this.mode) { + case 0: + mode0(); + break; + case 1: + if (this.player.openContainer != null + && this.player.openContainer instanceof ContainerFluidInterface) { + ContainerFluidInterface container = (ContainerFluidInterface) this.player.openContainer; + container.fluidInterface.setFilter( + ForgeDirection.getOrientation(this.filterSlot), + FluidRegistry.getFluid(this.fluidID)); + } + break; + default: + } + + } + + @SideOnly(Side.CLIENT) + private void mode0() { + EntityPlayer p = Minecraft.getMinecraft().thePlayer; + if (p.openContainer != null + && p.openContainer instanceof ContainerFluidInterface) { + ContainerFluidInterface container = (ContainerFluidInterface) p.openContainer; + if (Minecraft.getMinecraft().currentScreen != null + && Minecraft.getMinecraft().currentScreen instanceof GuiFluidInterface) { + GuiFluidInterface gui = (GuiFluidInterface) Minecraft + .getMinecraft().currentScreen; + for (int i = 0; i < this.tank.length; i++) { + container.fluidInterface.setFluidTank( + ForgeDirection.getOrientation(i), this.tank[i]); + } + for (int i = 0; i < filter.length; i++) { + if (gui.filter[i] != null) { + gui.filter[i].setFluid(FluidRegistry + .getFluid(filter[i])); + } + } + } + } + } + + @Override + public void readData(ByteBuf in) { + switch (this.mode) { + case 0: + NBTTagCompound tag = ByteBufUtils.readTag(in); + this.tank = new FluidStack[tag.getInteger("lengthTank")]; + for (int i = 0; i < this.tank.length; i++) { + if (tag.hasKey("tank#" + i)) + this.tank[i] = FluidStack.loadFluidStackFromNBT(tag + .getCompoundTag("tank#" + i)); + else + this.tank[i] = null; + } + this.filter = new String[tag.getInteger("lengthFilter")]; + for (int i = 0; i < this.filter.length; i++) { + if (tag.hasKey("filter#" + i)) { + this.filter[i] = tag.getString("filter#" + i); + } else { + this.filter[i] = ""; + } + } + break; + case 1: + this.filterSlot = in.readInt(); + this.fluidID = in.readInt(); + break; + default: + } + + } + + @Override + public void writeData(ByteBuf out) { + switch (this.mode) { + case 0: + NBTTagCompound tag = new NBTTagCompound(); + tag.setInteger("lengthTank", this.tank.length); + for (int i = 0; i < this.tank.length; i++) { + if (this.tank[i] != null) { + tag.setTag("tank#" + i, + this.tank[i].writeToNBT(new NBTTagCompound())); + } + } + tag.setInteger("lengthFilter", this.filter.length); + for (int i = 0; i < this.filter.length; i++) { + if (this.filter[i] != null) { + tag.setString("filter#" + i, this.filter[i]); + } + } + ByteBufUtils.writeTag(out, tag); + break; + case 1: + out.writeInt(this.filterSlot); + out.writeInt(this.fluidID); + break; + default: + } + + } - public PacketFluidInterface() { - } - - public PacketFluidInterface(FluidStack[] tank, String[] filter, EntityPlayer player) { - super(player); - this.mode = 0; - this.tank = tank; - this.filter = filter; - } - - public PacketFluidInterface(String fluidName, int filterSlot, EntityPlayer player) { - super(player); - this.mode = 1; - this.fluidName = fluidName; - this.filterSlot = filterSlot; - } - - @Override - public void execute() { - switch (this.mode) { - case 0: - mode0(); - break; - case 1: - if (this.player.openContainer != null && this.player.openContainer instanceof ContainerFluidInterface) { - ContainerFluidInterface container = (ContainerFluidInterface) this.player.openContainer; - container.fluidInterface.setFilter( - ForgeDirection.getOrientation(this.filterSlot), - FluidRegistry.getFluid(this.fluidName)); - } - break; - default: - } - - } - - @SideOnly(Side.CLIENT) - private void mode0() { - EntityPlayer p = Minecraft.getMinecraft().thePlayer; - if (p.openContainer instanceof ContainerFluidInterface) { - - ContainerFluidInterface container = (ContainerFluidInterface) p.openContainer; - - if (Minecraft.getMinecraft().currentScreen != null && Minecraft.getMinecraft().currentScreen instanceof GuiFluidInterface) { - - GuiFluidInterface gui = (GuiFluidInterface) Minecraft.getMinecraft().currentScreen; - - for (int i = 0; i < this.tank.length; i++) { - container.fluidInterface.setFluidTank(ForgeDirection.getOrientation(i), this.tank[i]); - } - for (int i = 0; i < filter.length; i++) { - if (gui.filter[i] != null) { - gui.filter[i].setFluid(FluidRegistry.getFluid(filter[i])); - } - } - } - } - } - - @Override - public void readData(ByteBuf in) { - switch (this.mode) { - case 0: - NBTTagCompound tag = ByteBufUtils.readTag(in); - tank = new FluidStack[tag.getInteger("lengthTank")]; - for (int i = 0; i < tank.length; i++) { - if (tag.hasKey("tank#" + i)) - tank[i] = FluidStack.loadFluidStackFromNBT(tag.getCompoundTag("tank#" + i)); - else - tank[i] = null; - } - filter = new String[tag.getInteger("lengthFilter")]; - for (int i = 0; i < filter.length; i++) { - if (tag.hasKey("filter#" + i)) { - filter[i] = tag.getString("filter#" + i); - } else { - filter[i] = ""; - } - } - break; - case 1: - this.filterSlot = in.readInt(); - this.fluidName = ByteBufUtils.readUTF8String(in); - break; - default: - } - - } - - @Override - public void writeData(ByteBuf out) { - NBTTagCompound tag = new NBTTagCompound(); - tag.setInteger("lengthTank", this.tank.length); - for (int i = 0; i < this.tank.length; i++) { - if (this.tank[i] != null) { - tag.setTag("tank#" + i, - this.tank[i].writeToNBT(new NBTTagCompound())); - } - } - tag.setInteger("lengthFilter", this.filter.length); - for (int i = 0; i < this.filter.length; i++) { - if (this.filter[i] != null) { - tag.setString("filter#" + i, this.filter[i]); - } - } - ByteBufUtils.writeTag(out, tag); - } }