Advertisement
Camellias_

TileEntityMicroFissionGenerator

Nov 8th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. package com.camellias.stardust.common.tileentities;
  2.  
  3. import com.camellias.stardust.utils.energy.StardustForgeEnergyStorage;
  4.  
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.init.Items;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.EnumFacing;
  12. import net.minecraft.util.ITickable;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.util.text.ITextComponent;
  15. import net.minecraft.util.text.TextComponentTranslation;
  16. import net.minecraft.world.World;
  17. import net.minecraftforge.common.capabilities.Capability;
  18. import net.minecraftforge.energy.CapabilityEnergy;
  19. import net.minecraftforge.energy.IEnergyStorage;
  20. import net.minecraftforge.fml.common.registry.GameRegistry;
  21. import net.minecraftforge.items.CapabilityItemHandler;
  22. import net.minecraftforge.items.ItemStackHandler;
  23.  
  24. public class TileEntityMicroFissionReactor extends TileEntity implements ITickable
  25. {
  26.     public int capacity = 10240000;
  27.     public int maxOutput = 512;
  28.     private StardustForgeEnergyStorage storage = new StardustForgeEnergyStorage(capacity, 0, maxOutput);
  29.     private String customName;
  30.    
  31.     public ItemStackHandler handler = new ItemStackHandler(1);
  32.     public int burnTime;
  33.     public int energy = storage.getEnergyStored();
  34.    
  35.     @Override
  36.     public void update()
  37.     {
  38.         if(!world.isRemote)
  39.         {
  40.             if(this.isBurning() == false && isItemFuel(handler.getStackInSlot(0)) && energy <= (capacity - 256))
  41.             {
  42.                 handler.getStackInSlot(0).shrink(1);
  43.                 burnTime = 12000;
  44.             }
  45.             if(this.isBurning() == true && energy <= capacity - 256)
  46.             {
  47.                 energy += 256;
  48.                 burnTime--;
  49.             }
  50.            
  51.             outputEnergy();
  52.         }
  53.     }
  54.    
  55.     public void outputEnergy()
  56.     {
  57.         for(EnumFacing dir : EnumFacing.values())
  58.         {
  59.             TileEntity tile = world.getTileEntity(pos.offset(dir));
  60.            
  61.             if(tile == null)
  62.             {
  63.                 continue;
  64.             }
  65.            
  66.             IEnergyStorage energyCapability = tile.getCapability(CapabilityEnergy.ENERGY, null);
  67.            
  68.             if(energyCapability == null)
  69.             {
  70.                 continue;
  71.             }
  72.            
  73.            
  74.            
  75.             if(energy >= maxOutput)
  76.             {
  77.                 energyCapability.receiveEnergy(maxOutput, false);
  78.                 energy -= maxOutput;
  79.             }
  80.            
  81.             if(energy < maxOutput)
  82.             {
  83.                 energyCapability.receiveEnergy(energy, false);
  84.                 energy -= energy;
  85.             }
  86.         }
  87.     }
  88.    
  89.     @Override
  90.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
  91.     {
  92.         return oldState.getBlock() != newState.getBlock();
  93.     }
  94.    
  95.     public boolean isBurning()
  96.     {
  97.         return this.burnTime > 0;
  98.     }
  99.    
  100.     private boolean isItemFuel(ItemStack stack)
  101.     {
  102.         return getFuelValue(stack) > 0;
  103.     }
  104.  
  105.     private int getFuelValue(ItemStack stack)
  106.     {
  107.         if(stack.isEmpty())
  108.         {
  109.             return 0;
  110.         }
  111.         else
  112.         {
  113.             if(stack.getItem() == Items.DIAMOND) return 1;
  114.            
  115.             return GameRegistry.getFuelValue(stack);
  116.         }
  117.     }
  118.  
  119.     @Override
  120.     public <T> T getCapability(Capability<T> capability, EnumFacing facing)
  121.     {
  122.         if(capability == CapabilityEnergy.ENERGY)
  123.         {
  124.             return (T)this.storage;
  125.         }
  126.        
  127.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  128.         {
  129.             return (T)this.handler;
  130.         }
  131.        
  132.         return super.getCapability(capability, facing);
  133.     }
  134.    
  135.     @Override
  136.     public boolean hasCapability(Capability<?> capability, EnumFacing facing)
  137.     {
  138.         if(capability == CapabilityEnergy.ENERGY)
  139.         {
  140.             return true;
  141.         }
  142.         if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  143.         {
  144.             return true;
  145.         }
  146.        
  147.         return super.hasCapability(capability, facing);
  148.     }
  149.    
  150.     @Override
  151.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  152.     {
  153.         super.writeToNBT(compound);
  154.        
  155.         compound.setTag("Inventory", this.handler.serializeNBT());
  156.         compound.setInteger("BurnTime", this.burnTime);
  157.         compound.setInteger("GUIEnergy", this.energy);
  158.         compound.setString("Name", getDisplayName().toString());
  159.         this.storage.writeToNBT(compound);
  160.        
  161.         return compound;
  162.     }
  163.    
  164.     @Override
  165.     public void readFromNBT(NBTTagCompound compound)
  166.     {
  167.         super.readFromNBT(compound);
  168.        
  169.         this.handler.deserializeNBT(compound.getCompoundTag("Inventory"));
  170.         this.burnTime = compound.getInteger("BurnTime");
  171.         this.energy = compound.getInteger("GUIEnergy");
  172.         this.customName = compound.getString("Name");
  173.         this.storage.readFromNBT(compound);
  174.     }
  175.    
  176.     @Override
  177.     public ITextComponent getDisplayName()
  178.     {
  179.         return new TextComponentTranslation("container.microfission_reactor");
  180.     }
  181.    
  182.     public int getEnergyStored()
  183.     {
  184.         return this.energy;
  185.     }
  186.    
  187.     public int getMaxEnergyStored()
  188.     {
  189.         return this.storage.getMaxEnergyStored();
  190.     }
  191.    
  192.     public int getField(int id)
  193.     {
  194.         switch(id)
  195.         {
  196.         case 0:
  197.             return this.energy;
  198.         case 1:
  199.             return this.burnTime;
  200.         default:
  201.             return 0;
  202.         }
  203.     }
  204.    
  205.     public void setField(int id, int value)
  206.     {
  207.         switch(id)
  208.         {
  209.         case 0:
  210.             this.energy = value;
  211.         case 1:
  212.             this.burnTime = value;
  213.         }
  214.     }
  215.    
  216.     public boolean isUsableByPlayer(EntityPlayer player)
  217.     {
  218.         return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq(
  219.                 (double)this.pos.getX() + 0.5D,
  220.                 (double)this.pos.getY() + 0.5D,
  221.                 (double)this.pos.getZ() + 0.5D) <= 64.0D;
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement