use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.
the class TileChute method update.
@Override
public void update() {
super.update();
if (worldObj.isRemote || isEmpty || worldObj.getTotalWorldTime() % 2 != 0) {
return;
}
TileEntity outputTile = getTile(EnumFacing.DOWN);
ITransactor transactor = Transactor.getTransactorFor(outputTile, EnumFacing.UP);
if (transactor == null) {
if (outputTile instanceof IInjectable && getBattery().getEnergyStored() >= 10) {
ItemStack stackToOutput = null;
int internalSlot = 0;
getBattery().useEnergy(10, 10, false);
for (; internalSlot < inventory.getSizeInventory(); internalSlot++) {
ItemStack stackInSlot = inventory.getStackInSlot(internalSlot);
if (stackInSlot == null || stackInSlot.stackSize == 0) {
continue;
}
stackToOutput = stackInSlot.copy();
stackToOutput.stackSize = 1;
break;
}
if (stackToOutput != null) {
int used = ((IInjectable) outputTile).injectItem(stackToOutput, true, EnumFacing.UP, null);
if (used > 0) {
decrStackSize(internalSlot, 1);
}
}
}
return;
}
for (int internalSlot = 0; internalSlot < inventory.getSizeInventory(); internalSlot++) {
ItemStack stackInSlot = inventory.getStackInSlot(internalSlot);
if (stackInSlot == null || stackInSlot.stackSize == 0) {
continue;
}
ItemStack clonedStack = stackInSlot.copy().splitStack(1);
if (transactor.addNew(clonedStack, true).stackSize > 0) {
inventory.decrStackSize(internalSlot, 1);
return;
}
}
}
use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.
the class InventoryUtil method addToRandomInjectable.
/**
* Look around the tile given in parameter in all 6 position, tries to add the items to a random injectable tile
* around. Will make sure that the location from which the items are coming from (identified by the from parameter)
* isn't used again so that entities doesn't go backwards. Returns true if successful, false otherwise.
*/
@Nonnull
public static ItemStack addToRandomInjectable(World world, BlockPos pos, EnumFacing ignore, @Nonnull ItemStack stack) {
if (stack.isEmpty()) {
return StackUtil.EMPTY;
}
List<EnumFacing> toTry = new ArrayList<>(6);
Collections.addAll(toTry, EnumFacing.VALUES);
Collections.shuffle(toTry);
for (EnumFacing face : toTry) {
if (face == ignore)
continue;
TileEntity tile = world.getTileEntity(pos.offset(face));
IInjectable injectable = ItemTransactorHelper.getInjectable(tile, face.getOpposite());
stack = injectable.injectItem(stack, true, face.getOpposite(), null, 0);
if (stack.isEmpty()) {
return StackUtil.EMPTY;
}
}
return stack;
}
use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.
the class PipeFlowItems method onItemReachEnd.
private void onItemReachEnd(TravellingItem item) {
IPipeHolder holder = pipe.getHolder();
PipeEventItem.ReachEnd reachEnd = new PipeEventItem.ReachEnd(holder, this, item.colour, item.stack, item.side);
holder.fireEvent(reachEnd);
item.colour = reachEnd.colour;
item.stack = reachEnd.getStack();
ItemStack excess = item.stack;
if (excess.isEmpty()) {
return;
}
if (pipe.isConnected(item.side)) {
ConnectedType type = pipe.getConnectedType(item.side);
EnumFacing oppositeSide = item.side.getOpposite();
switch(type) {
case PIPE:
{
IPipe oPipe = pipe.getConnectedPipe(item.side);
if (oPipe == null) {
break;
}
PipeFlow flow = oPipe.getFlow();
if (flow instanceof IFlowItems) {
IFlowItems oFlow = (IFlowItems) flow;
ItemStack before = excess;
excess = oFlow.injectItem(excess.copy(), true, oppositeSide, item.colour, item.speed);
if (!excess.isEmpty()) {
before.shrink(excess.getCount());
}
excess = fireEventEjectIntoPipe(oFlow, item.side, before, excess);
}
break;
}
case TILE:
{
TileEntity tile = pipe.getConnectedTile(item.side);
IInjectable injectable = ItemTransactorHelper.getInjectable(tile, oppositeSide);
ItemStack before = excess;
excess = injectable.injectItem(excess.copy(), true, oppositeSide, item.colour, item.speed);
if (!excess.isEmpty()) {
IItemTransactor transactor = ItemTransactorHelper.getTransactor(tile, oppositeSide);
excess = transactor.insert(excess, false, false);
}
excess = fireEventEjectIntoTile(tile, item.side, before, excess);
break;
}
}
}
if (excess.isEmpty()) {
return;
}
item.tried.add(item.side);
item.toCenter = true;
item.stack = excess;
item.genTimings(holder.getPipeWorld().getTotalWorldTime(), getPipeLength(item.side));
items.add(item.timeToDest, item);
sendItemDataToClient(item);
}
use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.
the class Utils method addToRandomInjectableAround.
/**
* Look around the tile given in parameter in all 6 position, tries to add the items to a random injectable tile
* around. Will make sure that the location from which the items are coming from (identified by the from parameter)
* isn't used again so that entities doesn't go backwards. Returns true if successful, false otherwise.
*/
public static int addToRandomInjectableAround(World world, BlockPos pos, EnumFacing from, ItemStack stack) {
List<IInjectable> possiblePipes = new ArrayList<>();
List<EnumFacing> pipeDirections = new ArrayList<>();
for (EnumFacing side : EnumFacing.VALUES) {
if (side.getOpposite() == from) {
continue;
}
BlockPos newpos = pos.offset(side);
TileEntity tile = world.getTileEntity(newpos);
if (tile instanceof IInjectable) {
if (!((IInjectable) tile).canInjectItems(side.getOpposite())) {
continue;
}
possiblePipes.add((IInjectable) tile);
pipeDirections.add(side.getOpposite());
} else {
IInjectable wrapper = CompatHooks.INSTANCE.getInjectableWrapper(tile, side);
if (wrapper != null) {
possiblePipes.add(wrapper);
pipeDirections.add(side.getOpposite());
}
}
}
if (possiblePipes.size() > 0) {
int choice = RANDOM.nextInt(possiblePipes.size());
IInjectable pipeEntry = possiblePipes.get(choice);
return pipeEntry.injectItem(stack, true, pipeDirections.get(choice), null);
}
return 0;
}
use of buildcraft.api.transport.IInjectable in project BuildCraft by BuildCraft.
the class AIRobotUnload method unload.
public static boolean unload(EntityRobotBase robot, DockingStation station, boolean doUnload) {
if (station == null) {
return false;
}
IInjectable output = station.getItemOutput();
if (output == null) {
return false;
}
EnumFacing injectSide = station.getItemOutputSide().face;
if (!output.canInjectItems(injectSide)) {
return false;
}
for (IInvSlot robotSlot : InventoryIterator.getIterable(robot)) {
if (robotSlot.getStackInSlot() == null) {
continue;
}
if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackOrListFilter(robotSlot.getStackInSlot()), ActionStationAcceptItems.class)) {
continue;
}
ItemStack stack = robotSlot.getStackInSlot();
int used = output.injectItem(stack, doUnload, injectSide, null);
if (used > 0) {
if (doUnload) {
robotSlot.decreaseStackInSlot(used);
}
return true;
}
}
if (robot.getHeldItem() != null) {
if (!ActionRobotFilter.canInteractWithItem(station, new ArrayStackOrListFilter(robot.getHeldItem()), ActionStationAcceptItems.class)) {
return false;
}
ItemStack stack = robot.getHeldItem();
int used = output.injectItem(stack, doUnload, injectSide, null);
if (used > 0) {
if (doUnload) {
if (stack.stackSize <= used) {
robot.setItemInUse(null);
} else {
stack.stackSize -= used;
}
}
return true;
}
}
return false;
}
Aggregations