2020-02-02 22:21:55 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2020-02-02 22:50:02 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
import net.minecraft.world.entity.EntityType;
|
|
|
|
import net.minecraft.world.entity.animal.SnowGolem;
|
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.levelgen.Heightmap;
|
|
|
|
import net.minecraft.world.level.material.Fluids;
|
2020-02-02 22:21:55 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntitySnowCreator extends BlockEntityImpl implements ITickableBlockEntity {
|
2020-02-02 22:50:02 +01:00
|
|
|
|
|
|
|
private int snowmanCount;
|
|
|
|
|
2021-12-08 00:31:29 +01:00
|
|
|
public BlockEntitySnowCreator(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.SNOW_CREATOR, pos, state);
|
2020-02-02 22:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getRange() {
|
|
|
|
return this.redstonePower * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRedstonePowerChange(int newPower) {
|
|
|
|
super.onRedstonePowerChange(newPower);
|
|
|
|
this.sendToClients();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tick() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var range = this.getRange();
|
2020-02-02 22:21:55 +01:00
|
|
|
if (range <= 0)
|
|
|
|
return;
|
|
|
|
|
2023-02-16 19:03:26 +01:00
|
|
|
var toDrain = 300;
|
|
|
|
if (!this.canUseRightNow(toDrain))
|
|
|
|
return;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.level.isClientSide) {
|
|
|
|
if (this.level.getGameTime() % 10 != 0)
|
2020-02-02 22:21:55 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
var angle = this.level.random.nextFloat() * Math.PI * 2;
|
|
|
|
var pos = this.worldPosition.offset(Math.cos(angle) * range * this.level.random.nextFloat(), 0, Math.sin(angle) * range * this.level.random.nextFloat());
|
2021-12-08 00:31:29 +01:00
|
|
|
pos = this.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos);
|
2021-12-15 16:30:22 +01:00
|
|
|
var down = pos.below();
|
2020-02-02 22:21:55 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var fluid = this.level.getFluidState(down).getType();
|
2020-02-02 22:50:02 +01:00
|
|
|
if (fluid == Fluids.WATER) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.getBlockState(down).getMaterial().isReplaceable())
|
2021-12-08 00:31:29 +01:00
|
|
|
this.level.setBlockAndUpdate(down, Blocks.ICE.defaultBlockState());
|
|
|
|
} else if (Blocks.SNOW.defaultBlockState().canSurvive(this.level, pos) && this.level.getBlockState(pos).getBlock() != Blocks.SNOW && this.level.getBlockState(pos).getMaterial().isReplaceable()) {
|
|
|
|
this.level.setBlockAndUpdate(pos, Blocks.SNOW.defaultBlockState());
|
2020-02-02 22:21:55 +01:00
|
|
|
|
2021-12-08 00:31:29 +01:00
|
|
|
if (this.snowmanCount < range / 2 && this.level.random.nextFloat() >= 0.995F) {
|
2020-02-02 22:50:02 +01:00
|
|
|
this.snowmanCount++;
|
2021-12-08 00:31:29 +01:00
|
|
|
Entity golem = new SnowGolem(EntityType.SNOW_GOLEM, this.level);
|
|
|
|
golem.setPos(pos.getX() + 0.5F, pos.getY(), pos.getZ() + 0.5F);
|
|
|
|
this.level.addFreshEntity(golem);
|
2020-02-02 22:50:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var auraPos = IAuraChunk.getHighestSpot(this.level, this.worldPosition, 30, this.worldPosition);
|
2023-02-16 19:03:26 +01:00
|
|
|
IAuraChunk.getAuraChunk(this.level, auraPos).drainAura(auraPos, toDrain);
|
2020-02-02 22:50:02 +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.SNOW_CREATOR));
|
2020-02-02 22:50:02 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-02-02 22:21:55 +01:00
|
|
|
} else {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (this.level.getGameTime() % 30 != 0)
|
2020-02-02 22:21:55 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = range * 4; i >= 0; i--) {
|
|
|
|
var angle = this.level.random.nextFloat() * Math.PI * 2;
|
|
|
|
var pos = this.worldPosition.offset(
|
2021-12-08 00:31:29 +01:00
|
|
|
Math.cos(angle) * range * this.level.random.nextFloat(),
|
|
|
|
Mth.nextInt(this.level.random, range / 2, range),
|
|
|
|
Math.sin(angle) * range * this.level.random.nextFloat());
|
2020-02-02 22:21:55 +01:00
|
|
|
NaturesAuraAPI.instance().spawnMagicParticle(
|
2021-12-08 00:31:29 +01:00
|
|
|
pos.getX() + this.level.random.nextFloat(), pos.getY() + 1, pos.getZ() + this.level.random.nextFloat(),
|
|
|
|
this.level.random.nextGaussian() * 0.05, 0, this.level.random.nextGaussian() * 0.05,
|
|
|
|
0xdbe9ff, 1 + this.level.random.nextFloat() * 1.5F, 10 * range, 0.05F + this.level.random.nextFloat() * 0.05F, true, true
|
2020-02-02 22:21:55 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 22:50:02 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2020-02-02 22:50:02 +01:00
|
|
|
super.writeNBT(compound, type);
|
|
|
|
if (type == SaveType.TILE)
|
|
|
|
compound.putInt("snowman_count", this.snowmanCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2020-02-02 22:50:02 +01:00
|
|
|
super.readNBT(compound, type);
|
|
|
|
if (type == SaveType.TILE)
|
|
|
|
this.snowmanCount = compound.getInt("snowman_count");
|
|
|
|
}
|
2023-02-16 19:03:26 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean allowsLowerLimiter() {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-02 22:21:55 +01:00
|
|
|
}
|