Advertisement
XeNe

TileentityCombinerMachine

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