Advertisement
tigres810

My TileEntity

Feb 14th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. /**
  2. Copyright (C) 2019 by tigres810
  3.  
  4. This file is part of jabelar's Minecraft Forge modding examples; as such,
  5. you can redistribute it and/or modify it under the terms of the GNU
  6. General Public License as published by the Free Software Foundation,
  7. either version 3 of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.
  15. */
  16.  
  17. package com.tigres810.adventurermod.blocks.pipes.tileentity;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import com.tigres810.adventurermod.blocks.machines.tileentity.TileEntityFluxGenerator;
  23. import com.tigres810.adventurermod.blocks.pipes.BlockEntityFluxPipe;
  24. import com.tigres810.adventurermod.energy.CustomEnergyStorage;
  25. import com.tigres810.adventurermod.init.ModBlocks;
  26. import com.tigres810.adventurermod.init.ModFluids;
  27. import com.tigres810.adventurermod.init.ModItems;
  28.  
  29. import net.minecraft.block.Block;
  30. import net.minecraft.block.BlockHorizontal;
  31. import net.minecraft.block.properties.PropertyBool;
  32. import net.minecraft.block.properties.PropertyDirection;
  33. import net.minecraft.block.properties.PropertyInteger;
  34. import net.minecraft.block.state.IBlockState;
  35. import net.minecraft.entity.player.EntityPlayer;
  36. import net.minecraft.entity.player.InventoryPlayer;
  37. import net.minecraft.init.Items;
  38. import net.minecraft.inventory.Container;
  39. import net.minecraft.inventory.ISidedInventory;
  40. import net.minecraft.inventory.ItemStackHelper;
  41. import net.minecraft.item.ItemStack;
  42. import net.minecraft.nbt.NBTTagCompound;
  43. import net.minecraft.tileentity.TileEntity;
  44. import net.minecraft.tileentity.TileEntityLockable;
  45. import net.minecraft.util.EnumFacing;
  46. import net.minecraft.util.ITickable;
  47. import net.minecraft.util.NonNullList;
  48. import net.minecraft.util.math.BlockPos;
  49. import net.minecraft.util.text.ITextComponent;
  50. import net.minecraft.util.text.TextComponentTranslation;
  51. import net.minecraft.world.IBlockAccess;
  52. import net.minecraft.world.World;
  53. import net.minecraftforge.common.capabilities.Capability;
  54. import net.minecraftforge.common.capabilities.Capability.IStorage;
  55. import net.minecraftforge.energy.CapabilityEnergy;
  56. import net.minecraftforge.energy.IEnergyStorage;
  57. import net.minecraftforge.fluids.FluidStack;
  58. import net.minecraftforge.fluids.FluidUtil;
  59. import net.minecraftforge.fluids.capability.templates.FluidHandlerItemStack;
  60. import net.minecraftforge.items.CapabilityItemHandler;
  61. import net.minecraftforge.items.IItemHandler;
  62. import net.minecraftforge.items.ItemStackHandler;
  63.  
  64.  
  65. public class TileEntityFluxPipe extends TileEntity implements ITickable
  66. {
  67. private PropertyDirection FACING = BlockHorizontal.FACING;
  68. private BlockPos neighbourPos = null;
  69.  
  70. private IBlockState neighbourState = null;
  71.  
  72. private Block neighbourBlock = null;
  73. private CustomEnergyStorage storage = new CustomEnergyStorage(500);
  74. public int energy = storage.getEnergyStored();
  75. private boolean output = false;
  76. public int count = 0;
  77. private String customName;
  78.  
  79. @Override
  80. public void update()
  81. {
  82. //Handle check block nearby
  83. if(world.getBlockState(pos).getValue(FACING) != null) {
  84. //Check block neighbourPos
  85. EnumFacing face = (EnumFacing)world.getBlockState(pos).getValue(FACING);
  86. if( face.equals( EnumFacing.NORTH ) ) {
  87. neighbourPos = pos.offset(EnumFacing.NORTH);
  88.  
  89. neighbourState = world.getBlockState(neighbourPos);
  90.  
  91. neighbourBlock = neighbourState.getBlock();
  92.  
  93. if ( neighbourBlock == ModBlocks.FLUX_GENERATOR_BLOCK ) {
  94.  
  95. if( neighbourState.getValue(FACING).equals( EnumFacing.EAST ) || neighbourState.getValue(FACING).equals( EnumFacing.WEST ) ) {
  96. TileEntityFluxGenerator tile = (TileEntityFluxGenerator) world.getTileEntity(neighbourPos);
  97. if(tile != null) {
  98. if(tile.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.NORTH) && energy < storage.getMaxEnergyStored()) {
  99. if(world.getTileEntity(pos).hasCapability(CapabilityEnergy.ENERGY, EnumFacing.NORTH)) {
  100. int FUEL = tile.getFuelValueFromGenerator();
  101. if(FUEL > 0) {
  102. energy += 1;
  103. tile.consumeEnergy(1);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. } else if( face.equals( EnumFacing.SOUTH ) ) {
  111. neighbourPos = pos.offset(EnumFacing.SOUTH);
  112.  
  113. neighbourState = world.getBlockState(neighbourPos);
  114.  
  115. neighbourBlock = neighbourState.getBlock();
  116.  
  117. if ( neighbourBlock == ModBlocks.FLUX_GENERATOR_BLOCK ) {
  118.  
  119. if( neighbourState.getValue(FACING).equals( EnumFacing.EAST ) || neighbourState.getValue(FACING).equals( EnumFacing.WEST ) ) {
  120. TileEntityFluxGenerator tile = (TileEntityFluxGenerator) world.getTileEntity(neighbourPos);
  121. if(tile != null) {
  122. if(tile.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.SOUTH) && energy < storage.getMaxEnergyStored()) {
  123. if(world.getTileEntity(pos).hasCapability(CapabilityEnergy.ENERGY, EnumFacing.SOUTH)) {
  124. int FUEL = tile.getFuelValueFromGenerator();
  125. if(FUEL > 0) {
  126. energy += 1;
  127. tile.consumeEnergy(1);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. } else if( face.equals( EnumFacing.EAST ) ) {
  135. neighbourPos = pos.offset(EnumFacing.EAST);
  136.  
  137. neighbourState = world.getBlockState(neighbourPos);
  138.  
  139. neighbourBlock = neighbourState.getBlock();
  140.  
  141. if ( neighbourBlock == ModBlocks.FLUX_GENERATOR_BLOCK ) {
  142.  
  143. if( neighbourState.getValue(FACING).equals( EnumFacing.NORTH ) || neighbourState.getValue(FACING).equals( EnumFacing.SOUTH ) ) {
  144. TileEntityFluxGenerator tile = (TileEntityFluxGenerator) world.getTileEntity(neighbourPos);
  145. if(tile != null) {
  146. if(tile.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.EAST) && energy < storage.getMaxEnergyStored()) {
  147. if(world.getTileEntity(pos).hasCapability(CapabilityEnergy.ENERGY, EnumFacing.EAST)) {
  148. int FUEL = tile.getFuelValueFromGenerator();
  149. if(FUEL > 0) {
  150. energy += 1;
  151. tile.consumeEnergy(1);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. } else if( face.equals( EnumFacing.WEST ) ) {
  159. neighbourPos = pos.offset(EnumFacing.WEST);
  160.  
  161. neighbourState = world.getBlockState(neighbourPos);
  162.  
  163. neighbourBlock = neighbourState.getBlock();
  164.  
  165. if ( neighbourBlock == ModBlocks.FLUX_GENERATOR_BLOCK ) {
  166.  
  167. if( neighbourState.getValue(FACING).equals( EnumFacing.NORTH ) || neighbourState.getValue(FACING).equals( EnumFacing.SOUTH ) ) {
  168. TileEntityFluxGenerator tile = (TileEntityFluxGenerator) world.getTileEntity(neighbourPos);
  169. if(tile != null) {
  170. if(tile.hasCapability(CapabilityEnergy.ENERGY, EnumFacing.WEST) && energy < storage.getMaxEnergyStored()) {
  171. if(world.getTileEntity(pos).hasCapability(CapabilityEnergy.ENERGY, EnumFacing.WEST)) {
  172. int FUEL = tile.getFuelValueFromGenerator();
  173. if(FUEL > 0) {
  174. energy += 1;
  175. tile.consumeEnergy(1);
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186. public void consumeEnergy(int amount) {
  187. energy -= amount;
  188. }
  189.  
  190. public int getFuelValueFromPipe()
  191. {
  192. return energy;
  193. }
  194.  
  195. @Override
  196. public boolean hasFastRenderer() {
  197. return true;
  198. }
  199.  
  200. @Override
  201. public boolean hasCapability(Capability<?> capability, EnumFacing facing)
  202. {
  203. if(capability == CapabilityEnergy.ENERGY) return true;
  204. return super.hasCapability(capability, facing);
  205. }
  206.  
  207. @Override
  208. public <T> T getCapability(Capability<T> capability, EnumFacing facing)
  209. {
  210. if(capability == CapabilityEnergy.ENERGY) return (T)this.storage;
  211. return super.getCapability(capability, facing);
  212. }
  213.  
  214. @Override
  215. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  216. {
  217. super.writeToNBT(compound);
  218. compound.setInteger("GuiEnergy", this.energy);
  219. compound.setString("Name", this.getDisplayName().toString());
  220. this.storage.writeToNBT(compound);
  221. return compound;
  222. }
  223.  
  224. @Override
  225. public void readFromNBT(NBTTagCompound compound)
  226. {
  227. super.readFromNBT(compound);
  228. this.energy = compound.getInteger("GuiEnergy");
  229. this.customName = compound.getString("Name");
  230. this.storage.readFromNBT(compound);
  231. }
  232.  
  233. @Override
  234. public ITextComponent getDisplayName()
  235. {
  236. return new TextComponentTranslation("container.energy_storage");
  237. }
  238.  
  239. public int getEnergyStored()
  240. {
  241. return energy;
  242. }
  243.  
  244. public int getMaxEnergyStored()
  245. {
  246. return this.storage.getMaxEnergyStored();
  247. }
  248.  
  249. public int getField(int id)
  250. {
  251. switch(id)
  252. {
  253. case 0:
  254. return this.energy;
  255. default:
  256. return 0;
  257. }
  258. }
  259.  
  260. public void setField(int id, int value)
  261. {
  262. switch(id)
  263. {
  264. case 0:
  265. this.energy = value;
  266. }
  267. }
  268.  
  269. public boolean isUsableByPlayer(EntityPlayer player)
  270. {
  271. return this.world.getTileEntity(pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64D;
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement