Guest User

Untitled

a guest
Nov 10th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. package com.mrpablo2000.machinecraft.tileentity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.init.Items;
  5. import net.minecraft.inventory.ISidedInventory;
  6. import net.minecraft.item.Item;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.item.crafting.FurnaceRecipes;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraftforge.oredict.OreDictionary;
  13.  
  14. import com.mrpablo2000.machinecraft.MachineCraftBase;
  15. import com.mrpablo2000.machinecraft.blocks.BlockGenerator;
  16. import com.mrpablo2000.machinecraft.items.ItemPowerStorage;
  17.  
  18. import cpw.mods.fml.relauncher.Side;
  19. import cpw.mods.fml.relauncher.SideOnly;
  20.  
  21. public class TileEntityGenerator extends TileEntity implements ISidedInventory{
  22. private static final int[] slots_top = new int[] {};
  23. private static final int[] slots_bottom = new int[] {};
  24. private static final int[] slots_sides = new int[] {};
  25.  
  26. /**
  27. * The ItemStacks that hold the items currently being used in the furnace
  28. */
  29. private ItemStack[] slots = new ItemStack[2];
  30.  
  31. /** the speed of this furnace, 200 is normal / how many ticks it takes : 30 ticks = 1 second */
  32. public int maceratingSpeed = 100;
  33.  
  34. /** The number of ticks that the furnace will keep burning */
  35. public int power;
  36. public int maxPower = 10000;
  37.  
  38. /** The number of ticks that the current item has been cooking for */
  39. public int cookTime;
  40.  
  41. private String field_94130_e;
  42.  
  43. /**
  44. * Returns the number of slots in the inventory.
  45. */
  46. public int getSizeInventory()
  47. {
  48. return this.slots.length;
  49. }
  50.  
  51. /**
  52. * Returns the stack in slot i
  53. */
  54. public ItemStack getStackInSlot(int par1)
  55. {
  56. return this.slots[par1];
  57. }
  58.  
  59. /**
  60. * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
  61. * new stack.
  62. */
  63. public ItemStack decrStackSize(int par1, int par2)
  64. {
  65. if (this.slots[par1] != null)
  66. {
  67. ItemStack itemstack;
  68.  
  69. if (this.slots[par1].stackSize <= par2)
  70. {
  71. itemstack = this.slots[par1];
  72. this.slots[par1] = null;
  73. return itemstack;
  74. }
  75. else
  76. {
  77. itemstack = this.slots[par1].splitStack(par2);
  78.  
  79. if (this.slots[par1].stackSize == 0)
  80. {
  81. this.slots[par1] = null;
  82. }
  83.  
  84. return itemstack;
  85. }
  86. }
  87. else
  88. {
  89. return null;
  90. }
  91. }
  92.  
  93. /**
  94. * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
  95. * like when you close a workbench GUI.
  96. */
  97. public ItemStack getStackInSlotOnClosing(int par1)
  98. {
  99. if (this.slots[par1] != null)
  100. {
  101. ItemStack itemstack = this.slots[par1];
  102. this.slots[par1] = null;
  103. return itemstack;
  104. }else{
  105. return null;
  106. }
  107. }
  108.  
  109. /**
  110. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  111. */
  112. public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
  113. {
  114. this.slots[par1] = par2ItemStack;
  115.  
  116. if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
  117. {
  118. par2ItemStack.stackSize = this.getInventoryStackLimit();
  119. }
  120. }
  121.  
  122. /**
  123. * Returns the name of the inventory.
  124. */
  125. public String getInvName()
  126. {
  127. return this.isInvNameLocalized() ? this.field_94130_e : "container.generator";
  128. }
  129.  
  130. /**
  131. * If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's
  132. * language. Otherwise it will be used directly.
  133. */
  134. public boolean isInvNameLocalized()
  135. {
  136. return this.field_94130_e != null && this.field_94130_e.length() > 0;
  137. }
  138.  
  139. /**
  140. * Sets the custom display name to use when opening a GUI linked to this tile entity.
  141. */
  142. public void setGuiDisplayName(String par1Str)
  143. {
  144. this.field_94130_e = par1Str;
  145. }
  146.  
  147. /**
  148. * Reads a tile entity from NBT.
  149. */
  150. public void readFromNBT(NBTTagCompound par1NBTTagCompound)
  151. {
  152. super.readFromNBT(par1NBTTagCompound);
  153. NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items", 0);
  154. this.slots = new ItemStack[this.getSizeInventory()];
  155.  
  156. for (int i = 0; i < nbttaglist.tagCount(); ++i)
  157. {
  158. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  159. byte b0 = nbttagcompound1.getByte("Slot");
  160.  
  161. if (b0 >= 0 && b0 < this.slots.length)
  162. {
  163. this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  164. }
  165. }
  166.  
  167. this.power = par1NBTTagCompound.getShort("power");
  168. this.cookTime = par1NBTTagCompound.getShort("CookTime");
  169.  
  170. if (par1NBTTagCompound.hasKey("CustomName"))
  171. {
  172. this.field_94130_e = par1NBTTagCompound.getString("CustomName");
  173. }
  174. }
  175.  
  176. /**
  177. * Writes a tile entity to NBT.
  178. */
  179. public void writeToNBT(NBTTagCompound par1NBTTagCompound)
  180. {
  181. super.writeToNBT(par1NBTTagCompound);
  182. par1NBTTagCompound.setShort("power", (short)this.power);
  183. par1NBTTagCompound.setShort("CookTime", (short)this.cookTime);
  184. NBTTagList nbttaglist = new NBTTagList();
  185.  
  186. for (int i = 0; i < this.slots.length; ++i)
  187. {
  188. if (this.slots[i] != null)
  189. {
  190. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  191. nbttagcompound1.setByte("Slot", (byte)i);
  192. this.slots[i].writeToNBT(nbttagcompound1);
  193. nbttaglist.appendTag(nbttagcompound1);
  194. }
  195. }
  196.  
  197. par1NBTTagCompound.setTag("Items", nbttaglist);
  198.  
  199. if (this.isInvNameLocalized())
  200. {
  201. par1NBTTagCompound.setString("CustomName", this.field_94130_e);
  202. }
  203. }
  204.  
  205. /**
  206. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
  207. * this more of a set than a get?*
  208. */
  209. public int getInventoryStackLimit()
  210. {
  211. return 64;
  212. }
  213.  
  214. @SideOnly(Side.CLIENT)
  215.  
  216. /**
  217. * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  218. * cooked
  219. */
  220. public int getCookProgressScaled(int par1)
  221. {
  222. return this.cookTime * par1 / this.maceratingSpeed;
  223. }
  224.  
  225. public int getPowerRemainingScaled(int par1){
  226. return this.power * par1 / this.maxPower;
  227. }
  228.  
  229. /**
  230. * Returns true if the furnace is currently burning
  231. */
  232. public boolean hasPower()
  233. {
  234. return this.power > 0;
  235. }
  236.  
  237. public boolean isMacerating(){
  238. return this.cookTime > 0;
  239. }
  240.  
  241. /**
  242. * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
  243. * ticks and creates a new spawn inside its implementation.
  244. */
  245. public void updateEntity(){
  246. boolean flag = this.power > 0;
  247. boolean flag1 = false;
  248.  
  249. if (hasPower() && isMacerating()){
  250. this.power--;
  251. }
  252. if (!this.worldObj.isRemote){
  253.  
  254. if(isItemPowerStorage() && this.slots[0].getItemDamage() > 0 && this.power > 0)
  255. {
  256. this.slots[0].setItemDamage(this.slots[0].getItemDamage()-1);
  257. this.power = this.power-10;
  258. }
  259. if (this.power < this.maxPower && this.getItemPower(this.slots[1]) > 0){
  260. if(!this.slots[1].isItemStackDamageable())
  261. {
  262. this.power += getItemPower(this.slots[1]);
  263.  
  264. flag1 = true;
  265.  
  266. if (this.slots[1] != null){
  267. this.slots[1].stackSize--;
  268.  
  269. if (this.slots[1].stackSize == 0){
  270. this.slots[1] = this.slots[1].getItem().getContainerItem(slots[1]);
  271. }
  272. }
  273. }else{
  274. if(this.slots[1].getItemDamage() < this.slots[1].getMaxDamage()){
  275. this.power += getItemPower(this.slots[1]);
  276. this.slots[1] = new ItemStack(this.slots[1].getItem(), this.slots[1].stackSize, this.slots[1].getItemDamage()+1);
  277. }
  278. }
  279. }
  280.  
  281. if (flag != this.power > 0)
  282. {
  283. flag1 = true;
  284. BlockGenerator.updateFurnaceBlockState(this.power > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  285. }
  286. }
  287. }
  288.  
  289. public boolean isOre(ItemStack itemstack){
  290. String[] oreNames = OreDictionary.getOreNames();
  291.  
  292. for(int i = 0; i < oreNames.length; i++){
  293. if(oreNames[i].contains("ore")){
  294. if(OreDictionary.getOres(oreNames[i]) != null){
  295. if(OreDictionary.getOres(oreNames[i]).get(0).getItem() == itemstack.getItem()){
  296. return true;
  297. }
  298. }
  299. }
  300. }
  301.  
  302. return false;
  303. }
  304.  
  305.  
  306. private boolean isItemPowerStorage()
  307. {
  308. if(this.slots[0] != null) return this.slots[0].getItem() instanceof ItemPowerStorage;
  309. return false;
  310.  
  311. }
  312.  
  313.  
  314.  
  315. /**
  316. * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
  317. * fuel
  318. */
  319. public static int getItemPower(ItemStack par0ItemStack){
  320. if (par0ItemStack == null){
  321. return 0;
  322. }else{
  323. Item i = par0ItemStack.getItem();
  324.  
  325. if (i == Items.redstone) return 10;
  326. if (i == MachineCraftBase.itemBattery) return 10;
  327. return 0;
  328. }
  329. }
  330.  
  331. /**
  332. * Return true if item is a fuel source (getItempower() > 0).
  333. */
  334. public static boolean isItemFuel(ItemStack par0ItemStack)
  335. {
  336. return getItemPower(par0ItemStack) > 0;
  337. }
  338.  
  339. /**
  340. * Do not make give this method the name canInteractWith because it clashes with Container
  341. */
  342. public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
  343. {
  344. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  345. }
  346.  
  347. public void openChest() {}
  348.  
  349. public void closeChest() {}
  350.  
  351. /**
  352. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  353. */
  354. public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack)
  355. {
  356. return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true);
  357. }
  358.  
  359. /**
  360. * Returns an array containing the indices of the slots that can be accessed by automation on the given side of this
  361. * block.
  362. */
  363. public int[] getAccessibleSlotsFromSide(int par1)
  364. {
  365. return par1 == 0 ? slots_bottom : (par1 == 1 ? slots_top : slots_sides);
  366. }
  367.  
  368. /**
  369. * Returns true if automation can insert the given item in the given slot from the given side. Args: Slot, item,
  370. * side
  371. */
  372. public boolean canInsertItem(int par1, ItemStack par2ItemStack, int par3)
  373. {
  374. return this.isItemValidForSlot(par1, par2ItemStack);
  375. }
  376.  
  377. /**
  378. * Returns true if automation can extract the given item in the given slot from the given side. Args: Slot, item,
  379. * side
  380. */
  381. public boolean canExtractItem(int par1, ItemStack par2ItemStack, int par3)
  382. {
  383. return par3 != 0 || par1 != 1 || par2ItemStack.getItem() == Items.bucket;
  384. }
  385.  
  386. @Override
  387. public String getInventoryName() {
  388. return this.hasCustomInventoryName() ? this.field_94130_e : "container.macerator";
  389.  
  390. }
  391.  
  392. @Override
  393. public boolean hasCustomInventoryName() {
  394. return this.field_94130_e != null && this.field_94130_e.length() > 0;
  395. }
  396.  
  397. @Override
  398. public void openInventory() {
  399. }
  400.  
  401. @Override
  402. public void closeInventory() {
  403. }
  404. }
Add Comment
Please, Sign In to add comment