Reworked Extraction Exclusion hooks for API

This commit is contained in:
CovertJaguar 2012-07-27 14:16:34 -07:00
parent 0122ef17db
commit 7bba02643e
6 changed files with 109 additions and 59 deletions

View file

@ -21,6 +21,8 @@ import buildcraft.api.gates.ActionManager;
import buildcraft.api.gates.Trigger;
import buildcraft.api.recipes.AssemblyRecipe;
import buildcraft.api.transport.IPipe;
import buildcraft.api.transport.IExtractionHandler;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.CoreProxy;
import buildcraft.core.DefaultProps;
import buildcraft.core.ItemBuildCraft;
@ -72,6 +74,7 @@ import net.minecraft.src.CraftingManager;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.World;
import net.minecraft.src.forge.Configuration;
import net.minecraft.src.forge.MinecraftForge;
import net.minecraft.src.forge.Property;
@ -170,6 +173,45 @@ public class BuildCraftTransport {
MinecraftForge.setGuiHandler(mod_BuildCraftTransport.instance, new GuiHandler());
}
private static class ExtractionHandler implements IExtractionHandler {
private final String[] items;
private final String[] liquids;
public ExtractionHandler(String[] items, String[] liquids){
this.items = items;
this.liquids = liquids;
}
@Override
public boolean canExtractItems(World world, int i, int j, int k) {
return testStrings(items, world, i, j, k);
}
@Override
public boolean canExtractLiquids(World world, int i, int j, int k) {
return testStrings(liquids, world, i, j, k);
}
private boolean testStrings(String[] excludedBlocks, World world, int i, int j, int k) {
int id = world.getBlockId(i, j, k);
Block block = Block.blocksList[id];
if(block == null)
return false;
int meta = world.getBlockMetadata(i, j, k);
for (String excluded : excludedBlocks) {
if (excluded.equals(block.getBlockName()))
return false;
String[] tokens = excluded.split(":");
if(tokens[0].equals(Integer.toString(id)) && (tokens.length == 1 || tokens[1].equals(Integer.toString(meta))))
return false;
}
return true;
}
}
public static void initialize() {
if (initialized)
return;
@ -186,10 +228,19 @@ public class BuildCraftTransport {
Configuration.CATEGORY_GENERAL, DefaultProps.USE_PIPELOSS);
PipeLoss.comment = "Set to false to turn off energy loss over distance on all power pipes";
Property exclusionList = BuildCraftCore.mainConfiguration.getOrCreateProperty("woodenPipe.exclusion",
Configuration.CATEGORY_BLOCK, "");
Property exclusionItemList = BuildCraftCore.mainConfiguration.getOrCreateProperty("woodenPipe.item.exclusion", Configuration.CATEGORY_BLOCK, "");
PipeLogicWood.excludedBlocks = exclusionList.value.split(",");
String[] excludedItemBlocks = exclusionItemList.value.split(",");
for (int j = 0; j < excludedItemBlocks.length; ++j)
excludedItemBlocks[j] = excludedItemBlocks[j].trim();
Property exclusionLiquidList = BuildCraftCore.mainConfiguration.getOrCreateProperty("woodenPipe.liquid.exclusion", Configuration.CATEGORY_BLOCK, "");
String[] excludedLiquidBlocks = exclusionLiquidList.value.split(",");
for (int j = 0; j < excludedLiquidBlocks.length; ++j)
excludedLiquidBlocks[j] = excludedLiquidBlocks[j].trim();
PipeManager.registerExtractionHandler(new ExtractionHandler(excludedItemBlocks, excludedLiquidBlocks));
Property maxItemInPipesProp = BuildCraftCore.mainConfiguration.getOrCreateIntProperty("pipes.maxItems",
Configuration.CATEGORY_GENERAL, 100);
@ -206,8 +257,6 @@ public class BuildCraftTransport {
Property genericPipeId = BuildCraftCore.mainConfiguration.getOrCreateBlockIdProperty("pipe.id",
DefaultProps.GENERIC_PIPE_ID);
for (int j = 0; j < PipeLogicWood.excludedBlocks.length; ++j)
PipeLogicWood.excludedBlocks[j] = PipeLogicWood.excludedBlocks[j].trim();
BuildCraftCore.mainConfiguration.save();

View file

@ -1,10 +0,0 @@
package buildcraft.api.transport;
import net.minecraft.src.World;
/**
* Implemented by blocks that may want to suppress connections from wooden pipes.
*/
public interface IBlockExtractable {
boolean mayExtract(World world, int x, int y, int z);
}

View file

@ -1,9 +1,33 @@
package buildcraft.api.transport;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.List;
public class PipeManager {
import net.minecraft.src.World;
public abstract class PipeManager {
public static TreeMap<Integer, IPipedItem> allEntities = new TreeMap<Integer, IPipedItem>();
public static List<IExtractionHandler> extractionHandlers = new ArrayList<IExtractionHandler>();
public static void registerExtractionHandler(IExtractionHandler handler) {
extractionHandlers.add(handler);
}
public static boolean canExtractItems(World world, int i, int j, int k) {
for(IExtractionHandler handler : extractionHandlers)
if(!handler.canExtractItems(world, i, j, k))
return false;
return true;
}
public static boolean canExtractLiquids(World world, int i, int j, int k) {
for(IExtractionHandler handler : extractionHandlers)
if(!handler.canExtractLiquids(world, i, j, k))
return false;
return true;
}
}

View file

@ -14,7 +14,7 @@ import buildcraft.api.APIProxy;
import buildcraft.api.core.Orientations;
import buildcraft.api.liquids.ITankContainer;
import buildcraft.api.tools.IToolWrench;
import buildcraft.api.transport.IBlockExtractable;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.Utils;
import buildcraft.transport.pipes.PipeLiquidsVoid;
import buildcraft.transport.pipes.PipeLiquidsWood;
@ -27,8 +27,6 @@ import net.minecraft.src.World;
public class PipeLogicWood extends PipeLogic {
public static String[] excludedBlocks = new String[0];
public void switchSource() {
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
int newMeta = 6;
@ -40,7 +38,7 @@ public class PipeLogicWood extends PipeLogic {
TileEntity tile = container.getTile(o);
if (isInput(tile))
if (!isExcludedFromExtraction(block, tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord)) {
if (PipeManager.canExtractItems(tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord) || PipeManager.canExtractLiquids(tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord) ) {
newMeta = o.ordinal();
break;
}
@ -58,16 +56,6 @@ public class PipeLogicWood extends PipeLogic {
&& Utils.checkPipesConnections(container, tile);
}
public static boolean isExcludedFromExtraction(Block block, World world, int x, int y, int z) {
if (block == null)
return true;
if(!(block instanceof IBlockExtractable))
return false;
return !((IBlockExtractable)block).mayExtract(world, x, y, z);
}
@Override
public boolean blockActivated(EntityPlayer entityplayer) {
Item equipped = entityplayer.getCurrentEquippedItem() != null ? entityplayer.getCurrentEquippedItem().getItem() : null;

View file

@ -17,6 +17,7 @@ import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.api.power.PowerProvider;
import buildcraft.api.transport.IPipedItem;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.DefaultProps;
import buildcraft.core.EntityPassiveItem;
import buildcraft.core.Utils;
@ -92,14 +93,12 @@ public class PipeItemsWood extends Pipe implements IPowerReceptor {
Position pos = new Position(xCoord, yCoord, zCoord, Orientations.values()[meta]);
pos.moveForwards(1);
int blockId = w.getBlockId((int) pos.x, (int) pos.y, (int) pos.z);
TileEntity tile = w.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
if (tile == null || !(tile instanceof IInventory || tile instanceof ITankContainer)
|| PipeLogicWood.isExcludedFromExtraction(Block.blocksList[blockId], tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord))
if (tile instanceof IInventory) {
if (!PipeManager.canExtractItems(w, (int) pos.x, (int) pos.y, (int) pos.z))
return;
if (tile instanceof IInventory) {
IInventory inventory = (IInventory) tile;
ItemStack[] extracted = checkExtract(inventory, true, pos.orientation.reverse());

View file

@ -16,6 +16,7 @@ import buildcraft.api.liquids.LiquidStack;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.DefaultProps;
import buildcraft.core.network.TileNetworkData;
import buildcraft.transport.Pipe;
@ -62,16 +63,15 @@ public class PipeLiquidsWood extends Pipe implements IPowerReceptor {
Position pos = new Position(xCoord, yCoord, zCoord, Orientations.values()[meta]);
pos.moveForwards(1);
int blockId = w.getBlockId((int) pos.x, (int) pos.y, (int) pos.z);
TileEntity tile = w.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
if (tile == null || !(tile instanceof ITankContainer)
|| PipeLogicWood.isExcludedFromExtraction(Block.blocksList[blockId], tile.worldObj, tile.xCoord, tile.yCoord, tile.zCoord))
if (tile instanceof ITankContainer) {
if (!PipeManager.canExtractLiquids(w, (int) pos.x, (int) pos.y, (int) pos.z))
return;
if (tile instanceof ITankContainer)
if (liquidToExtract <= BuildCraftAPI.BUCKET_VOLUME)
liquidToExtract += powerProvider.useEnergy(1, 1, true) * BuildCraftAPI.BUCKET_VOLUME;
}
}
@Override