diff options
author | Martin Robertz <dream-master@gmx.net> | 2021-12-02 16:53:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 16:53:40 +0100 |
commit | c0a6800447fdb87e266acfb47f35b5f765c74fe3 (patch) | |
tree | 2066554d22848e6818689d41cd4ca7f683ebebb7 /src/main/java/gregtech/api/net | |
parent | 03b6fb248b1a20b9ccce7ac4da4c1a76875fa966 (diff) | |
download | GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.tar.gz GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.tar.bz2 GT5-Unofficial-c0a6800447fdb87e266acfb47f35b5f765c74fe3.zip |
add select circuit gui for machine and circuits itself (#773)
also fixed some issue with basic machine gui introduced in 9d42b299def1c41bbc7a1f01efe445be28f54399
also retrofitted volumetric flask to use the new INetworkUpdatableItem and GT_Packet_UpdateItem, deprecating MessageSetFlaskCapacity in the meanwhile.
To open the gui for machine, shift-left-click the circuit slot
To open the gui for circuit, click any block (need to be sneaking if it's chest, furnace, etc) with the circuit held in hand.
Signed-off-by: Glease <4586901+Glease@users.noreply.github.com>
Co-authored-by: Glease <4586901+Glease@users.noreply.github.com>
Diffstat (limited to 'src/main/java/gregtech/api/net')
3 files changed, 164 insertions, 0 deletions
diff --git a/src/main/java/gregtech/api/net/GT_Packet_New.java b/src/main/java/gregtech/api/net/GT_Packet_New.java index 59f6dc7251..10beae0e4d 100644 --- a/src/main/java/gregtech/api/net/GT_Packet_New.java +++ b/src/main/java/gregtech/api/net/GT_Packet_New.java @@ -1,6 +1,7 @@ package gregtech.api.net; import com.google.common.io.ByteArrayDataInput; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; diff --git a/src/main/java/gregtech/api/net/GT_Packet_SetConfigurationCircuit.java b/src/main/java/gregtech/api/net/GT_Packet_SetConfigurationCircuit.java new file mode 100644 index 0000000000..aeb183a02c --- /dev/null +++ b/src/main/java/gregtech/api/net/GT_Packet_SetConfigurationCircuit.java @@ -0,0 +1,102 @@ +package gregtech.api.net; + +import com.google.common.io.ByteArrayDataInput; +import cpw.mods.fml.common.network.ByteBufUtils; +import gregtech.api.interfaces.metatileentity.IMetaTileEntity; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_BasicMachine; +import gregtech.api.util.GT_Utility; +import gregtech.api.util.ISerializableObject; +import io.netty.buffer.ByteBuf; +import net.minecraft.item.ItemStack; +import net.minecraft.network.INetHandler; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; + +/** + * Client -> Server: Update machine configuration data + */ + +public class GT_Packet_SetConfigurationCircuit extends GT_Packet_New { + protected int mX; + protected short mY; + protected int mZ; + protected int dimId; + + protected ItemStack circuit; + + public GT_Packet_SetConfigurationCircuit() { + super(true); + } + + public GT_Packet_SetConfigurationCircuit(IGregTechTileEntity tile, ItemStack circuit) { + this(tile.getXCoord(), tile.getYCoord(), tile.getZCoord(), circuit); + } + + public GT_Packet_SetConfigurationCircuit(int x, short y, int z, ItemStack circuit) { + super(false); + + this.mX = x; + this.mY = y; + this.mZ = z; + + this.circuit = circuit; + } + + @Override + public byte getPacketID() { + return 12; + } + + @Override + public void encode(ByteBuf aOut) { + aOut.writeInt(mX); + aOut.writeShort(mY); + aOut.writeInt(mZ); + + // no null check needed. ByteBufUtils will handle it + ByteBufUtils.writeItemStack(aOut, this.circuit); + } + + @Override + public void setINetHandler(INetHandler aHandler) { + if (aHandler instanceof NetHandlerPlayServer) { + dimId = ((NetHandlerPlayServer) aHandler).playerEntity.dimension; + } else { + // packet sent to wrong side, so we need to ignore this one + // but there is no way to disrupt packet pipeline + // so we will instead go find world -2, which (hopefully) doesn't exist + // then we will fail silently in process() + dimId = -2; + } + } + + @Override + public GT_Packet_New decode(ByteArrayDataInput aData) { + return new GT_Packet_SetConfigurationCircuit( + aData.readInt(), + aData.readShort(), + aData.readInt(), + + ISerializableObject.readItemStackFromGreggyByteBuf(aData)); + } + + @Override + public void process(IBlockAccess aWorld) { + World world = DimensionManager.getWorld(dimId); + if (world == null) return; + TileEntity tile = world.getTileEntity(mX, mY, mZ); + if (!(tile instanceof IGregTechTileEntity) || ((IGregTechTileEntity) tile).isDead()) return; + IMetaTileEntity mte = ((IGregTechTileEntity) tile).getMetaTileEntity(); + if (!(mte instanceof GT_MetaTileEntity_BasicMachine)) return; + GT_MetaTileEntity_BasicMachine machine = (GT_MetaTileEntity_BasicMachine) mte; + if (!machine.allowSelectCircuit()) return; + machine.getConfigurationCircuits().stream() + .filter(stack -> GT_Utility.areStacksEqual(stack, circuit)) + .findFirst() + .ifPresent(stack -> mte.setInventorySlotContents(machine.getCircuitSlot(), stack)); + } +} diff --git a/src/main/java/gregtech/api/net/GT_Packet_UpdateItem.java b/src/main/java/gregtech/api/net/GT_Packet_UpdateItem.java new file mode 100644 index 0000000000..537f62ccec --- /dev/null +++ b/src/main/java/gregtech/api/net/GT_Packet_UpdateItem.java @@ -0,0 +1,61 @@ +package gregtech.api.net; + +import com.google.common.io.ByteArrayDataInput; +import cpw.mods.fml.common.network.ByteBufUtils; +import gregtech.api.interfaces.INetworkUpdatableItem; +import gregtech.api.util.ISerializableObject; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.INetHandler; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.world.IBlockAccess; + +/** + * Client -> Server: send arbitrary data to server and update the currently held item. + */ +public class GT_Packet_UpdateItem extends GT_Packet_New { + private NBTTagCompound tag; + private EntityPlayerMP mPlayer; + + public GT_Packet_UpdateItem() { + super(true); + } + + public GT_Packet_UpdateItem(NBTTagCompound tag) { + super(false); + this.tag = tag; + } + + @Override + public byte getPacketID() { + return 13; + } + + @Override + public void setINetHandler(INetHandler aHandler) { + if (aHandler instanceof NetHandlerPlayServer) { + mPlayer = ((NetHandlerPlayServer) aHandler).playerEntity; + } + } + + @Override + public void encode(ByteBuf aOut) { + ByteBufUtils.writeTag(aOut, tag); + } + + @Override + public GT_Packet_New decode(ByteArrayDataInput aData) { + return new GT_Packet_UpdateItem(ISerializableObject.readCompoundTagFromGreggyByteBuf(aData)); + } + + @Override + public void process(IBlockAccess aWorld) { + if (mPlayer == null) return; + ItemStack stack = mPlayer.inventory.getCurrentItem(); + if (stack != null && stack.getItem() instanceof INetworkUpdatableItem) { + ((INetworkUpdatableItem) stack.getItem()).receive(stack, mPlayer, tag); + } + } +} |