123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- package me.km.blocks;
- import java.util.Arrays;
- import me.km.inventory.ContainerCampFire;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntityLockable;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.NonNullList;
- public class TileEntityCampFire extends TileEntityLockable implements ITickable
- {
- private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(6, ItemStack.EMPTY);
-
- private int burnTime;
-
- /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
- private int[] cookTime;
- private String furnaceCustomName;
- public TileEntityCampFire()
- {
- this.cookTime = new int[3];
- Arrays.fill(this.cookTime, 0);
- this.furnaceCustomName = null;
- this.burnTime = 9600;
- }
-
- @Override
- public int getSizeInventory()
- {
- return this.stacks.size();
- }
- @Override
- public boolean isEmpty()
- {
- return !this.stacks.stream().anyMatch(stack -> !stack.isEmpty());
- }
-
- @Override
- public ItemStack getStackInSlot(int index)
- {
- return this.stacks.get(index);
- }
- @Override
- public ItemStack decrStackSize(int index, int count)
- {
- return ItemStackHelper.getAndSplit(this.stacks, index, count);
- }
- @Override
- public ItemStack removeStackFromSlot(int index)
- {
- return ItemStackHelper.getAndRemove(this.stacks, index);
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack)
- {
- ItemStack itemstack = this.stacks.get(index);
- boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
- this.stacks.set(index, stack);
- if(stack.getCount() > this.getInventoryStackLimit())
- {
- stack.setCount(this.getInventoryStackLimit());
- }
- if(index >= 0 && index <= 2 && !flag)
- {
- this.cookTime[index] = 0;
- this.markDirty();
- }
- }
- @Override
- public String getName()
- {
- return this.hasCustomName() ? this.furnaceCustomName : "container.campfire";
- }
- @Override
- public boolean hasCustomName()
- {
- return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
- }
- public void setCustomInventoryName(String name)
- {
- this.furnaceCustomName = name;
- }
- @Override
- public void readFromNBT(NBTTagCompound com)
- {
- super.readFromNBT(com);
- this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
- ItemStackHelper.loadAllItems(com, this.stacks);
- this.burnTime = com.getInteger("BurnTime");
- this.cookTime = com.getIntArray("CookTime");
- if(com.hasKey("CustomName", 8))
- {
- this.furnaceCustomName = com.getString("CustomName");
- }
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("BurnTime", this.burnTime);
- compound.setIntArray("CookTime", this.cookTime);
- ItemStackHelper.saveAllItems(compound, this.stacks);
- if(this.hasCustomName())
- {
- compound.setString("CustomName", this.furnaceCustomName);
- }
- return compound;
- }
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- public boolean isBurning()
- {
- return this.burnTime > 0;
- }
- @Override
- public void update()
- {
- if(!this.world.isRemote)
- {
- boolean update = false;
- for(int i = 0; i < 3; i++)
- {
- ItemStack input = stacks.get(i);
- if(!input.isEmpty())
- {
- ItemStack output = stacks.get(i + 3);
- if(output.getCount() >= output.getMaxStackSize())
- {
- continue;
- }
- ItemStack result = getResult(input);
- if(result == ItemStack.EMPTY)
- {
- continue;
- }
- boolean empty = output.isEmpty();
- if(empty || output.isItemEqual(result))
- {
- this.cookTime[i]++;
- this.burnTime--;
- if(this.cookTime[i] >= 200)
- {
- this.cookTime[i] = 0;
- input.shrink(1);
- if(empty)
- {
- output = result;
- stacks.set(i + 3, output);
- }
- else
- {
- output.grow(1);
- }
- update = true;
- }
- }
- }
- }
- if(this.burnTime < 0)
- {
- world.setBlockState(pos, ModBlocks.campFireBurnt.getDefaultState());
- }
- if(update)
- {
- this.markDirty();
- }
- }
- }
-
- public ItemStack getResult(ItemStack stack)
- {
- Item item = stack.getItem();
- Item result = null;
- int dv = 0;
- if(item == Items.PORKCHOP)
- {
- result = Items.COOKED_PORKCHOP;
- }
- else if(item == Items.BEEF)
- {
- result = Items.COOKED_BEEF;
- }
- else if(item == Items.CHICKEN)
- {
- result = Items.COOKED_CHICKEN;
- }
- else if(item == Items.FISH)
- {
- result = Items.COOKED_FISH;
- dv = stack.getItemDamage();
- }
- else if(item == Items.POTATO)
- {
- result = Items.BAKED_POTATO;
- }
- else if(item == Items.MUTTON)
- {
- result = Items.COOKED_MUTTON;
- }
- else if(item == Items.RABBIT)
- {
- result = Items.COOKED_RABBIT;
- }
- if(result != null)
- {
- return new ItemStack(result, 1, dv);
- }
- return ItemStack.EMPTY;
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer p)
- {
- if(this.world.getTileEntity(this.pos) != this)
- {
- return false;
- }
- else
- {
- return p.getDistanceSq(pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d) <= 64.0D;
- }
- }
- @Override
- public void openInventory(EntityPlayer player)
- {
- }
- @Override
- public void closeInventory(EntityPlayer player)
- {
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack)
- {
- return false;
- }
- @Override
- public String getGuiID()
- {
- return "km:campfire";
- }
- @Override
- public Container createContainer(InventoryPlayer pInv, EntityPlayer p)
- {
- return new ContainerCampFire(pInv, this);
- }
- @Override
- public int getField(int id)
- {
- return this.cookTime[id];
- }
- @Override
- public void setField(int id, int value)
- {
- this.cookTime[id] = value;
- }
- @Override
- public int getFieldCount()
- {
- return 3;
- }
- @Override
- public void clear()
- {
- this.stacks.clear();
- }
- }
|