refactor the stripes pipe's extension code into a separate handler

This commit is contained in:
Hea3veN 2015-02-11 20:40:51 -03:00
parent aae0a98842
commit c2466911d0
3 changed files with 98 additions and 63 deletions

View file

@ -151,6 +151,7 @@ import buildcraft.transport.statements.TriggerPipeSignal;
import buildcraft.transport.statements.TriggerRedstoneFaderInput; import buildcraft.transport.statements.TriggerRedstoneFaderInput;
import buildcraft.transport.stripes.StripesHandlerArrow; import buildcraft.transport.stripes.StripesHandlerArrow;
import buildcraft.transport.stripes.StripesHandlerBucket; import buildcraft.transport.stripes.StripesHandlerBucket;
import buildcraft.transport.stripes.StripesHandlerPipes;
import buildcraft.transport.stripes.StripesHandlerRightClick; import buildcraft.transport.stripes.StripesHandlerRightClick;
import buildcraft.transport.stripes.StripesHandlerShears; import buildcraft.transport.stripes.StripesHandlerShears;
@ -462,6 +463,7 @@ public class BuildCraftTransport extends BuildCraftMod {
PipeManager.registerStripesHandler(new StripesHandlerBucket()); PipeManager.registerStripesHandler(new StripesHandlerBucket());
PipeManager.registerStripesHandler(new StripesHandlerArrow()); PipeManager.registerStripesHandler(new StripesHandlerArrow());
PipeManager.registerStripesHandler(new StripesHandlerShears()); PipeManager.registerStripesHandler(new StripesHandlerShears());
PipeManager.registerStripesHandler(new StripesHandlerPipes());
PipeManager.registerPipePluggable(FacadePluggable.class, "facade"); PipeManager.registerPipePluggable(FacadePluggable.class, "facade");
PipeManager.registerPipePluggable(GatePluggable.class, "gate"); PipeManager.registerPipePluggable(GatePluggable.class, "gate");

View file

@ -26,15 +26,12 @@ import cofh.api.energy.IEnergyHandler;
import buildcraft.BuildCraftTransport; import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider; import buildcraft.api.core.IIconProvider;
import buildcraft.api.core.Position; import buildcraft.api.core.Position;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.IStripesHandler; import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesHandler.StripesHandlerType; import buildcraft.api.transport.IStripesHandler.StripesHandlerType;
import buildcraft.api.transport.IStripesPipe; import buildcraft.api.transport.IStripesPipe;
import buildcraft.api.transport.PipeManager; import buildcraft.api.transport.PipeManager;
import buildcraft.core.proxy.CoreProxy; import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtils; import buildcraft.core.utils.BlockUtils;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.ItemPipe;
import buildcraft.transport.Pipe; import buildcraft.transport.Pipe;
import buildcraft.transport.PipeIconProvider; import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.PipeTransportItems; import buildcraft.transport.PipeTransportItems;
@ -102,7 +99,6 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
/** /**
* Check if there's a handler for this item type. * Check if there's a handler for this item type.
*/ */
for (IStripesHandler handler : PipeManager.stripesHandlers) { for (IStripesHandler handler : PipeManager.stripesHandlers) {
if (handler.getType() == StripesHandlerType.ITEM_USE if (handler.getType() == StripesHandlerType.ITEM_USE
&& handler.shouldHandle(stack)) { && handler.shouldHandle(stack)) {
@ -117,45 +113,7 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
* Special, generic actions not handled by the handler. * Special, generic actions not handled by the handler.
*/ */
if (convertPipe(transport, event.item)) { if (stack.getItem() instanceof ItemBlock) {
int moves = 0;
while (stack.stackSize > 0) {
if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) != Blocks.air) {
break;
}
stack.getItem().onItemUse(new ItemStack(stack.getItem(), 1, stack.getItemDamage()),
player, getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0, 0, 0
);
stack.stackSize--;
p.moveForwards(1.0);
moves++;
}
if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) != Blocks.air) {
p.moveBackwards(1.0);
stack.stackSize++;
getWorld().setBlockToAir((int) p.x, (int) p.y, (int) p.z);
}
BuildCraftTransport.pipeItemsStripes.onItemUse(new ItemStack(
BuildCraftTransport.pipeItemsStripes, 1, this.container.getItemMetadata()), player, getWorld(), (int) p.x,
(int) p.y, (int) p.z, 1, 0, 0, 0
);
this.container.initializeFromItemMetadata(stack.getItemDamage() - 1);
if (stack.stackSize > 0) {
TileEntity targetTile = getWorld().getTileEntity((int) p.x, (int) p.y, (int) p.z);
if (targetTile instanceof IPipeTile) {
TravelingItem newItem = TravelingItem.make(
container.xCoord + 0.5,
container.yCoord + TransportUtils.getPipeFloorOf(
new ItemStack(BuildCraftTransport.pipeItemsStripes)),
container.zCoord + 0.5, stack.copy());
((PipeTransportItems) ((Pipe<?>) ((IPipeTile) targetTile).getPipe()).transport).injectItem(newItem, event.direction.getOpposite());
stack.stackSize = 0;
}
}
} else if (stack.getItem() instanceof ItemBlock) {
if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) == Blocks.air) { if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) == Blocks.air) {
stack.tryPlaceItemIntoWorld( stack.tryPlaceItemIntoWorld(
player, player,
@ -190,26 +148,6 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
transport.injectItem(newItem, direction); transport.injectItem(newItem, direction);
} }
private boolean convertPipe(PipeTransportItems pipe, TravelingItem item) {
if (item.getItemStack().getItem() instanceof ItemPipe) {
if (!(item.getItemStack().getItem() == BuildCraftTransport.pipeItemsStripes)) {
Pipe<?> newPipe = BlockGenericPipe.createPipe(item.getItemStack().getItem());
newPipe.setTile(this.container);
this.container.pipe = newPipe;
item.getItemStack().stackSize--;
if (item.getItemStack().stackSize <= 0) {
((PipeTransportItems) newPipe.transport).items.remove(item);
}
return true;
}
}
return false;
}
@Override @Override
public IIconProvider getIconProvider() { public IIconProvider getIconProvider() {
return BuildCraftTransport.instance.pipeIconProvider; return BuildCraftTransport.instance.pipeIconProvider;

View file

@ -0,0 +1,95 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.Position;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesPipe;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.ItemPipe;
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeTransportItems;
import buildcraft.transport.TileGenericPipe;
public class StripesHandlerPipes implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemPipe;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
if (!(stack.getItem() instanceof ItemPipe) || (stack.getItem() == BuildCraftTransport.pipeItemsStripes)) {
return false;
}
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;
}
TileGenericPipe pipeTile = (TileGenericPipe) tile;
if (!(pipeTile.pipe.transport instanceof PipeTransportItems)) {
return false;
}
// Checks done, start to actually do stuff
if (!copyPipeTo(world, pipeTile, x, y, z, player)) {
return false;
}
pipeTile.pipe.transport.container.initializeFromItemMetadata(stack.getItemDamage() - 1);
Pipe<?> newPipe = BlockGenericPipe.createPipe(stack.getItem());
newPipe.setTile(pipeTile.pipe.container);
pipeTile.pipe.container.pipe = newPipe;
pipeTile.updateEntity(); // Needed so that the tile does computeConnections()
ItemStack transportStack = stack.copy();
stack.stackSize = 0;
transportStack.stackSize--;
if (transportStack.stackSize > 0) {
pipeTile.pipe.container.injectItem(transportStack, 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;
}
}