2020-10-17 21:13:45 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.block.CauldronBlock;
|
|
|
|
import net.minecraft.fluid.FluidState;
|
|
|
|
import net.minecraft.fluid.Fluids;
|
|
|
|
import net.minecraft.tags.FluidTags;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.tileentity.ITickableBlockEntity;
|
2020-10-17 21:13:45 +02:00
|
|
|
import net.minecraft.util.Direction;
|
|
|
|
import net.minecraft.util.SoundCategory;
|
|
|
|
import net.minecraft.util.SoundEvents;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-01-14 22:39:57 +01:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
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-04 15:40:09 +01:00
|
|
|
public BlockEntitySpring() {
|
2020-10-17 21:13:45 +02:00
|
|
|
super(ModTileEntities.SPRING);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void validate() {
|
|
|
|
super.validate();
|
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-04 15:40:09 +01:00
|
|
|
AxisAlignedBB area = new AxisAlignedBB(this.worldPosition).grow(5, 1, 5);
|
|
|
|
this.waterTicket = FarmlandWaterManager.addAABBTicket(this.level, area);
|
2020-10-17 21:13:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
super.remove();
|
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-04 15:40:09 +01:00
|
|
|
BlockPos up = this.worldPosition.up();
|
|
|
|
BlockState upState = this.level.getBlockState(up);
|
2020-10-17 21:13:45 +02:00
|
|
|
if (upState.hasProperty(CauldronBlock.LEVEL)) {
|
|
|
|
int level = upState.get(CauldronBlock.LEVEL);
|
|
|
|
if (level < 3) {
|
2021-12-04 15:40:09 +01:00
|
|
|
this.level.setBlockState(up, upState.with(CauldronBlock.LEVEL, level + 1));
|
|
|
|
this.level.playSound(null, up, SoundEvents.ITEM_BUCKET_FILL, SoundCategory.BLOCKS, 1, 1);
|
2020-10-17 21:13:45 +02:00
|
|
|
this.consumeAura(2500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wet sponges
|
|
|
|
int spongeRadius = 2;
|
|
|
|
for (int x = -spongeRadius; x <= spongeRadius; x++) {
|
|
|
|
for (int y = -spongeRadius; y <= spongeRadius; y++) {
|
|
|
|
for (int z = -spongeRadius; z <= spongeRadius; z++) {
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockPos pos = this.worldPosition.add(x, y, z);
|
|
|
|
BlockState state = this.level.getBlockState(pos);
|
2020-10-17 21:13:45 +02:00
|
|
|
if (state.getBlock() == Blocks.SPONGE) {
|
2021-12-04 15:40:09 +01:00
|
|
|
this.level.setBlockState(pos, Blocks.WET_SPONGE.getDefaultState(), 2);
|
|
|
|
this.level.playEvent(2001, pos, Block.getStateId(Blocks.WATER.getDefaultState()));
|
2020-10-17 21:13:45 +02:00
|
|
|
this.consumeAura(2500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate obsidian
|
|
|
|
for (Direction dir : Direction.Plane.HORIZONTAL) {
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockPos side = this.worldPosition.offset(dir);
|
2020-10-17 21:13:45 +02:00
|
|
|
if (this.isLava(side, true)) {
|
2021-12-04 15:40:09 +01:00
|
|
|
this.level.setBlockState(side, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, side, side, Blocks.OBSIDIAN.getDefaultState()));
|
|
|
|
this.level.playEvent(1501, side, 0);
|
2020-10-17 21:13:45 +02:00
|
|
|
this.consumeAura(1500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate stone
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockPos twoUp = this.worldPosition.up(2);
|
|
|
|
if (this.isLava(twoUp, false) && (this.level.getBlockState(up).isAir(this.level, up) || this.isLava(up, false))) {
|
|
|
|
this.level.setBlockState(up, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, up, twoUp, Blocks.STONE.getDefaultState()));
|
|
|
|
this.level.playEvent(1501, up, 0);
|
2020-10-17 21:13:45 +02:00
|
|
|
this.consumeAura(150);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate cobblestone
|
|
|
|
for (Direction dir : Direction.Plane.HORIZONTAL) {
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockPos twoSide = this.worldPosition.offset(dir, 2);
|
|
|
|
BlockPos side = this.worldPosition.offset(dir);
|
|
|
|
if (this.isLava(twoSide, false) && (this.level.getBlockState(side).isAir(this.level, side) || this.isLava(side, false))) {
|
|
|
|
this.level.setBlockState(side, ForgeEventFactory.fireFluidPlaceBlockEvent(this.level, side, twoSide, Blocks.COBBLESTONE.getDefaultState()));
|
|
|
|
this.level.playEvent(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-04 15:40:09 +01:00
|
|
|
BlockPos pos = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 35, this.worldPosition);
|
|
|
|
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-04 15:40:09 +01:00
|
|
|
FluidState state = this.level.getFluidState(offset);
|
2020-10-17 21:13:45 +02:00
|
|
|
return (!source || state.isSource()) && state.getFluid().isIn(FluidTags.LAVA);
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:41:48 +02:00
|
|
|
private class InfiniteTank implements IFluidTank, IFluidHandler {
|
2020-10-17 21:13:45 +02:00
|
|
|
@Override
|
|
|
|
public FluidStack getFluid() {
|
2020-10-19 20:30:55 +02:00
|
|
|
return new FluidStack(Fluids.WATER, 1000);
|
2020-10-17 21:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getFluidAmount() {
|
2020-10-19 20:30:55 +02:00
|
|
|
return 1000;
|
2020-10-17 21:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCapacity() {
|
2020-10-19 20:30:55 +02:00
|
|
|
return 1000;
|
2020-10-17 21:13:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isFluidValid(FluidStack stack) {
|
|
|
|
return stack.getFluid().isIn(FluidTags.WATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int fill(FluidStack resource, IFluidHandler.FluidAction action) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FluidStack drain(int maxDrain, IFluidHandler.FluidAction action) {
|
2020-10-19 20:30:55 +02:00
|
|
|
int drain = Math.min(maxDrain, 1000);
|
2020-10-17 21:13:45 +02:00
|
|
|
if (action.execute())
|
2021-12-04 15:40:09 +01:00
|
|
|
BlockEntitySpring.this.consumeAura(MathHelper.ceil(drain / 2F));
|
2020-10-19 20:30:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|