From 36ec39a59e54b709d4cf9222128058a273623887 Mon Sep 17 00:00:00 2001 From: asiekierka Date: Fri, 20 Mar 2015 15:07:12 +0100 Subject: [PATCH] improvements to Stripes Pipes --- buildcraft_resources/changelog/7.0.0 | 4 + common/buildcraft/BuildCraftTransport.java | 29 ++++ .../buildcraft/builders/LibraryDatabase.java | 2 +- .../builders/TileConstructionMarker.java | 1 - .../buildcraft/transport/TileGenericPipe.java | 15 +- .../transport/render/FacadeRenderHelper.java | 4 +- .../stripes/PipeExtensionListener.java | 149 ++++++++++++++++++ .../stripes/StripesHandlerPipeWires.java | 60 +++++++ .../stripes/StripesHandlerPipes.java | 74 +-------- .../stripes/StripesHandlerPlaceBlock.java | 8 + 10 files changed, 264 insertions(+), 82 deletions(-) create mode 100644 common/buildcraft/transport/stripes/PipeExtensionListener.java create mode 100644 common/buildcraft/transport/stripes/StripesHandlerPipeWires.java diff --git a/buildcraft_resources/changelog/7.0.0 b/buildcraft_resources/changelog/7.0.0 index a5422aeb..1b452083 100644 --- a/buildcraft_resources/changelog/7.0.0 +++ b/buildcraft_resources/changelog/7.0.0 @@ -14,6 +14,10 @@ Additions: * Light Sensor expansion for detecting brightness in front of a gate (asie) * Pipes: * Massive optimizations - up to 3x less server-intensive (asie, Player) + * Improvements to Stripes Pipes: + * Extending or retracting a Stripes Pipe now carries over all its gates, pipe wires and pluggables (asie) + * You can now extend pipe wires by putting them into a Stripes Pipe (asie) + * PlaceBlock now works directly in front of a block again (asie) * Made items in pipes move at a much smoother rate (asie) * Robots: * Improved robot light handling - the light now denotes sleep state and battery charge level (asie) diff --git a/common/buildcraft/BuildCraftTransport.java b/common/buildcraft/BuildCraftTransport.java index 6732d0f1..4effb727 100644 --- a/common/buildcraft/BuildCraftTransport.java +++ b/common/buildcraft/BuildCraftTransport.java @@ -20,7 +20,9 @@ import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; +import net.minecraft.world.WorldServer; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLInitializationEvent; @@ -29,9 +31,15 @@ import cpw.mods.fml.common.event.FMLInterModComms.IMCEvent; import cpw.mods.fml.common.event.FMLMissingMappingsEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; +import net.minecraftforge.common.DimensionManager; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Property; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.oredict.RecipeSorter; @@ -160,10 +168,12 @@ import buildcraft.transport.statements.TriggerPipeContents; import buildcraft.transport.statements.TriggerPipeContents.PipeContents; import buildcraft.transport.statements.TriggerPipeSignal; import buildcraft.transport.statements.TriggerRedstoneFaderInput; +import buildcraft.transport.stripes.PipeExtensionListener; import buildcraft.transport.stripes.StripesHandlerArrow; import buildcraft.transport.stripes.StripesHandlerBucket; import buildcraft.transport.stripes.StripesHandlerEntityInteract; import buildcraft.transport.stripes.StripesHandlerHoe; +import buildcraft.transport.stripes.StripesHandlerPipeWires; import buildcraft.transport.stripes.StripesHandlerPipes; import buildcraft.transport.stripes.StripesHandlerPlaceBlock; import buildcraft.transport.stripes.StripesHandlerRightClick; @@ -255,6 +265,8 @@ public class BuildCraftTransport extends BuildCraftMod { public static float gateCostMultiplier = 1.0F; + public static PipeExtensionListener pipeExtensionListener; + private static LinkedList pipeRecipes = new LinkedList(); private static ChannelHandler transportChannelHandler; @@ -495,6 +507,7 @@ public class BuildCraftTransport extends BuildCraftMod { PipeManager.registerStripesHandler(new StripesHandlerArrow()); PipeManager.registerStripesHandler(new StripesHandlerShears()); PipeManager.registerStripesHandler(new StripesHandlerPipes()); + PipeManager.registerStripesHandler(new StripesHandlerPipeWires()); PipeManager.registerStripesHandler(new StripesHandlerEntityInteract()); PipeManager.registerStripesHandler(new StripesHandlerPlaceBlock()); PipeManager.registerStripesHandler(new StripesHandlerHoe()); @@ -541,6 +554,22 @@ public class BuildCraftTransport extends BuildCraftMod { } } + @Mod.EventHandler + public void serverLoading(FMLServerStartingEvent event) { + pipeExtensionListener = new PipeExtensionListener(); + FMLCommonHandler.instance().bus().register(pipeExtensionListener); + } + + @Mod.EventHandler + public void serverUnloading(FMLServerStoppingEvent event) { + // One last tick + for (WorldServer w : DimensionManager.getWorlds()) { + pipeExtensionListener.tick(new TickEvent.WorldTickEvent(Side.SERVER, TickEvent.Phase.END, w)); + } + FMLCommonHandler.instance().bus().unregister(pipeExtensionListener); + pipeExtensionListener = null; + } + private void postInitSilicon() { Map recipes = GateExpansions.getRecipesForPostInit(); int recipeId = 0; diff --git a/common/buildcraft/builders/LibraryDatabase.java b/common/buildcraft/builders/LibraryDatabase.java index 117c8a18..9c281d2b 100644 --- a/common/buildcraft/builders/LibraryDatabase.java +++ b/common/buildcraft/builders/LibraryDatabase.java @@ -150,7 +150,7 @@ public class LibraryDatabase { LibraryId id = new LibraryId(); - int sepIndex = fileName.lastIndexOf(BuildCraftBuilders.BPT_SEP_CHARACTER); + int sepIndex = fileName.lastIndexOf(LibraryId.BPT_SEP_CHARACTER); int dotIndex = fileName.lastIndexOf('.'); String extension = fileName.substring(dotIndex + 1); diff --git a/common/buildcraft/builders/TileConstructionMarker.java b/common/buildcraft/builders/TileConstructionMarker.java index adb7e776..e36951a4 100755 --- a/common/buildcraft/builders/TileConstructionMarker.java +++ b/common/buildcraft/builders/TileConstructionMarker.java @@ -28,7 +28,6 @@ import buildcraft.core.Box; import buildcraft.core.Box.Kind; import buildcraft.core.IBoxProvider; import buildcraft.core.LaserData; -import buildcraft.core.blueprints.BlueprintUtils; import buildcraft.core.lib.block.TileBuildCraft; import buildcraft.core.blueprints.Blueprint; import buildcraft.core.blueprints.BlueprintBase; diff --git a/common/buildcraft/transport/TileGenericPipe.java b/common/buildcraft/transport/TileGenericPipe.java index 36d03ef9..2e8ef218 100644 --- a/common/buildcraft/transport/TileGenericPipe.java +++ b/common/buildcraft/transport/TileGenericPipe.java @@ -982,15 +982,16 @@ public class TileGenericPipe extends TileEntity implements IFluidHandler, return; } + if (pipe == null && coreState.pipeId != 0) { + initialize(BlockGenericPipe.createPipe((Item) Item.itemRegistry.getObjectById(coreState.pipeId))); + } + + if (pipe == null) { + return; + } + switch (stateId) { case 0: - if (pipe == null && coreState.pipeId != 0) { - initialize(BlockGenericPipe.createPipe((Item) Item.itemRegistry.getObjectById(coreState.pipeId))); - } - - if (pipe == null) { - break; - } worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord); break; diff --git a/common/buildcraft/transport/render/FacadeRenderHelper.java b/common/buildcraft/transport/render/FacadeRenderHelper.java index f9dd6519..016ca2e9 100644 --- a/common/buildcraft/transport/render/FacadeRenderHelper.java +++ b/common/buildcraft/transport/render/FacadeRenderHelper.java @@ -196,7 +196,7 @@ public final class FacadeRenderHelper { setRenderBounds(renderblocks, rotated, direction); renderblocks.renderStandardBlock(blockStateMachine.getBlock(), x, y, z); } - BlockGenericPipe.facadeRenderColor = -1; + ((FakeBlock) blockStateMachine.getBlock()).setColor(0xFFFFFF); if (renderBlock.getRenderType() == 31) { renderblocks.uvRotateSouth = 0; @@ -209,7 +209,7 @@ public final class FacadeRenderHelper { } } - BlockGenericPipe.facadeRenderColor = -1; + ((FakeBlock) blockStateMachine.getBlock()).setColor(0xFFFFFF); } ((TextureStateManager) textureManager.getTextureState()).pushArray(); diff --git a/common/buildcraft/transport/stripes/PipeExtensionListener.java b/common/buildcraft/transport/stripes/PipeExtensionListener.java new file mode 100644 index 00000000..8e6925fa --- /dev/null +++ b/common/buildcraft/transport/stripes/PipeExtensionListener.java @@ -0,0 +1,149 @@ +package buildcraft.transport.stripes; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import net.minecraftforge.common.util.ForgeDirection; +import buildcraft.BuildCraftTransport; +import buildcraft.api.core.Position; +import buildcraft.api.transport.IStripesActivator; +import buildcraft.api.transport.IStripesHandler; +import buildcraft.core.proxy.CoreProxy; +import buildcraft.transport.BlockGenericPipe; +import buildcraft.transport.Pipe; +import buildcraft.transport.PipeTransportItems; +import buildcraft.transport.TileGenericPipe; +import buildcraft.transport.TravelingItem; +import buildcraft.transport.utils.TransportUtils; + +/** + * Created by asie on 3/20/15. + */ +public class PipeExtensionListener { + private class PipeExtensionRequest { + public ItemStack stack; + public int x, y, z; + public ForgeDirection o; + public IStripesActivator h; + } + + private final Map> requests = new HashMap>(); + + public void requestPipeExtension(ItemStack stack, World world, int x, int y, int z, ForgeDirection o, IStripesActivator h) { + if (world.isRemote) { + return; + } + + if (!requests.containsKey(world)) { + requests.put(world, new HashSet()); + } + PipeExtensionRequest r = new PipeExtensionRequest(); + r.stack = stack; + r.x = x; + r.y = y; + r.z = z; + r.o = o; + r.h = h; + requests.get(world).add(r); + } + + @SubscribeEvent + public void tick(TickEvent.WorldTickEvent event) { + if (event.phase == TickEvent.Phase.END && requests.containsKey(event.world)) { + HashSet rSet = requests.get(event.world); + World w = event.world; + for (PipeExtensionRequest r : rSet) { + Position target = new Position(r.x, r.y, r.z); + target.orientation = r.o; + + boolean retract = r.stack.getItem() == BuildCraftTransport.pipeItemsVoid; + ArrayList removedPipeStacks = null; + + if (retract) { + target.moveBackwards(1.0D); + } else { + target.moveForwards(1.0D); + if (!w.isAirBlock((int) target.x, (int) target.y, (int) target.z)) { + r.h.sendItem(r.stack, r.o.getOpposite()); + continue; + } + } + + // Step 1: Copy over and remove existing pipe + Block oldBlock = w.getBlock(r.x, r.y, r.z); + int oldMeta = w.getBlockMetadata(r.x, r.y, r.z); + NBTTagCompound nbt = new NBTTagCompound(); + w.getTileEntity(r.x, r.y, r.z).writeToNBT(nbt); + w.setBlockToAir(r.x, r.y, r.z); + + // Step 2: If retracting, remove previous pipe; if extending, add new pipe + if (retract) { + removedPipeStacks = w.getBlock((int) target.x, (int) target.y, (int) target.z).getDrops(w, (int) target.x, (int) target.y, (int) target.z, + w.getBlockMetadata((int) target.x, (int) target.y, (int) target.z), 0); + + w.setBlockToAir((int) target.x, (int) target.y, (int) target.z); + } else { + r.stack.getItem().onItemUse(r.stack, + CoreProxy.proxy.getBuildCraftPlayer((WorldServer) w, r.x, r.y, r.z).get(), + w, r.x, r.y, r.z, 1, 0, 0, 0); + } + + // Step 3: Place stripes pipe back + // - Correct NBT coordinates + nbt.setInteger("x", (int) target.x); + nbt.setInteger("y", (int) target.y); + nbt.setInteger("z", (int) target.z); + // - Create block and tile + TileGenericPipe pipeTile = (TileGenericPipe) TileEntity.createAndLoadEntity(nbt); + + w.setBlock((int) target.x, (int) target.y, (int) target.z, oldBlock, oldMeta, 3); + w.setTileEntity((int) target.x, (int) target.y, (int) target.z, pipeTile); + + pipeTile.setWorldObj(w); + pipeTile.validate(); + pipeTile.updateEntity(); + + // Step 4: Hope for the best, clean up. + PipeTransportItems items = (PipeTransportItems) pipeTile.pipe.transport; + if (!retract) { + r.stack.stackSize--; + } + + if (r.stack.stackSize > 0) { + sendItem(items, r.stack, r.o.getOpposite()); + } + if (removedPipeStacks != null) { + for (ItemStack s : removedPipeStacks) { + sendItem(items, s, r.o.getOpposite()); + } + } + + if (!retract) { + TileGenericPipe newPipeTile = (TileGenericPipe) w.getTileEntity(r.x, r.y, r.z); + newPipeTile.updateEntity(); + pipeTile.scheduleNeighborChange(); + } + } + rSet.clear(); + } + } + + private void sendItem(PipeTransportItems transport, ItemStack itemStack, ForgeDirection direction) { + TravelingItem newItem = TravelingItem.make( + transport.container.xCoord + 0.5, + transport.container.yCoord + TransportUtils.getPipeFloorOf(itemStack), + transport.container.zCoord + 0.5, itemStack); + transport.injectItem(newItem, direction); + } +} diff --git a/common/buildcraft/transport/stripes/StripesHandlerPipeWires.java b/common/buildcraft/transport/stripes/StripesHandlerPipeWires.java new file mode 100644 index 00000000..68170ed9 --- /dev/null +++ b/common/buildcraft/transport/stripes/StripesHandlerPipeWires.java @@ -0,0 +1,60 @@ +package buildcraft.transport.stripes; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import net.minecraftforge.common.util.ForgeDirection; +import buildcraft.api.core.Position; +import buildcraft.api.transport.IStripesActivator; +import buildcraft.api.transport.IStripesHandler; +import buildcraft.transport.ItemPipeWire; +import buildcraft.transport.TileGenericPipe; + +public class StripesHandlerPipeWires implements IStripesHandler { + @Override + public StripesHandlerType getType() { + return StripesHandlerType.ITEM_USE; + } + + @Override + public boolean shouldHandle(ItemStack stack) { + return (stack.getItem() instanceof ItemPipeWire); + } + + @Override + public boolean handle(World world, int x, int y, int z, ForgeDirection direction, ItemStack stack, EntityPlayer player, IStripesActivator activator) { + int pipesToTry = 8; + int pipeWireColor = stack.getItemDamage(); + + Position p = new Position(x, y, z); + p.orientation = direction; + + while (pipesToTry > 0) { + p.moveBackwards(1.0); + + TileEntity tile = world.getTileEntity((int) p.x, (int) p.y, (int) p.z); + if (tile instanceof TileGenericPipe) { + TileGenericPipe pipeTile = (TileGenericPipe) tile; + + if (!pipeTile.pipe.wireSet[pipeWireColor]) { + pipeTile.pipe.wireSet[pipeWireColor] = true; + pipeTile.pipe.signalStrength[pipeWireColor] = 0; + + pipeTile.pipe.updateSignalState(); + pipeTile.scheduleRenderUpdate(); + world.notifyBlocksOfNeighborChange(pipeTile.xCoord, pipeTile.yCoord, pipeTile.zCoord, pipeTile.getBlock()); + return true; + } else { + pipesToTry--; + continue; + } + } else { + // Not a pipe, don't follow chain + break; + } + } + + return false; + } +} diff --git a/common/buildcraft/transport/stripes/StripesHandlerPipes.java b/common/buildcraft/transport/stripes/StripesHandlerPipes.java index 7b3e8267..91352249 100644 --- a/common/buildcraft/transport/stripes/StripesHandlerPipes.java +++ b/common/buildcraft/transport/stripes/StripesHandlerPipes.java @@ -8,8 +8,6 @@ */ package buildcraft.transport.stripes; -import java.util.ArrayList; - import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; @@ -22,9 +20,7 @@ import buildcraft.BuildCraftTransport; import buildcraft.api.core.Position; import buildcraft.api.transport.IStripesActivator; import buildcraft.api.transport.IStripesHandler; -import buildcraft.transport.BlockGenericPipe; import buildcraft.transport.ItemPipe; -import buildcraft.transport.Pipe; import buildcraft.transport.PipeTransportItems; import buildcraft.transport.TileGenericPipe; @@ -49,12 +45,8 @@ public class StripesHandlerPipes implements IStripesHandler { return false; } - if (world.getBlock(x, y, z) != Blocks.air) { - return false; - } - Position p = new Position(x, y, z, direction); - p.moveBackwards(1.0d); + p.moveBackwards(1.0D); TileEntity tile = world.getTileEntity((int) p.x, (int) p.y, (int) p.z); if (!(tile instanceof TileGenericPipe)) { @@ -64,69 +56,9 @@ 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.initializeFromItemMetadata(stack.getItemDamage()); - Pipe newPipe = BlockGenericPipe.createPipe(stack.getItem()); - newPipe.setTile(pipeTile); - pipeTile.pipe = newPipe; - pipeTile.updateEntity(); // Needed so that the tile does computeConnections() - - stack.stackSize--; - if (stack.stackSize > 0) { - pipeTile.injectItem(stack, true, direction.getOpposite()); - } - return true; - } - - private boolean copyPipeTo(World world, TileGenericPipe pipeTile, int x, int y, int z, EntityPlayer player) { - int meta = pipeTile.pipe.container.getItemMetadata(); - ItemStack stack = new ItemStack(BuildCraftTransport.pipeItemsStripes, 1, meta); - if (!BuildCraftTransport.pipeItemsStripes.onItemUse(stack, player, world, x, y, z, 1, 0, 0, 0)) { - return false; - } - 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); - } + // Checks done, request extension + BuildCraftTransport.pipeExtensionListener.requestPipeExtension(stack, world, (int) p.x, (int) p.y, (int) p.z, direction, activator); return true; } } diff --git a/common/buildcraft/transport/stripes/StripesHandlerPlaceBlock.java b/common/buildcraft/transport/stripes/StripesHandlerPlaceBlock.java index 0c83b0a0..31e88e3d 100644 --- a/common/buildcraft/transport/stripes/StripesHandlerPlaceBlock.java +++ b/common/buildcraft/transport/stripes/StripesHandlerPlaceBlock.java @@ -15,6 +15,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; +import buildcraft.api.core.Position; import buildcraft.api.transport.IStripesActivator; import buildcraft.api.transport.IStripesHandler; @@ -36,6 +37,13 @@ public class StripesHandlerPlaceBlock implements IStripesHandler { IStripesActivator activator) { if (!world.isAirBlock(x, y, z) && stack.tryPlaceItemIntoWorld(player, world, x, y, z, 1, 0.0f, 0.0f, 0.0f)) { return true; + } else if (world.isAirBlock(x, y, z)) { + Position src = new Position(x, y, z); + src.orientation = direction; + src.moveBackwards(1.0D); + if (stack.tryPlaceItemIntoWorld(player, world, (int) src.x, (int) src.y, (int) src.z, direction.ordinal(), 0.0f, 0.0f, 0.0f)) { + return true; + } } return false; }