Advertisement
Guest User

Untitled

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