refactor stripes pipes, create early version of stripes pipe API, for #1670

This commit is contained in:
asiekierka 2014-10-22 16:30:53 +02:00
parent 559387ba38
commit 42f6d298ee
12 changed files with 268 additions and 104 deletions

View file

@ -0,0 +1,13 @@
package buildcraft.api.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
public interface IStripesItemHandler {
boolean shouldHandle(ItemStack stack);
boolean handle(World world, int x, int y, int z, ForgeDirection direction,
ItemStack stack, EntityPlayer player, IStripesPipe pipe);
}

View file

@ -0,0 +1,9 @@
package buildcraft.api.stripes;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.transport.IPipe;
public interface IStripesPipe extends IPipe {
void rollbackItem(ItemStack itemStack, ForgeDirection direction);
}

View file

@ -0,0 +1,22 @@
package buildcraft.api.stripes;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
public final class StripesPipeAPI {
private static final LinkedList<IStripesItemHandler> handlers = new LinkedList<IStripesItemHandler>();
private StripesPipeAPI() {
}
public static Collection<IStripesItemHandler> getHandlerList() {
return Collections.unmodifiableCollection(handlers);
}
public static void registerHandler(IStripesItemHandler handler) {
if (!handlers.contains(handler)) {
handlers.add(handler);
}
}
}

View file

@ -0,0 +1,11 @@
/**
* 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
*/
@API(apiVersion = "1.0", owner = "BuildCraftAPI|core", provides = "BuildCraftAPI|stripes")
package buildcraft.api.stripes;
import cpw.mods.fml.common.API;

View file

@ -42,6 +42,7 @@ import buildcraft.api.gates.IAction;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.gates.StatementManager;
import buildcraft.api.recipes.BuildcraftRecipeRegistry;
import buildcraft.api.stripes.StripesPipeAPI;
import buildcraft.api.transport.IExtractionHandler;
import buildcraft.api.transport.PipeManager;
import buildcraft.api.transport.PipeWire;
@ -120,6 +121,10 @@ import buildcraft.transport.schematics.BptItemPipeFilters;
import buildcraft.transport.schematics.BptPipeIron;
import buildcraft.transport.schematics.BptPipeWooden;
import buildcraft.transport.schematics.SchematicPipe;
import buildcraft.transport.stripes.StripesHandlerArrow;
import buildcraft.transport.stripes.StripesHandlerBucket;
import buildcraft.transport.stripes.StripesHandlerRightClick;
import buildcraft.transport.stripes.StripesHandlerShears;
import buildcraft.transport.triggers.ActionEnergyPulsar;
import buildcraft.transport.triggers.ActionExtractionPreset;
import buildcraft.transport.triggers.ActionParameterSignal;
@ -536,6 +541,11 @@ public class BuildCraftTransport extends BuildCraftMod {
StatementManager.registerTriggerProvider(new PipeTriggerProvider());
StatementManager.registerActionProvider(new PipeActionProvider());
StripesPipeAPI.registerHandler(new StripesHandlerRightClick());
StripesPipeAPI.registerHandler(new StripesHandlerBucket());
StripesPipeAPI.registerHandler(new StripesHandlerArrow());
StripesPipeAPI.registerHandler(new StripesHandlerShears());
if (BuildCraftCore.loadDefaultRecipes) {
loadRecipes();
}

View file

@ -47,7 +47,6 @@ public class PipeItemsObsidian extends Pipe<PipeTransportItems> implements IEner
private int[] entitiesDropped;
private int entitiesDroppedIndex = 0;
private int ticks = 0;
public PipeItemsObsidian(Item item) {
super(new PipeTransportItems(), item);
@ -150,9 +149,7 @@ public class PipeItemsObsidian extends Pipe<PipeTransportItems> implements IEner
public void updateEntity () {
super.updateEntity();
ticks++;
if ((ticks % 16) == 0 && battery.getEnergyStored() > 0) {
if (battery.getEnergyStored() > 0) {
for (int j = 1; j < 5; ++j) {
if (suckItem(j)) {
return;

View file

@ -10,27 +10,21 @@ package buildcraft.transport.pipes;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyHandler;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
import buildcraft.api.core.Position;
import buildcraft.api.stripes.IStripesItemHandler;
import buildcraft.api.stripes.IStripesPipe;
import buildcraft.api.stripes.StripesPipeAPI;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtil;
import buildcraft.transport.BlockGenericPipe;
@ -43,7 +37,7 @@ import buildcraft.transport.TravelingItem;
import buildcraft.transport.pipes.events.PipeEventItem;
import buildcraft.transport.utils.TransportUtils;
public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnergyHandler {
public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnergyHandler, IStripesPipe {
public PipeItemsStripes(Item item) {
super(new PipeTransportItems(), item);
}
@ -63,48 +57,10 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
p.moveForwards(1.0);
ItemStack stack = event.entity.getEntityItem();
if (convertPipe(transport, event.item)) {
BuildCraftTransport.pipeItemsStripes.onItemUse(new ItemStack(
BuildCraftTransport.pipeItemsStripes), CoreProxy
.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(), getWorld(), (int) p.x,
(int) p.y, (int) p.z, 1, 0, 0, 0
);
} else if (stack.getItem() instanceof ItemBlock) {
if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) == Blocks.air) {
stack.tryPlaceItemIntoWorld(
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(),
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
}
} else if (stack.getItem() == Items.shears) {
Block block = getWorld().getBlock((int) p.x, (int) p.y, (int) p.z);
if (block instanceof BlockLeavesBase) {
getWorld().playSoundEffect((int) p.x, (int) p.y, (int) p.z, Block.soundTypeGrass.getBreakSound(), 1, 1);
getWorld().setBlockToAir((int) p.x, (int) p.y, (int) p.z);
stack.damageItem(1, CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get());
}
} else if (stack.getItem() == Items.arrow) {
stack.stackSize--;
ForgeDirection direction = event.direction;
EntityArrow entityArrow = new EntityArrow(getWorld(),
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(), 0);
entityArrow.setPosition(p.x + 0.5d, p.y + 0.5d, p.z + 0.5d);
entityArrow.setDamage(3);
entityArrow.setKnockbackStrength(1);
entityArrow.motionX = direction.offsetX * 1.8d + getWorld().rand.nextGaussian() * 0.007499999832361937D;
entityArrow.motionY = direction.offsetY * 1.8d + getWorld().rand.nextGaussian() * 0.007499999832361937D;
entityArrow.motionZ = direction.offsetZ * 1.8d + getWorld().rand.nextGaussian() * 0.007499999832361937D;
getWorld().spawnEntityInWorld(entityArrow);
} else if ((stack.getItem() == Items.potionitem && ItemPotion.isSplash(stack.getItemDamage()))
|| stack.getItem() == Items.egg
|| stack.getItem() == Items.snowball) {
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld(),
(int) p.x, (int) p.y, (int) p.z).get();
switch (event.direction) {
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld(),
(int) p.x, (int) p.y, (int) p.z).get();
switch (event.direction) {
case DOWN:
player.rotationPitch = 90;
player.rotationYaw = 0;
@ -131,58 +87,43 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
break;
case UNKNOWN:
break;
}
for (IStripesItemHandler handler : StripesPipeAPI.getHandlerList()) {
if (handler.shouldHandle(stack)) {
if (handler.handle(getWorld(), (int) p.x, (int) p.y, (int) p.z,
event.direction, stack, player, this)) {
return;
}
}
stack.getItem().onItemRightClick(
stack,
getWorld(),
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld(),
(int) p.x, (int) p.y, (int) p.z).get());
} else if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) == Blocks.air) {
if (stack.getItem() instanceof ItemBucket) {
Block underblock = getWorld().getBlock((int) p.x, (int) p.y - 1, (int) p.z);
Item newBucket = Items.bucket;
if (underblock == Blocks.water) {
newBucket = Items.water_bucket;
}
if (underblock == Blocks.lava) {
newBucket = Items.lava_bucket;
}
boolean rollback = false;
if (((ItemBucket) stack.getItem()).tryPlaceContainedLiquid(getWorld(),
(int) p.x, (int) p.y - 1, (int) p.z)) {
rollback = true;
} else if (newBucket != Items.bucket) {
getWorld().setBlockToAir((int) p.x, (int) p.y - 1, (int) p.z);
rollback = true;
}
if (rollback) {
stack.stackSize = 0;
rollbackItem(newBucket, 1, event.direction);
}
} else {
}
/**
* Special, generic actions not handled by the handler.
*/
if (convertPipe(transport, event.item)) {
BuildCraftTransport.pipeItemsStripes.onItemUse(new ItemStack(
BuildCraftTransport.pipeItemsStripes), player, getWorld(), (int) p.x,
(int) p.y, (int) p.z, 1, 0, 0, 0
);
} else if (stack.getItem() instanceof ItemBlock) {
if (getWorld().getBlock((int) p.x, (int) p.y, (int) p.z) == Blocks.air) {
stack.tryPlaceItemIntoWorld(
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(),
getWorld(), (int) p.x, (int) p.y - 1, (int) p.z, 1, 0.0f, 0.0f, 0.0f);
player,
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
}
} else {
stack.tryPlaceItemIntoWorld(
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(),
player,
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
}
}
private void rollbackItem(Item item, int quantity, ForgeDirection direction) {
rollbackItem(new ItemStack(item, quantity), direction);
}
private void rollbackItem(ItemStack itemStack, ForgeDirection direction) {
@Override
public void rollbackItem(ItemStack itemStack, ForgeDirection direction) {
TravelingItem newItem = TravelingItem.make(
container.xCoord + 0.5,
container.yCoord + TransportUtils.getPipeFloorOf(itemStack),
@ -190,7 +131,7 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
transport.injectItem(newItem, direction.getOpposite());
}
public boolean convertPipe(PipeTransportItems pipe, TravelingItem item) {
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());
@ -244,7 +185,7 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
if (maxReceive == 0) {
return 0;
} else if (simulate) {
return 10;
return Math.min(maxReceive, 10);
}
ForgeDirection o = getOpenOrientation();

View file

@ -41,7 +41,7 @@ public class PipeItemsWood extends Pipe<PipeTransportItems> implements IEnergyHa
protected int standardIconIndex = PipeIconProvider.TYPE.PipeItemsWood_Standard.ordinal();
protected int solidIconIndex = PipeIconProvider.TYPE.PipeAllWood_Solid.ordinal();
private int ticks = 0;
private int ticksSincePull = 0;
private PipeLogicWood logic = new PipeLogicWood(this) {
@Override
@ -109,7 +109,7 @@ public class PipeItemsWood extends Pipe<PipeTransportItems> implements IEnergyHa
return;
}
ticks++;
ticksSincePull++;
if (shouldTick()) {
if (transport.getNumberOfStacks() < PipeTransportItems.MAX_PIPE_STACKS) {
@ -117,6 +117,7 @@ public class PipeItemsWood extends Pipe<PipeTransportItems> implements IEnergyHa
}
battery.setEnergy(0);
ticksSincePull = 0;
}
}
@ -124,7 +125,7 @@ public class PipeItemsWood extends Pipe<PipeTransportItems> implements IEnergyHa
if (battery.getEnergyStored() >= 64 * 10) {
return true;
} else {
return (ticks % 16) == 0 && battery.getEnergyStored() >= 10;
return ticksSincePull >= 16 && battery.getEnergyStored() >= 10;
}
}

View file

@ -0,0 +1,37 @@
package buildcraft.transport.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.stripes.IStripesItemHandler;
import buildcraft.api.stripes.IStripesPipe;
public class StripesHandlerArrow implements IStripesItemHandler {
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() == Items.arrow;
}
@Override
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);
entityArrow.setDamage(3);
entityArrow.setKnockbackStrength(1);
entityArrow.motionX = direction.offsetX * 1.8d + world.rand.nextGaussian() * 0.007499999832361937D;
entityArrow.motionY = direction.offsetY * 1.8d + world.rand.nextGaussian() * 0.007499999832361937D;
entityArrow.motionZ = direction.offsetZ * 1.8d + world.rand.nextGaussian() * 0.007499999832361937D;
world.spawnEntityInWorld(entityArrow);
return true;
}
}

View file

@ -0,0 +1,58 @@
package buildcraft.transport.stripes;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.stripes.IStripesItemHandler;
import buildcraft.api.stripes.IStripesPipe;
public class StripesHandlerBucket implements IStripesItemHandler {
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemBucket;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
IStripesPipe pipe) {
if (world.getBlock(x, y, z) == Blocks.air) {
Block underblock = world.getBlock(x, y - 1, z);
Item newBucket = Items.bucket;
if (underblock == Blocks.water) {
newBucket = Items.water_bucket;
}
if (underblock == Blocks.lava) {
newBucket = Items.lava_bucket;
}
boolean rollback = false;
if (((ItemBucket) stack.getItem()).tryPlaceContainedLiquid(world,
x, y - 1, z)) {
rollback = true;
} else if (newBucket != Items.bucket) {
world.setBlockToAir(x, y - 1, z);
rollback = true;
}
if (rollback) {
stack.stackSize = 0;
pipe.rollbackItem(new ItemStack(newBucket, 1), direction);
}
return true;
}
return false;
}
}

View file

@ -0,0 +1,29 @@
package buildcraft.transport.stripes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.stripes.IStripesItemHandler;
import buildcraft.api.stripes.IStripesPipe;
public class StripesHandlerRightClick implements IStripesItemHandler {
@Override
public boolean shouldHandle(ItemStack stack) {
return (stack.getItem() == Items.potionitem && ItemPotion.isSplash(stack.getItemDamage()))
|| stack.getItem() == Items.egg
|| stack.getItem() == Items.snowball;
}
@Override
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);
return true;
}
}

View file

@ -0,0 +1,36 @@
package buildcraft.transport.stripes;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
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.util.ForgeDirection;
import buildcraft.api.stripes.IStripesItemHandler;
import buildcraft.api.stripes.IStripesPipe;
public class StripesHandlerShears implements IStripesItemHandler {
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemShears;
}
@Override
public boolean handle(World world, int x, int y, int z,
ForgeDirection direction, ItemStack stack, EntityPlayer player,
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;
}
return false;
}
}