use of net.minecraftforge.items.wrapper.InvWrapper in project MinecraftForge by MinecraftForge.
the class FluidUtil method interactWithFluidHandler.
/**
* Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
* First it tries to fill the container item from the fluid handler,
* if that action fails then it tries to drain the container item into the fluid handler.
*
* @param stack The filled or empty fluid container.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param fluidHandler The fluid handler to interact with.
* @param player The player doing the interaction between the item and fluid handler.
* @return a {@link FluidActionResult} holding the result and resulting container.
*/
@Nonnull
public static FluidActionResult interactWithFluidHandler(@Nonnull ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player) {
if (stack.isEmpty() || fluidHandler == null || player == null) {
return FluidActionResult.FAILURE;
}
IItemHandler playerInventory = new InvWrapper(player.inventory);
FluidActionResult fillResult = tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
if (fillResult.isSuccess()) {
return fillResult;
} else {
return tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class WindowHutBuilder method pullResourcesFromHut.
/**
* Retrieve resources from the building to display in GUI.
*/
private void pullResourcesFromHut() {
final AbstractBuilding.View newView = builder.getColony().getBuilding(builder.getID());
if (newView instanceof BuildingBuilderView) {
final BuildingBuilderView updatedView = (BuildingBuilderView) newView;
final InventoryPlayer inventory = this.mc.player.inventory;
resources.clear();
resources.addAll(updatedView.getResources().values());
for (final BuildingBuilderResource resource : resources) {
resource.setPlayerAmount(InventoryUtils.getItemCountInItemHandler(new InvWrapper(inventory), resource.getItem(), resource.getDamageValue()));
}
resources.sort(new BuildingBuilderResource.ResourceComparator());
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class TileEntityWareHouse method dumpInventoryIntoWareHouse.
/**
* Dump the inventory of a citizen into the warehouse.
* Go through all items and search the right chest to dump it in.
* @param inventoryCitizen the inventory of the citizen
*/
public void dumpInventoryIntoWareHouse(@NotNull final InventoryCitizen inventoryCitizen) {
for (int i = 0; i < new InvWrapper(inventoryCitizen).getSlots(); i++) {
final ItemStack stack = inventoryCitizen.getStackInSlot(i);
if (InventoryUtils.isItemStackEmpty(stack)) {
continue;
}
@Nullable final TileEntityChest chest = searchRightChestForStack(stack);
if (chest == null) {
LanguageHandler.sendPlayersMessage(getColony().getMessageEntityPlayers(), COM_MINECOLONIES_COREMOD_WAREHOUSE_FULL);
return;
}
InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(new InvWrapper(inventoryCitizen), i, chest);
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIGoHome method handleSaturation.
/**
* Handle the saturation of the citizen.
*
* @param pos the position.
*/
private void handleSaturation(@NotNull final BlockPos pos) {
if (citizen.isWorkerAtSiteWithMove(pos, 2) && citizen.getColony() != null && citizen.getCitizenData() != null && citizen.getCitizenData().getSaturation() < EntityCitizen.HIGH_SATURATION) {
final double currentSaturation = citizen.getCitizenData().getSaturation();
boolean tookFood = false;
final AbstractBuilding home = citizen.getColony().getBuilding(pos);
if (home instanceof BuildingHome && currentSaturation < EntityCitizen.FULL_SATURATION) {
final int slot = InventoryUtils.findFirstSlotInProviderNotEmptyWith(home.getTileEntity(), itemStack -> itemStack.getItem() instanceof ItemFood);
if (slot != -1) {
final ItemStack stack = home.getTileEntity().getStackInSlot(slot);
if (!InventoryUtils.isItemStackEmpty(stack)) {
final int slotToSet = InventoryUtils.getFirstOpenSlotFromItemHandler(new InvWrapper(citizen.getInventoryCitizen()));
if (slotToSet == -1) {
InventoryUtils.forceItemStackToItemHandler(new InvWrapper(citizen.getInventoryCitizen()), new ItemStack(stack.getItem(), 1), stack1 -> citizen.getWorkBuilding() == null || !citizen.getWorkBuilding().neededForWorker(stack1));
} else {
citizen.getInventoryCitizen().setInventorySlotContents(slotToSet, new ItemStack(stack.getItem(), 1));
}
tookFood = true;
stack.setCount(stack.getCount() - 1);
}
((BuildingHome) home).setFoodNeeded(false);
}
}
if (!tookFood) {
requestFoodIfRequired(currentSaturation, home);
}
}
}
use of net.minecraftforge.items.wrapper.InvWrapper in project minecolonies by Minecolonies.
the class EntityAIStructureMiner method setBlockFromInventory.
private void setBlockFromInventory(@NotNull final BlockPos location, final Block block, final IBlockState metadata) {
final int slot;
if (block instanceof BlockLadder) {
slot = worker.findFirstSlotInInventoryWith(block, -1);
} else {
slot = worker.findFirstSlotInInventoryWith(block, block.getMetaFromState(metadata));
}
if (slot != -1) {
new InvWrapper(getInventory()).extractItem(slot, 1, false);
//Flag 1+2 is needed for updates
world.setBlockState(location, metadata, 0x03);
}
}
Aggregations