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

237 lines
8.2 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;
import de.ellpeck.actuallyadditions.api.recipe.OilGenRecipe;
2018-04-14 19:54:08 +02:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntListValues;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerOilGenerator;
import de.ellpeck.actuallyadditions.mod.util.Util;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
2021-02-27 16:33:00 +01:00
import net.minecraft.fluid.Fluid;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fluids.FluidStack;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.IFluidHandler;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.fluids.capability.templates.FluidTank;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2015-05-20 22:39:43 +02:00
public class TileEntityOilGenerator extends TileEntityBase implements ISharingEnergyProvider, ISharingFluidHandler, INamedContainerProvider {
2015-05-20 22:39:43 +02:00
2018-04-14 19:54:08 +02:00
int[] i = ConfigIntListValues.OIL_POWER.getValue();
2019-02-27 19:53:05 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(50000, 0, Math.max(Math.max(this.i[0], this.i[1]), Math.max(this.i[2], this.i[3])) + 20);
2021-02-27 16:33:00 +01:00
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
2019-05-02 09:10:29 +02:00
public final FluidTank tank = new FluidTank(2 * Util.BUCKET) {
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
}
@Override
2019-05-02 09:10:29 +02:00
public boolean canFillFluidType(FluidStack stack) {
2021-02-26 22:15:48 +01:00
Fluid fluid = stack == null
? null
: stack.getFluid();
return fluid != null && getRecipeForFluid(fluid.getName()) != null;
2016-06-05 02:16:52 +02:00
}
};
2021-02-27 16:33:00 +01:00
public final LazyOptional<IFluidHandler> lazyTank = LazyOptional.of(() -> this.tank);
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;
2015-05-20 22:39:43 +02:00
2019-05-02 09:10:29 +02:00
public TileEntityOilGenerator() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.OILGENERATOR_TILE.get());
2015-05-20 22:39:43 +02:00
}
2019-05-02 09:10:29 +02:00
private static OilGenRecipe getRecipeForFluid(String fluidName) {
if (fluidName != null) {
for (OilGenRecipe recipe : ActuallyAdditionsAPI.OIL_GENERATOR_RECIPES) {
2021-02-26 22:15:48 +01:00
if (recipe != null && fluidName.equals(recipe.fluidName)) {
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;
}
2019-05-02 09:10:29 +02:00
private OilGenRecipe getRecipeForCurrentFluid() {
FluidStack stack = this.tank.getFluid();
2019-05-02 09:10:29 +02:00
if (stack != null) {
Fluid fluid = stack.getFluid();
2021-02-26 22:15:48 +01:00
if (fluid != null) {
return getRecipeForFluid(fluid.getName());
}
}
return null;
}
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT 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);
}
this.storage.writeToNBT(compound);
this.tank.writeToNBT(compound);
super.writeSyncableNBT(compound, type);
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT 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");
}
this.storage.readFromNBT(compound);
this.tank.readFromNBT(compound);
super.readSyncableNBT(compound, type);
}
2015-05-20 22:39:43 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
2015-06-21 02:28:49 +02:00
boolean flag = this.currentBurnTime > 0;
2015-05-20 22:39:43 +02:00
2019-05-02 09:10:29 +02:00
if (this.currentBurnTime > 0 && this.currentEnergyProduce > 0) {
2015-05-20 22:39:43 +02:00
this.currentBurnTime--;
2016-11-28 12:51:45 +01:00
this.storage.receiveEnergyInternal(this.currentEnergyProduce, false);
2019-05-02 09:10:29 +02:00
} else if (!this.isRedstonePowered) {
int fuelUsed = 50;
OilGenRecipe recipe = this.getRecipeForCurrentFluid();
2019-05-02 09:10:29 +02:00
if (recipe != null && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored() && this.tank.getFluidAmount() >= fuelUsed) {
this.currentEnergyProduce = recipe.genAmount;
this.maxBurnTime = recipe.genTime;
this.currentBurnTime = this.maxBurnTime;
2021-02-27 16:33:00 +01:00
this.tank.drain(fuelUsed, IFluidHandler.FluidAction.EXECUTE);
2019-05-02 09:10:29 +02:00
} else {
this.currentEnergyProduce = 0;
this.currentBurnTime = 0;
this.maxBurnTime = 0;
}
2015-05-20 22:39:43 +02:00
}
2019-05-02 09:10:29 +02:00
if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) {
2016-11-28 13:02:42 +01:00
this.lastCompare = this.getComparatorStrength();
this.markDirty();
2015-06-21 02:28:49 +02:00
}
2019-05-02 09:10:29 +02:00
if ((this.storage.getEnergyStored() != this.lastEnergy || this.tank.getFluidAmount() != this.lastTank || this.lastBurnTime != this.currentBurnTime || this.lastEnergyProduce != this.currentEnergyProduce || this.lastMaxBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()) {
this.lastEnergy = this.storage.getEnergyStored();
this.lastTank = this.tank.getFluidAmount();
this.lastBurnTime = this.currentBurnTime;
this.lastEnergyProduce = this.currentEnergyProduce;
this.lastMaxBurnTime = this.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
2021-02-27 16:33:00 +01:00
public LazyOptional<IFluidHandler> getFluidHandler(Direction facing) {
return this.lazyTank;
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
2019-05-02 09:10:29 +02:00
public boolean canShareTo(TileEntity tile) {
return true;
}
2016-11-26 08:58:42 +01:00
@Override
2021-02-27 16:33:00 +01:00
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
return this.lazyEnergy;
2016-11-26 08:58:42 +01:00
}
@Override
public ITextComponent getDisplayName() {
return StringTextComponent.EMPTY;
}
@Nullable
@Override
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity player) {
return new ContainerOilGenerator(windowId, playerInventory, this);
}
2015-05-20 22:39:43 +02:00
}