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;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
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;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFurnaceDouble;
|
2016-08-12 18:14:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2018-06-23 01:39:30 +02:00
|
|
|
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;
|
2016-08-12 18:14:41 +02:00
|
|
|
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;
|
|
|
|
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;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2022-01-02 17:58:59 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2022-01-09 17:18:45 +01:00
|
|
|
import java.util.Optional;
|
2021-03-01 18:46:12 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class TileEntityPoweredFurnace extends TileEntityInventoryBase implements IButtonReactor, MenuProvider {
|
2015-03-07 02:23:31 +01:00
|
|
|
|
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;
|
2016-12-01 18:42:24 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(30000, 150, 0);
|
2021-02-27 13:24:45 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
2015-03-07 02:23:31 +01:00
|
|
|
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;
|
2015-07-02 04:21:21 +02:00
|
|
|
private int lastSecondSmelt;
|
2016-08-12 18:14:41 +02:00
|
|
|
private boolean lastAutoSplit;
|
2017-02-22 12:37:29 +01:00
|
|
|
private boolean lastSmelted;
|
2016-08-12 18:14:41 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityPoweredFurnace(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.POWERED_FURNACE.getTileEntityType(), pos, state, 4);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public static void autoSplit(ItemStackHandlerAA inv, int slot1, int slot2) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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;
|
2018-06-23 01:39:30 +02:00
|
|
|
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();
|
2021-02-27 13:24:45 +01:00
|
|
|
ItemStack secondSplit = splitFirst.split(splitFirst.getCount() / 2);
|
2018-06-23 01:39:30 +02:00
|
|
|
inv.setStackInSlot(slot1, splitFirst);
|
|
|
|
inv.setStackInSlot(slot2, secondSplit);
|
2016-09-12 20:45:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
compound.putInt("FirstSmeltTime", this.firstSmeltTime);
|
|
|
|
compound.putInt("SecondSmeltTime", this.secondSmeltTime);
|
|
|
|
compound.putBoolean("IsAutoSplit", this.isAutoSplit);
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2018-08-10 05:04:07 +02:00
|
|
|
if (type != NBTType.SAVE_BLOCK) {
|
2021-02-27 13:24:45 +01:00
|
|
|
this.firstSmeltTime = compound.getInt("FirstSmeltTime");
|
|
|
|
this.secondSmeltTime = compound.getInt("SecondSmeltTime");
|
2016-08-12 18:14:41 +02:00
|
|
|
this.isAutoSplit = compound.getBoolean("IsAutoSplit");
|
2016-07-02 15:01:46 +02:00
|
|
|
}
|
2016-02-01 20:32:49 +01:00
|
|
|
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);
|
2016-08-12 18:14:41 +02:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
//TODO all tile logic needs redone someday
|
2016-08-12 18:14:41 +02:00
|
|
|
|
2017-02-22 12:37:29 +01:00
|
|
|
boolean smelted = false;
|
2015-03-07 02:23:31 +01:00
|
|
|
|
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);
|
2015-03-07 02:23:31 +01:00
|
|
|
|
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;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.storage.extractEnergyInternal(ENERGY_USE, false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2017-02-22 12:37:29 +01:00
|
|
|
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
|
|
|
}
|
2015-03-07 02:23:31 +01: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;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.storage.extractEnergyInternal(ENERGY_USE, false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2017-02-22 12:37:29 +01:00
|
|
|
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
|
|
|
}
|
2015-03-07 02:23:31 +01: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-02 21:23:08 +01:00
|
|
|
tile.level.setBlock(tile.worldPosition, state.setValue(BlockStateProperties.LIT, changeTo), Block.UPDATE_ALL);
|
2017-02-22 12:37:29 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2015-07-02 04:21:21 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 17:18:45 +01:00
|
|
|
public boolean validInput(ItemStack stack) {
|
|
|
|
return getOutputForInput(stack).isPresent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<ItemStack> getOutputForInput(ItemStack stack) {
|
2024-03-03 01:20:53 +01:00
|
|
|
return level.getServer().getRecipeManager().getRecipeFor(RecipeType.SMELTING, new SingleItem(stack), level).map(recipe -> recipe.getResultItem(this.level.registryAccess()));
|
2022-01-09 17:18:45 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public Optional<SmeltingRecipe> getRecipeForInput(ItemStack stack) {
|
|
|
|
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() {
|
2022-01-09 17:18:45 +01:00
|
|
|
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) {
|
2022-01-09 17:18:45 +01:00
|
|
|
ItemStack input = this.inv.getStackInSlot(theInput);
|
|
|
|
ItemStack output = this.inv.getStackInSlot(theOutput);
|
|
|
|
if (!input.isEmpty()) {
|
2024-03-02 21:23:08 +01:00
|
|
|
Optional<SmeltingRecipe> recipe = getRecipeForInput(input);
|
2024-03-03 01:20:53 +01:00
|
|
|
return recipe.map($ -> output.isEmpty() || ItemStack.isSameItem(output, $.getResultItem(this.level.registryAccess())) && output.getCount() <= output.getMaxStackSize() - $.getResultItem(this.level.registryAccess()).getCount()).orElse(false);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public void finishBurning(int theInput, int theOutput) {
|
2022-01-09 17:18:45 +01:00
|
|
|
ItemStack output = getOutputForInput(inv.getStackInSlot(theInput)).orElse(ItemStack.EMPTY);
|
|
|
|
if (inv.getStackInSlot(theOutput).isEmpty()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.setStackInSlot(theOutput, output.copy());
|
2018-08-10 05:04:07 +02:00
|
|
|
} else if (this.inv.getStackInSlot(theOutput).getItem() == output.getItem()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.getStackInSlot(theOutput).grow(output.getCount());
|
2015-10-02 16:48:01 +02:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
this.inv.getStackInSlot(theInput).shrink(1);
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getFirstTimeToScale(int i) {
|
|
|
|
return this.firstSmeltTime * i / SMELT_TIME;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public int getSecondTimeToScale(int i) {
|
|
|
|
return this.secondSmeltTime * i / SMELT_TIME;
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2015-04-02 12:06:42 +02:00
|
|
|
|
2016-08-12 18:14:41 +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) {
|
2016-08-12 18:14:41 +02:00
|
|
|
this.isAutoSplit = !this.isAutoSplit;
|
2021-11-13 18:02:42 +01:00
|
|
|
this.setChanged();
|
2016-08-12 18:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-27 13:24:45 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
2022-01-02 17:58:59 +01:00
|
|
|
@Nonnull
|
2021-03-01 18:46:12 +01:00
|
|
|
@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");
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, @Nonnull Inventory playerInventory, @Nonnull Player player) {
|
2021-03-01 18:46:12 +01:00
|
|
|
return new ContainerFurnaceDouble(windowId, playerInventory, this);
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|