From 5f3ea0fe0b4fc53aa7d0761d2ac749a43f0d57e8 Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Fri, 20 Feb 2015 08:46:32 -0300 Subject: [PATCH] add stripes pipe retraction with void pipes --- .../buildcraft/transport/TileGenericPipe.java | 2 + .../stripes/StripesHandlerPipes.java | 59 +++++++++++++++---- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/common/buildcraft/transport/TileGenericPipe.java b/common/buildcraft/transport/TileGenericPipe.java index 0723645c..86a3977c 100644 --- a/common/buildcraft/transport/TileGenericPipe.java +++ b/common/buildcraft/transport/TileGenericPipe.java @@ -432,6 +432,8 @@ public class TileGenericPipe extends TileEntity implements IFluidHandler, public void initializeFromItemMetadata(int i) { if (i >= 1 && i <= 16) { setPipeColor((i - 1) & 15); + } else { + setPipeColor(-1); } } diff --git a/common/buildcraft/transport/stripes/StripesHandlerPipes.java b/common/buildcraft/transport/stripes/StripesHandlerPipes.java index af56f048..b2c08fe1 100644 --- a/common/buildcraft/transport/stripes/StripesHandlerPipes.java +++ b/common/buildcraft/transport/stripes/StripesHandlerPipes.java @@ -8,6 +8,8 @@ */ package buildcraft.transport.stripes; +import java.util.ArrayList; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; @@ -32,7 +34,7 @@ public class StripesHandlerPipes implements IStripesHandler { public StripesHandlerType getType() { return StripesHandlerType.ITEM_USE; } - + @Override public boolean shouldHandle(ItemStack stack) { return stack.getItem() instanceof ItemPipe; @@ -50,10 +52,10 @@ public class StripesHandlerPipes implements IStripesHandler { if (world.getBlock(x, y, z) != Blocks.air) { return false; } - + Position p = new Position(x, y, z, direction); p.moveBackwards(1.0d); - + TileEntity tile = world.getTileEntity((int) p.x, (int) p.y, (int) p.z); if (!(tile instanceof TileGenericPipe)) { return false; @@ -62,23 +64,32 @@ public class StripesHandlerPipes implements IStripesHandler { if (!(pipeTile.pipe.transport instanceof PipeTransportItems)) { return false; } - // Checks done, start to actually do stuff + + if (stack.getItem() != BuildCraftTransport.pipeItemsVoid) { + return extendPipe(world, x, y, z, direction, stack, player, pipeTile); + } else { + p.moveBackwards(1.0d); + return retractPipe(world, (int) p.x, (int) p.y, (int) p.z, direction, stack, player, pipeTile); + } + } + + private boolean extendPipe(World world, int x, int y, int z, ForgeDirection direction, ItemStack stack, EntityPlayer player, + TileGenericPipe pipeTile) { if (!copyPipeTo(world, pipeTile, x, y, z, player)) { return false; } - pipeTile.pipe.transport.container.initializeFromItemMetadata(stack.getItemDamage() - 1); + pipeTile.initializeFromItemMetadata(stack.getItemDamage()); Pipe newPipe = BlockGenericPipe.createPipe(stack.getItem()); - newPipe.setTile(pipeTile.pipe.container); - pipeTile.pipe.container.pipe = newPipe; + newPipe.setTile(pipeTile); + pipeTile.pipe = newPipe; pipeTile.updateEntity(); // Needed so that the tile does computeConnections() - + stack.stackSize--; if (stack.stackSize > 0) { - pipeTile.pipe.container.injectItem(stack, true, direction.getOpposite()); + pipeTile.injectItem(stack, true, direction.getOpposite()); } - return true; } @@ -90,4 +101,32 @@ public class StripesHandlerPipes implements IStripesHandler { } return true; } + + private boolean retractPipe(World world, int x, int y, int z, ForgeDirection direction, ItemStack stack, EntityPlayer player, + TileGenericPipe pipeTile) { + TileEntity prevTile = world.getTileEntity(x, y, z); + if (!(prevTile instanceof TileGenericPipe)) { + return false; + } + TileGenericPipe prevPipeTile = (TileGenericPipe) prevTile; + if (!(prevPipeTile.pipe.transport instanceof PipeTransportItems)) { + return false; + } + + int meta = prevPipeTile.getItemMetadata(); + ArrayList removedPipeStacks = world.getBlock(x, y, z).getDrops(world, x, y, z, meta, 0); + + prevPipeTile.initializeFromItemMetadata(pipeTile.getItemMetadata()); + Pipe newPipe = BlockGenericPipe.createPipe(BuildCraftTransport.pipeItemsStripes); + newPipe.setTile(prevPipeTile); + prevPipeTile.pipe = newPipe; + + world.setBlockToAir(pipeTile.x(), pipeTile.y(), pipeTile.z()); + + prevPipeTile.injectItem(stack, true, direction); + for (ItemStack itemStack : removedPipeStacks) { + prevPipeTile.injectItem(itemStack, true, direction); + } + return true; + } }