下面列出了怎么用net.minecraft.inventory.ISidedInventory的API类实例代码及写法,或者点击链接到github查看源代码。
@Asynchronous(false)
@ScriptCallable(description = "Swap two slots in the inventory")
public void swapStacks(IInventory target,
@Arg(name = "from", description = "The first slot") Index fromSlot,
@Arg(name = "to", description = "The other slot") Index intoSlot,
@Optionals @Arg(name = "fromDirection") ForgeDirection fromDirection,
@Arg(name = "fromDirection") ForgeDirection toDirection) {
IInventory inventory = InventoryUtils.getInventory(target);
Preconditions.checkNotNull(inventory, "Invalid target!");
final int size = inventory.getSizeInventory();
fromSlot.checkElementIndex("first slot id", size);
intoSlot.checkElementIndex("second slot id", size);
if (inventory instanceof ISidedInventory) {
InventoryUtils.swapStacks((ISidedInventory)inventory,
fromSlot.value, Objects.firstNonNull(fromDirection, ForgeDirection.UNKNOWN),
intoSlot.value, Objects.firstNonNull(toDirection, ForgeDirection.UNKNOWN));
} else InventoryUtils.swapStacks(inventory, fromSlot.value, intoSlot.value);
inventory.markDirty();
}
public InventoryRange(IInventory inv, int side)
{
this.inv = inv;
this.face = EnumFacing.values()[side];
if(inv instanceof ISidedInventory)
{
sidedInv = (ISidedInventory)inv;
slots = sidedInv.getSlotsForFace(face);
}
else
{
slots = new int[inv.getSizeInventory()];
for(int i = 0; i < slots.length; i++)
slots[i] = i;
}
}
/**
* Returns a set of integers of slots that are accessible for the given sides.
* @param inventory
* @param accessibleSides a boolean[6], representing for each of the sides if it is accessible or not.
* @return
*/
public static Set<Integer> getAccessibleSlotsForInventoryAndSides(IInventory inventory, boolean[] accessibleSides){
Set<Integer> slots = new HashSet<Integer>();
if(inventory instanceof ISidedInventory) {
for(int i = 0; i < accessibleSides.length; i++) {
if(accessibleSides[i]) {
int[] accessibleSlots = ((ISidedInventory)inventory).getAccessibleSlotsFromSide(i);
for(int accessibleSlot : accessibleSlots) {
slots.add(accessibleSlot);
}
}
}
} else {
for(boolean bool : accessibleSides) {
if(bool) {
for(int i = 0; i < inventory.getSizeInventory(); i++) {
slots.add(i);
}
break;
}
}
}
return slots;
}
public static int[] getAccessibleSlotsForInventory(IInventory inv, ForgeDirection side){
int[] accessibleSlots;
if(inv != null) {
if(inv instanceof ISidedInventory) {
accessibleSlots = ((ISidedInventory)inv).getAccessibleSlotsFromSide(side.ordinal());
} else {
accessibleSlots = new int[inv.getSizeInventory()];
for(int i = 0; i < accessibleSlots.length; i++)
accessibleSlots[i] = i;
}
return accessibleSlots;
} else {
return new int[0];
}
}
public static ItemStack insert(IInventory inventory, ItemStack itemStack, int side, boolean simulate){
if(inventory instanceof ISidedInventory && side > -1) {
ISidedInventory isidedinventory = (ISidedInventory)inventory;
int[] aint = isidedinventory.getAccessibleSlotsFromSide(side);
for(int j = 0; j < aint.length && itemStack != null && itemStack.stackSize > 0; ++j) {
itemStack = insert(inventory, itemStack, aint[j], side, simulate);
}
} else if(inventory != null) {
int k = inventory.getSizeInventory();
for(int l = 0; l < k && itemStack != null && itemStack.stackSize > 0; ++l) {
itemStack = insert(inventory, itemStack, l, side, simulate);
}
}
if(itemStack != null && itemStack.stackSize == 0) {
itemStack = null;
}
return itemStack;
}
public InventoryRange(IInventory inv, Direction side) {
this.inv = inv;
this.face = side;
if (inv instanceof ISidedInventory) {
sidedInv = (ISidedInventory) inv;
slots = sidedInv.getSlotsForFace(face);
} else {
slots = new int[inv.getSizeInventory()];
for (int i = 0; i < slots.length; i++) {
slots[i] = i;
}
}
}
public InventoryRange(IInventory inv, InventoryRange access) {
this.inv = inv;
this.slots = access.slots;
this.face = access.face;
if (inv instanceof ISidedInventory) {
sidedInv = (ISidedInventory) inv;
}
}
private static boolean isInventoryEmpty(IInventory inventoryIn, EnumFacing side)
{
if (inventoryIn instanceof ISidedInventory)
{
ISidedInventory isidedinventory = (ISidedInventory)inventoryIn;
int[] aint = isidedinventory.getSlotsForFace(side);
for (int i : aint)
{
if (isidedinventory.getStackInSlot(i) != null)
{
return false;
}
}
}
else
{
int j = inventoryIn.getSizeInventory();
for (int k = 0; k < j; ++k)
{
if (inventoryIn.getStackInSlot(k) != null)
{
return false;
}
}
}
return true;
}
public InventoryRange(IInventory inv, InventoryRange access)
{
this.inv = inv;
this.slots = access.slots;
this.face = access.face;
if(inv instanceof ISidedInventory)
sidedInv = (ISidedInventory) inv;
}
public static IItemHandler tryGetHandler(TileEntity te, EnumFacing side) {
if (te == null) return null;
if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side))
return te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
if (te instanceof ISidedInventory)
return new SidedInvWrapper((ISidedInventory)te, side);
if (te instanceof IInventory)
return new InvWrapper((IInventory)te);
return null;
}
/**
* Gets if the slot passed in can be extracted from by a spark.
*/
public static boolean isValidSlot(IInventory inv, int slot) {
return !(inv instanceof ISidedInventory) || arrayHas(((ISidedInventory) inv).getAccessibleSlotsFromSide(ForgeDirection.UP.ordinal()), slot) && ((ISidedInventory) inv).canExtractItem(slot, inv.getStackInSlot(slot), ForgeDirection.UP.ordinal());
}
public static boolean canInterfaceWith(TileEntity tile, ForgeDirection direction){
if(tile instanceof IInventory) {
return !(tile instanceof ISidedInventory) || ((ISidedInventory)tile).getAccessibleSlotsFromSide(direction.ordinal()).length > 0;
}
return false;
}
public SidedInventoryHelper(ISidedInventory inventory, ForgeDirection side) {
super(inventory);
this.sidedInventory = inventory;
this.side = side;
}
/**
* @return ISidedInventory representing the inventory accessed from this block.
*/
ISidedInventory getStructureInventory();
public static boolean canInsertItemToInventory(IInventory inventory, ItemStack itemStack, int slot, int side){
return inventory.isItemValidForSlot(slot, itemStack) && (!(inventory instanceof ISidedInventory) || ((ISidedInventory)inventory).canInsertItem(slot, itemStack, side));
}
public static boolean canExtractItemFromInventory(IInventory inventory, ItemStack itemStack, int slot, int side){
return !(inventory instanceof ISidedInventory) || ((ISidedInventory)inventory).canExtractItem(slot, itemStack, side);
}