Guest User

EntityIceCreamMaker

a guest
Jan 23rd, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. package com.chef.mod.tileentity;
  2.  
  3. import com.chef.mod.Chef;
  4. import com.chef.mod.Debugger;
  5. import com.chef.mod.crafting.IceCreamMakerRecipes;
  6. import com.chef.mod.machines.IceCreamMaker;
  7.  
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.init.Items;
  10. import net.minecraft.inventory.ISidedInventory;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.tileentity.TileEntity;
  16.  
  17. public class TileEntityIceCreamMaker extends TileEntity implements ISidedInventory {
  18.  
  19. private ItemStack slots[];
  20.  
  21. public int dualPower;
  22. public int dualCookTime;
  23. public static final int maxPower = 10000;
  24. public static final int iceCreamMakerSpeed = 100;
  25.  
  26. public void Debugger() {
  27. Debugger.log(maxPower);
  28. }
  29.  
  30. private static final int[] slots_top = new int[] {0, 1};
  31. private static final int[] slots_bottom = new int[] {3};
  32. private static final int[] slots_side = new int[] {2};
  33.  
  34. private String customName;
  35.  
  36. public TileEntityIceCreamMaker() {
  37. slots = new ItemStack[4];
  38. }
  39.  
  40. @Override
  41. public int getSizeInventory() {
  42. return slots.length;
  43. }
  44.  
  45. @Override
  46. public ItemStack getStackInSlot(int i) {
  47. return slots[i];
  48. }
  49.  
  50. @Override
  51. public ItemStack getStackInSlotOnClosing(int i) {
  52. if (slots[i] != null) {
  53. ItemStack itemstack = slots[i];
  54. slots[i] = null;
  55. return itemstack;
  56. }else{
  57. return null;
  58. }
  59. }
  60.  
  61. @Override
  62. public void setInventorySlotContents(int i, ItemStack itemstack) {
  63. slots[i] = itemstack;
  64. if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  65. itemstack.stackSize = getInventoryStackLimit();
  66. }
  67. }
  68.  
  69. @Override
  70. public int[] getAccessibleSlotsFromSide(int i) {
  71. if (i == 0) {
  72. return slots_bottom;
  73. } else if (i == 1) {
  74. return slots_top;
  75. } else {
  76. return slots_side;
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83. @Override
  84. public int getInventoryStackLimit() {
  85. return 64;
  86. }
  87.  
  88. @Override
  89. public boolean isUseableByPlayer(EntityPlayer player) {
  90. if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
  91. return false;
  92. }else{
  93. return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64;
  94. }
  95. }
  96.  
  97. public void openInventory() {}
  98. public void closeInventory() {}
  99.  
  100. @Override
  101. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  102. return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
  103. }
  104.  
  105. public boolean hasItemPower(ItemStack itemstack) {
  106. return getItemPower(itemstack) > 0;
  107. }
  108.  
  109. private static int getItemPower (ItemStack itemstack) {
  110. if (itemstack == null) {
  111. return 0;
  112. } else {
  113. Item item = itemstack.getItem();
  114.  
  115. if (item == Chef.fuelIceShard) return 50;
  116.  
  117. return 0;
  118. }
  119. }
  120.  
  121. public ItemStack decrStackSize(int i, int j) {
  122. if (slots[i] != null) {
  123. if (slots[i].stackSize <= j) {
  124. ItemStack itemstack = slots[i];
  125. slots[i] = null;
  126. return itemstack;
  127. }
  128.  
  129. ItemStack itemstack1 = slots[i].splitStack(j);
  130.  
  131. if (slots[i].stackSize == 0) {
  132. slots[i] = null;
  133. }
  134.  
  135. return itemstack1;
  136. }else{
  137. return null;
  138. }
  139. }
  140.  
  141. public void readFromNBT (NBTTagCompound nbt) {
  142. super.readFromNBT(nbt);
  143. NBTTagList list = nbt.getTagList("Items", 10);
  144. slots = new ItemStack[getSizeInventory()];
  145.  
  146. for (int i = 0; i < list.tagCount(); i++) {
  147. NBTTagCompound nbt1 = (NBTTagCompound)list.getCompoundTagAt(i);
  148. byte b0 = nbt1.getByte("Slot");
  149.  
  150. if (b0 >= 0 && b0 < slots.length) {
  151. slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
  152. }
  153. }
  154.  
  155. dualPower = nbt.getShort("PowerTime");
  156. dualCookTime = nbt.getShort("CookTime");
  157. }
  158.  
  159. public void writeToNBT(NBTTagCompound nbt) {
  160. super.writeToNBT(nbt);
  161. nbt.setShort("PowerTime", (short)dualPower);
  162. nbt.setShort("CookTime", (short)dualCookTime);
  163. NBTTagList list = new NBTTagList();
  164.  
  165. for (int i = 0; i < slots.length; i++) {
  166. if (slots[i] != null) {
  167. NBTTagCompound nbt1 = new NBTTagCompound();
  168. nbt1.setByte("Slot", (byte)i);
  169. slots[i].writeToNBT(nbt1);
  170. list.appendTag(nbt1);
  171. }
  172. }
  173.  
  174. nbt.setTag("Items", list);
  175. }
  176.  
  177.  
  178. @Override
  179. public String getInventoryName() {
  180. return "container.iceCreamMaker";
  181. }
  182.  
  183. @Override
  184. public boolean canInsertItem(int var1, ItemStack itemstack, int var3) {
  185. return this.isItemValidForSlot(var1, itemstack);
  186. }
  187.  
  188. @Override
  189. public boolean canExtractItem(int i, ItemStack itemstack, int j) {
  190. return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
  191. }
  192.  
  193. @Override
  194. public boolean hasCustomInventoryName() {
  195. return this.customName != null && this.customName.length() > 0;
  196. }
  197.  
  198. public int getIceMakerProgressScaled(int i) {
  199. return (dualCookTime * i) / this.iceCreamMakerSpeed;
  200. }
  201.  
  202. public int getPowerRemainingScaled(int i) {
  203. return (dualPower * i) / maxPower;
  204. }
  205.  
  206. private boolean canIce() {
  207.  
  208. if (slots[0] == null || slots[1] == null) {
  209. return false;
  210. } else {
  211.  
  212. ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(slots[0].getItem(), slots[1].getItem());
  213.  
  214. if(itemstack == null) return false;
  215. if(this.slots[3] == null) return true;
  216. if(!this.slots[3].isItemEqual(itemstack)) return false;
  217.  
  218. int result = this.slots[3].stackSize + itemstack.stackSize;
  219.  
  220. return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  221. }
  222. }
  223.  
  224. private void iceItem() {
  225. if (canIce()) {
  226. ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(slots[0].getItem(), slots[1].getItem());
  227.  
  228. if (slots[3] == null) {
  229. slots[3] = itemstack.copy();
  230. }else if (slots[3].isItemEqual(itemstack)) {
  231. slots[3].stackSize += itemstack.stackSize;
  232. }
  233.  
  234. for (int i = 0; i < 2; i++) {
  235. if (slots[i].stackSize <= 0) {
  236. slots[i] = new ItemStack(slots[i].getItem().setFull3D());
  237. } else {
  238. slots[i].stackSize--;
  239. }
  240.  
  241. if (slots[i].stackSize <= 0){
  242. slots[i] = null;
  243. }
  244. }
  245. }
  246. }
  247.  
  248. public boolean hasPower() {
  249. return dualPower > 0;
  250. }
  251.  
  252. public boolean isIcing() {
  253. return this.dualCookTime > 0;
  254. }
  255.  
  256. public void updateEntity() {
  257. boolean flag = this.hasPower();
  258. boolean flag1 = false;
  259.  
  260. if(hasPower() && this.isIcing()) {
  261. this.dualPower--;
  262. }
  263.  
  264. if(!worldObj.isRemote) {
  265. if (this.hasItemPower(this.slots[2]) && this.dualPower < (this.maxPower - this.getItemPower(this.slots[2]))) {
  266. this.dualPower += getItemPower(this.slots[2]);
  267.  
  268. if(this.slots[2] != null) {
  269. flag1 = true;
  270.  
  271. this.slots[2].stackSize--;
  272.  
  273. if(this.slots[2].stackSize == 0) {
  274. this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
  275. }
  276. }
  277. }
  278.  
  279. if (hasPower() && canIce()) {
  280. dualCookTime++;
  281.  
  282. if (this.dualCookTime == this.iceCreamMakerSpeed) {
  283. this.dualCookTime = 0;
  284. this.iceItem();
  285. flag1 = true;
  286. }
  287. } else {
  288. dualCookTime = 0;
  289. }
  290. if (flag != this.isIcing()) {
  291. flag1 = true;
  292. IceCreamMaker.updateBlockState(this.hasPower(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  293. }
  294. }
  295.  
  296. if (flag1) {
  297. this.markDirty();
  298. }
  299. }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment