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
|
|
|
|
2021-12-19 18:27:43 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2021-08-31 00:44:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2021-12-19 18:27:43 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.SolidFuelRecipe;
|
2021-02-28 12:33:01 +01:00
|
|
|
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;
|
2018-09-27 21:17:23 +02:00
|
|
|
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.network.chat.TranslatableComponent;
|
|
|
|
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;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.common.ForgeHooks;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2015-05-20 22:39:43 +02:00
|
|
|
|
2021-02-28 12:33:01 +01: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);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
2019-02-27 19:53:05 +01:00
|
|
|
public int maxBurnTime;
|
|
|
|
public int currentBurnTime;
|
|
|
|
private int lastEnergy;
|
|
|
|
private int lastBurnTime;
|
|
|
|
private int lastCurrentBurnTime;
|
|
|
|
private int lastCompare;
|
2021-12-19 18:27:43 +01:00
|
|
|
private 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);
|
2021-12-29 03:36:23 +01:00
|
|
|
if (currentRecipe != null)
|
|
|
|
compound.putString("currentRecipe", currentRecipe.getId().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");
|
2021-12-29 03:36:23 +01:00
|
|
|
if (compound.contains("currentRecipe")) {
|
|
|
|
ResourceLocation id = new ResourceLocation(compound.getString("currentRecipe"));
|
|
|
|
for (SolidFuelRecipe fuelRecipe : ActuallyAdditionsAPI.SOLID_FUEL_RECIPES) {
|
|
|
|
if (fuelRecipe.getId().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--;
|
|
|
|
int produce = tile.currentRecipe.getTotalEnergy() / tile.currentRecipe.getBurnTime();
|
2019-11-08 18:07:11 +01:00
|
|
|
if (produce > 0) {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.storage.receiveEnergyInternal(produce, false);
|
2019-11-08 18:07:11 +01:00
|
|
|
}
|
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);
|
2021-12-19 18:27:43 +01:00
|
|
|
if (!stack.isEmpty()) {
|
2022-01-02 17:58:59 +01:00
|
|
|
ActuallyAdditionsAPI.SOLID_FUEL_RECIPES.stream().filter(r -> r.matches(stack)).findFirst().ifPresent(recipe -> {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.currentRecipe = recipe;
|
|
|
|
tile.maxBurnTime = recipe.getBurnTime();
|
|
|
|
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() {
|
2021-12-29 03:36:23 +01:00
|
|
|
return (slot, stack, automation) -> {
|
|
|
|
for (SolidFuelRecipe recipe : ActuallyAdditionsAPI.SOLID_FUEL_RECIPES) {
|
2021-12-30 18:30:01 +01:00
|
|
|
if (recipe.matches(stack))return true;
|
2021-12-29 03:36:23 +01:00
|
|
|
}
|
|
|
|
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-02 21:23:08 +01:00
|
|
|
return ForgeHooks.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
|
2021-02-27 13:24:45 +01:00
|
|
|
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
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2019-02-27 19:53:05 +01:00
|
|
|
}
|
2021-02-28 12:33:01 +01:00
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public Component getDisplayName() {
|
|
|
|
return new TranslatableComponent("container.actuallyadditions.coalGenerator");
|
2021-02-28 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
|
2021-02-28 12:33:01 +01:00
|
|
|
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
|
|
|
}
|