Guest User

TileEntitySauceMaker

a guest
Jan 26th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. package com.chef.mod.tileentity;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.material.Material;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.init.Blocks;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.inventory.ISidedInventory;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemBlock;
  11. import net.minecraft.item.ItemHoe;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.item.ItemSword;
  14. import net.minecraft.item.ItemTool;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.nbt.NBTTagList;
  17. import net.minecraft.tileentity.TileEntity;
  18.  
  19. import com.chef.mod.Chef;
  20. import com.chef.mod.crafting.SauceMakerRecipes;
  21. import com.chef.mod.machines.SauceMaker;
  22.  
  23. import cpw.mods.fml.common.registry.GameRegistry;
  24.  
  25. public class TileEntitySauceMaker extends TileEntity implements ISidedInventory {
  26.  
  27. private ItemStack slots[];
  28.  
  29. private static final int[] slots_top = new int[] {0, 1};
  30. private static final int[] slots_bottom = new int[] {3};
  31. private static final int[] slots_side = new int[] {2};
  32.  
  33. private String customName;
  34.  
  35. public TileEntitySauceMaker() {
  36. slots = new ItemStack [4];
  37. }
  38.  
  39. //Progress bar speed, The higher the number, the slower it goes [Default: 200]
  40. public int burnTime;
  41. public int currentItemBurnTime;
  42. public int cookTime;
  43. public static final int furnaceSpeed = 250;
  44.  
  45. @Override
  46. public int getSizeInventory() {
  47. return this.slots.length;
  48. }
  49.  
  50. @Override
  51. public ItemStack getStackInSlot(int i) {
  52. return slots[i];
  53. }
  54.  
  55. @Override
  56. public ItemStack getStackInSlotOnClosing(int i) {
  57. if (slots[i] != null) {
  58. ItemStack itemstack = slots[i];
  59. slots[i] = null;
  60. return itemstack;
  61. }else{
  62. return null;
  63. }
  64. }
  65.  
  66. @Override
  67. public void setInventorySlotContents(int i, ItemStack itemstack) {
  68. slots[i] = itemstack;
  69. if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  70. itemstack.stackSize = getInventoryStackLimit();
  71. }
  72. }
  73.  
  74. @Override
  75. public int[] getAccessibleSlotsFromSide(int i) {
  76. if (i == 0) {
  77. return slots_bottom;
  78. } else if (i == 1) {
  79. return slots_top;
  80. } else {
  81. return slots_side;
  82. }
  83. }
  84.  
  85. @Override
  86. //choose wich Stack limit every slot in the furnace has.
  87. public int getInventoryStackLimit() {
  88. return 64;
  89. }
  90.  
  91. @Override
  92. public boolean isUseableByPlayer(EntityPlayer player) {
  93. if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
  94. return false;
  95. }else{
  96. return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64;
  97. }
  98. }
  99.  
  100. public void openInventory() {}
  101. public void closeInventory() {}
  102.  
  103.  
  104. @Override
  105. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  106. return i == 2 ? false : (i == 1 ? isItemFuel(itemstack) : true);
  107. }
  108.  
  109.  
  110. public static boolean isItemFuel (ItemStack itemstack) {
  111. return getItemBurnTime(itemstack) > 0;
  112. }
  113.  
  114. public static boolean isItemBowl (ItemStack itemstack) {
  115. Item item = itemstack.getItem();
  116.  
  117. if (item == Items.bowl) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123.  
  124. public static boolean isItemIngredient(ItemStack itemstack) {
  125. Item item = itemstack.getItem();
  126.  
  127. if (item == Chef.foodChocolate) {
  128. return true;
  129. } else if (item == Chef.cropStrawberry) {
  130. return true;
  131. } else if (item == Chef.cropPerfectStrawberry) {
  132. return true;
  133. } else if (item == Chef.cropBlueberry) {
  134. return true;
  135. } else if (item == Chef.foodCaramel) {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141.  
  142. public static int getItemBurnTime(ItemStack itemstack)
  143. {
  144. if (itemstack == null)
  145. {
  146. return 0;
  147. }
  148. else
  149. {
  150. Item item = itemstack.getItem();
  151.  
  152. if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air)
  153. {
  154. Block block = Block.getBlockFromItem(item);
  155.  
  156. if (block == Blocks.wooden_slab)
  157. {
  158. return 150;
  159. }
  160.  
  161. if (block.getMaterial() == Material.wood)
  162. {
  163. return 300;
  164. }
  165.  
  166. if (block == Blocks.coal_block)
  167. {
  168. return 16000;
  169. }
  170. }
  171.  
  172. if (item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD")) return 200;
  173. if (item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD")) return 200;
  174. if (item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD")) return 200;
  175. if (item == Items.stick) return 100;
  176. if (item == Items.coal) return 1600;
  177. if (item == Items.lava_bucket) return 20000;
  178. if (item == Item.getItemFromBlock(Blocks.sapling)) return 100;
  179. if (item == Items.blaze_rod) return 2400;
  180. return GameRegistry.getFuelValue(itemstack);
  181. }
  182. }
  183.  
  184. @Override
  185. public ItemStack decrStackSize(int var1, int var2) {
  186. if(this.slots[var1] != null) {
  187. ItemStack itemstack;
  188.  
  189. if(this.slots[var1].stackSize <= var2) {
  190. itemstack = this.slots[var1];
  191. this.slots[var1] = null;
  192. return itemstack;
  193. }else{
  194. itemstack = this.slots[var1].splitStack(var2);
  195.  
  196. if(this.slots[var1].stackSize == 0) {
  197. this.slots[var1] = null;
  198. }
  199.  
  200. return itemstack;
  201. }
  202. }else{
  203. return null;
  204. }
  205. }
  206.  
  207. public void readFromNBT(NBTTagCompound nbt) {
  208. super.readFromNBT(nbt);
  209.  
  210. NBTTagList list = nbt.getTagList("Items", 10);
  211. this.slots = new ItemStack[this.getSizeInventory()];
  212.  
  213. for(int i = 0; i < list.tagCount(); i++) {
  214. NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
  215. byte b = compound.getByte("Slot");
  216.  
  217. if(b >= 0 && b < this.slots.length) {
  218. this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
  219. }
  220. }
  221.  
  222. this.burnTime = (int)nbt.getShort("BurnTime");
  223. this.cookTime = (int)nbt.getShort("CookTime");
  224. this.currentItemBurnTime = (int)nbt.getShort("CurrentBurnTime");
  225.  
  226. if(nbt.hasKey("CustomName")) {
  227. this.customName = nbt.getString("CustomName");
  228. }
  229. }
  230.  
  231. public void writeToNBT(NBTTagCompound nbt) {
  232. super.writeToNBT(nbt);
  233.  
  234. nbt.setShort("BurnTime", (short)this.burnTime);
  235. nbt.setShort("CookTime", (short)this.cookTime);
  236. nbt.setShort("CurrentBurnTime", (short)this.currentItemBurnTime);
  237.  
  238. NBTTagList list = new NBTTagList();
  239.  
  240. for (int i = 0; i < this.slots.length; i++) {
  241. if(this.slots[i] != null) {
  242. NBTTagCompound compound = new NBTTagCompound();
  243. compound.setByte("Slot", (byte)i);
  244. this.slots[i].writeToNBT(compound);
  245. list.appendTag(compound);
  246. }
  247. }
  248.  
  249. nbt.setTag("Items", list);
  250.  
  251. if (this.hasCustomInventoryName()) {
  252. nbt.setString("CustomName", this.customName);
  253. }
  254. }
  255.  
  256. public String getInventoryName() {
  257. return this.hasCustomInventoryName() ? this.customName : "container.sauceMaker";
  258. }
  259.  
  260.  
  261. @Override
  262. public boolean canInsertItem(int i, ItemStack itemstack, int j) {
  263. return this.isItemValidForSlot(i, itemstack);
  264. }
  265.  
  266. @Override
  267. public boolean canExtractItem(int i, ItemStack itemstack, int j) {
  268. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  269. }
  270.  
  271. public boolean hasCustomInventoryName() {
  272. return this.customName != null && this.customName.length() > 0;
  273. }
  274.  
  275. public int getBurnTimeRemainingScaled(int i) {
  276. if(this.currentItemBurnTime == 0) {
  277. this.currentItemBurnTime = this.furnaceSpeed;
  278. }
  279. return this.burnTime * i / this.currentItemBurnTime;
  280. }
  281.  
  282. public int getCookProgressScaled(int i) {
  283. return this.cookTime * i / this.furnaceSpeed;
  284. }
  285.  
  286.  
  287. private boolean canJuice() {
  288.  
  289. if (slots[0] == null || slots[1] == null) {
  290. return false;
  291. } else {
  292.  
  293. ItemStack itemstack = SauceMakerRecipes.getJuicingResult(slots[0].getItem(), slots[1].getItem());
  294.  
  295. if(itemstack == null) return false;
  296. if(this.slots[3] == null) return true;
  297. if(!this.slots[3].isItemEqual(itemstack)) return false;
  298.  
  299. int result = this.slots[3].stackSize + itemstack.stackSize;
  300.  
  301. return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  302. }
  303. }
  304.  
  305. private void juiceItem() {
  306. if (canJuice()) {
  307. ItemStack itemstack = SauceMakerRecipes.getJuicingResult(slots[0].getItem(), slots[1].getItem());
  308.  
  309. if (slots[3] == null) {
  310. slots[3] = itemstack.copy();
  311. }else if (slots[3].isItemEqual(itemstack)) {
  312. slots[3].stackSize += itemstack.stackSize;
  313. }
  314.  
  315. for (int i = 0; i < 2; i++) {
  316. if (slots[i].stackSize <= 0) {
  317. slots[i] = new ItemStack(slots[i].getItem().setFull3D());
  318. } else {
  319. slots[i].stackSize--;
  320. }
  321.  
  322. if (slots[i].stackSize <= 0){
  323. slots[i] = null;
  324. }
  325. }
  326. }
  327. }
  328.  
  329. public boolean isBurning() {
  330. return this.burnTime > 0;
  331. }
  332.  
  333. public void updateEntity() {
  334. boolean flag = this.burnTime > 0;
  335. boolean flag1 = false;
  336.  
  337. if(this.isBurning()) {
  338. this.burnTime--;
  339. }
  340. if(!this.worldObj.isRemote) {
  341. if(this.burnTime == 0 && this.canJuice()) {
  342. this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[2]);
  343.  
  344. if(this.isBurning()) {
  345. flag1 = true;
  346.  
  347. if(this.slots[2] != null) {
  348. this.slots[2].stackSize--;
  349. }
  350. }
  351. }
  352.  
  353. if(this.isBurning() && this.canJuice()) {
  354. this.cookTime++;
  355.  
  356. if(this.cookTime == this.furnaceSpeed) {
  357. this.cookTime = 0;
  358. this.juiceItem();
  359. flag1 = true;
  360. }
  361. } else {
  362. this.cookTime = 0;
  363. }
  364.  
  365. if (flag != this.isBurning()) {
  366. flag1 = true;
  367. SauceMaker.updateBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  368. }
  369. }
  370. if(flag1) {
  371. this.markDirty();
  372. }
  373. }
  374. }
Advertisement
Add Comment
Please, Sign In to add comment