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

234 lines
8.4 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityOilGenerator.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-10-02 15:59:42 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2022-04-02 23:41:52 +02:00
import de.ellpeck.actuallyadditions.mod.config.CommonConfig;
import de.ellpeck.actuallyadditions.mod.crafting.LiquidFuelRecipe;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerOilGenerator;
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;
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.energy.IEnergyStorage;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.templates.FluidTank;
2021-02-27 16:33:00 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2015-05-20 22:39:43 +02:00
2024-03-02 21:23:08 +01:00
public class TileEntityOilGenerator extends TileEntityBase implements ISharingEnergyProvider, ISharingFluidHandler, MenuProvider {
2022-04-02 23:41:52 +02:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(50000, 0, CommonConfig.Machines.OIL_GENERATOR_TRANSFER.get());
2024-03-03 01:20:53 +01:00
public final FluidTank tank = new FluidTank(2 * FluidType.BUCKET_VOLUME, fluid -> getRecipeForFluid(fluid) != null) {
2021-02-27 16:33:00 +01:00
@Nonnull
@Override
public FluidStack drain(FluidStack resource, FluidAction action) {
return FluidStack.EMPTY;
}
@Nonnull
2016-06-05 02:16:52 +02:00
@Override
2021-02-27 16:33:00 +01:00
public FluidStack drain(int maxDrain, FluidAction action) {
return FluidStack.EMPTY;
2016-06-05 02:16:52 +02:00
}
};
public int currentEnergyProduce;
2015-05-20 22:39:43 +02:00
public int currentBurnTime;
public int maxBurnTime;
2015-10-03 10:16:18 +02:00
private int lastEnergy;
private int lastTank;
private int lastBurnTime;
private int lastMaxBurnTime;
private int lastEnergyProduce;
2016-11-28 13:02:42 +01:00
private int lastCompare;
2022-04-02 23:41:52 +02:00
public int fuelUsage;
2015-05-20 22:39:43 +02:00
2024-03-02 21:23:08 +01:00
public TileEntityOilGenerator(BlockPos pos, BlockState state) {
super(ActuallyBlocks.OIL_GENERATOR.getTileEntityType(), pos, state);
2015-05-20 22:39:43 +02:00
}
2024-03-04 20:21:48 +01:00
private static RecipeHolder<LiquidFuelRecipe> getRecipeForFluid(FluidStack fluid) {
2022-04-02 23:41:52 +02:00
if (fluid != null) {
2024-03-04 20:21:48 +01:00
for (RecipeHolder<LiquidFuelRecipe> recipe : ActuallyAdditionsAPI.LIQUID_FUEL_RECIPES) {
if (recipe != null && recipe.value().matches(fluid)) {
2021-02-26 22:15:48 +01:00
return recipe;
}
2016-11-26 21:32:27 +01:00
}
}
return null;
}
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2019-05-02 09:10:29 +02:00
public int getBurningScaled(int i) {
return this.currentBurnTime * i / this.maxBurnTime;
}
2024-03-04 20:21:48 +01:00
private RecipeHolder<LiquidFuelRecipe> getRecipeForCurrentFluid() {
FluidStack stack = this.tank.getFluid();
2022-04-02 23:41:52 +02:00
if (!stack.isEmpty()) {
return getRecipeForFluid(stack);
}
return null;
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("BurnTime", this.currentBurnTime);
compound.putInt("CurrentEnergy", this.currentEnergyProduce);
compound.putInt("MaxBurnTime", this.maxBurnTime);
2022-04-02 23:41:52 +02:00
compound.putInt("FuelUsage", this.fuelUsage);
}
this.storage.writeToNBT(compound);
this.tank.writeToNBT(compound);
super.writeSyncableNBT(compound, type);
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.currentBurnTime = compound.getInt("BurnTime");
this.currentEnergyProduce = compound.getInt("CurrentEnergy");
this.maxBurnTime = compound.getInt("MaxBurnTime");
2022-04-02 23:41:52 +02:00
this.fuelUsage = compound.getInt("FuelUsage");
}
this.storage.readFromNBT(compound);
this.tank.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 TileEntityOilGenerator tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityOilGenerator tile) {
tile.serverTick();
boolean flag = tile.currentBurnTime > 0;
2015-05-20 22:39:43 +02:00
2024-03-02 21:23:08 +01:00
if (tile.currentBurnTime > 0 && tile.currentEnergyProduce > 0) {
tile.currentBurnTime--;
2016-11-28 12:51:45 +01:00
2024-03-02 21:23:08 +01:00
tile.storage.receiveEnergyInternal(tile.currentEnergyProduce, false);
} else if (!tile.isRedstonePowered) {
2024-03-04 20:21:48 +01:00
RecipeHolder<LiquidFuelRecipe> recipeHolder = tile.getRecipeForCurrentFluid();
2024-03-04 21:38:02 +01:00
if (recipeHolder != null && tile.storage.getEnergyStored() < tile.storage.getMaxEnergyStored() && tile.tank.getFluidAmount() >= recipeHolder.value().getFuelAmount()) {
2024-03-04 20:21:48 +01:00
LiquidFuelRecipe recipe = recipeHolder.value();
2024-03-02 21:23:08 +01:00
tile.fuelUsage = recipe.getFuelAmount();
tile.currentEnergyProduce = recipe.getTotalEnergy() / recipe.getBurnTime();
tile.maxBurnTime = recipe.getBurnTime();
tile.currentBurnTime = tile.maxBurnTime;
2024-03-02 21:23:08 +01:00
tile.tank.getFluid().shrink(tile.fuelUsage);
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.currentEnergyProduce = 0;
tile.currentBurnTime = 0;
tile.maxBurnTime = 0;
tile.fuelUsage = 0;
}
2015-05-20 22:39:43 +02:00
}
2024-03-02 21:23:08 +01:00
if (flag != tile.currentBurnTime > 0 || tile.lastCompare != tile.getComparatorStrength()) {
tile.lastCompare = tile.getComparatorStrength();
2016-11-28 13:02:42 +01:00
2024-03-02 21:23:08 +01:00
tile.setChanged();
2015-06-21 02:28:49 +02:00
}
2024-03-02 21:23:08 +01:00
if ((tile.storage.getEnergyStored() != tile.lastEnergy || tile.tank.getFluidAmount() != tile.lastTank || tile.lastBurnTime != tile.currentBurnTime || tile.lastEnergyProduce != tile.currentEnergyProduce || tile.lastMaxBurnTime != tile.maxBurnTime) && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.storage.getEnergyStored();
tile.lastTank = tile.tank.getFluidAmount();
tile.lastBurnTime = tile.currentBurnTime;
tile.lastEnergyProduce = tile.currentEnergyProduce;
tile.lastMaxBurnTime = tile.maxBurnTime;
}
}
}
2016-11-28 12:51:45 +01:00
@Override
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
float calc = (float) this.storage.getEnergyStored() / (float) this.storage.getMaxEnergyStored() * 15F;
return (int) calc;
2016-11-28 12:51:45 +01:00
}
2015-05-20 22:39:43 +02:00
@Override
2024-03-04 20:21:48 +01:00
public IFluidHandler getFluidHandler(Direction facing) {
return this.tank;
2015-05-20 22:39:43 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public int getMaxFluidAmountToSplitShare() {
return 0;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareFluid() {
return false;
}
@Override
public Direction[] getFluidShareSides() {
return null;
}
@Override
2019-05-02 09:10:29 +02:00
public int getEnergyToSplitShare() {
return this.storage.getEnergyStored();
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesShareEnergy() {
return true;
}
@Override
public Direction[] getEnergyShareSides() {
return Direction.values();
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-02 21:23:08 +01:00
public boolean canShareTo(BlockEntity tile) {
return true;
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +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.oilGenerator");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
return new ContainerOilGenerator(windowId, playerInventory, this);
}
2015-05-20 22:39:43 +02:00
}