下面列出了怎么用net.minecraft.inventory.ItemStackHelper的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void readFromNBT(NBTTagCompound par1NBTTagCompound) {
super.readFromNBT(par1NBTTagCompound);
this.furnaceItemStacks = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(par1NBTTagCompound, this.furnaceItemStacks);
this.furnaceBurnTime = par1NBTTagCompound.getShort("BurnTime");
this.cookTime = par1NBTTagCompound.getShort("CookTime");
this.currentItemBurnTime = par1NBTTagCompound.getShort("ItemBurnTime");
;
this.nigariTank.readFromNBT(par1NBTTagCompound.getCompoundTag("NigariTank"));
if (par1NBTTagCompound.hasKey("CustomName")) {
this.customName = par1NBTTagCompound.getString("CustomName");
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound) {
super.writeToNBT(par1NBTTagCompound);
par1NBTTagCompound.setShort("BurnTime", (short) this.furnaceBurnTime);
par1NBTTagCompound.setShort("CookTime", (short) this.cookTime);
par1NBTTagCompound.setInteger("ItemBurnTime", this.currentItemBurnTime);
NBTTagCompound nigariTag = this.nigariTank.writeToNBT(new NBTTagCompound());
par1NBTTagCompound.setTag("NigariTank", nigariTag);
ItemStackHelper.saveAllItems(par1NBTTagCompound, this.furnaceItemStacks);
if (this.hasCustomName()) {
par1NBTTagCompound.setString("CustomName", this.customName);
}
return par1NBTTagCompound;
}
@Override
public ItemStack decrStackSize(int i, int i1) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, i, i1);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
@Override
public ItemStack decrStackSize(int i, int i1) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, i, i1);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
public void writePacketNBT(NBTTagCompound cmp) {
ItemStackHelper.saveAllItems(cmp, this.inventory);
cmp.setInteger("workload", this.workload);
cmp.setInteger("current", this.current_workload);
NBTTagCompound tankTag = this.tank.writeToNBT(new NBTTagCompound());
cmp.setTag("Tank", tankTag);
}
public void readPacketNBT(NBTTagCompound cmp) {
this.inventory =
NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(cmp, this.inventory);
this.workload = cmp.getInteger("workload");
this.current_workload = cmp.getInteger("current");
this.tank.readFromNBT(cmp.getCompoundTag("Tank"));
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
public void writePacketNBT(NBTTagCompound cmp) {
ItemStackHelper.saveAllItems(cmp, this.inventory);
cmp.setInteger("MapleTime", this.mapleTime);
cmp.setInteger("CookTime", this.cookTime);
NBTTagCompound tankTag = this.tank.writeToNBT(new NBTTagCompound());
cmp.setTag("Tank", tankTag);
if (tank.getFluid() != null) {
liquidForRendering = tank.getFluid().copy();
}
}
public void readPacketNBT(NBTTagCompound cmp) {
this.inventory =
NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(cmp, this.inventory);
this.mapleTime = cmp.getInteger("MapleTime");
this.cookTime = cmp.getInteger("CookTime");
this.tank.readFromNBT(cmp.getCompoundTag("Tank"));
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
public void writePacketNBT(NBTTagCompound cmp) {
NBTTagCompound tankTag = this.tank.writeToNBT(new NBTTagCompound());
ItemStackHelper.saveAllItems(cmp, this.inventory);
cmp.setTag("Tank", tankTag);
NBTTagCompound resultTankTag = this.resultTank.writeToNBT(new NBTTagCompound());
cmp.setTag("ResultTank", resultTankTag);
cmp.setInteger(TAG_PROCESS, processTimer);
}
public void readPacketNBT(NBTTagCompound cmp) {
this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(cmp, this.inventory);
processTimer = cmp.getInteger(TAG_PROCESS);
this.tank.readFromNBT(cmp.getCompoundTag("Tank"));
this.resultTank.readFromNBT(cmp.getCompoundTag("ResultTank"));
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventorySlotItemStack, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
this.inventorySlotItemStack =
NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(compound, this.inventorySlotItemStack);
this.maxprocessTimer = compound.getInteger("maxProcessTimer");
this.processTimer = compound.getInteger("processTimer");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound = super.writeToNBT(compound);
ItemStackHelper.saveAllItems(compound, this.inventorySlotItemStack);
compound.setInteger("maxProcessTimer", this.maxprocessTimer);
compound.setInteger("processTimer", this.processTimer);
return compound;
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
public void writePacketNBT(NBTTagCompound cmp) {
NBTTagCompound tankTag = this.tank.writeToNBT(new NBTTagCompound());
ItemStackHelper.saveAllItems(cmp, this.inventory);
cmp.setTag("Tank", tankTag);
NBTTagCompound resultTankTag = this.resultTank.writeToNBT(new NBTTagCompound());
cmp.setTag("ResultTank", resultTankTag);
cmp.setInteger(TAG_PROCESS, processTimer);
}
public void readPacketNBT(NBTTagCompound cmp) {
this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(cmp, this.inventory);
processTimer = cmp.getInteger(TAG_PROCESS);
this.tank.readFromNBT(cmp.getCompoundTag("Tank"));
this.resultTank.readFromNBT(cmp.getCompoundTag("ResultTank"));
}
@Override
public ItemStack decrStackSize(int index, int count) {
ItemStack itemstack = ItemStackHelper.getAndSplit(inventory, index, count);
if (!itemstack.isEmpty()) {
this.markDirty();
}
return itemstack;
}
public void writePacketNBT(NBTTagCompound cmp) {
ItemStackHelper.saveAllItems(cmp, this.inventory);
cmp.setInteger("BurnTime", this.burnTime);
cmp.setInteger("CookTime", this.cookTime);
NBTTagCompound tankTag = this.tank.writeToNBT(new NBTTagCompound());
cmp.setTag("Tank", tankTag);
if (tank.getFluid() != null) {
liquidForRendering = tank.getFluid().copy();
}
}
public void readPacketNBT(NBTTagCompound cmp) {
this.inventory =
NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(cmp, this.inventory);
this.burnTime = cmp.getInteger("BurnTime");
this.cookTime = cmp.getInteger("CookTime");
this.tank.readFromNBT(cmp.getCompoundTag("Tank"));
}
@SubscribeEvent
public void onGuiDraw(GuiScreenEvent.DrawScreenEvent.Post event) {
if (!(event.getGui() instanceof ContainerScreen<?>)) return;
ContainerScreen<?> screen = (ContainerScreen<?>) event.getGui();
if (screen.getSlotUnderMouse() == null) return;
if (!(screen.getSlotUnderMouse().getStack().getItem() instanceof BlockItem)) return;
if (!(((BlockItem) screen.getSlotUnderMouse().getStack().getItem()).getBlock() instanceof ContainerBlock)) return;
NonNullList<ItemStack> items = NonNullList.withSize(27, new ItemStack(Items.AIR));
CompoundNBT nbt = screen.getSlotUnderMouse().getStack().getTag();
if (nbt != null && nbt.contains("BlockEntityTag")) {
CompoundNBT itemnbt = nbt.getCompound("BlockEntityTag");
if (itemnbt.contains("Items")) ItemStackHelper.loadAllItems(itemnbt, items);
}
GlStateManager.translatef(0.0F, 0.0F, 500.0F);
Block block = ((BlockItem) screen.getSlotUnderMouse().getStack().getItem()).getBlock();
int count = block instanceof HopperBlock || block instanceof DispenserBlock ? 18 : 0;
for (ItemStack i: items) {
if (count > 26) break;
int x = event.getMouseX() + 8 + (17 * (count % 9));
int y = event.getMouseY() - 68 + (17 * (count / 9));
if (i.getItem() != Items.AIR) {
Screen.fill(x, y, x+17, y+17, 0x90000000);
Screen.fill(x, y, x+17, y+1, 0xff000000); Screen.fill(x, y+1, x+1, y+17, 0xff000000);
Screen.fill(x+16, y+1, x+17, y+17, 0xff000000); Screen.fill(x+1, y+16, x+17, y+17, 0xff000000);
}
mc.getItemRenderer().renderItemAndEffectIntoGUI(i, x, y);
mc.getItemRenderer().renderItemOverlayIntoGUI(mc.fontRenderer, i, x, y, i.getCount() > 1 ? i.getCount() + "" : "");
count++;
}
}
@Override
public void onCommand(String command, String[] args) throws Exception {
ItemStack item = mc.player.inventory.getCurrentItem();
if (!(item.getItem() instanceof BlockItem)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
if (!(((BlockItem) item.getItem()).getBlock() instanceof ContainerBlock)) {
BleachLogger.errorMessage("Must be holding a containter to peek.");
return;
}
NonNullList<ItemStack> items = NonNullList.withSize(27, new ItemStack(Items.AIR));
CompoundNBT nbt = item.getTag();
if (nbt != null && nbt.contains("BlockEntityTag")) {
CompoundNBT itemnbt = nbt.getCompound("BlockEntityTag");
if (itemnbt.contains("Items")) ItemStackHelper.loadAllItems(itemnbt, items);
}
Inventory inv = new Inventory(items.toArray(new ItemStack[27]));
BleachQueue.queue.add(() -> {
mc.displayGuiScreen(new ShulkerBoxScreen(
new ShulkerBoxContainer(420, mc.player.inventory, inv),
mc.player.inventory,
item.getDisplayName()));
});
}
@Override
public void readFromNBT(NBTTagCompound c) {
super.readFromNBT(c);
processor.readFromNBT(c.getCompoundTag(NBT_PROCESSOR));
codeItemStacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
ItemStackHelper.loadAllItems(c, codeItemStacks);
loadTime = c.getShort(NBT_LOAD_TIME);
if (c.hasKey(NBT_CUSTOM_NAME, 8)) {
this.customName = c.getString(NBT_CUSTOM_NAME);
}
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound cIn) {
NBTTagCompound c = super.writeToNBT(cIn);
c.setTag(NBT_PROCESSOR, processor.writeToNBT());
c.setShort(NBT_LOAD_TIME, (short) loadTime);
ItemStackHelper.saveAllItems(c, codeItemStacks);
if (this.hasCustomName()) {
c.setString(NBT_CUSTOM_NAME, this.customName);
}
return c;
}
public static List<ItemStack> getShulkerContents(ItemStack stack) { // TODO: move somewhere else
NonNullList<ItemStack> contents = NonNullList.withSize(27, ItemStack.EMPTY);
NBTTagCompound compound = stack.getTagCompound();
if (compound != null && compound.hasKey("BlockEntityTag", 10)) {
NBTTagCompound tags = compound.getCompoundTag("BlockEntityTag");
if (tags.hasKey("Items", 9)) {
// load in the items
ItemStackHelper.loadAllItems(tags, contents);
}
}
return contents;
}
@Override
@Nonnull
public ItemStack decrStackSize(int index, int count) {
final ItemStack result = ItemStackHelper.getAndSplit(this.inventoryContents, index, count);
if (!result.isEmpty())
onInventoryChanged(index);
return result;
}
@Override
public ItemStack removeStackFromSlot(int i) {
return ItemStackHelper.getAndRemove(inventory, i);
}