Inventory Triggers are now ISided bound

This may break some peoples factories, but in the end it will provide
you with more control.

Does not break the API, but it does add a new interface to the API. At
some point this interface should be merged with ITrigger.
This commit is contained in:
CovertJaguar 2012-11-29 09:09:24 -08:00
parent edc62249bf
commit f951f6740d
3 changed files with 78 additions and 28 deletions

View file

@ -0,0 +1,20 @@
package buildcraft.api.gates;
import net.minecraft.src.TileEntity;
import net.minecraftforge.common.ForgeDirection;
// TODO: Merge with ITrigger
/**
* This interface was added to expand on the existing Trigger interface without
* breaking the API. At some point when it is safe to break the API, this
* function should replace the one in ITrigger.
*/
public interface ITriggerDirectional extends ITrigger {
/**
* Return true if the tile given in parameter activates the trigger, given
* the parameters.
*/
public abstract boolean isTriggerActive(ForgeDirection side, TileEntity tile, ITriggerParameter parameter);
}

View file

@ -9,15 +9,20 @@
package buildcraft.core.triggers;
import buildcraft.api.gates.ITriggerDirectional;
import buildcraft.api.gates.ITriggerParameter;
import buildcraft.api.gates.Trigger;
import buildcraft.core.DefaultProps;
import buildcraft.core.utils.SidedInventoryAdapter;
import buildcraft.core.utils.Utils;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
public class TriggerInventory extends Trigger implements ITriggerDirectional {
public class TriggerInventory extends Trigger {
public enum State {
Empty, Contains, Space, Full
@ -67,51 +72,69 @@ public class TriggerInventory extends Trigger {
}
}
@Override
public boolean isTriggerActive(TileEntity tile, ITriggerParameter parameter) {
public boolean isTriggerActive(ForgeDirection side, TileEntity tile, ITriggerParameter parameter) {
ItemStack searchedStack = null;
if (parameter != null)
if (parameter != null) {
searchedStack = parameter.getItem();
}
if (tile instanceof IInventory && ((IInventory) tile).getSizeInventory() > 0) {
IInventory inv = Utils.getInventory(((IInventory) tile));
if (tile instanceof IInventory ) {
IInventory inv = Utils.getInventory(((IInventory) tile));
if(side != ForgeDirection.UNKNOWN && inv instanceof ISidedInventory){
inv = new SidedInventoryAdapter((ISidedInventory)inv, side);
}
int invSize = inv.getSizeInventory();
if(invSize <= 0){
return false;
}
boolean foundItems = false;
boolean foundSpace = false;
for (int i = 0; i < inv.getSizeInventory(); ++i) {
for (int i = 0; i < invSize; ++i) {
ItemStack stack = inv.getStackInSlot(i);
if (parameter == null || parameter.getItemStack() == null)
foundItems = foundItems || stack != null && stack.stackSize > 0;
else if (stack != null && stack.stackSize > 0)
foundItems = foundItems
|| (stack.itemID == parameter.getItemStack().itemID && stack.getItemDamage() == parameter.getItemStack()
.getItemDamage());
boolean slotEmpty = stack == null || stack.stackSize == 0;
if (stack == null || stack.stackSize == 0)
if (searchedStack == null) {
foundItems |= !slotEmpty;
} else if (!slotEmpty) {
foundItems |= stack.isItemEqual(searchedStack);
}
if (slotEmpty) {
foundSpace = true;
else if (searchedStack != null)
if (stack.stackSize < stack.getMaxStackSize() && stack.itemID == searchedStack.itemID
&& stack.getItemDamage() == searchedStack.getItemDamage())
} else if (searchedStack != null) {
if (stack.stackSize < stack.getMaxStackSize() && stack.isItemEqual(searchedStack)) {
foundSpace = true;
}
}
}
switch (state) {
case Empty:
return !foundItems;
case Contains:
return foundItems;
case Space:
return foundSpace;
default:
return !foundSpace;
case Empty:
return !foundItems;
case Contains:
return foundItems;
case Space:
return foundSpace;
default:
return !foundSpace;
}
}
return false;
}
@Override
public boolean isTriggerActive(TileEntity tile, ITriggerParameter parameter) {
return isTriggerActive(ForgeDirection.UNKNOWN, tile, parameter);
}
@Override
public String getTextureFile() {

View file

@ -22,6 +22,7 @@ import buildcraft.api.gates.ActionManager;
import buildcraft.api.gates.IAction;
import buildcraft.api.gates.IActionReceptor;
import buildcraft.api.gates.ITrigger;
import buildcraft.api.gates.ITriggerDirectional;
import buildcraft.api.gates.ITriggerParameter;
import buildcraft.api.gates.Trigger;
import buildcraft.api.gates.TriggerParameter;
@ -457,15 +458,21 @@ public abstract class Pipe implements IPipe, IDropControlInventory {
}
public boolean isNearbyTriggerActive(ITrigger trigger, ITriggerParameter parameter) {
if (trigger instanceof ITriggerPipe)
if (trigger instanceof ITriggerPipe) {
return ((ITriggerPipe) trigger).isTriggerActive(this, parameter);
else if (trigger != null)
} else if (trigger != null) {
for (ForgeDirection o : ForgeDirection.VALID_DIRECTIONS) {
TileEntity tile = container.getTile(o);
if (tile != null && !(tile instanceof TileGenericPipe) && trigger.isTriggerActive(tile, parameter))
return true;
if (tile != null && !(tile instanceof TileGenericPipe)) {
if (trigger instanceof ITriggerDirectional && ((ITriggerDirectional) trigger).isTriggerActive(o.getOpposite(), tile, parameter)) {
return true;
} else if (trigger.isTriggerActive(tile, parameter)) {
return true;
}
}
}
}
return false;
}