Advertisement
Guest User

NewBasicFurnaceTile

a guest
May 19th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.40 KB | None | 0 0
  1. public class TileEntityBasicFurnace extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
  2.    
  3.     public enum FurnaceSlots {
  4.        
  5.         //TODO Reason for existing gone, makes code messy so remove it
  6.  
  7.         CHARGE(0),
  8.         INPUT(0),
  9.         OUTPUT(0);
  10.  
  11.         int id;
  12.  
  13.         FurnaceSlots(int number) {
  14.            
  15.             id = number;
  16.            
  17.         }
  18.  
  19.         public int getId() {
  20.            
  21.             return id;
  22.            
  23.         }
  24.     }
  25.  
  26.     private MechaniCraftEnergyStorage energyStorage = createEnergy();
  27.    
  28.     private ItemStackHandler inputSlotHandler = createInput();
  29.     private ItemStackHandler inputSlotWrapperHandler = createInputWrapper(inputSlotHandler);
  30.     private ItemStackHandler outputSlotHandler = createOutput();
  31.     private ItemStackHandler chargeSlotHandler = createCharge();
  32.    
  33.     private final LazyOptional<IItemHandler> inputSlotWrapper  = LazyOptional.of(() -> inputSlotWrapperHandler);
  34.     private final LazyOptional<IItemHandler> outputSlot  = LazyOptional.of(() -> outputSlotHandler);
  35.     private final LazyOptional<IItemHandler> chargeSlot  = LazyOptional.of(() -> chargeSlotHandler);
  36.    
  37.     private final LazyOptional<IItemHandler> allSlots  = LazyOptional.of(() -> new CombinedInvWrapper(chargeSlotHandler, inputSlotWrapperHandler, outputSlotHandler));
  38.  
  39.     private LazyOptional<IEnergyStorage> energy = LazyOptional.of(() -> energyStorage);
  40.  
  41.     private int progress = 0;
  42.  
  43.     static final int WORK_TIME = 10 * 20;
  44.  
  45.     //TODO Create config for energy consumed and working times
  46.  
  47.     private int smeltingEnergy = 20/*PER TICK*/;
  48.  
  49.     static int MAX_ENERGY = 0;
  50.  
  51.     private final IIntArray fields = new IIntArray() {
  52.  
  53.         @Override
  54.         public int get(int index) {
  55.  
  56.             switch (index) {
  57.  
  58.             case 0:
  59.                 return energyStorage.getEnergyStored();
  60.             case 1:
  61.                 return progress;
  62.             default:
  63.                 return 0;
  64.  
  65.             }
  66.         }
  67.  
  68.         @Override
  69.         public void set(int index, int value) {
  70.  
  71.             switch (index) {
  72.  
  73.             case 0:
  74.                 energyStorage.setEnergyStored(value, false);
  75.                 break;
  76.             case 1:
  77.                 progress = value;
  78.                 break;
  79.             default:
  80.                 break;
  81.  
  82.             }
  83.         }
  84.  
  85.         @Override
  86.         public int getCount() {
  87.            
  88.             return 2;
  89.            
  90.         }
  91.     };
  92.  
  93.     public TileEntityBasicFurnace() {
  94.  
  95.         super(TileEntityHandler.TILE_ENTITY_BASIC_FURNACE.get());
  96.  
  97.     }
  98.  
  99.     private ItemStackHandler createCharge() {
  100.        
  101.         return new ItemStackHandler() {
  102.  
  103.             @Override
  104.             public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
  105.  
  106.                 if(slot == TileEntityBasicFurnace.FurnaceSlots.CHARGE.getId() && stack.getCapability(CapabilityEnergy.ENERGY).isPresent()) {
  107.  
  108.                     return true;
  109.  
  110.                 }
  111.  
  112.                 return false;
  113.                
  114.             }
  115.         };
  116.     }
  117.  
  118.     private ItemStackHandler createInput() {
  119.        
  120.         return new ItemStackHandler() {
  121.  
  122.             @Override
  123.             public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
  124.  
  125.                 Optional<FurnaceRecipe> recipe = getRecipeForInput(level, stack);
  126.  
  127.                 if(slot == TileEntityBasicFurnace.FurnaceSlots.INPUT.getId() && recipe.isPresent() && !(ItemStack.matches(getResultForItem(level, stack), stack))) {
  128.  
  129.                     return true;
  130.  
  131.                 }
  132.  
  133.                 return false;
  134.                
  135.             }
  136.         };
  137.     }
  138.    
  139.     private ItemStackHandler createInputWrapper(ItemStackHandler inputSlot) {
  140.        
  141.         return new InputStackHandler(this.inputSlotHandler) {
  142.  
  143.             @Override
  144.             public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
  145.  
  146.                 Optional<FurnaceRecipe> recipe = getRecipeForInput(level, stack);
  147.  
  148.                 if(slot == TileEntityBasicFurnace.FurnaceSlots.INPUT.getId() && recipe.isPresent() && !(ItemStack.matches(getResultForItem(level, stack), stack))) {
  149.  
  150.                     return true;
  151.  
  152.                 }
  153.  
  154.                 return false;
  155.                
  156.             }
  157.         };
  158.     }
  159.    
  160.     private ItemStackHandler createOutput() {
  161.        
  162.         return new ItemStackHandler() {
  163.  
  164.             @Override
  165.             public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
  166.  
  167.                 if(slot == TileEntityBasicFurnace.FurnaceSlots.OUTPUT.getId()) {
  168.  
  169.                     return false;
  170.  
  171.                 }
  172.  
  173.                 return false;
  174.             }
  175.         };
  176.     }
  177.    
  178.     private MechaniCraftEnergyStorage createEnergy() {
  179.  
  180.         return new MechaniCraftEnergyStorage(100000, 500, 0) {
  181.  
  182.             @Override
  183.             protected void onEnergyChanged() {
  184.  
  185.                 setChanged();
  186.  
  187.             }
  188.         };
  189.     }
  190.  
  191.     @Override
  192.     public void tick() {
  193.  
  194.         if (this.level == null || this.level.isClientSide) {
  195.  
  196.             return;
  197.  
  198.         }
  199.  
  200.         if(energyStorage.getMaxEnergyStored() > energyStorage.getEnergyStored()) {
  201.  
  202.             if(!(chargeSlotHandler.getStackInSlot(FurnaceSlots.CHARGE.id).equals(ItemStack.EMPTY))) {
  203.  
  204.                 receivePowerItem(chargeSlotHandler.getStackInSlot(FurnaceSlots.CHARGE.id));
  205.  
  206.             }
  207.  
  208.             else
  209.  
  210.                 receivePower();
  211.  
  212.         }
  213.  
  214.         if(progress <= 0 && this.level.getBlockState(this.worldPosition).getValue(BasicFurnace.LIT) == true) {
  215.  
  216.             this.level.setBlockAndUpdate(this.worldPosition, this.level.getBlockState(this.worldPosition).setValue(BasicFurnace.LIT, Boolean.valueOf(false)));
  217.  
  218.         }
  219.  
  220.         if(canSmelt()) {
  221.  
  222.             startSmelting();
  223.  
  224.         }
  225.        
  226.         if(inputSlotHandler.getStackInSlot(FurnaceSlots.INPUT.id).equals(ItemStack.EMPTY) && progress > 0) {
  227.  
  228.             progress = 0;
  229.  
  230.         }
  231.  
  232.         if(energyStorage.getEnergyStored() <= 0 && progress > 0) {
  233.  
  234.             progress -= 2;
  235.  
  236.         }
  237.  
  238.         if(progress > 0 && this.level.getBlockState(this.worldPosition).getValue(BasicFurnace.LIT) == false) {
  239.  
  240.             this.level.setBlockAndUpdate(this.worldPosition, this.level.getBlockState(this.worldPosition).setValue(BasicFurnace.LIT, Boolean.valueOf(true)));
  241.  
  242.         }
  243.     }
  244.  
  245.     //FOR GUI SYNC
  246.     private int setMaxEnergy() {
  247.  
  248.         return MAX_ENERGY = energyStorage.getMaxEnergyStored();
  249.  
  250.     }
  251.  
  252.     private boolean canSmelt() {
  253.  
  254.         ItemStack input = inputSlotHandler.getStackInSlot(FurnaceSlots.INPUT.id);
  255.         ItemStack output = outputSlotHandler.getStackInSlot(FurnaceSlots.OUTPUT.id);
  256.  
  257.         ItemStack result = getResultForItem(level, input);
  258.  
  259.         if(energyStorage.getEnergyStored() >= smeltingEnergy) {
  260.  
  261.             if (result.isEmpty() || input.isEmpty()) {
  262.  
  263.                 setMaxEnergy();
  264.                 return false;
  265.  
  266.             }
  267.  
  268.             if ((output.getCount() + result.getCount()) > output.getMaxStackSize()) {
  269.  
  270.                 setMaxEnergy();
  271.                 return false;
  272.  
  273.             }
  274.  
  275.             if (output.isEmpty() || output.getItem().equals(result.getItem())) {
  276.  
  277.                 setMaxEnergy();
  278.                 return true;
  279.  
  280.             }
  281.  
  282.             return false;
  283.  
  284.         }
  285.  
  286.         else return false;
  287.  
  288.     }
  289.  
  290.     private void startSmelting() {
  291.  
  292.         ItemStack input = inputSlotHandler.getStackInSlot(FurnaceSlots.INPUT.id);
  293.         ItemStack output = outputSlotHandler.getStackInSlot(FurnaceSlots.OUTPUT.id);
  294.  
  295.         ItemStack result = getResultForItem(level, input);
  296.  
  297.         if(canSmelt()) {
  298.  
  299.             if(progress < WORK_TIME) {
  300.  
  301.                 ++progress;
  302.                 energyStorage.consumeEnergy(smeltingEnergy);
  303.  
  304.             }
  305.  
  306.             if (progress >= WORK_TIME) {
  307.  
  308.                 if(output.isEmpty()) {
  309.  
  310.                     outputSlotHandler.setStackInSlot(FurnaceSlots.OUTPUT.id, result.copy());
  311.                     progress = 0;
  312.                     inputSlotHandler.extractItem(FurnaceSlots.INPUT.id, 1, false);
  313.  
  314.                 } else {
  315.  
  316.                     if(output.getItem().equals(result.getItem()) && output.getCount() < result.getMaxStackSize()) {
  317.  
  318.                         output.grow(result.getCount());
  319.                         progress = 0;
  320.                         inputSlotHandler.extractItem(FurnaceSlots.INPUT.id, 1, false);
  321.  
  322.                     }
  323.                 }
  324.             }
  325.         }
  326.     }
  327.  
  328.     public static Optional<FurnaceRecipe> getRecipeForInput(World world, ItemStack itemStack) {
  329.  
  330.         RecipeManager recipeManager = world.getRecipeManager();
  331.         Inventory singleItemInventory = new Inventory(itemStack);
  332.  
  333.         Optional<FurnaceRecipe> recipe = recipeManager.getRecipeFor(IRecipeType.SMELTING, singleItemInventory, world);
  334.  
  335.         return recipe;
  336.  
  337.     }
  338.  
  339.     public static ItemStack getResultForItem(World world, ItemStack itemStack) {
  340.  
  341.         Optional<FurnaceRecipe> recipe = getRecipeForInput(world, itemStack);
  342.         if (!recipe.isPresent()) return ItemStack.EMPTY;
  343.  
  344.         return recipe.get().getResultItem().copy();
  345.  
  346.     }
  347.  
  348.     private void receivePower() {
  349.  
  350.         if (energyStorage.getEnergyStored() <= 0 || energyStorage.getMaxEnergyStored() > energyStorage.getEnergyStored()) {
  351.  
  352.             for (Direction direction : Direction.values()) {
  353.  
  354.                 TileEntity te = level.getBlockEntity(worldPosition.relative(direction));
  355.  
  356.                 if (te != null) {
  357.  
  358.                     this.getCapability(CapabilityEnergy.ENERGY).ifPresent(energyStorage -> te.getCapability(CapabilityEnergy.ENERGY).ifPresent(tileEnergy -> {
  359.  
  360.                         if(!isEnergyBlock(tileEnergy) || tileEnergy.getEnergyStored() <= 0 || energyStorage.getEnergyStored() == energyStorage.getMaxEnergyStored()) {
  361.  
  362.                             return;
  363.  
  364.                         } else {
  365.  
  366.                             if(tileEnergy.getEnergyStored() < this.energyStorage.getMaxReceive()) {
  367.  
  368.                                 int energyToReceive = tileEnergy.extractEnergy(Math.min(energyStorage.getMaxEnergyStored() - energyStorage.getEnergyStored(), tileEnergy.getEnergyStored()), true);
  369.                                 tileEnergy.extractEnergy(energyToReceive, false);
  370.                                 this.energyStorage.receiveEnergy(energyToReceive, false);
  371.  
  372.                             } else {
  373.  
  374.                                 int energyToReceive = tileEnergy.extractEnergy(Math.min(energyStorage.getMaxEnergyStored() - energyStorage.getEnergyStored(), this.energyStorage.getMaxReceive()), true);
  375.                                 tileEnergy.extractEnergy(energyToReceive, false);
  376.                                 this.energyStorage.receiveEnergy(energyToReceive, false);
  377.  
  378.                             }
  379.                         }
  380.                     }));
  381.                 }
  382.             }
  383.         }
  384.     }
  385.  
  386.     private void receivePowerItem(ItemStack stack) {
  387.  
  388.         //TODO Figure out why items not powering machine
  389.  
  390.         this.getCapability(CapabilityEnergy.ENERGY).ifPresent(energyStorage -> stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(itemEnergy -> {
  391.  
  392.             if(!isEnergyItem(itemEnergy) || itemEnergy.getEnergyStored() <= 0 || energyStorage.getEnergyStored() == energyStorage.getMaxEnergyStored()) {
  393.  
  394.                 return;
  395.  
  396.             } else {
  397.  
  398.                 int energyToReceive = itemEnergy.extractEnergy(Math.min(energyStorage.getMaxEnergyStored() - energyStorage.getEnergyStored(), this.energyStorage.getMaxExtract()), true);
  399.                 itemEnergy.extractEnergy(energyToReceive, false);
  400.                 this.energyStorage.receiveEnergy(energyToReceive, false);
  401.  
  402.             }
  403.         }));
  404.     }
  405.  
  406.     private boolean isEnergyItem(IEnergyStorage itemEnergy) {
  407.  
  408.         return itemEnergy.getEnergyStored() >= 0 && itemEnergy.extractEnergy(itemEnergy.getEnergyStored(), true) >= 0;
  409.  
  410.     }
  411.  
  412.     private boolean isEnergyBlock(IEnergyStorage blockEnergy) {
  413.  
  414.         return blockEnergy.getEnergyStored() >= 0 && blockEnergy.extractEnergy(blockEnergy.getEnergyStored(), true) >= 0;
  415.  
  416.     }
  417.  
  418.     @Nullable
  419.     @Override
  420.     public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity playerEntity) {
  421.  
  422.         assert level != null;
  423.         return new ContainerBasicFurnace(this, this.fields, id, playerInventory, new CombinedInvWrapper(chargeSlotHandler, inputSlotHandler, outputSlotHandler));
  424.  
  425.     }
  426.  
  427.     @Override
  428.     public void load(BlockState state, CompoundNBT tags) {
  429.  
  430.         super.load(state, tags);
  431.         this.progress = tags.getInt("Progress");
  432.         this.energyStorage.deserializeNBT(tags.getCompound("energy"));
  433.         inputSlotHandler.deserializeNBT(tags.getCompound("inputSlot"));
  434.         outputSlotHandler.deserializeNBT(tags.getCompound("outputSlot"));
  435.         chargeSlotHandler.deserializeNBT(tags.getCompound("chargeSlot"));
  436.  
  437.     }
  438.  
  439.     @Override
  440.     public CompoundNBT save(CompoundNBT tags) {
  441.  
  442.         super.save(tags);
  443.         tags.putInt("Progress", this.progress);
  444.         tags.put("energy", energyStorage.serializeNBT());
  445.         tags.put("inputSlot", inputSlotHandler.serializeNBT());
  446.         tags.put("outputSlot", outputSlotHandler.serializeNBT());
  447.         tags.put("chargeSlot", chargeSlotHandler.serializeNBT());
  448.         return tags;
  449.  
  450. }
  451.  
  452.     @Nullable
  453.     @Override
  454.     public SUpdateTileEntityPacket getUpdatePacket() {
  455.  
  456.         CompoundNBT tags = this.getUpdateTag();
  457.         return new SUpdateTileEntityPacket(this.worldPosition, 1, tags);
  458.  
  459.     }
  460.  
  461.     @Override
  462.     public CompoundNBT getUpdateTag() {
  463.  
  464.         return save(new CompoundNBT());
  465.  
  466.     }
  467.  
  468.     @Override
  469.     public void handleUpdateTag(BlockState stateIn, CompoundNBT tag) {
  470.  
  471.         load(stateIn, tag);
  472.  
  473.     }
  474.  
  475.     @Override
  476.     public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  477.  
  478.         load(this.getBlockState(), pkt.getTag());
  479.  
  480.     }
  481.  
  482.     @Nullable
  483.     @Override
  484.     public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction side) {
  485.  
  486.         if (!this.remove && side != null) {
  487.  
  488.             if (cap == CapabilityEnergy.ENERGY) {
  489.  
  490.                 return energy.cast();
  491.  
  492.             }
  493.  
  494.             if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  495.                
  496.                 return allSlots.cast();
  497.  
  498.             }
  499.         }
  500.  
  501.         return super.getCapability(cap, side);
  502.        
  503.     }
  504.  
  505.     @Override
  506.     public void setRemoved() {
  507.  
  508.         energy.invalidate();
  509.         inputSlotWrapper.invalidate();
  510.         outputSlot.invalidate();
  511.         chargeSlot.invalidate();
  512.         allSlots.invalidate();
  513.         super.setRemoved();
  514.  
  515.     }
  516.  
  517.     @Override
  518.     public ITextComponent getDisplayName() {
  519.  
  520.         return new TranslationTextComponent("container.mechanicraft.basic_furnace");
  521.  
  522.     }
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement