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

215 lines
7.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityCoalGenerator.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-05-20 22:39:43 +02:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2021-08-31 00:44:39 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.crafting.SolidFuelRecipe;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerCoalGenerator;
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.StackUtil;
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.resources.ResourceLocation;
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 20:21:48 +01:00
import net.minecraft.world.item.crafting.RecipeHolder;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.neoforge.common.CommonHooks;
import net.neoforged.neoforge.energy.IEnergyStorage;
2015-05-20 22:39:43 +02:00
import javax.annotation.Nullable;
2024-03-02 21:23:08 +01:00
public class TileEntityCoalGenerator extends TileEntityInventoryBase implements MenuProvider, ISharingEnergyProvider, IEnergyDisplay {
2015-05-20 22:39:43 +02:00
2019-02-27 19:53:05 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 0, 80);
public int maxBurnTime;
public int currentBurnTime;
private int lastEnergy;
private int lastBurnTime;
private int lastCurrentBurnTime;
private int lastCompare;
2024-03-04 20:21:48 +01:00
private RecipeHolder<SolidFuelRecipe> currentRecipe = null;
2019-02-27 19:53:05 +01:00
2024-03-02 21:23:08 +01:00
public TileEntityCoalGenerator(BlockPos pos, BlockState state) {
super(ActuallyBlocks.COAL_GENERATOR.getTileEntityType(), pos, state, 1);
2019-02-27 19:53:05 +01:00
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-02-27 19:53:05 +01:00
public int getEnergyScaled(int i) {
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-02-27 19:53:05 +01:00
public int getBurningScaled(int i) {
return this.currentBurnTime * i / this.maxBurnTime;
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2019-02-27 19:53:05 +01:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("BurnTime", this.currentBurnTime);
compound.putInt("MaxBurnTime", this.maxBurnTime);
if (currentRecipe != null)
2024-03-04 20:21:48 +01:00
compound.putString("currentRecipe", currentRecipe.id().toString());
2019-02-27 19:53:05 +01:00
}
this.storage.writeToNBT(compound);
super.writeSyncableNBT(compound, type);
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2019-02-27 19:53:05 +01:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.currentBurnTime = compound.getInt("BurnTime");
this.maxBurnTime = compound.getInt("MaxBurnTime");
if (compound.contains("currentRecipe")) {
ResourceLocation id = new ResourceLocation(compound.getString("currentRecipe"));
2024-03-04 20:21:48 +01:00
for (RecipeHolder<SolidFuelRecipe> fuelRecipe : ActuallyAdditionsAPI.SOLID_FUEL_RECIPES) {
if (fuelRecipe.id().equals(id)) {
this.currentRecipe = fuelRecipe;
break;
}
}
}
2019-02-27 19:53:05 +01:00
}
this.storage.readFromNBT(compound);
super.readSyncableNBT(compound, type);
}
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 TileEntityCoalGenerator tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityCoalGenerator tile) {
tile.serverTick();
boolean flag = tile.currentBurnTime > 0;
if (tile.currentBurnTime > 0 && tile.currentRecipe != null) {
tile.currentBurnTime--;
2024-03-04 20:21:48 +01:00
int produce = tile.currentRecipe.value().getTotalEnergy() / tile.currentRecipe.value().getBurnTime();
if (produce > 0) {
2024-03-02 21:23:08 +01:00
tile.storage.receiveEnergyInternal(produce, false);
}
2019-02-27 19:53:05 +01:00
}
2024-03-02 21:23:08 +01:00
if (!tile.isRedstonePowered && tile.currentBurnTime <= 0 && tile.storage.getEnergyStored() < tile.storage.getMaxEnergyStored()) {
ItemStack stack = tile.inv.getStackInSlot(0);
if (!stack.isEmpty()) {
2024-03-04 20:21:48 +01:00
ActuallyAdditionsAPI.SOLID_FUEL_RECIPES.stream().filter(r -> r.value().matches(stack)).findFirst().ifPresent(recipe -> {
2024-03-02 21:23:08 +01:00
tile.currentRecipe = recipe;
2024-03-04 20:21:48 +01:00
tile.maxBurnTime = recipe.value().getBurnTime();
2024-03-02 21:23:08 +01:00
tile.currentBurnTime = tile.maxBurnTime;
tile.inv.setStackInSlot(0, StackUtil.shrinkForContainer(stack, 1));
2022-01-02 17:58:59 +01:00
});
} else
2024-03-02 21:23:08 +01:00
tile.currentRecipe = null;
2019-02-27 19:53:05 +01:00
}
2024-03-02 21:23:08 +01:00
if (flag != tile.currentBurnTime > 0 || tile.lastCompare != tile.getComparatorStrength()) {
tile.lastCompare = tile.getComparatorStrength();
tile.setChanged();
2019-02-27 19:53:05 +01:00
}
2024-03-02 21:23:08 +01:00
if ((tile.storage.getEnergyStored() != tile.lastEnergy || tile.currentBurnTime != tile.lastCurrentBurnTime || tile.lastBurnTime != tile.maxBurnTime) && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.storage.getEnergyStored();
tile.lastCurrentBurnTime = tile.currentBurnTime;
tile.lastBurnTime = tile.currentBurnTime;
2019-02-27 19:53:05 +01:00
}
}
}
@Override
public int getComparatorStrength() {
float calc = (float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored() * 15F;
return (int) calc;
}
@Override
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> {
2024-03-04 20:21:48 +01:00
for (RecipeHolder<SolidFuelRecipe> recipe : ActuallyAdditionsAPI.SOLID_FUEL_RECIPES) {
if (recipe.value().matches(stack))return true;
}
return false;
};
2019-02-27 19:53:05 +01:00
}
@Override
public IRemover getRemover() {
return (slot, automation) -> {
2021-02-26 22:15:48 +01:00
if (!automation) {
return true;
}
2024-03-04 20:21:48 +01:00
return CommonHooks.getBurnTime(this.inv.getStackInSlot(0), null) <= 0;
2019-02-27 19:53:05 +01:00
};
}
@Override
public int getEnergyToSplitShare() {
return this.storage.getEnergyStored();
}
@Override
public boolean doesShareEnergy() {
return true;
}
@Override
public Direction[] getEnergyShareSides() {
return Direction.values();
2019-02-27 19:53:05 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public boolean canShareTo(BlockEntity tile) {
2019-02-27 19:53:05 +01:00
return true;
}
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2019-02-27 19:53:05 +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.coalGenerator");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
return new ContainerCoalGenerator(windowId, playerInventory, this);
}
2021-12-29 03:40:18 +01:00
@Override
public CustomEnergyStorage getEnergyStorage() {
return storage;
}
@Override
public boolean needsHoldShift() {
return false;
}
2015-05-20 22:39:43 +02:00
}