Guest User

CompressorTileEntity.java

a guest
Jan 18th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. package com.TechDweebGaming.MystTech.tileentity;
  2.  
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.nbt.NBTTagList;
  9. import net.minecraft.tileentity.TileEntity;
  10. import net.minecraft.util.BlockPos;
  11. import net.minecraft.util.ChatComponentText;
  12. import net.minecraft.util.ChatComponentTranslation;
  13. import net.minecraft.util.EnumFacing;
  14. import net.minecraft.util.IChatComponent;
  15. import net.minecraft.world.World;
  16. import cofh.api.energy.IEnergyReceiver;
  17.  
  18. import com.TechDweebGaming.MystTech.MystTech;
  19. import com.TechDweebGaming.MystTech.gui.ModGuiHandler;
  20.  
  21. public class CompressorTileEntity extends TileEntity implements IInventory, IEnergyReceiver {
  22.  
  23. //Energy Variables
  24. public int EnergyBuffer = 0;
  25. public final int EnergyLimit = 10000;
  26.  
  27. private ItemStack[] inventory;
  28. private String customName;
  29.  
  30. public CompressorTileEntity() {
  31. this.inventory = new ItemStack[this.getSizeInventory()];
  32. }
  33.  
  34. public String getCustomName() {
  35. return this.customName;
  36. }
  37.  
  38. public void setCustomName(String customName) {
  39. this.customName = customName;
  40. }
  41.  
  42. @Override
  43. public String getName() {
  44. return this.hasCustomName() ? this.customName : "Compressor";
  45. }
  46.  
  47. @Override
  48. public boolean hasCustomName() {
  49. return this.customName != null && !this.customName.equals("");
  50. }
  51.  
  52. @Override
  53. public IChatComponent getDisplayName() {
  54. return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
  55. }
  56.  
  57. @Override
  58. public int getSizeInventory() {
  59. return 4;
  60. }
  61.  
  62. @Override
  63. public ItemStack getStackInSlot(int index) {
  64. if (index < 0 || index >= this.getSizeInventory()) {
  65. return null;
  66. }
  67. return this.inventory[index];
  68. }
  69.  
  70. @Override
  71. public ItemStack decrStackSize(int index, int count) {
  72. if(this.getStackInSlot(index) != null) {
  73. ItemStack itemstack;
  74.  
  75. if(this.getStackInSlot(index).stackSize <= count) {
  76. itemstack = this.getStackInSlot(index);
  77. this.setInventorySlotContents(index, null);
  78. this.markDirty();
  79. return itemstack;
  80. } else {
  81. itemstack = this.getStackInSlot(index).splitStack(count);
  82.  
  83. if (this.getStackInSlot(index).stackSize <= 0) {
  84. this.setInventorySlotContents(index, null);
  85. } else {
  86. //Just to show that changes happened
  87. this.setInventorySlotContents(index, this.getStackInSlot(index));
  88. }
  89.  
  90. this.markDirty();
  91. return itemstack;
  92. }
  93. } else {
  94. return null;
  95. }
  96. }
  97.  
  98. //@Override - Tutorial says yes but eclipse says no...
  99. public ItemStack getStackInSlotOnClosing(int index) {
  100. ItemStack stack = this.getStackInSlot(index);
  101. this.setInventorySlotContents(index, null);
  102. return stack;
  103. }
  104.  
  105. @Override
  106. public void setInventorySlotContents(int index, ItemStack stack) {
  107. if (index < 0 || index >= this.getSizeInventory()) {
  108. return;
  109. }
  110.  
  111. if (stack != null && stack.stackSize > this.getInventoryStackLimit()) {
  112. stack.stackSize = this.getInventoryStackLimit();
  113. }
  114.  
  115. if (stack != null && stack.stackSize == 0) {
  116. stack = null;
  117. }
  118.  
  119. this.inventory[index] = stack;
  120. this.markDirty();
  121. }
  122.  
  123. @Override
  124. public int getInventoryStackLimit() {
  125. return 64;
  126. }
  127.  
  128. @Override
  129. public boolean isUseableByPlayer(EntityPlayer player) {
  130. return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  131. }
  132.  
  133. @Override
  134. public void openInventory(EntityPlayer player) {
  135. //Just so Eclipse doesn't yell at me
  136. }
  137.  
  138. @Override
  139. public void closeInventory(EntityPlayer player) {
  140. //Just so Eclipse doesn't yell at me
  141. }
  142.  
  143. @Override
  144. public boolean isItemValidForSlot(int index, ItemStack stack) {
  145. return true;
  146. }
  147.  
  148. @Override
  149. public int getField(int id) {
  150. return 0;
  151. }
  152.  
  153. @Override
  154. public void setField(int id, int value) {
  155. //Just so Eclipse doesn't yell at me
  156. }
  157.  
  158. @Override
  159. public int getFieldCount() {
  160. return 0;
  161. }
  162.  
  163. @Override
  164. public void clear() {
  165. for (int i = 0; i < this.getSizeInventory(); i++)
  166. this.setInventorySlotContents(i, null);
  167. }
  168.  
  169. @Override
  170. public void writeToNBT(NBTTagCompound nbt) {
  171. super.writeToNBT(nbt);
  172.  
  173. NBTTagList list = new NBTTagList();
  174. for (int i = 0; i < this.getSizeInventory(); ++i) {
  175. if (this.getStackInSlot(i) != null) {
  176. NBTTagCompound stackTag = new NBTTagCompound();
  177. stackTag.setByte("Slot", (byte) i);
  178. this.getStackInSlot(i).writeToNBT(stackTag);
  179. list.appendTag(stackTag);
  180. }
  181. }
  182. nbt.setTag("Items", list);
  183.  
  184. if (this.hasCustomName()) {
  185. nbt.setString("CustomName", this.getCustomName());
  186. }
  187.  
  188. nbt.setInteger("EnergyBuffer", EnergyBuffer);
  189. }
  190.  
  191.  
  192. @Override
  193. public void readFromNBT(NBTTagCompound nbt) {
  194. super.readFromNBT(nbt);
  195.  
  196. NBTTagList list = nbt.getTagList("Items", 10);
  197. for (int i = 0; i < list.tagCount(); ++i) {
  198. NBTTagCompound stackTag = list.getCompoundTagAt(i);
  199. int slot = stackTag.getByte("Slot") & 255;
  200. this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
  201. }
  202.  
  203. if (nbt.hasKey("CustomName", 8)) {
  204. this.setCustomName(nbt.getString("CustomName"));
  205. }
  206.  
  207. EnergyBuffer = nbt.getInteger("EnergyBuffer");
  208. }
  209.  
  210. @Override
  211. public ItemStack removeStackFromSlot(int index) {
  212. return null;
  213. }
  214.  
  215. public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
  216. if (!world.isRemote) {
  217. player.openGui(MystTech.instance, ModGuiHandler.MOD_COMPRESSOR_TILE_ENTITY_GUI, world, pos.getX(), pos.getY(), pos.getZ());
  218. }
  219. return true;
  220. }
  221.  
  222. @Override
  223. public boolean canConnectEnergy(EnumFacing facing) {
  224. return true;
  225. }
  226.  
  227. @Override
  228. public int receiveEnergy(EnumFacing facing, int maxReceive, boolean simulate) {
  229. int receive = Math.min(maxReceive, EnergyLimit - EnergyBuffer);
  230. if (!simulate && receive > 0) {
  231. EnergyBuffer += receive;
  232. markDirty();
  233. }
  234. return receive;
  235. }
  236.  
  237. @Override
  238. public int getEnergyStored(EnumFacing facing) {
  239. return EnergyBuffer;
  240. }
  241.  
  242. @Override
  243. public int getMaxEnergyStored(EnumFacing facing) {
  244. return EnergyLimit;
  245. }
  246.  
  247. public int getPowerLevel() {
  248. return EnergyBuffer;
  249. }
  250.  
  251. }
Add Comment
Please, Sign In to add comment