use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIWorkDeliveryman method gatherItems.
/**
* Gather item from chest.
* Gathers only one stack of the item.
*
* @param buildingToDeliver building to deliver to.
*/
private AIState gatherItems(@NotNull final AbstractBuilding buildingToDeliver, @NotNull final BlockPos position) {
final TileEntity tileEntity = world.getTileEntity(position);
if (tileEntity instanceof TileEntityChest) {
if (!(tileEntity instanceof TileEntityColonyBuilding)) {
if (((TileEntityChest) tileEntity).numPlayersUsing == 0) {
this.world.addBlockEvent(tileEntity.getPos(), tileEntity.getBlockType(), 1, 1);
this.world.notifyNeighborsOfStateChange(tileEntity.getPos(), tileEntity.getBlockType(), true);
this.world.notifyNeighborsOfStateChange(tileEntity.getPos().down(), tileEntity.getBlockType(), true);
setDelay(DUMP_AND_GATHER_DELAY);
return GATHER_IN_WAREHOUSE;
}
this.world.addBlockEvent(tileEntity.getPos(), tileEntity.getBlockType(), 1, 0);
this.world.notifyNeighborsOfStateChange(tileEntity.getPos(), tileEntity.getBlockType(), true);
this.world.notifyNeighborsOfStateChange(tileEntity.getPos().down(), tileEntity.getBlockType(), true);
}
if (buildingToDeliver instanceof BuildingHome) {
final int extraFood = worker.getCitizenData().getSaturation() < EntityCitizen.HIGH_SATURATION ? 1 : 0;
//Tries to extract a certain amount of the item of the chest.
if (InventoryUtils.transferXOfFirstSlotInProviderWithIntoNextFreeSlotInItemHandler(tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null), itemStack -> !InventoryUtils.isItemStackEmpty(itemStack) && itemStack.getItem() instanceof ItemFood, buildingToDeliver.getBuildingLevel() + extraFood, new InvWrapper(worker.getInventoryCitizen()))) {
worker.setHeldItem(SLOT_HAND);
setDelay(DUMP_AND_GATHER_DELAY);
return DELIVERY;
}
((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
itemsToDeliver.clear();
return START_WORKING;
} else if (itemsToDeliver.isEmpty() && !isToolInTileEntity((TileEntityChest) tileEntity, buildingToDeliver.getRequiredTool(), buildingToDeliver.getBuildingLevel())) {
((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
itemsToDeliver.clear();
return START_WORKING;
} else if (!itemsToDeliver.isEmpty()) {
final ItemStack stack = itemsToDeliver.get(0);
if (isInTileEntity((TileEntityChest) tileEntity, stack)) {
itemsToDeliver.remove(0);
worker.setHeldItem(SLOT_HAND);
setDelay(DUMP_AND_GATHER_DELAY);
return DELIVERY;
}
((BuildingDeliveryman) getOwnBuilding()).setBuildingToDeliver(null);
itemsToDeliver.clear();
return START_WORKING;
}
}
setDelay(DUMP_AND_GATHER_DELAY);
return GATHER_IN_WAREHOUSE;
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIWorkLumberjack method placeSaplings.
//todo: we need to use a different way to get Metadata
@SuppressWarnings(DEPRECATION)
private void placeSaplings(final int saplingSlot, @NotNull final ItemStack stack, @NotNull final Block block) {
while (!job.tree.getStumpLocations().isEmpty()) {
final BlockPos pos = job.tree.getStumpLocations().get(0);
if ((BlockPosUtil.setBlock(world, pos, block.getStateFromMeta(stack.getMetadata()), 0x02) && getInventory().getStackInSlot(saplingSlot) != null) || Objects.equals(world.getBlockState(pos), block.getStateFromMeta(stack.getMetadata()))) {
new InvWrapper(getInventory()).extractItem(saplingSlot, 1, false);
job.tree.removeStump(pos);
} else {
return;
}
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIWorkDeliveryman method gatherFromBuilding.
/**
* Gather not needed Items from building.
*
* @param building building to gather it from.
* @return true when finished.
*/
private boolean gatherFromBuilding(@NotNull final AbstractBuilding building) {
if (currentSlot >= building.getTileEntity().getSizeInventory()) {
return true;
}
final ItemStack stack = building.getTileEntity().getStackInSlot(currentSlot);
if (workerRequiresItem(building, stack, alreadyKept) || (building instanceof BuildingHome && stack.getItem() instanceof ItemFood)) {
return false;
}
InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandlers(building.getTileEntity().getSingleChestHandler(), currentSlot, new InvWrapper(worker.getInventoryCitizen()));
building.markDirty();
setDelay(DUMP_AND_GATHER_DELAY);
worker.setHeldItem(SLOT_HAND);
return false;
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class BuildingBuilder method updateAvailableResources.
/**
* Update the available resources.
* <p>
* which are needed for the build and in the builder's chest or inventory
*/
private void updateAvailableResources() {
final EntityCitizen builder = getWorkerEntity();
InventoryCitizen builderInventory = null;
if (builder != null) {
builderInventory = builder.getInventoryCitizen();
}
for (@NotNull final Map.Entry<String, BuildingBuilderResource> entry : neededResources.entrySet()) {
final BuildingBuilderResource resource = entry.getValue();
resource.setAvailable(0);
if (builderInventory != null) {
resource.addAvailable(InventoryUtils.getItemCountInItemHandler(new InvWrapper(builderInventory), resource.getItem(), resource.getDamageValue()));
}
final TileEntity chestInventory = this.getTileEntity();
if (chestInventory != null) {
resource.addAvailable(InventoryUtils.getItemCountInProvider(chestInventory, resource.getItem(), resource.getDamageValue()));
}
//Count in the additional chests as well
if (builder != null) {
for (final BlockPos pos : getAdditionalCountainers()) {
final TileEntity entity = builder.world.getTileEntity(pos);
if (entity instanceof TileEntityChest) {
resource.addAvailable(InventoryUtils.getItemCountInProvider(entity, resource.getItem(), resource.getDamageValue()));
}
}
}
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class TransferItemsRequestMessage method messageOnServerThread.
@Override
public void messageOnServerThread(final TransferItemsRequestMessage message, final EntityPlayerMP player) {
final Colony colony = ColonyManager.getColony(message.colonyId);
if (colony == null) {
Log.getLogger().warn("TransferItemsRequestMessage colony is null");
return;
}
final AbstractBuilding building = colony.getBuilding(message.buildingId);
if (building == null) {
Log.getLogger().warn("TransferItemsRequestMessage building is null");
return;
}
if (message.quantity <= 0) {
Log.getLogger().warn("TransferItemsRequestMessage quantity below 0");
return;
}
final Item item = message.itemStack.getItem();
final int amountToTake = Math.min(message.quantity, InventoryUtils.getItemCountInItemHandler(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage()));
final ItemStack itemStackToTake = new ItemStack(item, amountToTake, message.itemStack.getItemDamage());
ItemStack remainingItemStack = InventoryUtils.addItemStackToProviderWithResult(building.getTileEntity(), itemStackToTake);
if (!InventoryUtils.isItemStackEmpty(remainingItemStack)) {
//If we still have some to drop, let's try the additional chests now
if (InventoryUtils.getItemStackSize(remainingItemStack) > 0) {
final World world = colony.getWorld();
for (final BlockPos pos : building.getAdditionalCountainers()) {
final TileEntity entity = world.getTileEntity(pos);
remainingItemStack = InventoryUtils.addItemStackToProviderWithResult(entity, remainingItemStack);
if (InventoryUtils.isItemStackEmpty(remainingItemStack)) {
break;
}
}
}
}
if (InventoryUtils.isItemStackEmpty(remainingItemStack) || InventoryUtils.getItemStackSize(remainingItemStack) != InventoryUtils.getItemStackSize(itemStackToTake)) {
//Only doing this at the moment as the additional chest do not detect new content
building.getTileEntity().markDirty();
}
int amountToRemoveFromPlayer = amountToTake - InventoryUtils.getItemStackSize(remainingItemStack);
while (amountToRemoveFromPlayer > 0) {
final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage());
final ItemStack itemsTaken = player.inventory.decrStackSize(slot, amountToRemoveFromPlayer);
amountToRemoveFromPlayer -= InventoryUtils.getItemStackSize(itemsTaken);
}
}
Aggregations