TileEntityCampFire.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package me.km.blocks;
  2. import java.util.Arrays;
  3. import me.km.inventory.ContainerCampFire;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.init.Items;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.ItemStackHelper;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.tileentity.TileEntityLockable;
  13. import net.minecraft.util.ITickable;
  14. import net.minecraft.util.NonNullList;
  15. public class TileEntityCampFire extends TileEntityLockable implements ITickable
  16. {
  17. private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(6, ItemStack.EMPTY);
  18. private int burnTime;
  19. /** The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for */
  20. private int[] cookTime;
  21. private String furnaceCustomName;
  22. public TileEntityCampFire()
  23. {
  24. this.cookTime = new int[3];
  25. Arrays.fill(this.cookTime, 0);
  26. this.furnaceCustomName = null;
  27. this.burnTime = 9600;
  28. }
  29. @Override
  30. public int getSizeInventory()
  31. {
  32. return this.stacks.size();
  33. }
  34. @Override
  35. public boolean isEmpty()
  36. {
  37. return !this.stacks.stream().anyMatch(stack -> !stack.isEmpty());
  38. }
  39. @Override
  40. public ItemStack getStackInSlot(int index)
  41. {
  42. return this.stacks.get(index);
  43. }
  44. @Override
  45. public ItemStack decrStackSize(int index, int count)
  46. {
  47. return ItemStackHelper.getAndSplit(this.stacks, index, count);
  48. }
  49. @Override
  50. public ItemStack removeStackFromSlot(int index)
  51. {
  52. return ItemStackHelper.getAndRemove(this.stacks, index);
  53. }
  54. @Override
  55. public void setInventorySlotContents(int index, ItemStack stack)
  56. {
  57. ItemStack itemstack = this.stacks.get(index);
  58. boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
  59. this.stacks.set(index, stack);
  60. if(stack.getCount() > this.getInventoryStackLimit())
  61. {
  62. stack.setCount(this.getInventoryStackLimit());
  63. }
  64. if(index >= 0 && index <= 2 && !flag)
  65. {
  66. this.cookTime[index] = 0;
  67. this.markDirty();
  68. }
  69. }
  70. @Override
  71. public String getName()
  72. {
  73. return this.hasCustomName() ? this.furnaceCustomName : "container.campfire";
  74. }
  75. @Override
  76. public boolean hasCustomName()
  77. {
  78. return this.furnaceCustomName != null && !this.furnaceCustomName.isEmpty();
  79. }
  80. public void setCustomInventoryName(String name)
  81. {
  82. this.furnaceCustomName = name;
  83. }
  84. @Override
  85. public void readFromNBT(NBTTagCompound com)
  86. {
  87. super.readFromNBT(com);
  88. this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  89. ItemStackHelper.loadAllItems(com, this.stacks);
  90. this.burnTime = com.getInteger("BurnTime");
  91. this.cookTime = com.getIntArray("CookTime");
  92. if(com.hasKey("CustomName", 8))
  93. {
  94. this.furnaceCustomName = com.getString("CustomName");
  95. }
  96. }
  97. @Override
  98. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  99. {
  100. super.writeToNBT(compound);
  101. compound.setInteger("BurnTime", this.burnTime);
  102. compound.setIntArray("CookTime", this.cookTime);
  103. ItemStackHelper.saveAllItems(compound, this.stacks);
  104. if(this.hasCustomName())
  105. {
  106. compound.setString("CustomName", this.furnaceCustomName);
  107. }
  108. return compound;
  109. }
  110. @Override
  111. public int getInventoryStackLimit()
  112. {
  113. return 64;
  114. }
  115. public boolean isBurning()
  116. {
  117. return this.burnTime > 0;
  118. }
  119. @Override
  120. public void update()
  121. {
  122. if(!this.world.isRemote)
  123. {
  124. boolean update = false;
  125. for(int i = 0; i < 3; i++)
  126. {
  127. ItemStack input = stacks.get(i);
  128. if(!input.isEmpty())
  129. {
  130. ItemStack output = stacks.get(i + 3);
  131. if(output.getCount() >= output.getMaxStackSize())
  132. {
  133. continue;
  134. }
  135. ItemStack result = getResult(input);
  136. if(result == ItemStack.EMPTY)
  137. {
  138. continue;
  139. }
  140. boolean empty = output.isEmpty();
  141. if(empty || output.isItemEqual(result))
  142. {
  143. this.cookTime[i]++;
  144. this.burnTime--;
  145. if(this.cookTime[i] >= 200)
  146. {
  147. this.cookTime[i] = 0;
  148. input.shrink(1);
  149. if(empty)
  150. {
  151. output = result;
  152. stacks.set(i + 3, output);
  153. }
  154. else
  155. {
  156. output.grow(1);
  157. }
  158. update = true;
  159. }
  160. }
  161. }
  162. }
  163. if(this.burnTime < 0)
  164. {
  165. world.setBlockState(pos, ModBlocks.campFireBurnt.getDefaultState());
  166. }
  167. if(update)
  168. {
  169. this.markDirty();
  170. }
  171. }
  172. }
  173. public ItemStack getResult(ItemStack stack)
  174. {
  175. Item item = stack.getItem();
  176. Item result = null;
  177. int dv = 0;
  178. if(item == Items.PORKCHOP)
  179. {
  180. result = Items.COOKED_PORKCHOP;
  181. }
  182. else if(item == Items.BEEF)
  183. {
  184. result = Items.COOKED_BEEF;
  185. }
  186. else if(item == Items.CHICKEN)
  187. {
  188. result = Items.COOKED_CHICKEN;
  189. }
  190. else if(item == Items.FISH)
  191. {
  192. result = Items.COOKED_FISH;
  193. dv = stack.getItemDamage();
  194. }
  195. else if(item == Items.POTATO)
  196. {
  197. result = Items.BAKED_POTATO;
  198. }
  199. else if(item == Items.MUTTON)
  200. {
  201. result = Items.COOKED_MUTTON;
  202. }
  203. else if(item == Items.RABBIT)
  204. {
  205. result = Items.COOKED_RABBIT;
  206. }
  207. if(result != null)
  208. {
  209. return new ItemStack(result, 1, dv);
  210. }
  211. return ItemStack.EMPTY;
  212. }
  213. @Override
  214. public boolean isUsableByPlayer(EntityPlayer p)
  215. {
  216. if(this.world.getTileEntity(this.pos) != this)
  217. {
  218. return false;
  219. }
  220. else
  221. {
  222. return p.getDistanceSq(pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d) <= 64.0D;
  223. }
  224. }
  225. @Override
  226. public void openInventory(EntityPlayer player)
  227. {
  228. }
  229. @Override
  230. public void closeInventory(EntityPlayer player)
  231. {
  232. }
  233. @Override
  234. public boolean isItemValidForSlot(int index, ItemStack stack)
  235. {
  236. return false;
  237. }
  238. @Override
  239. public String getGuiID()
  240. {
  241. return "km:campfire";
  242. }
  243. @Override
  244. public Container createContainer(InventoryPlayer pInv, EntityPlayer p)
  245. {
  246. return new ContainerCampFire(pInv, this);
  247. }
  248. @Override
  249. public int getField(int id)
  250. {
  251. return this.cookTime[id];
  252. }
  253. @Override
  254. public void setField(int id, int value)
  255. {
  256. this.cookTime[id] = value;
  257. }
  258. @Override
  259. public int getFieldCount()
  260. {
  261. return 3;
  262. }
  263. @Override
  264. public void clear()
  265. {
  266. this.stacks.clear();
  267. }
  268. }