Guest User

TileEntitySauceMaker2

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