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;
|
2019-11-08 18:07:11 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
2021-12-19 18:27:43 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.SingleItem;
|
|
|
|
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;
|
2021-02-28 12:33:01 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.inventory.container.Container;
|
|
|
|
import net.minecraft.inventory.container.INamedContainerProvider;
|
2015-05-20 22:39:43 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-12-18 17:28:29 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2021-12-29 03:36:23 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2021-02-28 12:33:01 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
|
|
|
import net.minecraft.util.text.StringTextComponent;
|
2021-12-29 03:36:23 +01:00
|
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
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;
|
2021-12-19 18:27:43 +01:00
|
|
|
import net.minecraftforge.common.util.RecipeMatcher;
|
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;
|
|
|
|
|
2021-08-22 17:09:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
|
2021-12-19 18:27:43 +01:00
|
|
|
import net.minecraftforge.fml.server.ServerLifecycleHooks;
|
2021-08-22 17:09:06 +02:00
|
|
|
|
2021-12-29 03:40:18 +01:00
|
|
|
public class TileEntityCoalGenerator extends TileEntityInventoryBase implements INamedContainerProvider, 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
|
|
|
|
|
|
|
public TileEntityCoalGenerator() {
|
2021-08-31 00:44:39 +02:00
|
|
|
super(ActuallyBlocks.COAL_GENERATOR.getTileEntityType(), 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
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT 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
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity() {
|
|
|
|
super.updateEntity();
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
2019-02-27 19:53:05 +01:00
|
|
|
boolean flag = this.currentBurnTime > 0;
|
|
|
|
|
2021-12-19 18:27:43 +01:00
|
|
|
if (this.currentBurnTime > 0 && currentRecipe != null) {
|
2019-02-27 19:53:05 +01:00
|
|
|
this.currentBurnTime--;
|
2021-12-19 18:27:43 +01:00
|
|
|
int produce = currentRecipe.getTotalEnergy() / currentRecipe.getBurnTime();
|
2019-11-08 18:07:11 +01:00
|
|
|
if (produce > 0) {
|
|
|
|
this.storage.addEnergyRaw(produce);
|
|
|
|
}
|
2019-02-27 19:53:05 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 18:27:43 +01:00
|
|
|
if (!this.isRedstonePowered && this.currentBurnTime <= 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()) {
|
|
|
|
ItemStack stack = this.inv.getStackInSlot(0);
|
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
for (SolidFuelRecipe fuelRecipe : ActuallyAdditionsAPI.SOLID_FUEL_RECIPES) {
|
2021-12-30 18:30:01 +01:00
|
|
|
if (fuelRecipe.matches(stack)) {
|
2021-12-19 18:27:43 +01:00
|
|
|
this.currentRecipe = fuelRecipe;
|
|
|
|
this.maxBurnTime = fuelRecipe.getBurnTime();
|
|
|
|
this.currentBurnTime = this.maxBurnTime;
|
|
|
|
this.inv.setStackInSlot(0, StackUtil.shrinkForContainer(stack, 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 19:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flag != this.currentBurnTime > 0 || this.lastCompare != this.getComparatorStrength()) {
|
|
|
|
this.lastCompare = this.getComparatorStrength();
|
2021-08-22 17:09:06 +02:00
|
|
|
this.setChanged();
|
2019-02-27 19:53:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()) {
|
|
|
|
this.lastEnergy = this.storage.getEnergyStored();
|
|
|
|
this.lastCurrentBurnTime = this.currentBurnTime;
|
|
|
|
this.lastBurnTime = this.currentBurnTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
2021-02-27 16:33:00 +01:00
|
|
|
return ForgeHooks.getBurnTime(this.inv.getStackInSlot(0)) <= 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
|
|
|
|
public boolean canShareTo(TileEntity tile) {
|
|
|
|
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
|
|
|
|
public ITextComponent getDisplayName() {
|
2021-12-29 03:36:23 +01:00
|
|
|
return new TranslationTextComponent("container.actuallyadditions.coalGenerator");
|
2021-02-28 12:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity 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
|
|
|
}
|