ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPoweredFurnace.java

267 lines
11 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFurnaceDouble.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2021-11-13 18:02:42 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2022-01-02 17:58:59 +01:00
import de.ellpeck.actuallyadditions.mod.crafting.SingleItem;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFurnaceDouble;
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
2024-03-04 21:38:02 +01:00
import net.minecraft.world.item.crafting.RecipeHolder;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.energy.IEnergyStorage;
2022-01-02 17:58:59 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Optional;
2024-03-02 21:23:08 +01:00
public class TileEntityPoweredFurnace extends TileEntityInventoryBase implements IButtonReactor, MenuProvider {
2015-05-20 22:39:43 +02:00
public static final int SLOT_INPUT_1 = 0;
public static final int SLOT_OUTPUT_1 = 1;
public static final int SLOT_INPUT_2 = 2;
public static final int SLOT_OUTPUT_2 = 3;
2015-12-01 19:48:09 +01:00
public static final int ENERGY_USE = 25;
private static final int SMELT_TIME = 80;
public final CustomEnergyStorage storage = new CustomEnergyStorage(30000, 150, 0);
public int firstSmeltTime;
public int secondSmeltTime;
2016-09-12 20:45:29 +02:00
public boolean isAutoSplit;
2015-10-03 10:16:18 +02:00
private int lastEnergy;
private int lastFirstSmelt;
private int lastSecondSmelt;
private boolean lastAutoSplit;
private boolean lastSmelted;
2024-03-02 21:23:08 +01:00
public TileEntityPoweredFurnace(BlockPos pos, BlockState state) {
super(ActuallyBlocks.POWERED_FURNACE.getTileEntityType(), pos, state, 4);
}
2018-08-10 05:04:07 +02:00
public static void autoSplit(ItemStackHandlerAA inv, int slot1, int slot2) {
ItemStack first = inv.getStackInSlot(slot1);
ItemStack second = inv.getStackInSlot(slot2);
2016-09-12 20:45:29 +02:00
2022-10-19 19:15:46 +02:00
if (!first.isEmpty() || !second.isEmpty()) {
2022-06-24 21:38:07 +02:00
ItemStack toSplit = ItemStack.EMPTY;
2022-10-19 19:15:46 +02:00
if (first.isEmpty() && !second.isEmpty() && second.getCount() > 1) {
2016-09-12 20:45:29 +02:00
toSplit = second;
2022-10-19 19:15:46 +02:00
} else if (second.isEmpty() && !first.isEmpty() && first.getCount() > 1) {
2016-09-12 20:45:29 +02:00
toSplit = first;
2018-08-10 05:04:07 +02:00
} else if (ItemUtil.canBeStacked(first, second)) {
if (first.getCount() < first.getMaxStackSize() || second.getCount() < second.getMaxStackSize()) {
2019-02-27 19:53:05 +01:00
if (!(first.getCount() <= second.getCount() + 1 && first.getCount() >= second.getCount() - 1 || second.getCount() <= first.getCount() + 1 && second.getCount() >= first.getCount() - 1)) {
2016-09-12 20:45:29 +02:00
toSplit = first;
toSplit.grow(second.getCount());
2016-09-12 20:45:29 +02:00
}
}
}
2022-10-19 19:15:46 +02:00
if (!toSplit.isEmpty()) {
2016-09-12 20:45:29 +02:00
ItemStack splitFirst = toSplit.copy();
ItemStack secondSplit = splitFirst.split(splitFirst.getCount() / 2);
inv.setStackInSlot(slot1, splitFirst);
inv.setStackInSlot(slot2, secondSplit);
2016-09-12 20:45:29 +02:00
}
}
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
compound.putInt("FirstSmeltTime", this.firstSmeltTime);
compound.putInt("SecondSmeltTime", this.secondSmeltTime);
compound.putBoolean("IsAutoSplit", this.isAutoSplit);
}
this.storage.writeToNBT(compound);
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
this.firstSmeltTime = compound.getInt("FirstSmeltTime");
this.secondSmeltTime = compound.getInt("SecondSmeltTime");
this.isAutoSplit = compound.getBoolean("IsAutoSplit");
}
this.storage.readFromNBT(compound);
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityPoweredFurnace tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityPoweredFurnace tile) {
tile.serverTick();
if (tile.isAutoSplit) {
autoSplit(tile.inv, SLOT_INPUT_1, SLOT_INPUT_2);
}
2024-03-02 21:23:08 +01:00
//TODO all tile logic needs redone someday
boolean smelted = false;
2024-03-02 21:23:08 +01:00
boolean canSmeltOnFirst = tile.canSmeltOn(SLOT_INPUT_1, SLOT_OUTPUT_1);
boolean canSmeltOnSecond = tile.canSmeltOn(SLOT_INPUT_2, SLOT_OUTPUT_2);
2018-08-10 05:04:07 +02:00
if (canSmeltOnFirst) {
2024-03-02 21:23:08 +01:00
if (tile.storage.getEnergyStored() >= ENERGY_USE) {
tile.firstSmeltTime++;
if (tile.firstSmeltTime >= SMELT_TIME) {
tile.finishBurning(SLOT_INPUT_1, SLOT_OUTPUT_1);
tile.firstSmeltTime = 0;
}
2024-03-02 21:23:08 +01:00
tile.storage.extractEnergyInternal(ENERGY_USE, false);
}
smelted = true;
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.firstSmeltTime = 0;
2015-10-02 16:48:01 +02:00
}
2018-08-10 05:04:07 +02:00
if (canSmeltOnSecond) {
2024-03-02 21:23:08 +01:00
if (tile.storage.getEnergyStored() >= ENERGY_USE) {
tile.secondSmeltTime++;
if (tile.secondSmeltTime >= SMELT_TIME) {
tile.finishBurning(SLOT_INPUT_2, SLOT_OUTPUT_2);
tile.secondSmeltTime = 0;
}
2024-03-02 21:23:08 +01:00
tile.storage.extractEnergyInternal(ENERGY_USE, false);
}
smelted = true;
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.secondSmeltTime = 0;
2015-10-02 16:48:01 +02:00
}
2024-03-02 21:23:08 +01:00
boolean current = state.getValue(BlockStateProperties.LIT);
2019-03-03 23:09:59 +01:00
boolean changeTo = current;
2024-03-02 21:23:08 +01:00
if (tile.lastSmelted != smelted) {
2021-02-26 22:15:48 +01:00
changeTo = smelted;
}
2024-03-02 21:23:08 +01:00
if (tile.isRedstonePowered) {
2021-02-26 22:15:48 +01:00
changeTo = true;
}
2024-03-02 21:23:08 +01:00
if (!smelted && !tile.isRedstonePowered) {
2021-02-26 22:15:48 +01:00
changeTo = false;
}
2019-03-03 23:09:59 +01:00
if (changeTo != current) {
2024-03-09 15:44:35 +01:00
level.setBlock(pos, state.setValue(BlockStateProperties.LIT, changeTo), Block.UPDATE_ALL);
}
2024-03-02 21:23:08 +01:00
tile.lastSmelted = smelted;
2019-03-03 23:09:59 +01:00
2024-03-02 21:23:08 +01:00
if ((tile.lastEnergy != tile.storage.getEnergyStored() || tile.lastFirstSmelt != tile.firstSmeltTime || tile.lastSecondSmelt != tile.secondSmeltTime || tile.isAutoSplit != tile.lastAutoSplit) && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.storage.getEnergyStored();
tile.lastFirstSmelt = tile.firstSmeltTime;
tile.lastAutoSplit = tile.isAutoSplit;
tile.lastSecondSmelt = tile.secondSmeltTime;
}
}
}
public boolean validInput(ItemStack stack) {
return getOutputForInput(stack).isPresent();
}
public Optional<ItemStack> getOutputForInput(ItemStack stack) {
2024-03-04 21:38:02 +01:00
return level.getServer().getRecipeManager().getRecipeFor(RecipeType.SMELTING, new SingleItem(stack), level).map(recipe -> recipe.value().getResultItem(this.level.registryAccess()));
}
2024-03-04 21:38:02 +01:00
public Optional<RecipeHolder<SmeltingRecipe>> getRecipeForInput(ItemStack stack) {
2024-03-02 21:23:08 +01:00
return level.getServer().getRecipeManager().getRecipeFor(RecipeType.SMELTING, new SingleItem(stack), level);
2022-10-19 19:15:46 +02:00
}
2016-02-01 17:49:55 +01:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !automation || (slot == SLOT_INPUT_1 || slot == SLOT_INPUT_2) && validInput(stack);
2018-08-10 05:04:07 +02:00
}
@Override
public IRemover getRemover() {
2019-02-27 19:53:05 +01:00
return (slot, automation) -> !automation || slot == SLOT_OUTPUT_1 || slot == SLOT_OUTPUT_2;
2016-02-01 17:49:55 +01:00
}
2018-08-10 05:04:07 +02:00
public boolean canSmeltOn(int theInput, int theOutput) {
ItemStack input = this.inv.getStackInSlot(theInput);
ItemStack output = this.inv.getStackInSlot(theOutput);
if (!input.isEmpty()) {
2024-03-04 21:38:02 +01:00
Optional<RecipeHolder<SmeltingRecipe>> recipe = getRecipeForInput(input);
return recipe.map($ -> output.isEmpty() || ItemStack.isSameItem(output, $.value().getResultItem(this.level.registryAccess())) && output.getCount() <= output.getMaxStackSize() - $.value().getResultItem(this.level.registryAccess()).getCount()).orElse(false);
}
return false;
}
2018-08-10 05:04:07 +02:00
public void finishBurning(int theInput, int theOutput) {
ItemStack output = getOutputForInput(inv.getStackInSlot(theInput)).orElse(ItemStack.EMPTY);
if (inv.getStackInSlot(theOutput).isEmpty()) {
this.inv.setStackInSlot(theOutput, output.copy());
2018-08-10 05:04:07 +02:00
} else if (this.inv.getStackInSlot(theOutput).getItem() == output.getItem()) {
this.inv.getStackInSlot(theOutput).grow(output.getCount());
2015-10-02 16:48:01 +02:00
}
this.inv.getStackInSlot(theInput).shrink(1);
}
2018-08-10 05:04:07 +02:00
public int getFirstTimeToScale(int i) {
return this.firstSmeltTime * i / SMELT_TIME;
}
2018-08-10 05:04:07 +02:00
public int getSecondTimeToScale(int i) {
return this.secondSmeltTime * i / SMELT_TIME;
}
2015-04-02 12:06:42 +02:00
@Override
2024-03-02 21:23:08 +01:00
public void onButtonPressed(int buttonID, Player player) {
2018-08-10 05:04:07 +02:00
if (buttonID == 0) {
this.isAutoSplit = !this.isAutoSplit;
2021-11-13 18:02:42 +01:00
this.setChanged();
}
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
2024-03-04 21:38:02 +01:00
return this.storage;
2016-11-26 08:58:42 +01:00
}
2022-01-02 17:58:59 +01:00
@Nonnull
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.translatable("container.actuallyadditions.powered_furnace");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, @Nonnull Inventory playerInventory, @Nonnull Player player) {
return new ContainerFurnaceDouble(windowId, playerInventory, this);
}
}