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

205 lines
7.4 KiB
Java
Raw Normal View History

2020-10-17 21:13:45 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
2020-10-17 21:13:45 +02:00
import net.minecraft.tags.FluidTags;
2021-12-08 00:31:29 +01:00
import net.minecraft.util.Mth;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.AABB;
2020-10-17 21:13:45 +02:00
import net.minecraftforge.common.FarmlandWaterManager;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.ticket.AABBTicket;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
2021-12-04 15:40:09 +01:00
public class BlockEntitySpring extends BlockEntityImpl implements ITickableBlockEntity {
2020-10-17 21:13:45 +02:00
2020-10-19 21:51:26 +02:00
private final LazyOptional<IFluidHandler> tank = LazyOptional.of(InfiniteTank::new);
2020-10-17 21:13:45 +02:00
private AABBTicket waterTicket;
2021-12-08 00:31:29 +01:00
public BlockEntitySpring(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.SPRING, pos, state);
2020-10-17 21:13:45 +02:00
}
@Override
2021-12-08 00:31:29 +01:00
public void onLoad() {
super.onLoad();
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide) {
2020-10-17 21:13:45 +02:00
// add a ticket to water crops
2021-12-15 16:30:22 +01:00
var area = new AABB(this.worldPosition).inflate(5, 1, 5);
2021-12-04 15:40:09 +01:00
this.waterTicket = FarmlandWaterManager.addAABBTicket(this.level, area);
2020-10-17 21:13:45 +02:00
}
}
@Override
2021-12-08 00:31:29 +01:00
public void setRemoved() {
super.setRemoved();
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide && this.waterTicket != null && this.waterTicket.isValid()) {
2020-10-17 21:13:45 +02:00
this.waterTicket.invalidate();
this.waterTicket = null;
}
2020-10-19 21:51:26 +02:00
this.tank.invalidate();
2020-10-17 21:13:45 +02:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide || this.level.getGameTime() % 35 != 0)
2020-10-17 21:13:45 +02:00
return;
// fill cauldrons
2021-12-15 16:30:22 +01:00
var up = this.worldPosition.above();
var upState = this.level.getBlockState(up);
2021-12-08 00:31:29 +01:00
if (upState.hasProperty(BlockStateProperties.LEVEL_CAULDRON)) {
int level = upState.getValue(BlockStateProperties.LEVEL_CAULDRON);
2020-10-17 21:13:45 +02:00
if (level < 3) {
2021-12-08 00:31:29 +01:00
this.level.setBlockAndUpdate(up, upState.setValue(BlockStateProperties.LEVEL_CAULDRON, level + 1));
this.level.playSound(null, up, SoundEvents.BUCKET_FILL, SoundSource.BLOCKS, 1, 1);
2020-10-17 21:13:45 +02:00
this.consumeAura(2500);
return;
}
}
// wet sponges
2021-12-15 16:30:22 +01:00
var spongeRadius = 2;
for (var x = -spongeRadius; x <= spongeRadius; x++) {
for (var y = -spongeRadius; y <= spongeRadius; y++) {
for (var z = -spongeRadius; z <= spongeRadius; z++) {
var pos = this.worldPosition.offset(x, y, z);
var state = this.level.getBlockState(pos);
2020-10-17 21:13:45 +02:00
if (state.getBlock() == Blocks.SPONGE) {
2021-12-08 00:31:29 +01:00
this.level.setBlock(pos, Blocks.WET_SPONGE.defaultBlockState(), 2);
this.level.levelEvent(2001, pos, Block.getId(Blocks.WATER.defaultBlockState()));
2020-10-17 21:13:45 +02:00
this.consumeAura(2500);
return;
}
}
}
}
// generate obsidian
2021-12-15 16:30:22 +01:00
for (var dir : Direction.Plane.HORIZONTAL) {
var side = this.worldPosition.relative(dir);
2020-10-17 21:13:45 +02:00
if (this.isLava(side, true)) {
2021-12-08 00:31:29 +01:00
this.level.setBlockAndUpdate(side, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, side, side, Blocks.OBSIDIAN.defaultBlockState()));
this.level.levelEvent(1501, side, 0);
2020-10-17 21:13:45 +02:00
this.consumeAura(1500);
return;
}
}
// generate stone
2021-12-15 16:30:22 +01:00
var twoUp = this.worldPosition.above(2);
2021-12-08 00:31:29 +01:00
if (this.isLava(twoUp, false) && (this.level.getBlockState(up).isAir() || this.isLava(up, false))) {
this.level.setBlockAndUpdate(up, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, up, twoUp, Blocks.STONE.defaultBlockState()));
this.level.levelEvent(1501, up, 0);
2020-10-17 21:13:45 +02:00
this.consumeAura(150);
return;
}
// generate cobblestone
2021-12-15 16:30:22 +01:00
for (var dir : Direction.Plane.HORIZONTAL) {
var twoSide = this.worldPosition.relative(dir, 2);
var side = this.worldPosition.relative(dir);
2021-12-08 00:31:29 +01:00
if (this.isLava(twoSide, false) && (this.level.getBlockState(side).isAir() || this.isLava(side, false))) {
this.level.setBlockAndUpdate(side, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, side, twoSide, Blocks.COBBLESTONE.defaultBlockState()));
this.level.levelEvent(1501, side, 0);
2020-10-17 21:13:45 +02:00
this.consumeAura(100);
return;
}
}
}
@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) {
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
2020-10-19 21:26:32 +02:00
return this.tank.cast();
2020-10-17 21:13:45 +02:00
return LazyOptional.empty();
}
public void consumeAura(int amount) {
while (amount > 0) {
2021-12-15 16:30:22 +01:00
var pos = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
2021-12-04 15:40:09 +01:00
amount -= IAuraChunk.getAuraChunk(this.level, pos).drainAura(pos, amount);
2020-10-17 21:13:45 +02:00
}
}
private boolean isLava(BlockPos offset, boolean source) {
2021-12-15 16:30:22 +01:00
var state = this.level.getFluidState(offset);
2021-12-08 00:31:29 +01:00
return (!source || state.isSource()) && state.getType().is(FluidTags.LAVA);
2020-10-17 21:13:45 +02:00
}
2020-10-19 20:41:48 +02:00
private class InfiniteTank implements IFluidTank, IFluidHandler {
2021-12-08 00:31:29 +01:00
2020-10-17 21:13:45 +02:00
@Override
public FluidStack getFluid() {
return new FluidStack(Fluids.WATER, 1000);
2020-10-17 21:13:45 +02:00
}
@Override
public int getFluidAmount() {
return 1000;
2020-10-17 21:13:45 +02:00
}
@Override
public int getCapacity() {
return 1000;
2020-10-17 21:13:45 +02:00
}
@Override
public boolean isFluidValid(FluidStack stack) {
2021-12-08 00:31:29 +01:00
return stack.getFluid().is(FluidTags.WATER);
2020-10-17 21:13:45 +02:00
}
@Override
public int fill(FluidStack resource, IFluidHandler.FluidAction action) {
return 0;
}
@Override
public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) {
2021-12-15 16:30:22 +01:00
var drain = Math.min(maxDrain, 1000);
2020-10-17 21:13:45 +02:00
if (action.execute())
2021-12-04 19:17:21 +01:00
BlockEntitySpring.this.consumeAura(Mth.ceil(drain / 2F));
return new FluidStack(Fluids.WATER, drain);
2020-10-17 21:13:45 +02:00
}
@Override
public FluidStack drain(FluidStack resource, IFluidHandler.FluidAction action) {
if (this.isFluidValid(resource))
return this.drain(resource.getAmount(), action);
return FluidStack.EMPTY;
}
2020-10-19 20:41:48 +02:00
@Override
public int getTanks() {
return 1;
}
@Override
public FluidStack getFluidInTank(int tank) {
return this.getFluid();
}
@Override
public int getTankCapacity(int tank) {
return this.getCapacity();
}
@Override
public boolean isFluidValid(int tank, FluidStack stack) {
return this.isFluidValid(stack);
}
2020-10-17 21:13:45 +02:00
}
}