use of net.minecraftforge.items.IItemHandlerModifiable in project Tropicraft by Tropicraft.
the class BambooDoubleChestItemHandler method setStackInSlot.
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
boolean accessingUpperChest = slot < 27;
int targetSlot = accessingUpperChest ? slot : slot - 27;
TileEntityBambooChest chest = getChest(accessingUpperChest);
if (chest != null) {
IItemHandler singleHandler = chest.getSingleChestHandler();
if (singleHandler instanceof IItemHandlerModifiable) {
((IItemHandlerModifiable) singleHandler).setStackInSlot(targetSlot, stack);
}
}
chest = getChest(!accessingUpperChest);
if (chest != null)
chest.markDirty();
}
use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.
the class ArtisanRecipe method onCraftReduceIngredients.
protected void onCraftReduceIngredients(ICraftingContext context) {
IItemHandlerModifiable matrixHandler = context.getCraftingMatrixHandler();
IFluidHandler fluidHandler = context.getFluidHandler();
List<ItemStack> remainingItems = this.getRemainingItems(context, NonNullList.withSize(matrixHandler.getSlots(), ItemStack.EMPTY));
for (int i = 0; i < remainingItems.size(); i++) {
ItemStack itemStack = matrixHandler.getStackInSlot(i);
ItemStack remainingItemStack = remainingItems.get(i);
if (!itemStack.isEmpty()) {
matrixHandler.setStackInSlot(i, Util.decrease(itemStack.copy(), 1, false));
itemStack = matrixHandler.getStackInSlot(i);
}
if (!remainingItemStack.isEmpty()) {
if (itemStack.isEmpty()) {
matrixHandler.setStackInSlot(i, remainingItemStack);
} else if (ItemStack.areItemsEqual(itemStack, remainingItemStack) && ItemStack.areItemStackTagsEqual(itemStack, remainingItemStack)) {
int combinedStackSize = remainingItemStack.getCount() + itemStack.getCount();
if (combinedStackSize > itemStack.getMaxStackSize()) {
// Special handling if combined stack size exceeds max stack size.
// First, set the existing slot to the maximum stack size. Next,
// loop while the combined stack size is larger than the max stack
// size and put the items into the player's inventory or on the
// ground. Finally, put the remaining items in the player's
// inventory or on the ground.
ItemStack copy = remainingItemStack.copy();
copy.setCount(itemStack.getMaxStackSize());
matrixHandler.setStackInSlot(i, copy);
combinedStackSize -= itemStack.getMaxStackSize();
while (combinedStackSize > itemStack.getMaxStackSize()) {
copy = remainingItemStack.copy();
copy.setCount(itemStack.getMaxStackSize());
if (!context.getPlayer().addItemStackToInventory(copy)) {
context.getPlayer().dropItem(copy, false);
}
combinedStackSize -= itemStack.getMaxStackSize();
}
remainingItemStack.grow(itemStack.getCount());
if (!context.getPlayer().addItemStackToInventory(remainingItemStack)) {
context.getPlayer().dropItem(remainingItemStack, false);
}
} else {
remainingItemStack.grow(itemStack.getCount());
matrixHandler.setStackInSlot(i, remainingItemStack);
}
} else if (!context.getPlayer().addItemStackToInventory(remainingItemStack)) {
context.getPlayer().dropItem(remainingItemStack, false);
}
}
}
FluidStack fluidIngredient = this.getFluidIngredient();
if (fluidIngredient != null) {
fluidHandler.drain(fluidIngredient, true);
}
}
use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.
the class ArtisanRecipe method onCraftDamageTool.
protected void onCraftDamageTool(ICraftingContext context, int toolIndex) {
World world = context.getWorld();
EntityPlayer player = context.getPlayer();
IItemHandlerModifiable toolHandler = context.getToolHandler();
ItemStack itemStack = toolHandler.getStackInSlot(toolIndex);
if (!itemStack.isEmpty() && this.isValidTool(itemStack, toolIndex)) {
int itemDamage = itemStack.getMetadata() + this.getToolDamage(toolIndex);
if (itemDamage >= itemStack.getItem().getMaxDamage(itemStack)) {
toolHandler.setStackInSlot(toolIndex, ItemStack.EMPTY);
if (!world.isRemote) {
world.playSound(player, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ITEM_BREAK, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
} else {
ItemStack copy = itemStack.copy();
copy.setItemDamage(itemDamage);
toolHandler.setStackInSlot(toolIndex, copy);
}
}
}
use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.
the class ArtisanRecipe method onCraftReduceSecondaryIngredients.
protected void onCraftReduceSecondaryIngredients(ICraftingContext context) {
IItemHandlerModifiable secondaryIngredientHandler = context.getSecondaryIngredientHandler();
if (!this.consumeSecondaryIngredients() || secondaryIngredientHandler == null) {
return;
}
List<IArtisanIngredient> secondaryIngredients = this.secondaryIngredients;
if (!secondaryIngredients.isEmpty()) {
for (IArtisanIngredient requiredIngredient : secondaryIngredients) {
int requiredAmount = requiredIngredient.getAmount();
int slotCount = secondaryIngredientHandler.getSlots();
for (int i = 0; i < slotCount; i++) {
ItemStack stackInSlot = secondaryIngredientHandler.getStackInSlot(i);
if (stackInSlot.isEmpty()) {
continue;
}
if (requiredIngredient.matchesIgnoreAmount(stackInSlot)) {
// get the remaining secondary item
ItemStack remainingItemStack = this.getRemainingSecondaryItem(context, requiredIngredient, stackInSlot);
int existingStackCount = stackInSlot.getCount();
if (existingStackCount <= requiredAmount) {
if (remainingItemStack.isEmpty()) {
// If the remaining item is empty, empty the slot.
secondaryIngredientHandler.setStackInSlot(i, ItemStack.EMPTY);
} else {
// First, empty the slot. Next, increase the remaining item count to match
// the slot's previous stack count. Finally, attempt to insert the remaining
// item into the secondary ingredient slots spilling any overflow into the
// player's inventory or onto the ground.
secondaryIngredientHandler.setStackInSlot(i, ItemStack.EMPTY);
remainingItemStack.setCount(existingStackCount);
ItemStack itemStack = secondaryIngredientHandler.insertItem(i, remainingItemStack, false);
for (int j = 0; !itemStack.isEmpty() && j < secondaryIngredientHandler.getSlots() && j != i; j++) {
itemStack = secondaryIngredientHandler.insertItem(j, itemStack, false);
}
if (!itemStack.isEmpty() && !context.getPlayer().addItemStackToInventory(itemStack)) {
context.getPlayer().dropItem(itemStack, false);
}
}
requiredAmount -= existingStackCount;
} else if (existingStackCount > requiredAmount) {
// Replace partial stack in slot with remaining item.
// First, decrease the existing stack size. Next, since we know that the
// existing stack has been decreased by the required amount, we increase
// the remaining item stack count by the required amount. Finally, attempt
// to insert the remaining item stack into the secondary ingredient slots
// spilling any overflow into the player's inventory or onto the ground.
ItemStack decreasedStack = Util.decrease(stackInSlot.copy(), requiredAmount, false);
secondaryIngredientHandler.setStackInSlot(i, decreasedStack);
if (!remainingItemStack.isEmpty()) {
remainingItemStack.setCount(requiredAmount);
ItemStack itemStack = secondaryIngredientHandler.insertItem(i, remainingItemStack, false);
for (int j = 0; !itemStack.isEmpty() && j < secondaryIngredientHandler.getSlots() && j != i; j++) {
itemStack = secondaryIngredientHandler.insertItem(j, itemStack, false);
}
if (!itemStack.isEmpty() && !context.getPlayer().addItemStackToInventory(itemStack)) {
context.getPlayer().dropItem(itemStack, false);
}
}
requiredAmount = 0;
}
if (requiredAmount == 0) {
break;
}
}
}
if (requiredAmount > 0) {
// TODO: failed to find all required ingredients... shouldn't happen if the matching code is correct
}
}
}
}
use of net.minecraftforge.items.IItemHandlerModifiable in project artisan-worktables by codetaylor.
the class ArtisanRecipe method onCraftCheckAndReplaceTool.
protected void onCraftCheckAndReplaceTool(ICraftingContext context, int toolIndex) {
IItemHandlerModifiable toolHandler = context.getToolHandler();
ItemStack itemStack = toolHandler.getStackInSlot(toolIndex);
if (!this.hasSufficientToolDurability(itemStack, toolIndex)) {
// Tool needs to be replaced
IItemHandler capability = context.getToolReplacementHandler();
if (capability == null) {
return;
}
int slotCount = capability.getSlots();
for (int i = 0; i < slotCount; i++) {
ItemStack potentialTool = capability.getStackInSlot(i);
if (potentialTool.isEmpty()) {
continue;
}
if (this.isValidTool(potentialTool, toolIndex) && this.hasSufficientToolDurability(potentialTool, toolIndex)) {
// Found an acceptable tool
potentialTool = capability.extractItem(i, 1, false);
capability.insertItem(i, toolHandler.getStackInSlot(toolIndex), false);
toolHandler.setStackInSlot(toolIndex, potentialTool);
}
}
}
}
Aggregations