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

142 lines
5 KiB
Java
Raw Normal View History

2020-01-26 02:20:08 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
2020-01-26 15:52:16 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-06 14:38:12 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.block.entity.BlastFurnaceBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-10 11:29:12 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
2020-01-26 17:59:58 +01:00
import javax.annotation.Nonnull;
import java.util.List;
2020-01-26 02:20:08 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEntityBlastFurnaceBooster extends BlockEntityImpl implements ITickableBlockEntity {
2020-01-26 02:20:08 +01:00
2021-12-06 14:38:12 +01:00
public BlockEntityBlastFurnaceBooster(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.BLAST_FURNACE_BOOSTER, pos, state);
2020-01-26 02:20:08 +01:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide)
2020-01-26 02:20:08 +01:00
return;
2023-02-16 19:03:26 +01:00
var toUse = 6500;
if (!this.canUseRightNow(toUse))
return;
2021-12-15 16:30:22 +01:00
var below = this.level.getBlockEntity(this.worldPosition.below());
2021-12-06 14:38:12 +01:00
if (!(below instanceof BlastFurnaceBlockEntity tile))
2020-01-26 02:20:08 +01:00
return;
2024-03-10 11:29:12 +01:00
Recipe<?> recipe = this.level.getRecipeManager().getRecipeFor(BlockEntityFurnaceHeater.getRecipeType(tile), tile, this.level).orElse(null).value();
2020-01-26 02:20:08 +01:00
if (recipe == null)
return;
if (!this.isApplicable(recipe.getIngredients()))
return;
2020-01-26 02:20:08 +01:00
2021-12-15 16:30:22 +01:00
var data = BlockEntityFurnaceHeater.getFurnaceData(tile);
var doneDiff = data.get(3) - data.get(2);
2020-01-26 15:52:16 +01:00
if (doneDiff > 1)
2020-01-26 02:20:08 +01:00
return;
2021-12-06 14:38:12 +01:00
if (this.level.random.nextFloat() > 0.45F) {
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.BLAST_FURNACE_BOOSTER, 0));
2020-01-26 02:20:08 +01:00
return;
2020-01-26 15:52:16 +01:00
}
2020-01-26 02:20:08 +01:00
2021-12-15 16:30:22 +01:00
var output = tile.getItem(2);
2020-01-26 02:20:08 +01:00
if (output.getCount() >= output.getMaxStackSize())
return;
2020-01-26 15:52:16 +01:00
2020-01-26 02:20:08 +01:00
if (output.isEmpty()) {
2023-07-08 12:32:27 +02:00
var result = recipe.getResultItem(this.level.registryAccess());
2021-12-06 14:38:12 +01:00
tile.setItem(2, result.copy());
2020-01-26 02:20:08 +01:00
} else {
output.grow(1);
}
2020-01-26 15:52:16 +01:00
2021-12-15 16:30:22 +01:00
var pos = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 30, this.worldPosition);
2023-02-16 19:03:26 +01:00
IAuraChunk.getAuraChunk(this.level, pos).drainAura(pos, toUse);
2020-01-26 15:52:16 +01:00
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.BLAST_FURNACE_BOOSTER, 1));
2020-01-26 02:20:08 +01:00
}
2020-01-26 17:59:58 +01:00
private boolean isApplicable(List<Ingredient> ingredients) {
2021-12-15 16:30:22 +01:00
for (var ing : ingredients) {
for (var stack : ing.getItems()) {
if (stack.is(Tags.Items.ORES) || stack.is(Tags.Items.RAW_MATERIALS))
return true;
}
}
return false;
}
2020-10-19 21:26:32 +02:00
public IItemHandlerModifiable getItemHandler() {
2021-12-15 16:30:22 +01:00
var below = this.level.getBlockEntity(this.worldPosition.below());
2021-12-04 15:40:09 +01:00
if (!(below instanceof BlastFurnaceBlockEntity))
2020-01-26 17:59:58 +01:00
return null;
2024-03-10 11:29:12 +01:00
var handler = this.level.getCapability(Capabilities.ItemHandler.BLOCK, below.getBlockPos(), below.getBlockState(), below, Direction.UP);
2020-01-26 17:59:58 +01:00
if (handler == null)
return null;
return new IItemHandlerModifiable() {
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
if (handler instanceof IItemHandlerModifiable)
((IItemHandlerModifiable) handler).setStackInSlot(0, stack);
}
@Override
public int getSlots() {
return 1;
}
@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
return handler.getStackInSlot(0);
}
@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
return handler.insertItem(0, stack, simulate);
}
@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
return handler.extractItem(0, amount, simulate);
}
@Override
public int getSlotLimit(int slot) {
return handler.getSlotLimit(0);
}
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return handler.isItemValid(0, stack);
}
};
}
@Override
public void dropInventory() {
}
2023-02-16 19:03:26 +01:00
@Override
public boolean allowsLowerLimiter() {
return true;
}
2024-03-10 11:29:12 +01:00
2020-01-26 02:20:08 +01:00
}