further stripes pipe refactor, improve API, close #1670

This commit is contained in:
asiekierka 2014-10-22 16:42:42 +02:00
parent 42f6d298ee
commit 3323779c74
12 changed files with 106 additions and 79 deletions

View file

@ -1,9 +0,0 @@
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

@ -1,22 +0,0 @@
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

@ -1,11 +0,0 @@
/**
* 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

@ -1,11 +1,18 @@
package buildcraft.api.stripes;
package buildcraft.api.transport;
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 {
public interface IStripesHandler {
public static enum StripesHandlerType {
ITEM_USE,
BLOCK_BREAK
}
StripesHandlerType getType();
boolean shouldHandle(ItemStack stack);
boolean handle(World world, int x, int y, int z, ForgeDirection direction,

View file

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

View file

@ -15,11 +15,16 @@ import net.minecraft.world.World;
public abstract class PipeManager {
public static List<IStripesHandler> stripesHandlers = new ArrayList<IStripesHandler>();
public static List<IExtractionHandler> extractionHandlers = new ArrayList<IExtractionHandler>();
public static void registerExtractionHandler(IExtractionHandler handler) {
extractionHandlers.add(handler);
}
public static void registerStripesHandler(IStripesHandler handler) {
stripesHandlers.add(handler);
}
/**
* param extractor can be null

View file

@ -42,7 +42,6 @@ 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;
@ -541,10 +540,10 @@ 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());
PipeManager.registerStripesHandler(new StripesHandlerRightClick());
PipeManager.registerStripesHandler(new StripesHandlerBucket());
PipeManager.registerStripesHandler(new StripesHandlerArrow());
PipeManager.registerStripesHandler(new StripesHandlerShears());
if (BuildCraftCore.loadDefaultRecipes) {
loadRecipes();

View file

@ -10,6 +10,7 @@ package buildcraft.transport.pipes;
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;
@ -22,9 +23,10 @@ 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.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.BlockUtil;
import buildcraft.transport.BlockGenericPipe;
@ -89,8 +91,9 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
break;
}
for (IStripesItemHandler handler : StripesPipeAPI.getHandlerList()) {
if (handler.shouldHandle(stack)) {
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)) {
return;
@ -123,12 +126,23 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
}
@Override
public void rollbackItem(ItemStack itemStack, ForgeDirection direction) {
public void dropItem(ItemStack itemStack, ForgeDirection direction) {
Position p = new Position(container.xCoord, container.yCoord,
container.zCoord, direction);
p.moveForwards(1.0);
itemStack.tryPlaceItemIntoWorld(CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld()).get(),
getWorld(), (int) p.x, (int) p.y, (int) p.z, 1, 0.0f, 0.0f,
0.0f);
}
@Override
public void sendItem(ItemStack itemStack, ForgeDirection direction) {
TravelingItem newItem = TravelingItem.make(
container.xCoord + 0.5,
container.yCoord + TransportUtils.getPipeFloorOf(itemStack),
container.zCoord + 0.5, itemStack);
transport.injectItem(newItem, direction.getOpposite());
transport.injectItem(newItem, direction);
}
private boolean convertPipe(PipeTransportItems pipe, TravelingItem item) {
@ -185,7 +199,7 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
if (maxReceive == 0) {
return 0;
} else if (simulate) {
return Math.min(maxReceive, 10);
return maxReceive;
}
ForgeDirection o = getOpenOrientation();
@ -196,20 +210,32 @@ public class PipeItemsStripes extends Pipe<PipeTransportItems> implements IEnerg
p.moveForwards(1.0);
if (!BlockUtil.isUnbreakableBlock(getWorld(), (int) p.x, (int) p.y, (int) p.z)) {
ArrayList<ItemStack> stacks = getWorld().getBlock(
(int) p.x, (int) p.y, (int) p.z).getDrops(
getWorld(),
(int) p.x,
(int) p.y,
(int) p.z,
getWorld().getBlockMetadata((int) p.x, (int) p.y,
(int) p.z), 0
Block block = getWorld().getBlock((int) p.x, (int) p.y, (int) p.z);
int metadata = getWorld().getBlockMetadata((int) p.x, (int) p.y, (int) p.z);
ItemStack stack = new ItemStack(block, 1, metadata);
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) getWorld(),
(int) p.x, (int) p.y, (int) p.z).get();
for (IStripesHandler handler : PipeManager.stripesHandlers) {
if (handler.getType() == StripesHandlerType.BLOCK_BREAK
&& handler.shouldHandle(stack)) {
if (handler.handle(getWorld(), (int) p.x, (int) p.y, (int) p.z,
o, stack, player, this)) {
return maxReceive;
}
}
}
ArrayList<ItemStack> stacks = block.getDrops(
getWorld(), (int) p.x, (int) p.y, (int) p.z,
metadata, 0
);
if (stacks != null) {
for (ItemStack s : stacks) {
if (s != null) {
rollbackItem(s, o);
sendItem(s, o.getOpposite());
}
}
}

View file

@ -6,11 +6,16 @@ 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;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerArrow implements IStripesItemHandler {
public class StripesHandlerArrow implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() == Items.arrow;

View file

@ -9,11 +9,17 @@ 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;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesHandler.StripesHandlerType;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerBucket implements IStripesItemHandler {
public class StripesHandlerBucket implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemBucket;
@ -47,7 +53,7 @@ public class StripesHandlerBucket implements IStripesItemHandler {
if (rollback) {
stack.stackSize = 0;
pipe.rollbackItem(new ItemStack(newBucket, 1), direction);
pipe.sendItem(new ItemStack(newBucket, 1), direction.getOpposite());
}
return true;

View file

@ -6,11 +6,17 @@ 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;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesHandler.StripesHandlerType;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerRightClick implements IStripesItemHandler {
public class StripesHandlerRightClick implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return (stack.getItem() == Items.potionitem && ItemPotion.isSplash(stack.getItemDamage()))

View file

@ -7,11 +7,17 @@ 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;
import buildcraft.api.transport.IStripesHandler;
import buildcraft.api.transport.IStripesHandler.StripesHandlerType;
import buildcraft.api.transport.IStripesPipe;
public class StripesHandlerShears implements IStripesItemHandler {
public class StripesHandlerShears implements IStripesHandler {
@Override
public StripesHandlerType getType() {
return StripesHandlerType.ITEM_USE;
}
@Override
public boolean shouldHandle(ItemStack stack) {
return stack.getItem() instanceof ItemShears;