Guest User

Untitled

a guest
Jul 17th, 2018
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.natura.minestuckarsenal.tileentity;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import javax.annotation.Nullable;
  6.  
  7. import com.mraof.minestuck.item.MinestuckItems;
  8.  
  9. import net.minecraft.block.state.IBlockState;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.util.EnumFacing;
  13. import net.minecraft.util.ITickable;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.world.World;
  16. import net.minecraftforge.common.capabilities.Capability;
  17. import net.minecraftforge.common.capabilities.ICapabilityProvider;
  18. import net.minecraftforge.energy.CapabilityEnergy;
  19. import net.minecraftforge.energy.EnergyStorage;
  20. import net.minecraftforge.energy.IEnergyStorage;
  21. import net.minecraftforge.items.CapabilityItemHandler;
  22. import net.minecraftforge.items.ItemStackHandler;
  23.  
  24. /*
  25.  * Thanks to the Actually Additions author for the energy transfer code.
  26.  * https://github.com/Ellpeck/ActuallyAdditions
  27.  */
  28.  
  29. public class TileEntityHub<IItemHandler,IEnergyStorage> extends TileEntity implements ICapabilityProvider,ITickable {
  30.    
  31.     private StackMaster inventory = new StackMaster(1);
  32.     private EnergyMaster energy = new EnergyMaster(1000000);
  33.     protected TileEntity[] tilesAround = new TileEntity[6];
  34.     protected int ticksElapsed;
  35.    
  36.     @Override
  37.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  38.         super.writeToNBT(compound);
  39.         compound.setTag("inventory", inventory.serializeNBT());
  40.         compound.setInteger("energy", energy.getEnergyStored());
  41.         return compound;
  42.     }
  43.    
  44.     @Override
  45.     public void readFromNBT(NBTTagCompound compound) {
  46.         super.readFromNBT(compound);
  47.         inventory.deserializeNBT(compound.getCompoundTag("inventory"));
  48.         this.energy.setEnergy(compound.getInteger("energy"));
  49.     }
  50.    
  51.     @Override
  52.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
  53.     {
  54.         return oldState.getBlock() != newSate.getBlock();
  55.     }
  56.  
  57.     @Override
  58.     public void update() {
  59.        
  60.         int timerMax = 1200;
  61.        
  62.         if(ticksElapsed == timerMax) {
  63.             if(inventory.getStackInSlot(0).getItem() == MinestuckItems.rawUranium) {
  64.                 if(energy.getEnergyStored() == energy.getMaxEnergyStored()) {
  65.                
  66.                 }
  67.                 else if((energy.getEnergyStored() + 20000) <= energy.getMaxEnergyStored()) {
  68.                     energy.increaseEnergy(20000);
  69.                     inventory.extractItem(0, 1, false);
  70.                 }
  71.                 else {
  72.                     int mod = energy.getMaxEnergyStored() - energy.getEnergyStored();
  73.                
  74.                     energy.increaseEnergy(mod);
  75.                     inventory.extractItem(0, 1, false);
  76.                 }
  77.             }
  78.             ticksElapsed = 0;
  79.         }
  80.        
  81.         else {
  82.             ticksElapsed ++;
  83.             if(inventory.getStackInSlot(0).getItem() == MinestuckItems.rawUranium) {
  84.                 if(energy.getEnergyStored() == energy.getMaxEnergyStored()) {
  85.                
  86.                 }
  87.                 else if((energy.getEnergyStored() + 10000) <= energy.getMaxEnergyStored()) {
  88.                     energy.increaseEnergy(10000);
  89.                    
  90.                 }
  91.                 else {
  92.                     int mod = energy.getMaxEnergyStored() - energy.getEnergyStored();
  93.                     energy.increaseEnergy(mod);
  94.                 }
  95.             }
  96.         }
  97.         NBTTagCompound compound = new NBTTagCompound();
  98.         compound.setInteger("energy", energy.getEnergyStored());
  99.         compound.setTag("inventory", inventory.serializeNBT());
  100.         writeToNBT(compound);
  101.         this.markDirty();
  102.        
  103.         this.updateEntity();
  104.     }
  105.    
  106.     public void updateEntity() {
  107.         int total = energy.getEnergyStored();
  108.         if(total > 0){
  109.             EnumFacing[] sides = EnumFacing.VALUES;
  110.             int amount = total/sides.length;
  111.             if(amount <= 0){
  112.                 amount = total;
  113.             }
  114.             getMaterialsAround(this.world, this.pos);
  115.             for(EnumFacing side : sides){
  116.                 TileEntity tile = this.tilesAround[side.ordinal()];
  117.                 if(tile != null){
  118.                     doEnergyInteraction(this, tile, side, amount);
  119.                 }
  120.             }
  121.         }
  122.  
  123.     }
  124.    
  125.     public TileEntity[] getMaterialsAround(World world, BlockPos pos) {
  126.         this.tilesAround[0]  = (world.getTileEntity(pos.offset(EnumFacing.UP)));
  127.         this.tilesAround[1]  = (world.getTileEntity(pos.offset(EnumFacing.DOWN)));
  128.         this.tilesAround[2]  = (world.getTileEntity(pos.offset(EnumFacing.NORTH)));
  129.         this.tilesAround[3]  = (world.getTileEntity(pos.offset(EnumFacing.EAST)));
  130.         this.tilesAround[4]  = (world.getTileEntity(pos.offset(EnumFacing.SOUTH)));
  131.         this.tilesAround[5]  = (world.getTileEntity(pos.offset(EnumFacing.WEST)));
  132.         return tilesAround;
  133.     }
  134.  
  135.     public void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) {
  136.  
  137.         if (maxTransfer > 0) {
  138.                 EnumFacing opp = sideTo == null ? null : sideTo.getOpposite();
  139.                 if (tileFrom.hasCapability(CapabilityEnergy.ENERGY, sideTo) && tileTo.hasCapability(CapabilityEnergy.ENERGY, opp)) {
  140.                     net.minecraftforge.energy.IEnergyStorage handlerFrom = tileFrom.getCapability(CapabilityEnergy.ENERGY, sideTo);
  141.                     net.minecraftforge.energy.IEnergyStorage handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, opp);
  142.                    
  143.                     if (handlerFrom != null && handlerTo != null) {
  144.                         int drain = handlerFrom.extractEnergy(maxTransfer, true);
  145.                         if (drain > 0) {
  146.                             int filled = handlerTo.receiveEnergy(drain, false);
  147.                             handlerFrom.extractEnergy(filled, false);
  148.                         }
  149.                     }
  150.                 }
  151.             }
  152.     }
  153.    
  154.    
  155.     @Override
  156.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  157.       if (capability == CapabilityEnergy.ENERGY || capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  158.         return true;
  159.       }
  160.       return super.hasCapability(capability, facing);
  161.     }
  162.    
  163.     @Override
  164.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  165.         if(capability == CapabilityEnergy.ENERGY) {
  166.             return (T)energy;
  167.         }
  168.         else if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  169.             return (T)inventory;
  170.         }
  171.        
  172.         return super.getCapability(capability, facing);
  173.     }
  174.    
  175. }
RAW Paste Data