thenaonao

UpdateEntity

Jul 27th, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.86 KB | None | 0 0
  1. package firstetestmod;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import ic2.api.energy.event.EnergyTileLoadEvent;
  7. import ic2.api.energy.event.EnergyTileUnloadEvent;
  8. import ic2.api.energy.prefab.BasicSink;
  9. import ic2.api.energy.tile.IEnergySink;
  10. import ic2.api.energy.tile.IEnergyTile;
  11. import ic2.api.info.Info;
  12. import ic2.api.network.INetworkDataProvider;
  13. import ic2.api.network.INetworkTileEntityEventListener;
  14. import ic2.api.network.INetworkUpdateListener;
  15. import ic2.api.tile.IWrenchable;
  16. import net.minecraft.entity.EntityLivingBase;
  17. import net.minecraft.entity.item.EntityTNTPrimed;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.init.Items;
  20. import net.minecraft.inventory.IInventory;
  21. import net.minecraft.inventory.ISidedInventory;
  22. import net.minecraft.item.Item;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.nbt.NBTTagList;
  26. import net.minecraft.tileentity.TileEntity;
  27. import net.minecraftforge.common.MinecraftForge;
  28. import net.minecraftforge.common.util.ForgeDirection;
  29. import cpw.mods.fml.common.FMLCommonHandler;
  30. import cpw.mods.fml.relauncher.Side;
  31. import cpw.mods.fml.relauncher.SideOnly;
  32.  
  33. public class TileEntityPlateMachine extends BasicSink implements
  34. ISidedInventory, IWrenchable, IEnergyTile, IInventory, IEnergySink {
  35. public TileEntityPlateMachine(TileEntity TileEntityPlateMachine, int maxPower, int tier1) {
  36. super(TileEntityPlateMachine, maxPower, tier1);
  37.  
  38. }
  39.  
  40. private static final int[] slots_top = new int[] { 0 };
  41. private static final int[] slots_bottom = new int[] { 2, 1 };
  42. private static final int[] slots_sides = new int[] { 1 };
  43.  
  44. /**
  45. * The ItemStacks that hold the items currently being used in the furnace
  46. */
  47. private ItemStack[] slots = new ItemStack[3];
  48.  
  49. /**
  50. * the speed of this furnace, 200 is normal / how many ticks it takes : 30
  51. * ticks = 1 second
  52. */
  53. public int maceratingSpeed = 500;
  54.  
  55. /** Power */
  56. public double power = 0.0D;
  57. /** Max Power */
  58. public double maxPower = 10000.0D;
  59. public boolean addedToEnet;
  60.  
  61. /** The number of ticks that the current item has been cooking for */
  62. public int cookTime;
  63.  
  64. private String field_94130_e;
  65. private String PlateMachineName;
  66. private Object field_145850_b;
  67.  
  68. public void PlateMachineName(String string) {
  69. this.PlateMachineName = string;
  70. }
  71.  
  72. /**
  73. * Returns the number of slots in the inventory.
  74. */
  75. public int getSizeInventory() {
  76. return this.slots.length;
  77. }
  78.  
  79. /**
  80. * Returns the stack in slot i
  81. */
  82. public ItemStack getStackInSlot(int par1) {
  83. return this.slots[par1];
  84. }
  85.  
  86. /**
  87. * Removes from an inventory slot (first arg) up to a specified number
  88. * (second arg) of items and returns them in a new stack.
  89. */
  90. @Override
  91. public ItemStack decrStackSize(int par1, int par2) {
  92. if (this.slots[par1] != null) {
  93. ItemStack itemstack;
  94.  
  95. if (this.slots[par1].stackSize <= par2) {
  96. itemstack = this.slots[par1];
  97. this.slots[par1] = null;
  98. return itemstack;
  99. } else {
  100. itemstack = this.slots[par1].splitStack(par2);
  101.  
  102. if (this.slots[par1].stackSize == 0) {
  103. this.slots[par1] = null;
  104. }
  105.  
  106. return itemstack;
  107. }
  108. } else {
  109. return null;
  110. }
  111. }
  112.  
  113. /**
  114. * When some containers are closed they call this on each slot, then drop
  115. * whatever it returns as an EntityItem - like when you close a workbench
  116. * GUI.
  117. */
  118. public ItemStack getStackInSlotOnClosing(int par1) {
  119. if (this.slots[par1] != null) {
  120. ItemStack itemstack = this.slots[par1];
  121. this.slots[par1] = null;
  122. return itemstack;
  123. } else {
  124. return null;
  125. }
  126. }
  127.  
  128. /**
  129. * Sets the given item stack to the specified slot in the inventory (can be
  130. * crafting or armor sections).
  131. */
  132. public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
  133. this.slots[par1] = par2ItemStack;
  134.  
  135. if (par2ItemStack != null
  136. && par2ItemStack.stackSize > this.getInventoryStackLimit()) {
  137. par2ItemStack.stackSize = this.getInventoryStackLimit();
  138. }
  139. }
  140.  
  141. /**
  142. * Returns the name of the inventory.
  143. */
  144. public String getInvName() {
  145. return this.isInvNameLocalized() ? this.field_94130_e
  146. : "container.PlateMachine";
  147. }
  148.  
  149. /**
  150. * If this returns false, the inventory name will be used as an unlocalized
  151. * name, and translated into the player's language. Otherwise it will be
  152. * used directly.
  153. */
  154. public boolean isInvNameLocalized() {
  155. return this.field_94130_e != null && this.field_94130_e.length() > 0;
  156. }
  157.  
  158. @Override
  159. public String getInventoryName() {
  160. return this.hasCustomInventoryName() ? this.field_94130_e
  161. : "container.PlateMachine";
  162. }
  163.  
  164. public boolean isInventoryName() {
  165. return this.field_94130_e != null && this.field_94130_e.length() > 0;
  166. }
  167.  
  168. /**
  169. * Sets the custom display name to use when opening a GUI linked to this
  170. * tile entity.
  171. */
  172. public void setGuiDisplayName(String par1Str) {
  173. this.field_94130_e = par1Str;
  174. }
  175.  
  176. /**
  177. * Reads a tile entity from NBT.
  178. */
  179. @Override
  180. public void readFromNBT(NBTTagCompound par1NBTTagCompound) {
  181. super.readFromNBT(par1NBTTagCompound);
  182.  
  183. if (par1NBTTagCompound.hasKey("power")) {
  184. this.power = par1NBTTagCompound.getDouble("power");
  185. }
  186.  
  187. NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items", 10);
  188. this.slots = new ItemStack[this.getSizeInventory()];
  189.  
  190. for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  191. NBTTagCompound nbttagcompound1 = (NBTTagCompound) nbttaglist
  192. .getCompoundTagAt(i);
  193. byte b0 = nbttagcompound1.getByte("Slot");
  194.  
  195. if (b0 >= 0 && b0 < this.slots.length) {
  196. this.slots[b0] = ItemStack
  197. .loadItemStackFromNBT(nbttagcompound1);
  198. }
  199. }
  200. this.cookTime = par1NBTTagCompound.getShort("CookTime");
  201.  
  202. if (par1NBTTagCompound.hasKey("container.PlateMachineName")) {
  203. this.PlateMachineName = par1NBTTagCompound
  204. .getString("container.PlateMachineName");
  205. }
  206. }
  207.  
  208. /**
  209. * Writes a tile entity to NBT.
  210. */
  211. @Override
  212. public void writeToNBT(NBTTagCompound par1NBTTagCompound) {
  213. super.writeToNBT(par1NBTTagCompound);
  214.  
  215. par1NBTTagCompound.setDouble("power", this.power);
  216. par1NBTTagCompound.setShort("power", (short) this.power);
  217. par1NBTTagCompound.setShort("CookTime", (short) this.cookTime);
  218. NBTTagList nbttaglist = new NBTTagList();
  219.  
  220. for (int i = 0; i < this.slots.length; ++i) {
  221. if (this.slots[i] != null) {
  222. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  223. nbttagcompound1.setByte("Slot", (byte) i);
  224. this.slots[i].writeToNBT(nbttagcompound1);
  225. nbttaglist.appendTag(nbttagcompound1);
  226. }
  227. }
  228. par1NBTTagCompound.setTag("Items", nbttaglist);
  229.  
  230. if (this.isInventoryName()) {
  231. par1NBTTagCompound.setString("container.PlateMachineName",
  232. this.PlateMachineName);
  233. }
  234. }
  235.  
  236. /**
  237. * Returns the maximum stack size for a inventory slot. Seems to always be
  238. * 64, possibly will be extended. *Isn't this more of a set than a get?*
  239. */
  240. public int getInventoryStackLimit() {
  241. return 64;
  242. }
  243.  
  244. @SideOnly(Side.CLIENT)
  245. /**
  246. * Returns an integer between 0 and the passed value representing how close the current item is to being completely
  247. * cooked
  248. */
  249. public int getCookProgressScaled(int par1) {
  250. return this.cookTime * par1 / this.maceratingSpeed;
  251. }
  252.  
  253. public double getPowerRemainingScaled(double par1) {
  254. return this.power * par1 / this.maxPower;
  255. }
  256.  
  257. /**
  258. * Returns true if the furnace is currently burning
  259. */
  260. public boolean hasPower() {
  261. return this.power > 0;
  262. }
  263.  
  264. public boolean isMacerating() {
  265. return this.cookTime > 0;
  266. }
  267.  
  268. /**
  269. * Allows the entity to update its state. Overridden in most subclasses,
  270. * e.g. the mob spawner uses this to count ticks and creates a new spawn
  271. * inside its implementation.
  272. */
  273. @Override
  274. public void updateEntity() {
  275. boolean flag = this.power > 0;
  276. boolean flag1 = false;
  277.  
  278. if (hasPower() && isMacerating()) {
  279. this.power--;
  280. }
  281. if (!this.worldObj.isRemote) {
  282. if(this.power !=0 ||this.slots[0] != null){
  283.  
  284. if (this.power == 0 && this.canSmelt()){
  285. if (this.power > 0)
  286. {
  287. flag1 = true;
  288. }
  289. }
  290.  
  291. if (this.canSmelt() && this.hasPower() ) {
  292. ++this.cookTime;
  293.  
  294. if (this.cookTime == this.maceratingSpeed) {
  295.  
  296. this.cookTime = 0;
  297. this.smeltItem();
  298. flag1 = true;
  299. }
  300. } else {
  301. this.cookTime = 0;
  302. }
  303. }
  304.  
  305. System.out.println("flag ? " + flag);
  306. System.out.println("power > 0 ? " + Boolean.valueOf(power > 0));
  307. if (flag != power > 0) {
  308. flag1 = true;
  309. PlateMachine.updateBlockState(this.power > 0,this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  310. }
  311. }
  312. if (flag1) {
  313. this.markDirty();
  314. }
  315. if (!addedToEnet) onLoaded();
  316. }
  317.  
  318. /**
  319. * Returns true if the furnace can smelt an item, i.e. has a source item,
  320. * destination stack isn't full, etc.
  321. */
  322.  
  323. private boolean canSmelt() {
  324. if (this.slots[0] == null) {
  325. return false;
  326. } else if(this.power > 0) {
  327. ItemStack itemstack = PlateMachineRecipe.getPlateMachineResult(this.slots[0].getItem());
  328. if (itemstack == null)
  329. return false;
  330. if (this.slots[2] == null)
  331. return true;
  332. if (!this.slots[2].isItemEqual(itemstack))
  333. return false;
  334.  
  335. return slots[2].stackSize < itemstack.getMaxStackSize();
  336. }
  337. return false;
  338. }
  339.  
  340. /**
  341. * Turn one item from the furnace source stack into the appropriate smelted
  342. * item in the furnace result stack
  343. */
  344. public void smeltItem() {
  345. if(this.canSmelt()) {
  346. ItemStack itemstack = PlateMachineRecipe.getPlateMachineResult(this.slots[0].getItem());
  347.  
  348. if (this.slots[2] == null) {
  349. this.slots[2] = itemstack.copy();
  350. } else if (this.slots[2].isItemEqual(itemstack)) {
  351. slots[2].stackSize += (itemstack.stackSize);
  352. }
  353.  
  354. --this.slots[0].stackSize;
  355.  
  356. if (this.slots[0].stackSize <= 0) {
  357. this.slots[0] = null;
  358. }
  359. }
  360. }
  361.  
  362. /**
  363. * Returns the number of ticks that the supplied fuel item will keep the
  364. * furnace burning, or 0 if the item isn't fuel
  365. * @deprecated
  366. */
  367. public static double getItemPower(ItemStack itemstack) {
  368. if (itemstack == null) {
  369. return 0;
  370. } else {
  371. Item item = itemstack.getItem();
  372.  
  373. if (item == Items.redstone)
  374. return 10.0D;
  375. if (item == Items.nether_star)
  376. return 50.0D;
  377. if (item == firsttestmodmain.LacirisItem)
  378. return 500.0D;
  379. if (item == firsttestmodmain.UniversalMatter)
  380. return 1000.0D;
  381. return 0.0D;
  382. }
  383. }
  384.  
  385. /**
  386. * Return true if item is a fuel source (getItempower() > 0).
  387. * @deprecated
  388. */
  389. public static boolean isItemFuel(ItemStack par0ItemStack) {
  390. return getItemPower(par0ItemStack) > 0;
  391. }
  392.  
  393. /**
  394. * Do not make give this method the name canInteractWith because it clashes
  395. * with Container
  396. */
  397. public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
  398. return this.worldObj.getTileEntity(this.xCoord, this.yCoord,
  399. this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq(
  400. (double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,
  401. (double) this.zCoord + 0.5D) <= 64.0D;
  402. }
  403.  
  404. public void openChest() {
  405. }
  406.  
  407. public void closeChest() {
  408. }
  409.  
  410. /**
  411. * Returns true if automation is allowed to insert the given stack (ignoring
  412. * stack size) into the given slot.
  413. */
  414. public boolean isItemValidForSlot(int par1, ItemStack par2ItemStack) {
  415. return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack)
  416. : true);
  417. }
  418.  
  419. /**
  420. * Returns an array containing the indices of the slots that can be accessed
  421. * by automation on the given side of this block.
  422. */
  423. public int[] getAccessibleSlotsFromSide(int par1) {
  424. return par1 == 0 ? slots_bottom : (par1 == 1 ? slots_top : slots_sides);
  425. }
  426.  
  427. /**
  428. * Returns true if automation can insert the given item in the given slot
  429. * from the given side. Args: Slot, item, side
  430. */
  431. public boolean canInsertItem(int par1, ItemStack par2ItemStack, int par3) {
  432. return this.isItemValidForSlot(par1, par2ItemStack);
  433. }
  434.  
  435. /**
  436. * Returns true if automation can extract the given item in the given slot
  437. * from the given side. Args: Slot, item, side
  438. */
  439. public boolean canExtractItem(int par1, ItemStack par2ItemStack, int par3) {
  440. return par3 != 0 || par1 != 1
  441. || par2ItemStack.getItem() == Items.bucket;
  442. }
  443.  
  444. public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
  445. return true;
  446. }
  447.  
  448. public float getWrenchDropRate() {
  449. return 1.0F;
  450. }
  451.  
  452. @Override
  453. public boolean hasCustomInventoryName() {
  454. return false;
  455. }
  456.  
  457. @Override
  458. public void openInventory() {
  459.  
  460. }
  461.  
  462. @Override
  463. public void closeInventory() {
  464.  
  465. }
  466.  
  467. @Override
  468. public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
  469. return false;
  470. }
  471.  
  472. @Override
  473. public short getFacing() {
  474. return 0;
  475. }
  476.  
  477. @Override
  478. public void setFacing(short facing) {
  479.  
  480. }
  481.  
  482. @Override
  483. public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
  484. return null;
  485. }
  486.  
  487. @Override
  488. public boolean acceptsEnergyFrom(TileEntity emitter,
  489. ForgeDirection direction) {
  490. return true;
  491. }
  492.  
  493. @Override
  494. public double getDemandedEnergy() {
  495. return this.maxPower - this.power;
  496. }
  497.  
  498. @Override
  499. public int getSinkTier() {
  500. return 1;
  501. }
  502.  
  503. @Override
  504. public double injectEnergy(ForgeDirection forgeDirection, double v,
  505. double v2) {
  506. if (this.power >= this.maxPower) {
  507. return v;
  508. }
  509. this.power += v;
  510.  
  511. return 0.0D;
  512. }
  513.  
  514.  
  515. @Override
  516. public void invalidate() {
  517. super.invalidate();
  518.  
  519. onChunkUnload();
  520. }
  521.  
  522. public void onLoaded() {
  523. if (!addedToEnet && !FMLCommonHandler.instance().getEffectiveSide().isClient() && Info.isIc2Available()) {
  524. MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
  525. this.addedToEnet = true;
  526.  
  527. }
  528. }
  529. @Override
  530. public void onChunkUnload() {
  531. if (addedToEnet && Info.isIc2Available()) {
  532. MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
  533.  
  534. addedToEnet = false;
  535. }
  536. }
  537.  
  538.  
  539.  
  540. public boolean isAddedToEnergyNet() {
  541. return this.addedToEnet;
  542. }
  543. }
Add Comment
Please, Sign In to add comment