下面列出了怎么用net.minecraft.inventory.IInventory的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean shouldTrackWithThisEntry(IBlockAccess world, int x, int y, int z, Block block, TileEntity te){
if(tileEntityClassToNameMapping == null) {
try {
tileEntityClassToNameMapping = (Map)ReflectionHelper.findField(TileEntity.class, "field_145853_j", "classToNameMap").get(null);
} catch(Exception e) {
Log.error("[BlockTrackEntryInventory.class] Uhm reflection failed here!");
e.printStackTrace();
}
}
if(te instanceof TileEntityChest) {
TileEntityChest chest = (TileEntityChest)te;
if(chest.adjacentChestXNeg != null || chest.adjacentChestZNeg != null) return false;
}
return te != null && !invBlackList.contains(tileEntityClassToNameMapping.get(te.getClass())) && te instanceof IInventory && ((IInventory)te).getSizeInventory() > 0 && !MinecraftForge.EVENT_BUS.post(new InventoryTrackEvent(te));
}
public ContainerBusFluidStorage(IInventory inventoryPlayer, IInventory inventoryTileEntity)
{
super(inventoryTileEntity.getSizeInventory());
tileentity = inventoryTileEntity;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 9; j++)
{
addSlotToContainer(new SlotFake(inventoryTileEntity, j + i * 9, 8 + j * 18, i * 18 - 10)
{
public boolean isItemValid(ItemStack itemstack)
{
return tileentity.isItemValidForSlot(0, itemstack);
}
});
}
}
bindPlayerInventory(inventoryPlayer);
}
@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 ContainerMinecoprocessor(IInventory playerInventory, IInventory te) {
this.te = te;
this.codeBookSlot = this.addSlotToContainer(new CodeBookSlot(te, 0, 80, 35));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (int k = 0; k < 9; ++k) {
this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
}
}
public static int getRequestedAmount(SemiBlockLogistics requester, ItemStack providingStack){
TileEntity te = requester.getTileEntity();
if(!(te instanceof IInventory)) return 0;
int requestedAmount = requester instanceof ISpecificRequester ? ((ISpecificRequester)requester).amountRequested(providingStack) : providingStack.stackSize;
if(requestedAmount == 0) return 0;
providingStack = providingStack.copy();
providingStack.stackSize = requestedAmount;
ItemStack remainder = providingStack.copy();
remainder.stackSize += requester.getIncomingItems(providingStack);
for(ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
remainder = IOHelper.insert(te, remainder, d, true);
if(remainder == null) break;
}
if(remainder != null) providingStack.stackSize -= remainder.stackSize;
if(providingStack.stackSize <= 0) return 0;
return providingStack.stackSize;
}
private boolean inventoryCanCarry(){
insertingInventoryHasSpace = true;
if(lastInsertingInventory == null) return true;
if(inventory[0] == null) return true;
TileEntity te = worldObj.getTileEntity(lastInsertingInventory.chunkPosX, lastInsertingInventory.chunkPosY, lastInsertingInventory.chunkPosZ);
IInventory inv = IOHelper.getInventoryForTE(te);
if(inv != null) {
ItemStack remainder = IOHelper.insert(inv, inventory[0].copy(), lastInsertingInventorySide.ordinal(), true);
insertingInventoryHasSpace = remainder == null;
return insertingInventoryHasSpace;
} else {
lastInsertingInventory = null;
lastInsertingInventorySide = null;
return true;
}
}
@Override
public void addInformation(World world, int x, int y, int z, TileEntity te, List<String> infoList){
try {
IInventory inventory = IOHelper.getInventoryForTE(te);
if(inventory != null) {
boolean empty = true;
ItemStack[] inventoryStacks = new ItemStack[inventory.getSizeInventory()];
for(int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack iStack = inventory.getStackInSlot(i);
if(iStack != null) empty = false;
inventoryStacks[i] = iStack;
}
if(empty) {
infoList.add("Contents: Empty");
} else {
infoList.add("Contents:");
PneumaticCraftUtils.sortCombineItemStacksAndToString(infoList, inventoryStacks);
}
}
} catch(Throwable e) {
addTileEntityToBlackList(te, e);
}
}
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 static void dropInventory(World world, int x, int y, int z){
TileEntity tileEntity = world.getTileEntity(x, y, z);
if(!(tileEntity instanceof IInventory)) {
return;
}
IInventory inventory = (IInventory)tileEntity;
for(int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack itemStack = inventory.getStackInSlot(i);
if(itemStack != null && itemStack.stackSize > 0) {
spawnItemInWorld(world, itemStack, x, y, z);
}
}
}
/**
* Returns true if the item can be used on the given entity, e.g. shears on sheep.
*/
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity)
{
if (entity.worldObj.isRemote)
{
return false;
}
if (entity instanceof EntityPlayer)
{
EntityPlayer sub = (EntityPlayer)entity;
IInventory baubles = BaublesApi.getBaubles(sub);
if(baubles.getStackInSlot(0) == null) {
if(!itemstack.hasTagCompound()){
NBTTagCompound tag = new NBTTagCompound();
itemstack.setTagCompound(tag);
}
itemstack.stackTagCompound.setString("owner", player.getDisplayName());
baubles.setInventorySlotContents(0, itemstack.copy());
itemstack.stackSize = 0;
sub.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.placescollar").replace("%s", player.getDisplayName())));
player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.youplacecollar").replace("%s", sub.getDisplayName())));
return true;
}
else
player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("message.collar.alreadywearing").replace("%s", sub.getDisplayName())));
}
return false;
}
protected void layoutContainer(IInventory playerInventory, IInventory chestInventory, int xSize, int ySize)
{
for(int y = 0; y < 3; y++)
{
for(int x = 0; x < 9; x++)
{
this.addSlotToContainer(new Slot(chestInventory, x+y*9, 8+x*18, 18+y*18));
}
}
}
private void dropItems(World world, int x, int y, int z)
{
Random rand = new Random();
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (!(tileEntity instanceof IInventory))
{
return;
}
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++)
{
ItemStack item = inventory.getStackInSlot(i);
if (item != null && item.stackSize > 0)
{
float rx = rand.nextFloat() * 0.8F + 0.1F;
float ry = rand.nextFloat() * 0.8F + 0.1F;
float rz = rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));
if (item.hasTagCompound())
{
entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
}
float factor = 0.05F;
entityItem.motionX = rand.nextGaussian() * factor;
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
world.spawnEntityInWorld(entityItem);
item.stackSize = 0;
}
}
}
private void ejectChipFrom(IInventory guidanceComputer) {
for(EnumFacing dir : EnumFacing.VALUES) {
TileEntity tile = world.getTileEntity(getPos().offset(dir));
if(tile instanceof IInventory) {
if(ZUtils.doesInvHaveRoom(guidanceComputer.getStackInSlot(0), (IInventory)tile)) {
ZUtils.mergeInventory(guidanceComputer.getStackInSlot(0), (IInventory)tile);
guidanceComputer.removeStackFromSlot(0);
}
}
}
}
private static int getEnchantmentSlotIndex(ContainerEnchantment container) {
IInventory inventory = container.tableInventory;
for (int i = 0; i < container.inventorySlots.size(); i++) {
Slot slot = container.inventorySlots.get(i);
if (slot.isHere(inventory, EnchantmentLapisSlot.ENCHANTMENT_LAPIS_SLOT_INDEX)) return i;
}
return -1;
}
@Override
public Inventory toNova(IInventory nativeObj) {
if (nativeObj instanceof FWInventory) {
return ((FWInventory) nativeObj).wrapped;
}
return new BWInventory(nativeObj);
}
@Override
protected void drawGuiContainerForegroundLayer(int x, int y){
if(getInvNameOffset() != null && te instanceof IInventory) {
IInventory inv = (IInventory)te;
String containerName = inv.hasCustomName() ? inv.getName() : I18n.format(inv.getName() + ".name");
fontRenderer.drawString(containerName, xSize / 2 - fontRenderer.getStringWidth(containerName) / 2 + getInvNameOffset().x, 6 + getInvNameOffset().y, 4210752);
}
if(getInvTextOffset() != null) fontRenderer.drawString(I18n.format("container.inventory"), 8 + getInvTextOffset().x, ySize - 94 + getInvTextOffset().y, 4210752);
}
private void dropInventory(World world, double x, double y, double z){
IInventory inventory = this;
Random rand = new Random();
for(int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack itemStack = inventory.getStackInSlot(i);
if(itemStack != null && itemStack.stackSize > 0) {
float dX = rand.nextFloat() * 0.8F - 0.4F;
float dY = rand.nextFloat() * 0.8F - 0.4F;
float dZ = rand.nextFloat() * 0.8F - 0.4F;
EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, new ItemStack(itemStack.getItem(), itemStack.stackSize, itemStack.getItemDamage()));
if(itemStack.hasTagCompound()) {
entityItem.getEntityItem().setTagCompound((NBTTagCompound)itemStack.getTagCompound().copy());
}
float factor = 0.05F;
entityItem.motionX = rand.nextGaussian() * factor;
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
world.spawnEntityInWorld(entityItem);
// itemStack.stackSize = 0;
}
}
this.inventory = new ItemStack[4];
}
public InventoryRange(IInventory inv, int fslot, int lslot)
{
this.inv = inv;
slots = new int[lslot-fslot];
for(int i = 0; i < slots.length; i++)
slots[i] = fslot+i;
}
public static String getInventoryName(IInventory inv)
{
String invName = inv.getName();
String prefix = "container.";
if (invName.startsWith(prefix))
invName = invName.substring(prefix.length());
return invName;
}
/**
* Consumes one item from slot in inv with support for containers.
*/
public static void consumeItem(IInventory inv, int slot) {
ItemStack stack = inv.getStackInSlot(slot);
Item item = stack.getItem();
if (item.hasContainerItem(stack)) {
ItemStack container = item.getContainerItem(stack);
inv.setInventorySlotContents(slot, container);
} else {
inv.decrStackSize(slot, 1);
}
}
public void returnStock(){
TileEntity provider = getProvidingTileEntity();
TileEntity returning = getReturningTileEntity();
invert();
while(inStock > 0) {
int stock = Math.min(inStock, 50);
stock = ContainerAmadron.capShoppingAmount(this, stock, returning instanceof IInventory ? (IInventory)returning : null, provider instanceof IInventory ? (IInventory)provider : null, returning instanceof IFluidHandler ? (IFluidHandler)returning : null, provider instanceof IFluidHandler ? (IFluidHandler)provider : null, null);
if(stock > 0) {
inStock -= stock;
if(getInput() instanceof ItemStack) {
ItemStack deliveringItems = (ItemStack)getInput();
int amount = deliveringItems.stackSize * stock;
List<ItemStack> stacks = new ArrayList<ItemStack>();
while(amount > 0) {
ItemStack stack = deliveringItems.copy();
stack.stackSize = Math.min(amount, stack.getMaxStackSize());
stacks.add(stack);
amount -= stack.stackSize;
}
PneumaticRegistry.getInstance().deliverItemsAmazonStyle(provider.getWorldObj(), provider.xCoord, provider.yCoord, provider.zCoord, stacks.toArray(new ItemStack[stacks.size()]));
} else {
FluidStack deliveringFluid = ((FluidStack)getInput()).copy();
deliveringFluid.amount *= stock;
PneumaticRegistry.getInstance().deliverFluidAmazonStyle(provider.getWorldObj(), provider.xCoord, provider.yCoord, provider.zCoord, deliveringFluid);
}
} else {
break;
}
}
}
public static Iterable<ItemStack> asIterable(final IInventory inv) {
return () -> new AbstractIterator<ItemStack>() {
final int size = inv.getSizeInventory();
int slot = 0;
@Override
protected ItemStack computeNext() {
if (slot >= size) return endOfData();
return inv.getStackInSlot(slot++);
}
};
}
protected void addLootToChest(IInventory chest) {
for (int i = 0; i < chest.getSizeInventory(); i++) {
int roll = rand.nextInt(8);
if (roll == 0) {
chest.setInventorySlotContents(i, new ItemStack(Items.BONE, rand.nextInt(5) + 1));
} else if (roll == 1) {
chest.setInventorySlotContents(i, new ItemStack(Items.ROTTEN_FLESH, rand.nextInt(5) + 1));
} else if (roll == 2) {
if (rand.nextInt(20) == 0) {
chest.setInventorySlotContents(i, new ItemStack(NICE_STUFF[rand.nextInt(NICE_STUFF.length)]));
}
}
}
}
public static IInventory getInventoryForTE(TileEntity te){
if(te instanceof IInventory) {
IInventory inv = (IInventory)te;
Block block = te.getBlockType();
if(block instanceof BlockChest) {
inv = ((BlockChest)block).func_149951_m(te.getWorldObj(), te.xCoord, te.yCoord, te.zCoord);
}
return inv;
} else {
return null;
}
}
@Override
protected boolean isValidPosition(ChunkPosition pos){
if(traversedPositions.add(pos)) {
curSlot = 0;
TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
return te instanceof IInventory;
}
return false;
}
public ArcanePackagerGui(InventoryPlayer playerInv, IInventory inventory) {
super(new ContainerArcanePackager(playerInv, inventory));
this.tile = (TileArcanePackager) inventory;
this.playerInv = playerInv;
this.ySize = 234;
this.xSize = 190;
}
protected void bindPlayerInventory(IInventory inventoryPlayer)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 9; j++)
{
addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, i * 18 + 79));
}
}
for (int i = 0; i < 9; i++)
{
addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 137));
}
}
public IInventory getFilters(){
return filters;
}
@Override
public ItemStack getCraftingResult(IInventory var1){ return output.copy(); }
@Override
public AspectList getAspects(IInventory inv) {
return aspects;
}