Advertisement
Guest User

Untitled

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