From b4daadcb4504a5ccb6252bc88177321fe96e452f Mon Sep 17 00:00:00 2001 From: Sen Date: Wed, 13 Aug 2025 17:10:55 +0200 Subject: [PATCH] add pipe display item --- .../java/client/network/ClientPlayer.java | 11 ++ .../renderer/tileentity/ItemPipeRenderer.java | 34 +++++ .../renderer/tileentity/SpecialRenderer.java | 2 + .../textures/blocks/suction_pipe.png | Bin 0 -> 173 bytes .../textures/blocks/suction_pipe_selected.png | Bin 0 -> 178 bytes .../block/tech/BlockDirectionalPipe.java | 57 +++++++++ .../java/common/block/tech/BlockItemPipe.java | 15 +++ .../java/common/block/tech/BlockPipe.java | 96 +++++---------- .../common/block/tech/BlockStraightPipe.java | 32 +++++ .../common/block/tech/BlockSuctionPipe.java | 15 +++ .../main/java/common/init/BlockRegistry.java | 3 +- common/src/main/java/common/init/Blocks.java | 3 +- common/src/main/java/common/init/Items.java | 1 + .../java/common/network/IClientPlayer.java | 2 + .../java/common/network/PacketRegistry.java | 2 + .../java/common/packet/SPacketUpdatePipe.java | 45 +++++++ ...ntityPipe.java => TileEntityItemPipe.java} | 116 +++++++++++------- 17 files changed, 322 insertions(+), 112 deletions(-) create mode 100644 client/src/main/java/client/renderer/tileentity/ItemPipeRenderer.java create mode 100755 client/src/main/resources/textures/blocks/suction_pipe.png create mode 100644 client/src/main/resources/textures/blocks/suction_pipe_selected.png create mode 100644 common/src/main/java/common/block/tech/BlockDirectionalPipe.java create mode 100644 common/src/main/java/common/block/tech/BlockItemPipe.java create mode 100644 common/src/main/java/common/block/tech/BlockStraightPipe.java create mode 100644 common/src/main/java/common/block/tech/BlockSuctionPipe.java create mode 100644 common/src/main/java/common/packet/SPacketUpdatePipe.java rename common/src/main/java/common/tileentity/{TileEntityPipe.java => TileEntityItemPipe.java} (82%) diff --git a/client/src/main/java/client/network/ClientPlayer.java b/client/src/main/java/client/network/ClientPlayer.java index dfdc97d5..ce48f392 100755 --- a/client/src/main/java/client/network/ClientPlayer.java +++ b/client/src/main/java/client/network/ClientPlayer.java @@ -119,6 +119,7 @@ import common.packet.SPacketTimeUpdate; import common.packet.SPacketTrades; import common.packet.SPacketUpdateDisplay; import common.packet.SPacketUpdateHealth; +import common.packet.SPacketUpdatePipe; import common.rng.Random; import common.sound.PositionedSound; import common.sound.Sound; @@ -126,6 +127,7 @@ import common.tileentity.TileEntity; import common.tileentity.TileEntityChest; import common.tileentity.Device; import common.tileentity.TileEntityDisplay; +import common.tileentity.TileEntityItemPipe; import common.tileentity.TileEntitySign; import common.util.BlockPos; import common.util.Pair; @@ -1267,6 +1269,15 @@ public class ClientPlayer implements IClientPlayer } } + public void handleUpdatePipe(SPacketUpdatePipe packet) { + NetHandler.checkThread(packet, this, this.gm, this.world); + if(this.gm.world.isBlockLoaded(packet.getPos())) { + TileEntity te = this.gm.world.getTileEntity(packet.getPos()); + if(te instanceof TileEntityItemPipe pipe) + pipe.setDisplayedItem(packet.getItem()); + } + } + /** * Sets the progressbar of the opened window to the specified value */ diff --git a/client/src/main/java/client/renderer/tileentity/ItemPipeRenderer.java b/client/src/main/java/client/renderer/tileentity/ItemPipeRenderer.java new file mode 100644 index 00000000..324ab022 --- /dev/null +++ b/client/src/main/java/client/renderer/tileentity/ItemPipeRenderer.java @@ -0,0 +1,34 @@ +package client.renderer.tileentity; + +import org.lwjgl.opengl.GL11; + +import client.Client; +import client.renderer.GlState; +import client.renderer.blockmodel.IBakedModel; +import client.renderer.entity.RenderItem; +import client.renderer.texture.TextureMap; +import common.item.ItemStack; +import common.tileentity.TileEntityItemPipe; + +public class ItemPipeRenderer extends ElementRenderer { + public void renderElements(TileEntityItemPipe te, double x, double y, double z, float partialTicks) { + ItemStack itemstack = te.getDisplayedItem(); + if(itemstack == null) + return; + Client.CLIENT.getTextureManager().bindTexture(TextureMap.BLOCKS); + GlState.enableRescaleNormal(); + GlState.alphaFunc(GL11.GL_GREATER, 0.1F); + GlState.enableBlend(); + GlState.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); + RenderItem itemRenderer = Client.CLIENT.getRenderItem(); + IBakedModel ibakedmodel = itemRenderer.getItemModelMesher().getItemModel(itemstack); + GL11.glPushMatrix(); + GL11.glTranslatef((float)x + 0.5F, (float)y + 0.5f, (float)z + 0.5F); + if(ibakedmodel.isGui3d()) + GL11.glScalef(0.5F, 0.5F, 0.5F); + itemRenderer.renderItem(itemstack, ibakedmodel); + GL11.glPopMatrix(); + GlState.disableRescaleNormal(); + GlState.disableBlend(); + } +} diff --git a/client/src/main/java/client/renderer/tileentity/SpecialRenderer.java b/client/src/main/java/client/renderer/tileentity/SpecialRenderer.java index ce9e4a64..d94c3fb2 100755 --- a/client/src/main/java/client/renderer/tileentity/SpecialRenderer.java +++ b/client/src/main/java/client/renderer/tileentity/SpecialRenderer.java @@ -9,6 +9,7 @@ import common.collect.Maps; import common.entity.Entity; import common.tileentity.TileEntity; import common.tileentity.TileEntityDisplay; +import common.tileentity.TileEntityItemPipe; import common.tileentity.TileEntitySign; import common.util.BlockPos; import common.world.World; @@ -29,6 +30,7 @@ public class SpecialRenderer { private SpecialRenderer() { this.renderers.put(TileEntitySign.class, new SignRenderer()); this.renderers.put(TileEntityDisplay.class, new DisplayRenderer()); + this.renderers.put(TileEntityItemPipe.class, new ItemPipeRenderer()); } private ElementRenderer getRenderer(Class clazz) { diff --git a/client/src/main/resources/textures/blocks/suction_pipe.png b/client/src/main/resources/textures/blocks/suction_pipe.png new file mode 100755 index 0000000000000000000000000000000000000000..dd6f1328e50128ede96e5e3133008a8584b6d732 GIT binary patch literal 173 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkEbHUgGKN%KnmrmsiMCQYs`AC}id7;uvCaIypgtwI|}pzjhtogR3fh zzevSY2`Hpwp7=jusp9`m(c@VRnngZS+72q1GK4E!QkQa9U|P literal 0 HcmV?d00001 diff --git a/client/src/main/resources/textures/blocks/suction_pipe_selected.png b/client/src/main/resources/textures/blocks/suction_pipe_selected.png new file mode 100644 index 0000000000000000000000000000000000000000..270be48fb7f1d5508d56e4532349b46e3896d6fc GIT binary patch literal 178 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkEbHUgGKN%Knmrmsgni{O%9EKp}fi7sn8b)5!@EtUWK7{!2>yYuDpF zxT>P`ixiuiPE~-ziAxjyD|$Qs6V~p#$`JHqh6Zo5V-~|Yhb8_deGUu^ZU1Gm%`(!; QfkrWSy85}Sb4q9e0In7?PXGV_ literal 0 HcmV?d00001 diff --git a/common/src/main/java/common/block/tech/BlockDirectionalPipe.java b/common/src/main/java/common/block/tech/BlockDirectionalPipe.java new file mode 100644 index 00000000..846102f7 --- /dev/null +++ b/common/src/main/java/common/block/tech/BlockDirectionalPipe.java @@ -0,0 +1,57 @@ +package common.block.tech; + +import common.model.Model; +import common.model.Model.ModelProvider; +import common.properties.Property; +import common.properties.PropertyBool; +import common.util.BlockPos; +import common.util.Clientside; +import common.util.Facing; +import common.world.IWorldAccess; +import common.world.State; + +public abstract class BlockDirectionalPipe extends BlockPipe { + public static final PropertyBool DOWN = PropertyBool.create("down"); + public static final PropertyBool UP = PropertyBool.create("up"); + public static final PropertyBool NORTH = PropertyBool.create("north"); + public static final PropertyBool EAST = PropertyBool.create("east"); + public static final PropertyBool SOUTH = PropertyBool.create("south"); + public static final PropertyBool WEST = PropertyBool.create("west"); + + public BlockDirectionalPipe() { + this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.DOWN).withProperty(DOWN, false).withProperty(UP, false).withProperty(NORTH, false).withProperty(EAST, false).withProperty(SOUTH, false).withProperty(WEST, false)); + } + + public State getState(State state, IWorldAccess world, BlockPos pos) { + return state.withProperty(DOWN, this.canConnectTo(world.getState(pos.down()).getBlock())) + .withProperty(UP, this.canConnectTo(world.getState(pos.up()).getBlock())) + .withProperty(NORTH, this.canConnectTo(world.getState(pos.north()).getBlock())) + .withProperty(EAST, this.canConnectTo(world.getState(pos.east()).getBlock())) + .withProperty(SOUTH, this.canConnectTo(world.getState(pos.south()).getBlock())) + .withProperty(WEST, this.canConnectTo(world.getState(pos.west()).getBlock())); + } + + public State getItemState() { + return this.getState().withProperty(FACING, Facing.EAST).withProperty(DOWN, false).withProperty(UP, false).withProperty(NORTH, false).withProperty(EAST, true).withProperty(SOUTH, false).withProperty(WEST, true); + } + + protected Property[] getUnsavedProperties() { + return new Property[] {NORTH, SOUTH, DOWN, UP, WEST, EAST}; + } + + protected Property[] getProperties() { + return new Property[] {FACING, DOWN, UP, NORTH, EAST, WEST, SOUTH}; + } + + @Clientside + public Model getModel(ModelProvider provider, String name, State state) { + Facing dir = state.getValue(FACING); + boolean down = state.getValue(DOWN); + boolean up = state.getValue(UP); + boolean north = state.getValue(NORTH); + boolean south = state.getValue(SOUTH); + boolean west = state.getValue(WEST); + boolean east = state.getValue(EAST); + return getPipeModel(provider, name, dir, down, up, north, south, west, east); + } +} diff --git a/common/src/main/java/common/block/tech/BlockItemPipe.java b/common/src/main/java/common/block/tech/BlockItemPipe.java new file mode 100644 index 00000000..dc514d04 --- /dev/null +++ b/common/src/main/java/common/block/tech/BlockItemPipe.java @@ -0,0 +1,15 @@ +package common.block.tech; + +import common.block.Block; +import common.tileentity.TileEntity; +import common.tileentity.TileEntityItemPipe; + +public class BlockItemPipe extends BlockDirectionalPipe { + public boolean canConnectTo(Block block) { + return block instanceof BlockMachine || block instanceof BlockItemPipe || block instanceof BlockSuctionPipe || block instanceof BlockChest; + } + + public TileEntity createNewTileEntity() { + return new TileEntityItemPipe(false); + } +} diff --git a/common/src/main/java/common/block/tech/BlockPipe.java b/common/src/main/java/common/block/tech/BlockPipe.java index e01ce372..bf6724ff 100755 --- a/common/src/main/java/common/block/tech/BlockPipe.java +++ b/common/src/main/java/common/block/tech/BlockPipe.java @@ -9,13 +9,13 @@ import common.block.Material; import common.entity.Entity; import common.entity.npc.EntityNPC; import common.entity.types.EntityLiving; +import common.init.Items; import common.item.CheatTab; +import common.item.ItemStack; import common.model.Model; import common.model.Model.ModelProvider; -import common.properties.Property; -import common.properties.PropertyBool; import common.tileentity.TileEntity; -import common.tileentity.TileEntityPipe; +import common.tileentity.TileEntityItemPipe; import common.util.BlockPos; import common.util.BoundingBox; import common.util.Clientside; @@ -25,51 +25,24 @@ import common.world.State; import common.world.World; import common.world.AWorldServer; -public class BlockPipe extends Block implements ITileEntityProvider, Directional { - public static final PropertyBool DOWN = PropertyBool.create("down"); - public static final PropertyBool UP = PropertyBool.create("up"); - public static final PropertyBool NORTH = PropertyBool.create("north"); - public static final PropertyBool EAST = PropertyBool.create("east"); - public static final PropertyBool SOUTH = PropertyBool.create("south"); - public static final PropertyBool WEST = PropertyBool.create("west"); - +public abstract class BlockPipe extends Block implements ITileEntityProvider, Directional { private static boolean keepInventory; - + public BlockPipe() { super(Material.SOLID); - this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.DOWN).withProperty(DOWN, false).withProperty(UP, false).withProperty(NORTH, false).withProperty(EAST, false).withProperty(SOUTH, false).withProperty(WEST, false)); this.setTab(CheatTab.TECHNOLOGY); this.setBlockBounds(0.25f, 0.25f, 0.25f, 0.75f, 0.75f, 0.75f); } - public boolean canConnectTo(Block block) { - return block instanceof BlockMachine || block instanceof BlockPipe || block instanceof BlockChest; - } - - public State getState(State state, IWorldAccess world, BlockPos pos) { - return state.withProperty(DOWN, this.canConnectTo(world.getState(pos.down()).getBlock())) - .withProperty(UP, this.canConnectTo(world.getState(pos.up()).getBlock())) - .withProperty(NORTH, this.canConnectTo(world.getState(pos.north()).getBlock())) - .withProperty(EAST, this.canConnectTo(world.getState(pos.east()).getBlock())) - .withProperty(SOUTH, this.canConnectTo(world.getState(pos.south()).getBlock())) - .withProperty(WEST, this.canConnectTo(world.getState(pos.west()).getBlock())); - } + public abstract boolean canConnectTo(Block block); - public State getItemState() { - return this.getState().withProperty(FACING, Facing.EAST).withProperty(DOWN, false).withProperty(UP, false).withProperty(NORTH, false).withProperty(EAST, true).withProperty(SOUTH, false).withProperty(WEST, true); - } - - protected Property[] getUnsavedProperties() { - return new Property[] {NORTH, SOUTH, DOWN, UP, WEST, EAST}; - } - - public void setBlockBounds(IWorldAccess world, BlockPos pos) { + public final void setBlockBounds(IWorldAccess world, BlockPos pos) { this.setBlockBounds(this.canConnectTo(world.getState(pos.west()).getBlock()) ? 0.0f : 0.25f, this.canConnectTo(world.getState(pos.down()).getBlock()) ? 0.0f : 0.25f, this.canConnectTo(world.getState(pos.north()).getBlock()) ? 0.0f : 0.25f, this.canConnectTo(world.getState(pos.east()).getBlock()) ? 1.0f : 0.75f, this.canConnectTo(world.getState(pos.up()).getBlock()) ? 1.0f : 0.75f, this.canConnectTo(world.getState(pos.south()).getBlock()) ? 1.0f : 0.75f); } - public void getCollisionBoxes(World world, BlockPos pos, State state, BoundingBox mask, List list, Entity collidingEntity) { + public final void getCollisionBoxes(World world, BlockPos pos, State state, BoundingBox mask, List list, Entity collidingEntity) { this.setBlockBounds(0.25f, 0.25f, 0.25f, 0.75f, 0.75f, 0.75f); super.getCollisionBoxes(world, pos, state, mask, list, collidingEntity); if(this.canConnectTo(world.getState(pos.west()).getBlock())) { @@ -103,19 +76,27 @@ public class BlockPipe extends Block implements ITileEntityProvider, Directional return this.getState().withProperty(FACING, facing.getOpposite()); } - public TileEntity createNewTileEntity() { - return new TileEntityPipe(); - } - public boolean onUse(World world, BlockPos pos, State state, EntityNPC player, Facing side, float hitX, float hitY, float hitZ) { + ItemStack stack = player.getHeldItem(); + if(stack == null || stack.getItem() != Items.hoe) + return false; if(world.client) return true; TileEntity te = world.getTileEntity(pos); - if(te instanceof TileEntityPipe) { - state = state.cycleProperty(FACING); + if(te instanceof TileEntityItemPipe) { + Facing face = state.getValue(FACING); + for(int z = 0; z < Facing.values().length; z++) { + Facing dir = Facing.values()[(face.ordinal() + 1 + z) % Facing.values().length]; + if(this.canConnectTo(world.getState(pos.offset(dir)).getBlock())) { + face = dir; + break; + } + } + if(face == state.getValue(FACING)) + return true; keepInventory = true; - world.setState(pos, state, 3); - world.setState(pos, state, 3); + world.setState(pos, state.withProperty(FACING, face), 3); + world.setState(pos, state.withProperty(FACING, face), 3); keepInventory = false; te.validate(); world.setTileEntity(pos, te); @@ -126,46 +107,35 @@ public class BlockPipe extends Block implements ITileEntityProvider, Directional public void onRemoved(AWorldServer worldIn, BlockPos pos, State state) { if(!keepInventory) { TileEntity tileentity = worldIn.getTileEntity(pos); - if(tileentity instanceof TileEntityPipe) - dropItems(worldIn, pos, (TileEntityPipe)tileentity); + if(tileentity instanceof TileEntityItemPipe) + dropItems(worldIn, pos, (TileEntityItemPipe)tileentity); } } - public boolean isFullCube() { + public final boolean isFullCube() { return false; } - public boolean isOpaqueCube() { + public final boolean isOpaqueCube() { return false; } @Clientside - public boolean canRender(IWorldAccess worldIn, BlockPos pos, Facing side) { + public final boolean canRender(IWorldAccess worldIn, BlockPos pos, Facing side) { return true; } @Clientside - public boolean hasTransparency() { + public final boolean hasTransparency() { return true; } - protected Property[] getProperties() { - return new Property[] {FACING, DOWN, UP, NORTH, EAST, WEST, SOUTH}; - } - - public boolean isMagnetic() { + public final boolean isMagnetic() { return true; } @Clientside - public Model getModel(ModelProvider provider, String name, State state) { - Facing dir = state.getValue(FACING); - boolean down = state.getValue(DOWN); - boolean up = state.getValue(UP); - boolean north = state.getValue(NORTH); - boolean south = state.getValue(SOUTH); - boolean west = state.getValue(WEST); - boolean east = state.getValue(EAST); + protected static Model getPipeModel(ModelProvider provider, String name, Facing dir, boolean down, boolean up, boolean north, boolean south, boolean west, boolean east) { String sel = name + "_selected"; Model model = provider.getModel(name); if(!down || !up || !north || !south || !west || !east) { @@ -196,5 +166,5 @@ public class BlockPipe extends Block implements ITileEntityProvider, Directional if(east) model.add(12, 4, 4, 16, 12, 12).du(dir == Facing.EAST ? sel : name).uv(4, 4, 8, 12).noCull().n(dir == Facing.EAST ? sel : name).uv(8, 4, 12, 12).noCull().s(dir == Facing.EAST ? sel : name).uv(4, 4, 8, 12).noCull(); return model; - } + } } diff --git a/common/src/main/java/common/block/tech/BlockStraightPipe.java b/common/src/main/java/common/block/tech/BlockStraightPipe.java new file mode 100644 index 00000000..516abeff --- /dev/null +++ b/common/src/main/java/common/block/tech/BlockStraightPipe.java @@ -0,0 +1,32 @@ +package common.block.tech; + +import common.model.Model; +import common.model.Model.ModelProvider; +import common.properties.Property; +import common.util.Clientside; +import common.util.Facing; +import common.util.Facing.Axis; +import common.world.State; + +public abstract class BlockStraightPipe extends BlockPipe { + public BlockStraightPipe() { + this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.DOWN)); + } + + public State getItemState() { + return this.getState().withProperty(FACING, Facing.EAST); + } + + protected Property[] getProperties() { + return new Property[] {FACING}; + } + + @Clientside + public Model getModel(ModelProvider provider, String name, State state) { + Facing dir = state.getValue(FACING); + boolean y = dir.getAxis() == Axis.Y; + boolean z = dir.getAxis() == Axis.Z; + boolean x = dir.getAxis() == Axis.X; + return getPipeModel(provider, name, dir, y, y, z, z, x, x); + } +} diff --git a/common/src/main/java/common/block/tech/BlockSuctionPipe.java b/common/src/main/java/common/block/tech/BlockSuctionPipe.java new file mode 100644 index 00000000..b87964b0 --- /dev/null +++ b/common/src/main/java/common/block/tech/BlockSuctionPipe.java @@ -0,0 +1,15 @@ +package common.block.tech; + +import common.block.Block; +import common.tileentity.TileEntity; +import common.tileentity.TileEntityItemPipe; + +public class BlockSuctionPipe extends BlockStraightPipe { + public boolean canConnectTo(Block block) { + return block instanceof BlockMachine || block instanceof BlockItemPipe || block instanceof BlockChest; + } + + public TileEntity createNewTileEntity() { + return new TileEntityItemPipe(true); + } +} diff --git a/common/src/main/java/common/init/BlockRegistry.java b/common/src/main/java/common/init/BlockRegistry.java index 75b44e28..d781dc9a 100755 --- a/common/src/main/java/common/init/BlockRegistry.java +++ b/common/src/main/java/common/init/BlockRegistry.java @@ -592,7 +592,8 @@ public abstract class BlockRegistry { register("sticky_piston_head", (new BlockPistonHead(true)).setDisplay("Klebriger Kolben")); register("dispenser", (new BlockDispenser(false)).setHardness(3.5F).setSound(SoundType.STONE).setDisplay("Werfer")); register("dropper", (new BlockDispenser(true)).setHardness(3.5F).setSound(SoundType.STONE).setDisplay("Spender")); - register("pipe", (new BlockPipe()).setHardness(3.0F).setResistance(8.0F).setSound(SoundType.STONE).setDisplay("Rohr")); + register("pipe", (new BlockItemPipe()).setHardness(3.0F).setResistance(8.0F).setSound(SoundType.STONE).setDisplay("Rohr")); + register("suction_pipe", (new BlockSuctionPipe()).setHardness(3.0F).setResistance(8.0F).setSound(SoundType.STONE).setDisplay("Saugrohr")); register("tian_reactor", (new BlockTianReactor()).setHardness(3.0F).setResistance(8.0F).setSound(SoundType.STONE).setDisplay("Tianreaktor")); register("rail", (new BlockRail()).setHardness(0.7F).setSound(SoundType.STONE).setDisplay("Schiene").setMiningTool(Equipment.PICKAXE, 0)); diff --git a/common/src/main/java/common/init/Blocks.java b/common/src/main/java/common/init/Blocks.java index 9605955b..04653bc6 100755 --- a/common/src/main/java/common/init/Blocks.java +++ b/common/src/main/java/common/init/Blocks.java @@ -867,7 +867,8 @@ public abstract class Blocks { public static final BlockFurnace neptunium_furnace = get("neptunium_furnace"); public static final BlockFurnace plutonium_furnace = get("plutonium_furnace"); public static final BlockFurnace titanium_furnace = get("titanium_furnace"); - public static final BlockPipe pipe = get("pipe"); + public static final BlockItemPipe pipe = get("pipe"); + public static final BlockSuctionPipe suction_pipe = get("suction_pipe"); private static T get(String id) { T block = (T)BlockRegistry.byNameExact(id); diff --git a/common/src/main/java/common/init/Items.java b/common/src/main/java/common/init/Items.java index b9724529..08084d36 100755 --- a/common/src/main/java/common/init/Items.java +++ b/common/src/main/java/common/init/Items.java @@ -1245,6 +1245,7 @@ public abstract class Items { public static final Item plutonium_furnace = get("plutonium_furnace"); public static final Item titanium_furnace = get("titanium_furnace"); public static final Item pipe = get("pipe"); + public static final Item suction_pipe = get("suction_pipe"); private static T get(String id) { T item = (T)ItemRegistry.byName(id); diff --git a/common/src/main/java/common/network/IClientPlayer.java b/common/src/main/java/common/network/IClientPlayer.java index 5934134c..a7c1db28 100644 --- a/common/src/main/java/common/network/IClientPlayer.java +++ b/common/src/main/java/common/network/IClientPlayer.java @@ -62,6 +62,7 @@ import common.packet.SPacketTimeUpdate; import common.packet.SPacketTrades; import common.packet.SPacketUpdateDisplay; import common.packet.SPacketUpdateHealth; +import common.packet.SPacketUpdatePipe; import common.sound.Sound; public interface IClientPlayer extends NetHandler { @@ -139,4 +140,5 @@ public interface IClientPlayer extends NetHandler { void handleServerConfig(SPacketServerConfig packet); void handleCelestials(SPacketCelestials packet); void handleDimensions(SPacketDimensions packet); + void handleUpdatePipe(SPacketUpdatePipe packet); } \ No newline at end of file diff --git a/common/src/main/java/common/network/PacketRegistry.java b/common/src/main/java/common/network/PacketRegistry.java index ea42e88e..593463c0 100755 --- a/common/src/main/java/common/network/PacketRegistry.java +++ b/common/src/main/java/common/network/PacketRegistry.java @@ -99,6 +99,7 @@ import common.packet.SPacketTimeUpdate; import common.packet.SPacketTrades; import common.packet.SPacketUpdateDisplay; import common.packet.SPacketUpdateHealth; +import common.packet.SPacketUpdatePipe; public enum PacketRegistry { HANDSHAKE {{ @@ -184,6 +185,7 @@ public enum PacketRegistry { this.server(SPacketServerConfig.class); this.server(SPacketCelestials.class); this.server(SPacketDimensions.class); + this.server(SPacketUpdatePipe.class); this.client(CPacketKeepAlive.class); this.client(CPacketMessage.class); diff --git a/common/src/main/java/common/packet/SPacketUpdatePipe.java b/common/src/main/java/common/packet/SPacketUpdatePipe.java new file mode 100644 index 00000000..c13d6973 --- /dev/null +++ b/common/src/main/java/common/packet/SPacketUpdatePipe.java @@ -0,0 +1,45 @@ +package common.packet; + +import java.io.IOException; + +import common.item.ItemStack; +import common.network.IClientPlayer; +import common.network.Packet; +import common.network.PacketBuffer; +import common.tileentity.TileEntityItemPipe; +import common.util.BlockPos; + +public class SPacketUpdatePipe implements Packet { + private BlockPos position; + private ItemStack item; + + public SPacketUpdatePipe() { + } + + public SPacketUpdatePipe(TileEntityItemPipe tile) { + this.position = tile.getPos(); + this.item = tile.getDisplayedItem(); + } + + public void readPacketData(PacketBuffer buf) throws IOException { + this.position = buf.readBlockPos(); + this.item = buf.readItemStack(); + } + + public void writePacketData(PacketBuffer buf) throws IOException { + buf.writeBlockPos(this.position); + buf.writeItemStack(this.item); + } + + public void processPacket(IClientPlayer handler) { + handler.handleUpdatePipe(this); + } + + public BlockPos getPos() { + return this.position; + } + + public ItemStack getItem() { + return this.item; + } +} diff --git a/common/src/main/java/common/tileentity/TileEntityPipe.java b/common/src/main/java/common/tileentity/TileEntityItemPipe.java similarity index 82% rename from common/src/main/java/common/tileentity/TileEntityPipe.java rename to common/src/main/java/common/tileentity/TileEntityItemPipe.java index 459c0f5d..0ba8e244 100755 --- a/common/src/main/java/common/tileentity/TileEntityPipe.java +++ b/common/src/main/java/common/tileentity/TileEntityItemPipe.java @@ -9,21 +9,41 @@ import common.block.tech.BlockChest; import common.block.tech.BlockPipe; import common.entity.Entity; import common.entity.item.EntityItem; -import common.entity.npc.EntityNPC; import common.entity.types.EntityLiving; import common.inventory.IInventory; import common.inventory.ISidedInventory; import common.item.ItemStack; +import common.network.Packet; +import common.packet.SPacketUpdatePipe; import common.tags.TagObject; import common.util.BlockPos; import common.util.BoundingBox; import common.util.Facing; +import common.util.Serverside; import common.vars.Vars; import common.world.State; -public class TileEntityPipe extends TileEntity implements IInventory, ITickable { +public class TileEntityItemPipe extends TileEntity implements IInventory, ITickable { + private final boolean suck; + private ItemStack stack = null; + @Serverside + private ItemStack last = null; + private ItemStack display = null; + private ItemStack prevDisplay = null; private int cooldown = -1; + + public TileEntityItemPipe(boolean suck) { + this.suck = suck; + } + + public ItemStack getDisplayedItem() { + return this.display == null ? this.prevDisplay : this.display; + } + + public void setDisplayedItem(ItemStack item) { + this.display = item; + } public void readTags(TagObject tag) { super.readTags(tag); @@ -90,44 +110,42 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable this.stack = null; } - public boolean isUseableByPlayer(EntityNPC player) { - return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D; - } - public void update() { if(this.world != null && !this.world.client) { + this.prevDisplay = this.display; + this.display = ItemStack.copy(this.stack); + --this.cooldown; - if(!this.isOnTransferCooldown()) { this.setTransferCooldown(0); - this.updatePipe(); + this.transferItems(); } + + if(!ItemStack.allEquals(this.last, this.getDisplayedItem())) + this.world.markBlockForUpdate(this.pos); + this.last = this.getDisplayedItem(); } } - protected boolean updatePipe() { - if(!this.isOnTransferCooldown()) { - State state = this.getState(); - if(!(state.getBlock() instanceof BlockPipe) /* || !this.decrPower() */) // TODO: power - return false; - boolean flag = false; + protected boolean transferItems() { + if(!(this.getState().getBlock() instanceof BlockPipe) /* || !this.decrPower() */) // TODO: power + return false; + boolean flag = false; - if(!this.isEmpty()) { - flag = this.transferOut(); - } - - if(!this.isFull()) { - flag |= this.transferIn(false); - } - - if(flag) { - this.setTransferCooldown(Vars.pipeDelay); - this.markDirty(); - return true; - } + if(!this.isEmpty()) { + flag = this.transferOut(); } - return false; + if(!this.isFull()) { + flag |= this.transferIn(); + } + + if(flag) { + this.setTransferCooldown(Vars.pipeDelay); + this.markDirty(); + } + + return flag; } private boolean isEmpty() { @@ -145,14 +163,18 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable public boolean isOnTransferCooldown() { return this.cooldown > 0; } - - public boolean mayTransfer() { - return this.cooldown <= 1; - } +// +// public boolean mayTransfer() { +// return this.cooldown <= 1; +// } public int getColor() { return 0x0040ff; } + + public Packet getDescriptionPacket() { + return new SPacketUpdatePipe(this); + } // out @@ -185,7 +207,7 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable protected boolean transferOut() { State state = this.getState(); - IInventory iinventory = this.getInventory(this.pos.offset(state.getValue(BlockPipe.FACING)), false); + IInventory iinventory = this.getInventory(this.pos.offset(state.getValue(BlockPipe.FACING)), true, false); if(iinventory == null) return false; Facing dir = state.getValue(BlockPipe.FACING).getOpposite(); @@ -285,11 +307,11 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable }); } - protected boolean transferIn(boolean suck) { + protected boolean transferIn() { State state = this.getState(); Facing facing = state.getValue(BlockPipe.FACING); for(Facing dir : Facing.values()) { - IInventory inv = dir == facing ? null : this.getInventory(this.pos.offset(dir), suck && dir == facing.getOpposite()); + IInventory inv = (this.suck ? dir == facing.getOpposite() : dir != facing) ? this.getInventory(this.pos.offset(dir), false, this.suck) : null; if(inv != null) { Facing opposite = dir.getOpposite(); @@ -316,7 +338,7 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable } } } - else if(suck && dir == facing.getOpposite()) { + else if(this.suck && dir == facing.getOpposite()) { for(EntityItem entity : this.findDroppedItems(this.pos.offset(dir))) { if(putDropInInventoryAllSlots(this, entity)) { return true; @@ -358,15 +380,15 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable } if(flag) { - if(inventoryIn instanceof TileEntityPipe) { - TileEntityPipe pipe = (TileEntityPipe)inventoryIn; - - if(pipe.mayTransfer()) { - pipe.setTransferCooldown(Vars.pipeDelay); - } - - inventoryIn.markDirty(); - } +// if(inventoryIn instanceof TileEntityItemPipe) { +// TileEntityItemPipe pipe = (TileEntityItemPipe)inventoryIn; +// +// if(pipe.mayTransfer()) { +// pipe.setTransferCooldown(Vars.pipeDelay); +// } +// +// inventoryIn.markDirty(); +// } inventoryIn.markDirty(); } @@ -409,13 +431,13 @@ public class TileEntityPipe extends TileEntity implements IInventory, ITickable return list.size() > 0 ? (IInventory)list.get(this.world.rand.zrange(list.size())) : null; } - private IInventory getInventory(BlockPos blockpos, boolean suck) { + private IInventory getInventory(BlockPos blockpos, boolean pipe, boolean suck) { IInventory inv = null; Block block = this.world.getState(blockpos).getBlock(); if(block instanceof ITileEntityProvider) { TileEntity te = this.world.getTileEntity(blockpos); - if(te instanceof IInventory) { + if(te instanceof IInventory && (pipe || !(te instanceof TileEntityItemPipe))) { inv = (IInventory)te; if(inv instanceof TileEntityChest && block instanceof BlockChest) {