use of net.minecraft.inventory.IInventory in project LogisticsPipes by RS485.
the class ModuleExtractor method tick.
@Override
public void tick() {
if (++currentTick < ticksToAction()) {
return;
}
currentTick = 0;
//Extract Item
IInventory realInventory = _service.getRealInventory();
if (realInventory == null) {
return;
}
ForgeDirection extractOrientation = _sneakyDirection;
if (extractOrientation == ForgeDirection.UNKNOWN) {
extractOrientation = _service.inventoryOrientation().getOpposite();
}
IInventoryUtil targetUtil = _service.getSneakyInventory(extractOrientation, true);
for (int i = 0; i < targetUtil.getSizeInventory(); i++) {
ItemStack slot = targetUtil.getStackInSlot(i);
if (slot == null) {
continue;
}
ItemIdentifier slotitem = ItemIdentifier.get(slot);
List<Integer> jamList = new LinkedList<>();
Pair<Integer, SinkReply> reply = _service.hasDestination(slotitem, true, jamList);
if (reply == null) {
continue;
}
int itemsleft = itemsToExtract();
while (reply != null) {
int count = Math.min(itemsleft, slot.stackSize);
count = Math.min(count, slotitem.getMaxStackSize());
if (reply.getValue2().maxNumberOfItems > 0) {
count = Math.min(count, reply.getValue2().maxNumberOfItems);
}
while (!_service.useEnergy(neededEnergy() * count) && count > 0) {
_service.spawnParticle(Particles.OrangeParticle, 2);
count--;
}
if (count <= 0) {
break;
}
ItemStack stackToSend = targetUtil.decrStackSize(i, count);
if (stackToSend == null || stackToSend.stackSize == 0) {
break;
}
count = stackToSend.stackSize;
_service.sendStack(stackToSend, reply, itemSendMode());
itemsleft -= count;
if (itemsleft <= 0) {
break;
}
slot = targetUtil.getStackInSlot(i);
if (slot == null) {
break;
}
jamList.add(reply.getValue1());
reply = _service.hasDestination(ItemIdentifier.get(slot), true, jamList);
}
break;
}
}
use of net.minecraft.inventory.IInventory in project LogisticsPipes by RS485.
the class SlotFinderNumberPacket method processPacket.
@Override
@SuppressWarnings("unchecked")
public void processPacket(EntityPlayer player) {
IInventory inv = this.getTile(player.worldObj, IInventory.class);
if (inv instanceof ISidedInventory) {
inv = new SidedInventoryMinecraftAdapter((ISidedInventory) inv, ForgeDirection.UNKNOWN, false);
}
IInventoryUtil util = SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil(inv);
Slot result = null;
if (((List<Slot>) player.openContainer.inventorySlots).get(inventorySlot).slotNumber == inventorySlot) {
result = ((List<Slot>) player.openContainer.inventorySlots).get(inventorySlot);
}
if (result == null) {
for (Slot slotObject : (List<Slot>) player.openContainer.inventorySlots) {
if (slotObject.slotNumber == inventorySlot) {
result = slotObject;
break;
}
}
}
if (result == null) {
player.addChatComponentMessage(new ChatComponentTranslation("lp.chat.slotnotfound"));
}
int resultIndex = -1;
if (resultIndex == -1) {
ItemStack content = result.getStack();
if (content != null) {
for (int i = 0; i < util.getSizeInventory(); i++) {
if (content == util.getStackInSlot(i)) {
resultIndex = i;
break;
}
}
} else {
ItemStack dummyStack = new ItemStack(Blocks.stone, 0, 0);
NBTTagCompound nbt = new NBTTagCompound();
//Make it unique
nbt.setBoolean("LPStackFinderBoolean", true);
dummyStack.setTagCompound(nbt);
result.putStack(dummyStack);
for (int i = 0; i < util.getSizeInventory(); i++) {
if (dummyStack == util.getStackInSlot(i)) {
resultIndex = i;
break;
}
}
if (resultIndex == -1) {
for (int i = 0; i < util.getSizeInventory(); i++) {
ItemStack stack = util.getStackInSlot(i);
if (stack == null) {
continue;
}
if (ItemIdentifier.get(stack).equals(ItemIdentifier.get(dummyStack)) && stack.stackSize == dummyStack.stackSize) {
resultIndex = i;
break;
}
}
}
result.putStack(null);
}
}
if (resultIndex == -1) {
player.addChatComponentMessage(new ChatComponentTranslation("lp.chat.slotnotfound"));
} else {
//Copy pipe to coordinates to use the getPipe method
setPosX(getPipePosX());
setPosY(getPipePosY());
setPosZ(getPipePosZ());
ModuleActiveSupplier module = this.getLogisticsModule(player, ModuleActiveSupplier.class);
if (module != null) {
module.slotArray[slot] = resultIndex;
}
}
}
use of net.minecraft.inventory.IInventory in project LogisticsPipes by RS485.
the class SlotFinderOpenGuiPacket method processPacket.
@Override
public void processPacket(EntityPlayer player) {
//hack to avoid wrenching blocks
int savedEquipped = player.inventory.currentItem;
boolean foundSlot = false;
//try to find a empty slot
for (int i = 0; i < 9; i++) {
if (player.inventory.getStackInSlot(i) == null) {
foundSlot = true;
player.inventory.currentItem = i;
break;
}
}
//okay, anything that's a block?
if (!foundSlot) {
for (int i = 0; i < 9; i++) {
ItemStack is = player.inventory.getStackInSlot(i);
if (is.getItem() instanceof ItemBlock) {
foundSlot = true;
player.inventory.currentItem = i;
break;
}
}
}
//give up and select whatever is right of the current slot
if (!foundSlot) {
player.inventory.currentItem = (player.inventory.currentItem + 1) % 9;
}
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(player.worldObj, getPosX(), getPosY(), getPosZ());
Iterator<AdjacentTileEntity> adjacentIt = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).iterator();
boolean found = false;
while (adjacentIt.hasNext()) {
AdjacentTileEntity adjacent = adjacentIt.next();
if (adjacent.tileEntity instanceof IInventory) {
if (!(SimpleServiceLocator.inventoryUtilFactory.getInventoryUtil((IInventory) adjacent.tileEntity) instanceof ISpecialInsertion)) {
continue;
}
}
for (ICraftingRecipeProvider provider : SimpleServiceLocator.craftingRecipeProviders) {
if (provider.canOpenGui(adjacent.tileEntity)) {
found = true;
break;
}
}
if (!found) {
found = (adjacent.tileEntity instanceof IInventory);
}
if (found) {
Block block = adjacent.tileEntity.getBlockType();
int xCoord = adjacent.tileEntity.xCoord;
int yCoord = adjacent.tileEntity.yCoord;
int zCoord = adjacent.tileEntity.zCoord;
if (SimpleServiceLocator.enderStorageProxy.isEnderChestBlock(block)) {
SimpleServiceLocator.enderStorageProxy.openEnderChest(player.worldObj, xCoord, yCoord, zCoord, player);
//@formatter:off
MainProxy.sendPacketToPlayer(PacketHandler.getPacket(SlotFinderActivatePacket.class).setTagetPosX(xCoord).setTagetPosY(yCoord).setTagetPosZ(zCoord).setSlot(getSlot()).setPacketPos(this), player);
//@formatter:on
}
if (block != null) {
if (block.onBlockActivated(player.worldObj, xCoord, yCoord, zCoord, player, 0, 0, 0, 0)) {
//@formatter:off
MainProxy.sendPacketToPlayer(PacketHandler.getPacket(SlotFinderActivatePacket.class).setTagetPosX(xCoord).setTagetPosY(yCoord).setTagetPosZ(zCoord).setSlot(getSlot()).setPacketPos(this), player);
//@formatter:on
break;
}
}
}
}
player.inventory.currentItem = savedEquipped;
}
use of net.minecraft.inventory.IInventory in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method getAllItems.
@Override
public void getAllItems(Map<ItemIdentifier, Integer> items, List<IFilter> filters) {
if (!isEnabled()) {
return;
}
HashMap<ItemIdentifier, Integer> addedItems = new HashMap<>();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Map<ItemIdentifier, Integer>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> getAdaptedInventoryUtil(adjacent).getItemsAndCount()).iterator();
outer: while (iterator.hasNext()) {
Iterator<Entry<ItemIdentifier, Integer>> entryIterator = iterator.next().entrySet().stream().filter(currentItem -> !items.containsKey(currentItem.getKey())).filter(currentItem -> !hasFilter() || (!isExcludeFilter() || !itemIsFiltered(currentItem.getKey())) && (isExcludeFilter() || itemIsFiltered(currentItem.getKey()))).iterator();
while (entryIterator.hasNext()) {
Entry<ItemIdentifier, Integer> next = entryIterator.next();
for (IFilter filter : filters) {
if (filter.isBlocked() == filter.isFilteredItem(next.getKey().getUndamaged()) || filter.blockProvider()) {
continue outer;
}
}
Integer addedAmount = addedItems.get(next.getKey());
if (addedAmount == null) {
addedItems.put(next.getKey(), next.getValue());
} else {
addedItems.put(next.getKey(), addedAmount + next.getValue());
}
}
}
// reduce what has been reserved, add.
for (Entry<ItemIdentifier, Integer> item : addedItems.entrySet()) {
int remaining = item.getValue() - _orderManager.totalItemsCountInOrders(item.getKey());
if (remaining < 1) {
continue;
}
items.put(item.getKey(), remaining);
}
}
use of net.minecraft.inventory.IInventory in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method sendStack.
private int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
ItemIdentifier item = stack.getItem();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Pair<IInventoryUtil, ForgeDirection>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>(getAdaptedInventoryUtil(adjacent), adjacent.direction)).iterator();
while (iterator.hasNext()) {
Pair<IInventoryUtil, ForgeDirection> next = iterator.next();
int available = next.getValue1().itemCount(item);
if (available == 0) {
continue;
}
int wanted = Math.min(available, stack.getStackSize());
wanted = Math.min(wanted, maxCount);
wanted = Math.min(wanted, item.getMaxStackSize());
IRouter dRtr = SimpleServiceLocator.routerManager.getRouterUnsafe(destination, false);
if (dRtr == null) {
_orderManager.sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(dRtr, null, true, stack.getItem(), null, true, false);
boolean defersend = false;
if (reply != null) {
// some pipes are not aware of the space in the adjacent inventory, so they return null
if (reply.maxNumberOfItems < wanted) {
wanted = reply.maxNumberOfItems;
if (wanted <= 0) {
_orderManager.deferSend();
return 0;
}
defersend = true;
}
}
if (!canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = next.getValue1().getMultipleItems(item, wanted);
if (removed == null || removed.stackSize == 0) {
continue;
}
int sent = removed.stackSize;
useEnergy(sent * neededEnergy());
IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(removed);
routedItem.setDestination(destination);
routedItem.setTransportMode(TransportMode.Active);
routedItem.setAdditionalTargetInformation(info);
super.queueRoutedItem(routedItem, next.getValue2());
_orderManager.sendSuccessfull(sent, defersend, routedItem);
return sent;
}
_orderManager.sendFailed();
return 0;
}
Aggregations