NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityFurnaceHeater.java

130 lines
5.6 KiB
Java
Raw Normal View History

2018-10-26 15:01:48 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
2020-01-23 20:57:56 +01:00
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.NaturesAura;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.blocks.BlockFurnaceHeater;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream;
2020-01-23 20:57:56 +01:00
import net.minecraft.block.AbstractFurnaceBlock;
2018-10-26 15:01:48 +02:00
import net.minecraft.item.ItemStack;
2020-01-23 20:57:56 +01:00
import net.minecraft.item.crafting.AbstractCookingRecipe;
import net.minecraft.item.crafting.IRecipeType;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2020-01-23 20:57:56 +01:00
import net.minecraft.tileentity.*;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2020-01-23 20:57:56 +01:00
import net.minecraft.util.IIntArray;
2018-10-26 15:01:48 +02:00
import net.minecraft.util.math.BlockPos;
2020-01-23 20:57:56 +01:00
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import java.lang.reflect.Field;
2018-10-26 15:01:48 +02:00
2021-12-04 15:40:09 +01:00
public class BlockEntityFurnaceHeater extends BlockEntityImpl implements ITickableBlockEntity {
2018-10-26 15:01:48 +02:00
2021-12-04 15:40:09 +01:00
private static final Field FURNACE_DATA_FIELD = ObfuscationReflectionHelper.findField(AbstractFurnaceBlockEntity.class, "field_214013_b");
2018-10-26 15:01:48 +02:00
public boolean isActive;
2021-12-04 15:40:09 +01:00
public BlockEntityFurnaceHeater() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.FURNACE_HEATER);
2020-01-21 21:04:44 +01:00
}
2021-12-04 15:40:09 +01:00
public static IIntArray getFurnaceData(AbstractFurnaceBlockEntity tile) {
2020-02-07 15:22:30 +01:00
try {
return (IIntArray) FURNACE_DATA_FIELD.get(tile);
} catch (IllegalAccessException e) {
NaturesAura.LOGGER.fatal("Couldn't reflect furnace field", e);
return null;
}
}
2021-12-04 15:40:09 +01:00
public static IRecipeType<? extends AbstractCookingRecipe> getRecipeType(AbstractFurnaceBlockEntity furnace) {
if (furnace instanceof BlastFurnaceBlockEntity) {
2020-02-07 15:22:30 +01:00
return IRecipeType.BLASTING;
2021-12-04 15:40:09 +01:00
} else if (furnace instanceof SmokerBlockEntity) {
2020-02-07 15:22:30 +01:00
return IRecipeType.SMOKING;
} else {
return IRecipeType.SMELTING;
}
}
2018-10-26 15:01:48 +02:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide && this.level.getGameTime() % 5 == 0) {
2018-10-26 15:01:48 +02:00
boolean did = false;
2021-12-04 15:40:09 +01:00
Direction facing = this.level.getBlockState(this.worldPosition).get(BlockFurnaceHeater.FACING);
BlockPos tilePos = this.worldPosition.offset(facing.getOpposite());
BlockEntity tile = this.level.getBlockEntity(tilePos);
if (tile instanceof AbstractFurnaceBlockEntity) {
AbstractFurnaceBlockEntity furnace = (AbstractFurnaceBlockEntity) tile;
2020-01-23 20:57:56 +01:00
if (this.isReady(furnace)) {
2020-01-26 02:20:08 +01:00
IIntArray data = getFurnaceData(furnace);
2020-01-23 20:57:56 +01:00
int burnTime = data.get(0);
if (burnTime <= 0)
2021-12-04 15:40:09 +01:00
this.level.setBlockState(tilePos, this.level.getBlockState(tilePos).with(AbstractFurnaceBlock.LIT, true));
2020-01-23 20:57:56 +01:00
2020-01-26 15:52:16 +01:00
data.set(0, 200);
2018-10-26 15:01:48 +02:00
//if set higher than 199, it'll never finish because the furnace does ++ and then ==
2020-01-26 15:52:16 +01:00
data.set(2, Math.min(data.get(3) - 1, data.get(2) + 5));
2018-10-26 15:01:48 +02:00
2021-12-04 15:40:09 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 20, this.worldPosition);
IAuraChunk chunk = IAuraChunk.getAuraChunk(this.level, spot);
2020-01-26 15:52:16 +01:00
chunk.drainAura(spot, MathHelper.ceil((200 - burnTime) * 16.6F));
2018-10-26 15:01:48 +02:00
did = true;
2021-12-04 15:40:09 +01:00
if (this.level.getGameTime() % 15 == 0) {
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticleStream(
this.worldPosition.getX() + (float) this.level.rand.nextGaussian() * 5F,
this.worldPosition.getY() + 1 + this.level.rand.nextFloat() * 5F,
this.worldPosition.getZ() + (float) this.level.rand.nextGaussian() * 5F,
tilePos.getX() + this.level.rand.nextFloat(),
tilePos.getY() + this.level.rand.nextFloat(),
tilePos.getZ() + this.level.rand.nextFloat(),
this.level.rand.nextFloat() * 0.07F + 0.07F, IAuraType.forLevel(this.level).getColor(), this.level.rand.nextFloat() + 0.5F
2020-01-22 23:21:52 +01:00
));
}
2018-10-26 15:01:48 +02:00
}
}
2018-11-04 16:38:09 +01:00
if (did != this.isActive) {
2018-10-26 15:01:48 +02:00
this.isActive = did;
this.sendToClients();
}
}
}
2021-12-04 15:40:09 +01:00
private boolean isReady(AbstractFurnaceBlockEntity furnace) {
2018-10-26 15:01:48 +02:00
if (!furnace.getStackInSlot(1).isEmpty())
return false;
ItemStack input = furnace.getStackInSlot(0);
if (!input.isEmpty()) {
2021-12-04 15:40:09 +01:00
AbstractCookingRecipe recipe = this.level.getRecipeManager().getRecipe(getRecipeType(furnace), furnace, this.level).orElse(null);
2020-01-23 20:57:56 +01:00
if (recipe == null)
return false;
ItemStack output = recipe.getRecipeOutput();
ItemStack currOutput = furnace.getStackInSlot(2);
return currOutput.isEmpty() || Helper.areItemsEqual(currOutput, output, true) && currOutput.getCount() + output.getCount() <= output.getMaxStackSize();
2018-10-26 15:01:48 +02:00
} else
return false;
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-11-04 16:38:09 +01:00
super.writeNBT(compound, type);
2018-10-26 15:01:48 +02:00
2018-11-04 16:38:09 +01:00
if (type == SaveType.SYNC)
2020-01-21 21:04:44 +01:00
compound.putBoolean("active", this.isActive);
2018-10-26 15:01:48 +02:00
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-11-04 16:38:09 +01:00
super.readNBT(compound, type);
2018-10-26 15:01:48 +02:00
2018-11-04 16:38:09 +01:00
if (type == SaveType.SYNC)
2018-10-26 15:01:48 +02:00
this.isActive = compound.getBoolean("active");
}
}