Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.konarjg.arcticraft.blocks.tileentities;
- import com.konarjg.arcticraft.ModGuiHandler;
- import com.konarjg.arcticraft.blocks.tileentities.containers.ContainerBarrel;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.inventory.Slot;
- import net.minecraft.item.ItemFood;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntityLockableLoot;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- public class TileEntityBarrel extends TileEntityLockableLoot implements ITickable, IInventory{
- public static NonNullList<ItemStack> contents = NonNullList.<ItemStack>withSize(16, ItemStack.EMPTY);
- private String customName;
- @Override
- public void update() {
- }
- public void openInventory(EntityPlayer player)
- {
- }
- public void closeInventory(EntityPlayer player)
- {
- }
- @Override
- public String getName() {
- return hasCustomName () ? getCustomName() : "container.barrel";
- }
- public String getCustomName () {
- return customName;
- }
- @Override
- public boolean hasCustomName() {
- return customName != null;
- }
- @Override
- public int getSizeInventory() {
- return 16;
- }
- public ItemStack decrStackSize(int slot, int amount) {
- ItemStack stack = getStackInSlot(slot);
- if(stack != null) {
- if(stack.getCount() > amount) {
- stack = stack.splitStack(amount);
- markDirty();
- } else {
- setInventorySlotContents(slot, ItemStack.EMPTY);
- }
- }
- return stack;
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- ItemStack stack = getStackInSlot (index);
- contents.set(index, ItemStack.EMPTY);
- return stack;
- }
- @Override
- public boolean isEmpty() {
- for (ItemStack i : contents) {
- if (!i.isEmpty()) return false;
- }
- return true;
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer player) {
- return true;
- }
- int numPlayerUsing;
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- if (!contents.get(index).isEmpty() || (contents.get(index).equals(stack) && contents.get(index).getCount() == contents.get(index).getMaxStackSize() || !contents.get(index).equals(stack))) return false;
- if (stack.getItem() instanceof ItemFood) return true;
- return false;
- }
- @Override
- public int getField(int id) {
- return 0;
- }
- @Override
- public void setField(int id, int value) {
- }
- @Override
- public int getFieldCount() {
- return 0;
- }
- public void setCustomName(String displayName) {
- customName = displayName;
- }
- @Override
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
- if (!world.isRemote)
- return new ContainerBarrel (playerInventory, this);
- return null;
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- if (!world.isRemote) {
- super.readFromNBT(compound);
- this.contents = NonNullList.<ItemStack>withSize(getSizeInventory(), ItemStack.EMPTY);
- if (!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, contents);
- if (compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
- }
- }
- @Override
- public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
- return false;
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound) {
- if (!world.isRemote) {
- super.writeToNBT(compound);
- if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, contents);
- if (compound.hasKey("CustomName")) compound.setString("CustomName", customName);
- }
- return compound;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- if (!world.isRemote) {
- return contents.get(index);
- }
- return ItemStack.EMPTY;
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (!world.isRemote) {
- contents.set(index, stack);
- }
- }
- @Override
- public String getGuiID() {
- return "" + ModGuiHandler.BARREL;
- }
- @Override
- protected NonNullList<ItemStack> getItems() {
- return contents;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment