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

109 lines
4.6 KiB
Java
Raw Normal View History

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;
2020-02-02 22:21:55 +01:00
import net.minecraft.block.Blocks;
2020-02-02 22:50:02 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.SnowGolemEntity;
2020-02-02 22:21:55 +01:00
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
2020-02-02 22:50:02 +01:00
import net.minecraft.nbt.CompoundNBT;
2020-02-02 22:21:55 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.gen.Heightmap;
public class TileEntitySnowCreator extends TileEntityImpl implements ITickableTileEntity {
2020-02-02 22:50:02 +01:00
private int snowmanCount;
2020-02-02 22:21:55 +01:00
public TileEntitySnowCreator() {
super(ModTileEntities.SNOW_CREATOR);
}
public int getRange() {
return this.redstonePower * 2;
}
@Override
public void onRedstonePowerChange(int newPower) {
super.onRedstonePowerChange(newPower);
this.sendToClients();
}
@Override
public void tick() {
int range = this.getRange();
if (range <= 0)
return;
if (!this.world.isRemote) {
if (this.world.getGameTime() % 10 != 0)
return;
2020-02-02 22:50:02 +01:00
for (int i = 0; i < 10; i++) {
double angle = this.world.rand.nextFloat() * Math.PI * 2;
BlockPos pos = this.pos.add(Math.cos(angle) * range * this.world.rand.nextFloat(), 0, Math.sin(angle) * range * this.world.rand.nextFloat());
2020-02-02 22:50:02 +01:00
pos = this.world.getHeight(Heightmap.Type.MOTION_BLOCKING, pos);
BlockPos down = pos.down();
2020-02-02 22:21:55 +01:00
2020-02-02 22:50:02 +01:00
Fluid fluid = this.world.getFluidState(down).getFluid();
if (fluid == Fluids.WATER) {
if (this.world.getBlockState(down).getMaterial().isReplaceable())
this.world.setBlockState(down, Blocks.ICE.getDefaultState());
} else if (Blocks.SNOW.getDefaultState().isValidPosition(this.world, pos) && this.world.getBlockState(pos).getBlock() != Blocks.SNOW && this.world.getBlockState(pos).getMaterial().isReplaceable()) {
2020-02-02 22:50:02 +01:00
this.world.setBlockState(pos, Blocks.SNOW.getDefaultState());
2020-02-02 22:21:55 +01:00
2020-02-02 22:50:02 +01:00
if (this.snowmanCount < range / 2 && this.world.rand.nextFloat() >= 0.995F) {
this.snowmanCount++;
Entity golem = new SnowGolemEntity(EntityType.SNOW_GOLEM, this.world);
golem.setPosition(pos.getX() + 0.5F, pos.getY(), pos.getZ() + 0.5F);
this.world.addEntity(golem);
}
} else {
continue;
}
BlockPos auraPos = IAuraChunk.getHighestSpot(this.world, this.pos, 30, this.pos);
IAuraChunk.getAuraChunk(this.world, auraPos).drainAura(auraPos, 300);
PacketHandler.sendToAllAround(this.world, this.pos, 32,
new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), PacketParticles.Type.SNOW_CREATOR));
break;
}
2020-02-02 22:21:55 +01:00
} else {
if (this.world.getGameTime() % 30 != 0)
return;
2020-02-02 23:27:40 +01:00
for (int i = range * 4; i >= 0; i--) {
double angle = this.world.rand.nextFloat() * Math.PI * 2;
BlockPos pos = this.pos.add(
Math.cos(angle) * range * this.world.rand.nextFloat(),
2020-02-02 22:21:55 +01:00
MathHelper.nextInt(this.world.rand, range / 2, range),
Math.sin(angle) * range * this.world.rand.nextFloat());
2020-02-02 22:21:55 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
pos.getX() + this.world.rand.nextFloat(), pos.getY() + 1, pos.getZ() + this.world.rand.nextFloat(),
2020-02-02 22:21:55 +01:00
this.world.rand.nextGaussian() * 0.05, 0, this.world.rand.nextGaussian() * 0.05,
0xdbe9ff, 1 + this.world.rand.nextFloat() * 1.5F, 10 * range, 0.05F + this.world.rand.nextFloat() * 0.05F, true, true
);
}
}
}
2020-02-02 22:50:02 +01:00
@Override
public void writeNBT(CompoundNBT compound, SaveType type) {
super.writeNBT(compound, type);
if (type == SaveType.TILE)
compound.putInt("snowman_count", this.snowmanCount);
}
@Override
public void readNBT(CompoundNBT compound, SaveType type) {
super.readNBT(compound, type);
if (type == SaveType.TILE)
this.snowmanCount = compound.getInt("snowman_count");
}
2020-02-02 22:21:55 +01:00
}