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

135 lines
4.9 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.inventory.ContainerData;
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.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2020-01-26 17:59:58 +01:00
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
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) {
super(ModTileEntities.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;
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;
2021-12-06 14:38:12 +01:00
Recipe<?> recipe = this.level.getRecipeManager().getRecipeFor(BlockEntityFurnaceHeater.getRecipeType(tile), tile, this.level).orElse(null);
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()) {
2021-12-15 16:30:22 +01:00
var result = recipe.getResultItem();
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);
2021-12-04 15:40:09 +01:00
IAuraChunk.getAuraChunk(this.level, pos).drainAura(pos, 6500);
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.getItem().getTags().stream().anyMatch(t -> t.getPath().startsWith("ores/")))
return true;
}
}
return false;
}
2020-01-26 17:59:58 +01:00
@Override
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;
2021-12-15 16:30:22 +01:00
var handler = below.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, Direction.UP).orElse(null);
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() {
}
2020-01-26 02:20:08 +01:00
}