aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/core/slots/SlotLockedInput.java
blob: c67f8acfeb5c60c7f8021203d14c0d05a7caf79c (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
package gtPlusPlus.core.slots;

import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import gregtech.api.interfaces.tileentity.IGregTechTileEntity;

import gtPlusPlus.core.util.reflect.ReflectionUtils;
import gtPlusPlus.xmod.gregtech.common.tileentities.storage.GT_MetaTileEntity_TieredChest;

public class SlotLockedInput extends Slot {

	private ItemStack mLockStack;
	private final IGregTechTileEntity mEntity;
	private boolean mChecked = false;
	
	public SlotLockedInput(final IGregTechTileEntity inventory, final int index, final int x, final int y, ItemStack lockStack) {
		super(inventory, index, x, y);
		mLockStack = lockStack;
		mEntity = inventory;
	}

	@Override
	public boolean isItemValid(final ItemStack itemstack) {	
		if (mEntity == null) {
			return false;
		}
		else {
			if (!mChecked) {
				try {
				mLockStack = (ItemStack) ReflectionUtils.getField(this.mEntity.getMetaTileEntity().getClass(), "mItemStack").get(this.mEntity.getMetaTileEntity());
				}
				catch (Throwable t) {
					t.printStackTrace();
					mLockStack = null;
				}
				mChecked = true;
			}
		}
		
		if (mLockStack == null) {
			return true;
		}
		else {
			if (ItemStack.areItemStacksEqual(itemstack, mLockStack)) {
				return true;
			}
		}		
		return false;
	}

	@Override
	public int getSlotStackLimit() {
		return mLockStack == null ? 64 : mLockStack.getMaxStackSize();
	}

}