aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/core/slots/SlotOutput.java
blob: 018df63a4ce504cfcd0a45cdbc1a65562180fe28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gtPlusPlus.core.slots;

import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;

public class SlotOutput extends SlotCrafting {

	private final IInventory craftMatrix;
	private final EntityPlayer thePlayer;
	private int amountCrafted;

	public SlotOutput(final EntityPlayer player, final InventoryCrafting craftingInventory,
			final IInventory p_i45790_3_, final int slotIndex, final int xPosition, final int yPosition) {
		super(player, craftingInventory, p_i45790_3_, slotIndex, xPosition, yPosition);
		this.thePlayer = player;
		this.craftMatrix = craftingInventory;
	}

	/**
	 * Check if the stack is a valid item for this slot. Always true beside for
	 * the armor slots.
	 */
	@Override
	public boolean isItemValid(final ItemStack par1ItemStack) {
		return false;
	}

	/**
	 * Decrease the size of the stack in slot (first int arg) by the amount of
	 * the second int arg. Returns the new stack.
	 */
	@Override
	public ItemStack decrStackSize(final int par1) {
		if (this.getHasStack()) {
			this.amountCrafted += Math.min(par1, this.getStack().stackSize);
		}
		return super.decrStackSize(par1);
	}

	/**
	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes,
	 * not ore and wood. Typically increases an internal count then calls
	 * onCrafting(item).
	 */
	@Override
	protected void onCrafting(final ItemStack par1ItemStack, final int par2) {
		this.amountCrafted += par2;
		this.onCrafting(par1ItemStack);
	}

	/**
	 * the itemStack passed in is the output - ie, iron ingots, and pickaxes,
	 * not ore and wood.
	 */
	@Override
	protected void onCrafting(final ItemStack stack) {
		stack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.amountCrafted);
		this.amountCrafted = 0;
	}

	@Override
	public void onPickupFromSlot(final EntityPlayer playerIn, final ItemStack stack) {
		{
			FMLCommonHandler.instance().firePlayerCraftingEvent(playerIn, stack, this.craftMatrix);
			this.onCrafting(stack);
			for (int i = 0; i < this.craftMatrix.getSizeInventory(); ++i) {
				final ItemStack itemstack1 = this.craftMatrix.getStackInSlot(i);
				if (itemstack1 != null) {
					this.craftMatrix.decrStackSize(i, 1);
					if (itemstack1.getItem().hasContainerItem(itemstack1)) {
						ItemStack itemstack2 = itemstack1.getItem().getContainerItem(itemstack1);
						if (itemstack2.isItemStackDamageable()
								&& (itemstack2.getItemDamage() > itemstack2.getMaxDamage())) {
							MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(this.thePlayer, itemstack2));
							itemstack2 = null;
						}
						if (!this.thePlayer.inventory.addItemStackToInventory(itemstack2)) {
							if (this.craftMatrix.getStackInSlot(i) == null) {
								this.craftMatrix.setInventorySlotContents(i, itemstack2);
							} else {
								this.thePlayer.dropPlayerItemWithRandomChoice(itemstack2, false);
							}
						}
					}
				}
			}
		}
	}
}