Guest User

TileEntitySauceMaker

a guest
Dec 5th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. package com.morefood.mod.tileentity;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.init.Blocks;
  6. import net.minecraft.init.Items;
  7. import net.minecraft.inventory.ISidedInventory;
  8. import net.minecraft.item.Item;
  9. import net.minecraft.item.ItemBlock;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.nbt.NBTTagList;
  13. import net.minecraft.tileentity.TileEntity;
  14.  
  15. import com.morefood.mod.Morefood;
  16. import com.morefood.mod.crafting.SauceMakerRecipes;
  17. import com.morefood.mod.machines.SauceMaker;
  18.  
  19. import cpw.mods.fml.common.registry.GameRegistry;
  20.  
  21. public class TileEntitySauceMaker extends TileEntity implements ISidedInventory {
  22.  
  23. private String localizedName;
  24.  
  25. private static final int[] slots_top = new int[] {0};
  26. private static final int[] slots_bottom = new int[] {2, 1};
  27. private static final int[] slots_side = new int[] {1};
  28.  
  29. private ItemStack[] slots = new ItemStack [4];
  30.  
  31. //Progress bar speed, The higher the number, the slower it goes
  32. public int furnaceSpeed = 300;
  33. /**
  34. * The number of ticks that the furnace will keep burning
  35. */
  36. public int burnTime;
  37. /**
  38. * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
  39. */
  40. public int currentItemBurnTime;
  41. /**
  42. * The number of ticks that the current item has been cooking for
  43. */
  44. public int cookTime;
  45.  
  46. public void setGuiDisplayName(String displayName) {
  47. this.localizedName = displayName;
  48. }
  49.  
  50. public String getInventoryName() {
  51. return this.hasCustomInventoryName() ? this.localizedName : "container.superhotFurnace";
  52. }
  53.  
  54. public boolean hasCustomInventoryName() {
  55. return this.localizedName != null && this.localizedName.length() > 0;
  56. }
  57.  
  58. public int getSizeInventory() {
  59. return this.slots.length;
  60. }
  61.  
  62. @Override
  63. public ItemStack getStackInSlot(int var1) {
  64. return this.slots[var1];
  65. }
  66.  
  67. @Override
  68. public ItemStack decrStackSize(int var1, int var2) {
  69. if(this.slots[var1] != null) {
  70. ItemStack itemstack;
  71.  
  72. if(this.slots[var1].stackSize <= var2) {
  73. itemstack = this.slots[var1];
  74. this.slots[var1] = null;
  75. return itemstack;
  76. }else{
  77. itemstack = this.slots[var1].splitStack(var2);
  78.  
  79. if(this.slots[var1].stackSize == 0) {
  80. this.slots[var1] = null;
  81. }
  82.  
  83. return itemstack;
  84. }
  85. }else{
  86. return null;
  87. }
  88. }
  89.  
  90. @Override
  91. public ItemStack getStackInSlotOnClosing(int i) {
  92. if(this.slots[i]!= null) {
  93. ItemStack itemstack = this.slots[i];
  94. this.slots[i] = null;
  95. return itemstack;
  96. }
  97. return null;
  98. }
  99.  
  100. @Override
  101. public void setInventorySlotContents(int i, ItemStack itemstack) {
  102. this.slots[i] = itemstack;
  103.  
  104. if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) {
  105. itemstack.stackSize = this.getInventoryStackLimit();
  106. }
  107. }
  108.  
  109. @Override
  110. //Choose which stack limit the furnace has
  111. public int getInventoryStackLimit() {
  112. return 64;
  113. }
  114.  
  115. @Override
  116. public boolean isUseableByPlayer(EntityPlayer entityplayer) {
  117. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0;
  118. }
  119.  
  120. public void openInventory() {}
  121. public void closeInventory() {}
  122.  
  123. @Override
  124. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  125. return i == 2 ? false : (i == 1 ? isItemFuel(itemstack) : true);
  126. }
  127.  
  128. public static boolean isItemFuel (ItemStack itemstack) {
  129. return getItemBurnTime(itemstack ) > 0;
  130. }
  131.  
  132. private static int getItemBurnTime(ItemStack itemstack) {
  133. if(itemstack == null) {
  134. return 0;
  135. }else{
  136. Item item = itemstack.getItem();
  137.  
  138. if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
  139. Block block = Block.getBlockFromItem(item);
  140.  
  141. }
  142. //Fuel that can be used in this furnace, in unit "ticks"
  143. if(item == Morefood.foodRawMutton) return 1200;
  144. }
  145. return GameRegistry.getFuelValue(itemstack);
  146. }
  147.  
  148. public boolean isBurning() {
  149. return this.burnTime > 0;
  150. }
  151.  
  152. public void updateEntity() {
  153. boolean flag = this.burnTime > 0;
  154. boolean flag1 = false;
  155.  
  156. if(this.isBurning()) {
  157. this.burnTime--;
  158. }
  159. if(!this.worldObj.isRemote) {
  160. if(this.burnTime == 0 && this.canJuice()) {
  161. this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[2]);
  162.  
  163. if(this.slots[2] != null) {
  164. flag1 = true;
  165.  
  166. this.slots[2].stackSize--;
  167.  
  168. if(this.slots[2].stackSize == 0) {
  169. this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
  170. }
  171. }
  172. }
  173. if(this.isBurning() && this.canJuice()) {
  174. this.cookTime++;
  175.  
  176. if(this.cookTime == this.furnaceSpeed) {
  177. this.cookTime = 0;
  178. this.juiceItem();
  179. flag1 = true;
  180. }
  181. }else{
  182. this.cookTime = 0;
  183. }
  184.  
  185. if (flag != this.isBurning()) {
  186. flag1 = true;
  187. SauceMaker.updateSauceMakerBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  188. }
  189. }
  190. if(flag1) {
  191. this.markDirty();
  192. }
  193. }
  194.  
  195. private boolean canJuice() {
  196. if (this.slots[0] == null || slots[1] == null) {
  197. return false;
  198. }
  199. ItemStack itemstack = SauceMakerRecipes.getJuicingResult(slots[0].getItem(), slots[1].getItem());
  200.  
  201. if (itemstack == null) {
  202. return false;
  203. }
  204. if (this.slots[3] == null) {
  205. return true;
  206. }
  207.  
  208. if(!slots[3].isItemEqual(itemstack)) {
  209. return false;
  210. }
  211.  
  212. if(slots[3].stackSize < getInventoryStackLimit() && slots[3].stackSize < slots[3].getMaxStackSize()) {
  213. return true;
  214. }else{
  215. return slots[3].stackSize < itemstack.getMaxStackSize();
  216. }
  217. }
  218.  
  219. private void juiceItem() {
  220. if (canJuice()) {
  221. ItemStack itemstack = SauceMakerRecipes.getJuicingResult(slots[0].getItem(), slots[1].getItem());
  222.  
  223. if (slots[3] == null) {
  224. slots[3] = itemstack.copy();
  225. }else if (slots[3].isItemEqual(itemstack)) {
  226. slots[3].stackSize += itemstack.stackSize;
  227. }
  228.  
  229. for (int i = 0; i < 2; i++) {
  230. if (slots[i].stackSize <= 0) {
  231. slots[i] = new ItemStack(slots[i].getItem().setFull3D());
  232. }else{
  233. slots[i].stackSize--;
  234. }
  235.  
  236. if (slots[i].stackSize <= 0) {
  237. slots[i] = null;
  238. }
  239. }
  240. }
  241. }
  242.  
  243. @Override
  244. public int[] getAccessibleSlotsFromSide(int var1) {
  245. return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_side);
  246. }
  247.  
  248. @Override
  249. public boolean canInsertItem(int i, ItemStack itemstack, int j) {
  250. return this.isItemValidForSlot(i, itemstack);
  251. }
  252.  
  253. @Override
  254. public boolean canExtractItem(int i, ItemStack itemstack, int j) {
  255. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  256. }
  257.  
  258. public float getBurnTimeRemainingScaled(float i) {
  259. if(this.currentItemBurnTime == 0) {
  260. this.currentItemBurnTime = this.furnaceSpeed;
  261. }
  262. return this.burnTime * i / this.currentItemBurnTime;
  263. }
  264.  
  265. public int getCookProgressScaled(int i) {
  266. return this.cookTime * i / this.furnaceSpeed;
  267. }
  268.  
  269. public void readFromNBT(NBTTagCompound nbt) {
  270. super.readFromNBT(nbt);
  271.  
  272. NBTTagList list = nbt.getTagList("Items", 10);
  273. this.slots = new ItemStack[this.getSizeInventory()];
  274.  
  275. for (int i = 0; i < list.tagCount(); i++) {
  276. NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
  277. byte b = compound.getByte("Slot");
  278.  
  279. if(b >= 0 && b < this.slots.length) {
  280. this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
  281. }
  282. }
  283.  
  284. this.burnTime = (int)nbt.getShort("BurnTime");
  285. this.cookTime = (int)nbt.getShort("CookTime");
  286. this.currentItemBurnTime = (int)nbt.getShort("CurrentBurnTime");
  287.  
  288. if(nbt.hasKey("CustomName")) {
  289. this.localizedName = nbt.getString("CustomName");
  290. }
  291. }
  292.  
  293. public void writeToNBT(NBTTagCompound nbt) {
  294. super.writeToNBT(nbt);
  295.  
  296. nbt.setShort("BurnTime", (short)this.burnTime);
  297. nbt.setShort("CookTime", (short)this.cookTime);
  298. nbt.setShort("CurrentBurnTime", (short)this.currentItemBurnTime);
  299.  
  300. NBTTagList list = new NBTTagList();
  301.  
  302. for (int i = 0; i < this.slots.length; i++) {
  303. if(this.slots[i] != null) {
  304. NBTTagCompound compound = new NBTTagCompound();
  305. compound.setByte("Slot", (byte)i);
  306. this.slots[i].writeToNBT(compound);
  307. list.appendTag(compound);
  308. }
  309. }
  310.  
  311. nbt.setTag("Items", list);
  312.  
  313. if (this.hasCustomInventoryName()) {
  314. nbt.setString("CustomName", this.localizedName);
  315. }
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment