Advertisement
koopasa

Untitled

Jan 28th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.79 KB | None | 0 0
  1. package mcjty.mymod.furnace;
  2.  
  3. import mcjty.mymod.tools.MyEnergyStorage;
  4. import net.minecraft.block.state.IBlockState;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.item.crafting.FurnaceRecipes;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.network.NetworkManager;
  10. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.util.EnumFacing;
  13. import net.minecraft.util.ITickable;
  14. import net.minecraftforge.common.capabilities.Capability;
  15. import net.minecraftforge.energy.CapabilityEnergy;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.ItemStackHandler;
  18. import net.minecraftforge.items.wrapper.CombinedInvWrapper;
  19.  
  20. import javax.annotation.Nonnull;
  21. import javax.annotation.Nullable;
  22.  
  23. public class TileFurnace extends TileEntity implements ITickable {
  24.  
  25.  
  26.     public static final int INPUT_SLOTS = 3;
  27.     public static final int OUTPUT_SLOTS = 3;
  28.     public static final int SIZE = INPUT_SLOTS + OUTPUT_SLOTS;
  29.     public static final int MAX_POWER = 100000;
  30.     public float RF_PER_TICK = 9;
  31.     public static final int RF_PER_TICK_INPUT = 250;
  32.     //static var means it is the same for all acelerating furnaces so only use it for something that wont change per furnace (ie slots)
  33.  
  34.  
  35.     //start time
  36.     private  final float furnaceSmeltTimeMax = 100;
  37.     //max end time actually idk anymore
  38.     private  final float furnaceSmeltTimeMin = 5;
  39.     public float time = furnaceSmeltTimeMax;
  40.  
  41.     public  float guiTime = 100;
  42.     private float progressRemaining = 0;
  43.     private float scale = 0;
  44.     private float increaseWithoutCompound = 0.0005f;
  45.     private float increaseWithCompound = 0.025f;
  46.     private float clientProgress = -1;
  47.     private FurnaceState state = FurnaceState.OFF;
  48.     private int clientEnergy = -1;
  49.     //public int progressInt = 0;
  50.  
  51.     private boolean changeScale = true;
  52.     //should it accelerate faster and faster? Basically if this is false, its more balanced
  53.     private boolean compound = false;
  54.  
  55.  
  56.     @Override
  57.     public void update() {
  58.         if (!world.isRemote) {
  59.             if (energyStorage.getEnergyStored() < Math.round(RF_PER_TICK)){
  60.  
  61.                 return;
  62.             }
  63.  
  64.             if (progressRemaining > 0) {
  65.                 setState(FurnaceState.WORKING);
  66.                 energyStorage.consumePower(Math.round(RF_PER_TICK));
  67.  
  68.  
  69.  
  70.                 progressRemaining --;
  71.                 //if the algorithm wont be below 0.1 then set the time to the algorithm
  72.                 if  (furnaceSmeltTimeMax - ((furnaceSmeltTimeMax - furnaceSmeltTimeMin)* scale) > 0.1){
  73.                     time = furnaceSmeltTimeMax - ((furnaceSmeltTimeMax - furnaceSmeltTimeMin)* scale);
  74.  
  75.                 }else{
  76.                     //if it is below 0.1 then set it to 0.1f
  77.                     time = 0.1f;
  78.                     //and change the boolean so the scale dosent keep increasing and the function dosent need to be called multiple times
  79.                     changeScale = false;
  80.                 }
  81.                 // System.out.println(time);
  82.                 //if there is no ticks left to remove, GO AND SMELT
  83.                 if (progressRemaining <= 0) {
  84.  
  85.                     attemptSmelt();
  86.                 }
  87.  
  88.                 markDirty();
  89.             } else {
  90.  
  91.                 //no ticks left? must have started, GO AND START THE SMELTING PROCESS LOL
  92.                 startSmelt();
  93.  
  94.             }
  95.  
  96.         }
  97.  
  98.  
  99.     }
  100.  
  101.  
  102.  
  103.     private boolean insertOutput(ItemStack output, boolean simulate) {
  104.         for (int i = 0; i < OUTPUT_SLOTS; i++) {
  105.             ItemStack remaining = outputHandler.insertItem(i, output, simulate);
  106.             if (remaining.isEmpty()) {
  107.                 return true;
  108.             }
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     private void startSmelt() {
  114.         for (int i = 0; i < INPUT_SLOTS; i++) {
  115.             ItemStack result = FurnaceRecipes.instance().getSmeltingResult(inputHandler.getStackInSlot(i));
  116.             if (!result.isEmpty()) {
  117.                 if (insertOutput(result.copy(), true)) {
  118.                     //if changescale is enable then increase scale
  119.                     if (changeScale == true){
  120.                         if(compound == false){
  121.                             scale = scale + (increaseWithoutCompound * time);
  122.                             RF_PER_TICK = RF_PER_TICK + 0.5f;
  123.                             System.out.println(RF_PER_TICK);
  124.                         }else{
  125.                             scale = scale + increaseWithCompound;
  126.                             RF_PER_TICK = RF_PER_TICK + 0.5f;
  127.                         }
  128.  
  129.  
  130.                     }
  131.                     //set progressremaining to time
  132.                     progressRemaining = time;
  133.                     guiTime = time;
  134.                     markDirty();
  135.                     }
  136.                 setState(FurnaceState.OFF);
  137.                 break;
  138.                 }
  139.             setState(FurnaceState.OFF);
  140.  
  141.             }
  142.         }
  143.  
  144.  
  145.     private void attemptSmelt() {
  146.         for (int i = 0; i < INPUT_SLOTS; i++) {
  147.             ItemStack result = FurnaceRecipes.instance().getSmeltingResult(inputHandler.getStackInSlot(i));
  148.             if (!result.isEmpty()) {
  149.                 // This copy is very important!(
  150.                 if (insertOutput(result.copy(), false)) {
  151.                     inputHandler.extractItem(i, 1, false);
  152.                     break;
  153.  
  154.                 }
  155.             }
  156.         }
  157.     }
  158.  
  159.  
  160.     public float getProgressRemaining() {
  161.         return progressRemaining;
  162.     }
  163.  
  164.     public void setProgressRemaining(float progressRemaining){
  165.         this.progressRemaining = progressRemaining;
  166.     }
  167.  
  168.     public float getTime(){
  169.         return time;
  170.     }
  171.  
  172.     //seperate gui time var for gui only
  173.  
  174.     public float getGuiTime(){
  175.         return guiTime;
  176.     }
  177.  
  178.  
  179.  
  180.     public void setGuiTime(float guiTime){
  181.         this.guiTime = guiTime;
  182.     }
  183.  
  184.     public float getClientProgress() {
  185.         return clientProgress;
  186.     }
  187.  
  188.     public void setClientProgress(float clientProgress) {
  189.         this.clientProgress = clientProgress;
  190.     }
  191.  
  192.     public int getClientEnergy() {
  193.         return clientEnergy;
  194.     }
  195.  
  196.     public void setClientEnergy(int clientEnergy) {
  197.         this.clientEnergy = clientEnergy;
  198.     }
  199.  
  200.     public int getEnergy(){
  201.         return energyStorage.getEnergyStored();
  202.     }
  203.  
  204.     @Override
  205.     public NBTTagCompound getUpdateTag() {
  206.         NBTTagCompound nbtTag = super.getUpdateTag();
  207.         nbtTag.setInteger("state", state.ordinal());
  208.         return nbtTag;
  209.     }
  210.  
  211.     @Nullable
  212.     @Override
  213.     public SPacketUpdateTileEntity getUpdatePacket() {
  214.         return new SPacketUpdateTileEntity(pos, 1, getUpdateTag());
  215.     }
  216.  
  217.  
  218.     //client
  219.     @Override
  220.     public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
  221.         int stateIndex = packet.getNbtCompound().getInteger("state");
  222.  
  223.         if (world.isRemote && stateIndex != state.ordinal()) {
  224.             state = FurnaceState.VALUES[stateIndex];
  225.             world.markBlockRangeForRenderUpdate(pos, pos);
  226.         }
  227.     }
  228.  
  229.     public void setState(FurnaceState state) {
  230.         if (this.state != state) {
  231.             this.state = state;
  232.             markDirty();
  233.             IBlockState blockState = world.getBlockState(pos);
  234.             getWorld().notifyBlockUpdate(pos, blockState, blockState, 3);
  235.         }
  236.     }
  237.  
  238.     public FurnaceState getState() {
  239.         return state;
  240.     }
  241.  
  242.     //-------------------------------------------------------------------------------------------------------------
  243.  
  244.  
  245.     // This item handler will hold our three input slots
  246.     private ItemStackHandler inputHandler = new ItemStackHandler(INPUT_SLOTS) {
  247.  
  248.         @Override
  249.         public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
  250.             ItemStack result = FurnaceRecipes.instance().getSmeltingResult(stack);
  251.             return !result.isEmpty();
  252.         }
  253.  
  254.  
  255.         @Override
  256.         protected void onContentsChanged(int slot) {
  257.             // We need to tell the tile entity that something has changed so
  258.             // that the chest contents is persisted
  259.             TileFurnace.this.markDirty();
  260.         }
  261.     };
  262.  
  263.  
  264.     // This item handler will hold our three output slots
  265.     private ItemStackHandler outputHandler = new ItemStackHandler(OUTPUT_SLOTS) {
  266.         @Override
  267.         protected void onContentsChanged(int slot) {
  268.             // We need to tell the tile entity that something has changed so
  269.             // that the chest contents is persisted
  270.             TileFurnace.this.markDirty();
  271.         }
  272.     };
  273.  
  274.     private CombinedInvWrapper combinedHandler = new CombinedInvWrapper(inputHandler, outputHandler);
  275.  
  276.     //-------------------------------------------------------------------------------------------------------------
  277.  
  278.     //reads that saved stuff
  279.     @Override
  280.     public void readFromNBT(NBTTagCompound compound) {
  281.         super.readFromNBT(compound);
  282.         if (compound.hasKey("itemsIn")) {
  283.             inputHandler.deserializeNBT((NBTTagCompound) compound.getTag("itemsIn"));
  284.         }
  285.         if (compound.hasKey("itemsOut")) {
  286.             outputHandler.deserializeNBT((NBTTagCompound) compound.getTag("itemsOut"));
  287.         }
  288.         progressRemaining = compound.getFloat("progressRemaining");
  289.         energyStorage.setEnergy(compound.getInteger("energy"));
  290.         time = compound.getFloat("time");
  291.         scale = compound.getFloat("scale");
  292.     }
  293.     //saves stuff in a file so it can be recalled upon boot up
  294.     @Override
  295.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  296.         super.writeToNBT(compound);
  297.         compound.setTag("itemsIn", inputHandler.serializeNBT());
  298.         compound.setTag("itemsOut", outputHandler.serializeNBT());
  299.         compound.setFloat("progressRemaining", progressRemaining);
  300.         compound.setFloat("scale", scale);
  301.         compound.setFloat("time", time);
  302.         compound.setInteger("energy", energyStorage.getEnergyStored());
  303.         return compound;
  304.     }
  305.  
  306.     //-------------------------------------------------------------------------------------------------------------
  307.  
  308.     private MyEnergyStorage energyStorage = new MyEnergyStorage(MAX_POWER, RF_PER_TICK_INPUT);
  309.  
  310.     //-------------------------------------------------------------------------------------------------------------
  311.  
  312.     public boolean canInteractWith(EntityPlayer playerIn) {
  313.         // If we are too far away from this tile entity you cannot use it
  314.         return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
  315.     }
  316.  
  317.     @Override
  318.     public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
  319.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  320.             return true;
  321.         }
  322.         if (capability == CapabilityEnergy.ENERGY){
  323.             return true;
  324.         }
  325.         return super.hasCapability(capability, facing);
  326.     }
  327.  
  328.     @Override
  329.     public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
  330.         if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  331.             if (facing == null) {
  332.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(combinedHandler);
  333.             } else if (facing == EnumFacing.UP) {
  334.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(inputHandler);
  335.             } else {
  336.                 return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(outputHandler);
  337.             }
  338.         }
  339.         if (capability == CapabilityEnergy.ENERGY){
  340.             return  CapabilityEnergy.ENERGY.cast(energyStorage);
  341.         }
  342.         return super.getCapability(capability, facing);
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement