resonant-induction/archive/java/dark/lib/prefab/invgui/SlotSpecific.java
2014-01-11 17:44:07 +08:00

93 lines
2.2 KiB
Java

package dark.lib.prefab.invgui;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Creates a slot with a specific amount of items that matches the slot's requirements. Allows easy
* shift right clicking management and slot blocking in classes. In your container you can use
* this.getSlot(i).isItemValid to justify the player's shift clicking actions to match the slot.
*
* @author Calclavia
*/
public class SlotSpecific extends Slot
{
public ItemStack[] validItemStacks = new ItemStack[0];
public Class[] validClasses = new Class[0];
public boolean isInverted = false;
public boolean isMetadataSensitive = false;
public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, ItemStack... itemStacks)
{
super(inventory, slotID, xPos, yPos);
this.setItemStacks(itemStacks);
}
public SlotSpecific(IInventory inventory, int slotID, int xPos, int yPos, Class... validClasses)
{
super(inventory, slotID, xPos, yPos);
this.setClasses(validClasses);
}
public SlotSpecific setMetadataSensitive()
{
this.isMetadataSensitive = true;
return this;
}
public SlotSpecific setItemStacks(ItemStack... validItemStacks)
{
this.validItemStacks = validItemStacks;
return this;
}
public SlotSpecific setClasses(Class... validClasses)
{
this.validClasses = validClasses;
return this;
}
public SlotSpecific toggleInverted()
{
this.isInverted = !this.isInverted;
return this;
}
/** Check if the stack is a valid item for this slot. Always true beside for the armor slots. */
@Override
public boolean isItemValid(ItemStack compareStack)
{
boolean returnValue = false;
for (ItemStack itemStack : this.validItemStacks)
{
if (compareStack.isItemEqual(itemStack) || (!this.isMetadataSensitive && compareStack.itemID == itemStack.itemID))
{
returnValue = true;
break;
}
}
if (!returnValue)
{
for (Class clazz : this.validClasses)
{
if (clazz.equals(compareStack.getItem().getClass()) || clazz.isInstance(compareStack.getItem()))
{
returnValue = true;
break;
}
}
}
if (this.isInverted)
{
return !returnValue;
}
return returnValue;
}
}