From 74d2239dc740f75e5355059e35715a3f263959d4 Mon Sep 17 00:00:00 2001 From: Flow86 Date: Thu, 19 Sep 2013 12:45:17 +0200 Subject: [PATCH] added non blocking mode to emerald pipe implements/closes #1195 --- .../lang/buildcraft/en_US.properties | 4 + .../core/network/IGuiReturnHandler.java | 20 ++++ .../core/network/PacketGuiReturn.java | 89 +++++++++++++++++ .../core/network/PacketHandler.java | 18 +++- common/buildcraft/core/network/PacketIds.java | 13 ++- .../buildcraft/transport/TileGenericPipe.java | 15 ++- .../transport/gui/GuiEmeraldPipe.java | 39 +++++++- .../transport/pipes/PipeItemsEmerald.java | 96 ++++++++++++++++--- 8 files changed, 268 insertions(+), 26 deletions(-) create mode 100644 common/buildcraft/core/network/IGuiReturnHandler.java create mode 100644 common/buildcraft/core/network/PacketGuiReturn.java diff --git a/buildcraft_resources/lang/buildcraft/en_US.properties b/buildcraft_resources/lang/buildcraft/en_US.properties index b9cc90b2..1d897c4a 100644 --- a/buildcraft_resources/lang/buildcraft/en_US.properties +++ b/buildcraft_resources/lang/buildcraft/en_US.properties @@ -24,6 +24,10 @@ gui.heat=Heat gui.assemblyRate=Energy Rate gui.assemblyCurrentRequired=Energy Required gui.clickcraft=-Click to Craft- +gui.pipes.emerald.blocking=Blocking +gui.pipes.emerald.blocking.tip=Extraction is blocked if one element in filter is missing +gui.pipes.emerald.nonblocking=Non Blocking +gui.pipes.emerald.nonblocking.tip=Extraction continues with the next filter in list if element is missing item.bucketFuel=Fuel Bucket item.bucketOil=Oil Bucket item.woodenGearItem=Wood Gear diff --git a/common/buildcraft/core/network/IGuiReturnHandler.java b/common/buildcraft/core/network/IGuiReturnHandler.java new file mode 100644 index 00000000..25b1a63b --- /dev/null +++ b/common/buildcraft/core/network/IGuiReturnHandler.java @@ -0,0 +1,20 @@ +package buildcraft.core.network; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.World; + +/** + * + * @author CovertJaguar + */ +public abstract interface IGuiReturnHandler { + public World getWorld(); + + public void writeGuiData(DataOutputStream paramDataOutputStream) throws IOException; + + public void readGuiData(DataInputStream paramDataInputStream, EntityPlayer paramEntityPlayer) throws IOException; +} diff --git a/common/buildcraft/core/network/PacketGuiReturn.java b/common/buildcraft/core/network/PacketGuiReturn.java new file mode 100644 index 00000000..e559b24b --- /dev/null +++ b/common/buildcraft/core/network/PacketGuiReturn.java @@ -0,0 +1,89 @@ +package buildcraft.core.network; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; +import cpw.mods.fml.common.network.PacketDispatcher; + +/** + * + * @author CovertJaguar + */ +public class PacketGuiReturn extends BuildCraftPacket { + private EntityPlayer sender; + private IGuiReturnHandler obj; + private byte[] extraData; + + public PacketGuiReturn(EntityPlayer sender) { + this.sender = sender; + } + + public PacketGuiReturn(IGuiReturnHandler obj) { + this.obj = obj; + this.extraData = null; + } + + public PacketGuiReturn(IGuiReturnHandler obj, byte[] extraData) { + this.obj = obj; + this.extraData = extraData; + } + + @Override + public void writeData(DataOutputStream data) throws IOException { + data.writeInt(obj.getWorld().provider.dimensionId); + if (obj instanceof TileEntity) { + TileEntity tile = (TileEntity) obj; + data.writeBoolean(true); + data.writeInt(tile.xCoord); + data.writeInt(tile.yCoord); + data.writeInt(tile.zCoord); + } else if (obj instanceof Entity) { + Entity entity = (Entity) obj; + data.writeBoolean(false); + data.writeInt(entity.entityId); + } else + return; + obj.writeGuiData(data); + if (extraData != null) + data.write(extraData); + } + + @Override + public void readData(DataInputStream data) throws IOException { + int dim = data.readInt(); + World world = DimensionManager.getWorld(dim); + boolean tileReturn = data.readBoolean(); + if (tileReturn) { + int x = data.readInt(); + int y = data.readInt(); + int z = data.readInt(); + + TileEntity t = world.getBlockTileEntity(x, y, z); + + if (t instanceof IGuiReturnHandler) + ((IGuiReturnHandler) t).readGuiData(data, sender); + + } else { + int entityId = data.readInt(); + Entity entity = world.getEntityByID(entityId); + + if (entity instanceof IGuiReturnHandler) + ((IGuiReturnHandler) entity).readGuiData(data, sender); + } + } + + public void sendPacket() { + PacketDispatcher.sendPacketToServer(getPacket()); + } + + @Override + public int getID() { + return PacketIds.GUI_RETURN; + } +} \ No newline at end of file diff --git a/common/buildcraft/core/network/PacketHandler.java b/common/buildcraft/core/network/PacketHandler.java index bac2e773..a999117e 100644 --- a/common/buildcraft/core/network/PacketHandler.java +++ b/common/buildcraft/core/network/PacketHandler.java @@ -1,15 +1,16 @@ package buildcraft.core.network; -import cpw.mods.fml.common.network.IPacketHandler; -import cpw.mods.fml.common.network.Player; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; +import cpw.mods.fml.common.network.IPacketHandler; +import cpw.mods.fml.common.network.Player; public class PacketHandler implements IPacketHandler { @@ -35,13 +36,14 @@ public class PacketHandler implements IPacketHandler { int packetID = data.read(); switch (packetID) { - case PacketIds.TILE_UPDATE: + case PacketIds.TILE_UPDATE: { PacketTileUpdate packetT = new PacketTileUpdate(); packetT.readData(data); onTileUpdate((EntityPlayer) player, packetT); break; + } - case PacketIds.STATE_UPDATE: + case PacketIds.STATE_UPDATE: { PacketTileState inPacket = new PacketTileState(); inPacket.readData(data); World world = ((EntityPlayer) player).worldObj; @@ -50,6 +52,14 @@ public class PacketHandler implements IPacketHandler { inPacket.applyStates(data, (ISyncedTile) tile); } break; + } + + case PacketIds.GUI_RETURN: { + PacketGuiReturn packet1 = new PacketGuiReturn((EntityPlayer) player); + packet1.readData(data); + // onGuiReturn((EntityPlayer) player, packet1); + break; + } } } catch (Exception ex) { ex.printStackTrace(); diff --git a/common/buildcraft/core/network/PacketIds.java b/common/buildcraft/core/network/PacketIds.java index a60e97c4..34af2c6f 100644 --- a/common/buildcraft/core/network/PacketIds.java +++ b/common/buildcraft/core/network/PacketIds.java @@ -3,32 +3,39 @@ package buildcraft.core.network; public class PacketIds { public static final int TILE_UPDATE = 0; -// public static final int PIPE_DESCRIPTION = 1; + // public static final int PIPE_DESCRIPTION = 1; public static final int PIPE_CONTENTS = 2; public static final int PIPE_LIQUID = 3; public static final int PIPE_POWER = 4; public static final int REQUEST_ITEM_NBT = 5; public static final int PIPE_ITEM_NBT = 6; + public static final int SELECTION_ASSEMBLY_GET = 20; /** Packet sent to server when a recipe is clicked on in the assembly table */ public static final int SELECTION_ASSEMBLY = 21; /** Packet to send recipes to client */ public static final int SELECTION_ASSEMBLY_SEND = 22; + public static final int DIAMOND_PIPE_SELECT = 31; public static final int EMERALD_PIPE_SELECT = 32; + public static final int GATE_ACTIONS = 40; public static final int GATE_REQUEST_INIT = 41; public static final int GATE_REQUEST_SELECTION = 42; public static final int GATE_SELECTION = 43; public static final int GATE_SELECTION_CHANGE = 44; public static final int GATE_TRIGGERS = 45; + public static final int REFINERY_FILTER_SET = 50; + public static final int ARCHITECT_NAME = 60; public static final int LIBRARY_ACTION = 61; public static final int LIBRARY_SELECT = 62; - public static final int STATE_UPDATE = 100; - public static final int ADVANCED_WORKBENCH_SETSLOT = 70; public static final int SELECTION_ADVANCED_WORKBENCH = 71; + + public static final int GUI_RETURN = 80; + + public static final int STATE_UPDATE = 100; } diff --git a/common/buildcraft/transport/TileGenericPipe.java b/common/buildcraft/transport/TileGenericPipe.java index 5ae87c2e..e9e38d46 100644 --- a/common/buildcraft/transport/TileGenericPipe.java +++ b/common/buildcraft/transport/TileGenericPipe.java @@ -46,6 +46,7 @@ import buildcraft.core.ITileBufferHolder; import buildcraft.core.TileBuffer; import buildcraft.core.inventory.InvUtils; import buildcraft.core.network.IClientState; +import buildcraft.core.network.IGuiReturnHandler; import buildcraft.core.network.ISyncedTile; import buildcraft.core.network.PacketTileState; import buildcraft.transport.Gate.GateKind; @@ -53,7 +54,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFluidHandler, IPipeTile, IOverrideDefaultTriggers, ITileBufferHolder, - IDropControlInventory, IPipeRenderState, ISyncedTile, ISolidSideTile { + IDropControlInventory, IPipeRenderState, ISyncedTile, ISolidSideTile, IGuiReturnHandler { private class CoreState implements IClientState { @@ -693,4 +694,16 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFlui public void markBlockForUpdate() { worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); } + + @Override + public void writeGuiData(DataOutputStream data) throws IOException { + if (BlockGenericPipe.isValid(pipe) && pipe instanceof IGuiReturnHandler) + ((IGuiReturnHandler) pipe).writeGuiData(data); + } + + @Override + public void readGuiData(DataInputStream data, EntityPlayer sender) throws IOException { + if (BlockGenericPipe.isValid(pipe) && pipe instanceof IGuiReturnHandler) + ((IGuiReturnHandler) pipe).readGuiData(data, sender); + } } diff --git a/common/buildcraft/transport/gui/GuiEmeraldPipe.java b/common/buildcraft/transport/gui/GuiEmeraldPipe.java index 89fcfe15..2134124c 100644 --- a/common/buildcraft/transport/gui/GuiEmeraldPipe.java +++ b/common/buildcraft/transport/gui/GuiEmeraldPipe.java @@ -7,28 +7,59 @@ */ package buildcraft.transport.gui; -import buildcraft.core.DefaultProps; -import buildcraft.core.gui.GuiBuildCraft; -import buildcraft.core.utils.StringUtils; -import buildcraft.transport.pipes.PipeItemsEmerald; import net.minecraft.inventory.IInventory; import net.minecraft.util.ResourceLocation; + import org.lwjgl.opengl.GL11; +import buildcraft.core.DefaultProps; +import buildcraft.core.gui.GuiBuildCraft; +import buildcraft.core.gui.buttons.GuiMultiButton; +import buildcraft.core.network.PacketGuiReturn; +import buildcraft.core.proxy.CoreProxy; +import buildcraft.core.utils.StringUtils; +import buildcraft.transport.pipes.PipeItemsEmerald; + public class GuiEmeraldPipe extends GuiBuildCraft { private static final ResourceLocation TEXTURE = new ResourceLocation("buildcraft", DefaultProps.TEXTURE_PATH_GUI + "/filter_2.png"); + private GuiMultiButton button; + IInventory playerInventory; IInventory filterInventory; + PipeItemsEmerald pipe; public GuiEmeraldPipe(IInventory playerInventory, PipeItemsEmerald pipe) { super(new ContainerEmeraldPipe(playerInventory, pipe), pipe.getFilters()); + + this.pipe = pipe; this.playerInventory = playerInventory; this.filterInventory = pipe.getFilters(); + xSize = 175; ySize = 132; } + @Override + public void initGui() { + super.initGui(); + + this.buttonList.clear(); + this.button = new GuiMultiButton(0, this.guiLeft + this.xSize - (80 + 4), this.guiTop + 2, 80, this.pipe.getStateController().copy()); + this.buttonList.add(this.button); + } + + @Override + public void onGuiClosed() { + if (CoreProxy.proxy.isRenderWorld(pipe.getWorld())) { + pipe.getStateController().setCurrentState(button.getController().getCurrentState()); + PacketGuiReturn pkt = new PacketGuiReturn(pipe.getContainer()); + pkt.sendPacket(); + } + + super.onGuiClosed(); + } + @Override protected void drawGuiContainerForegroundLayer(int par1, int par2) { fontRenderer.drawString(filterInventory.getInvName(), getCenteredOffset(filterInventory.getInvName()), 6, 0x404040); diff --git a/common/buildcraft/transport/pipes/PipeItemsEmerald.java b/common/buildcraft/transport/pipes/PipeItemsEmerald.java index 475aaf5a..dcc7fb20 100644 --- a/common/buildcraft/transport/pipes/PipeItemsEmerald.java +++ b/common/buildcraft/transport/pipes/PipeItemsEmerald.java @@ -7,19 +7,10 @@ */ package buildcraft.transport.pipes; -import buildcraft.BuildCraftTransport; -import buildcraft.api.inventory.ISelectiveInventory; -import buildcraft.api.inventory.ISpecialInventory; -import buildcraft.core.GuiIds; -import buildcraft.core.inventory.SimpleInventory; -import buildcraft.core.network.IClientState; -import buildcraft.core.proxy.CoreProxy; -import buildcraft.core.utils.Utils; -import buildcraft.transport.BlockGenericPipe; -import buildcraft.transport.PipeIconProvider; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; @@ -27,10 +18,57 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.ForgeDirection; +import buildcraft.BuildCraftTransport; +import buildcraft.api.inventory.ISelectiveInventory; +import buildcraft.api.inventory.ISpecialInventory; +import buildcraft.core.GuiIds; +import buildcraft.core.gui.buttons.IButtonTextureSet; +import buildcraft.core.gui.buttons.IMultiButtonState; +import buildcraft.core.gui.buttons.MultiButtonController; +import buildcraft.core.gui.buttons.StandardButtonTextureSets; +import buildcraft.core.gui.tooltips.ToolTip; +import buildcraft.core.gui.tooltips.ToolTipLine; +import buildcraft.core.inventory.SimpleInventory; +import buildcraft.core.network.IClientState; +import buildcraft.core.network.IGuiReturnHandler; +import buildcraft.core.proxy.CoreProxy; +import buildcraft.core.utils.StringUtils; +import buildcraft.core.utils.Utils; +import buildcraft.transport.BlockGenericPipe; +import buildcraft.transport.PipeIconProvider; -public class PipeItemsEmerald extends PipeItemsWood implements IClientState { +public class PipeItemsEmerald extends PipeItemsWood implements IClientState, IGuiReturnHandler { - private SimpleInventory filters = new SimpleInventory(9, "Filters", 1); + public static enum ButtonState implements IMultiButtonState { + BLOCKING("gui.pipes.emerald.blocking"), NONBLOCKING("gui.pipes.emerald.nonblocking"); + + private final String label; + private final ToolTip tip; + + private ButtonState(String label) { + this.label = label; + tip = new ToolTip(); + tip.add(new ToolTipLine(label + ".tip")); + } + + @Override + public String getLabel() { + return StringUtils.localize(this.label); + } + + @Override + public IButtonTextureSet getTextureSet() { + return StandardButtonTextureSets.SMALL_BUTTON; + } + + @Override + public ToolTip getToolTip() { + return this.tip; + } + } + + private final MultiButtonController stateController = MultiButtonController.getController(ButtonState.BLOCKING.ordinal(), ButtonState.values()); + private final SimpleInventory filters = new SimpleInventory(9, "Filters", 1); private int currentFilter = 0; public PipeItemsEmerald(int itemID) { @@ -68,8 +106,9 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { public ItemStack[] checkExtract(IInventory inventory, boolean doRemove, ForgeDirection from) { /* ISELECTIVEINVENTORY */ + // non blocking mode is not implemented for ISelectiveInventory yet if (inventory instanceof ISelectiveInventory) { - ItemStack[] stacks = ((ISelectiveInventory) inventory).extractItem(new ItemStack[]{getCurrentFilter()}, false, doRemove, from, (int) powerHandler.getEnergyStored()); + ItemStack[] stacks = ((ISelectiveInventory) inventory).extractItem(new ItemStack[] { getCurrentFilter() }, false, doRemove, from, (int) powerHandler.getEnergyStored()); if (doRemove) { for (ItemStack stack : stacks) { if (stack != null) { @@ -81,6 +120,7 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { return stacks; /* ISPECIALINVENTORY */ + // non blocking mode is not needed for ISpecialInventory since its not round robin anyway } else if (inventory instanceof ISpecialInventory) { ItemStack[] stacks = ((ISpecialInventory) inventory).extractItem(false, from, (int) powerHandler.getEnergyStored()); if (stacks != null) { @@ -117,8 +157,18 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { IInventory inv = Utils.getInventory(inventory); ItemStack result = checkExtractGeneric(inv, doRemove, from); + // check through every filter once if non-blocking + if (doRemove && stateController.getButtonState() == ButtonState.NONBLOCKING && result == null) { + int count = 1; + while (result == null && count < filters.getSizeInventory()) { + incrementFilter(); + result = checkExtractGeneric(inv, doRemove, from); + count++; + } + } + if (result != null) { - return new ItemStack[]{result}; + return new ItemStack[] { result }; } } @@ -176,6 +226,8 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { super.readFromNBT(nbt); filters.readFromNBT(nbt); currentFilter = nbt.getInteger("currentFilter"); + + stateController.readFromNBT(nbt, "state"); } @Override @@ -183,6 +235,8 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { super.writeToNBT(nbt); filters.writeToNBT(nbt); nbt.setInteger("currentFilter", currentFilter); + + stateController.writeToNBT(nbt, "state"); } // ICLIENTSTATE @@ -204,4 +258,18 @@ public class PipeItemsEmerald extends PipeItemsWood implements IClientState { public IInventory getFilters() { return filters; } + + public MultiButtonController getStateController() { + return stateController; + } + + @Override + public void writeGuiData(DataOutputStream data) throws IOException { + data.writeByte(stateController.getCurrentState()); + } + + @Override + public void readGuiData(DataInputStream data, EntityPlayer sender) throws IOException { + stateController.setCurrentState(data.readByte()); + } }