Merge pull request #2459 from hea3ven/stripes-refactor

Stripes refactor
This commit is contained in:
Adrian Siekierka 2015-02-16 17:27:52 +01:00
commit a92af4664a
10 changed files with 316 additions and 92 deletions

View file

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

View file

@ -12,9 +12,7 @@ import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;
@ -26,24 +24,25 @@ import cofh.api.energy.IEnergyHandler;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
import buildcraft.api.core.Position;
import buildcraft.api.transport.IPipeTile;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesHandler.StripesHandlerType;
import buildcraft.api.transport.IStripesPipe;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtils;
import buildcraft.transport.BlockGenericPipe;
import buildcraft.transport.ItemPipe;
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.PipeTransportItems;
import buildcraft.transport.TileGenericPipe;
import buildcraft.transport.TravelingItem;
import buildcraft.transport.pipes.events.PipeEventItem;
import buildcraft.transport.stripes.StripesHandlerDefault;
import buildcraft.transport.utils.TransportUtils;
public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnergyHandler, IStripesPipe {
private IStripesHandler defaultItemsHandler = new StripesHandlerDefault();
public PipeItemsStripes(Item item) {
super(new PipeTransportItems(), item);
}
@ -102,71 +101,20 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
/**
* Check if there's a handler for this item type.
*/
for (IStripesHandler handler : PipeManager.stripesHandlers) {
if (handler.getType() == StripesHandlerType.ITEM_USE
&& handler.shouldHandle(stack)) {
if (handler.handle(getWorld(), (int) p.x, (int) p.y, (int) p.z,
event.direction, stack, player, this)) {
event.entity = null;
return;
}
}
}
/**
* Special, generic actions not handled by the handler.
*/
if (convertPipe(transport, event.item)) {
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) {
stack.tryPlaceItemIntoWorld(
player,
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
}
} else {
stack.tryPlaceItemIntoWorld(
player,
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
if(defaultItemsHandler.handle(getWorld(), (int) p.x, (int) p.y, (int) p.z,
event.direction, stack, player, this)) {
event.entity = null;
}
}
@ -190,26 +138,6 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
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
public IIconProvider getIconProvider() {
return BuildCraftTransport.instance.pipeIconProvider;

View file

@ -27,7 +27,6 @@ public class StripesHandlerArrow implements IStripesHandler {
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
stack.stackSize--;
EntityArrow entityArrow = new EntityArrow(world, player, 0);
entityArrow.setPosition(x + 0.5d, y + 0.5d, z + 0.5d);
@ -38,6 +37,11 @@ public class StripesHandlerArrow implements IStripesHandler {
entityArrow.motionZ = direction.offsetZ * 1.8d + world.rand.nextGaussian() * 0.007499999832361937D;
world.spawnEntityInWorld(entityArrow);
stack.stackSize--;
if (stack.stackSize > 0) {
pipe.sendItem(stack, direction.getOpposite());
}
return true;
}

View file

@ -39,8 +39,11 @@ public class StripesHandlerBucket implements IStripesHandler {
Block underblock = world.getBlock(x, y - 1, z);
if (((ItemBucket) stack.getItem()).tryPlaceContainedLiquid(world, x, y - 1, z)) {
stack.stackSize = 0;
pipe.sendItem(emptyBucket, direction.getOpposite());
stack.stackSize--;
if (stack.stackSize > 0) {
pipe.sendItem(stack, direction.getOpposite());
}
return true;
} else {
@ -63,14 +66,15 @@ public class StripesHandlerBucket implements IStripesHandler {
if (filledBucket != null) {
world.setBlockToAir(x, y - 1, z);
stack.stackSize = 0;
pipe.sendItem(filledBucket, direction.getOpposite());
stack.stackSize--;
if (stack.stackSize > 0) {
pipe.sendItem(stack, direction.getOpposite());
}
return true;
}
}
return false;
}
return false;
}

View file

@ -0,0 +1,46 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.transport.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerDefault implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return true;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
if (!world.isAirBlock(x, y, z)) {
return false;
}
if (!stack.tryPlaceItemIntoWorld(player, world, x, y - 1, z, 1, 0.0f, 0.0f, 0.0f)) {
return false;
}
pipe.sendItem(stack, direction.getOpposite());
return true;
}
}

View file

@ -0,0 +1,86 @@
/**
* 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 java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerEntityInteract implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return true;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
AxisAlignedBB box = AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1);
List entities = world.getEntitiesWithinAABBExcludingEntity(null, box);
if (entities.size() <= 0) {
return false;
}
List<EntityLivingBase> livingEntities = new LinkedList<EntityLivingBase>();
for (Object entityObj : entities) {
if (entityObj instanceof EntityLivingBase) {
livingEntities.add((EntityLivingBase) entityObj);
}
}
player.setCurrentItemOrArmor(0, stack);
boolean successful = false;
Collections.shuffle(livingEntities);
while (livingEntities.size() > 0) {
EntityLivingBase entity = livingEntities.remove(0);
if (!player.interactWith(entity)) {
continue;
}
successful = true;
dropItemsExcept(stack, player, pipe, direction);
}
if (stack.stackSize > 0) {
pipe.sendItem(stack, direction.getOpposite());
}
return successful;
}
private void dropItemsExcept(ItemStack stack, EntityPlayer player, IStripesPipe pipe, ForgeDirection direction) {
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
ItemStack invStack = player.inventory.getStackInSlot(i);
if (invStack != null && invStack != stack) {
player.inventory.setInventorySlotContents(i, null);
pipe.sendItem(invStack, direction.getOpposite());
}
}
}
}

View file

@ -0,0 +1,93 @@
/**
* 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()
stack.stackSize--;
if (stack.stackSize > 0) {
pipeTile.pipe.container.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;
}
}

View file

@ -0,0 +1,43 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.transport.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerPlaceBlock implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemBlock;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
if (!world.isAirBlock(x, y, z)) {
return false;
}
return stack.tryPlaceItemIntoWorld(player, world, x, y, z, 1, 0.0f, 0.0f, 0.0f);
}
}

View file

@ -29,7 +29,8 @@ public class StripesHandlerRightClick implements IStripesHandler {
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
stack.getItem().onItemRightClick(stack, world, player);
ItemStack remainingStack = stack.getItem().onItemRightClick(stack, world, player);
pipe.sendItem(remainingStack, direction.getOpposite());
return true;
}

View file

@ -1,12 +1,16 @@
package buildcraft.transport.stripes;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemShears;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IStripesHandler;
@ -30,11 +34,20 @@ public class StripesHandlerShears implements IStripesHandler {
IStripesPipe pipe) {
Block block = world.getBlock(x, y, z);
if (block instanceof BlockLeavesBase) {
world.playSoundEffect(x, y, z, Block.soundTypeGrass.getBreakSound(), 1, 1);
world.setBlockToAir(x, y, z);
stack.damageItem(1, player);
return true;
if (block instanceof IShearable) {
IShearable shearableBlock = (IShearable) block;
if (shearableBlock.isShearable(stack, world, x, y, z)) {
world.playSoundEffect(x, y, z, Block.soundTypeGrass.getBreakSound(), 1, 1);
List<ItemStack> drops = shearableBlock.onSheared(stack, world, x, y, z,
EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, stack));
world.setBlockToAir(x, y, z);
stack.damageItem(1, player);
pipe.sendItem(stack, direction.getOpposite());
for (ItemStack dropStack : drops) {
pipe.sendItem(dropStack, direction.getOpposite());
}
return true;
}
}
return false;