NaturesAura/src/main/java/de/ellpeck/naturesaura/chunk/effect/GrassDieEffect.java

96 lines
3.8 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.blocks.ModBlocks;
import net.minecraft.block.*;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
2021-12-04 19:17:21 +01:00
import net.minecraft.util.math.Mth;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.Level;
import net.minecraft.level.chunk.Chunk;
import org.apache.commons.lang3.tuple.Pair;
public class GrassDieEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "grass_die");
private int amount;
private int dist;
2021-12-04 15:40:09 +01:00
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
if (spot < 0) {
2021-12-04 15:40:09 +01:00
Pair<Integer, Integer> auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 50);
int aura = auraAndSpots.getLeft();
if (aura < 0) {
2021-12-04 19:17:21 +01:00
this.amount = Math.min(300, Mth.ceil(Math.abs(aura) / 100000F / auraAndSpots.getRight()));
if (this.amount > 1) {
2021-12-04 19:17:21 +01:00
this.dist = Mth.clamp(Math.abs(aura) / 75000, 5, 75);
return true;
}
}
}
return false;
}
@Override
2021-12-04 15:40:09 +01:00
public ActiveType isActiveHere(Player player, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
if (!this.calcValues(player.level, pos, spot))
2020-01-23 16:05:52 +01:00
return ActiveType.INACTIVE;
2020-01-21 23:02:39 +01:00
if (player.getDistanceSq(pos.getX(), pos.getY(), pos.getZ()) > this.dist * this.dist)
2020-01-23 16:05:52 +01:00
return ActiveType.INACTIVE;
return ActiveType.ACTIVE;
}
@Override
public ItemStack getDisplayIcon() {
return new ItemStack(ModBlocks.DECAYED_LEAVES);
}
@Override
2021-12-04 15:40:09 +01:00
public void update(Level level, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
if (!this.calcValues(level, pos, spot))
return;
2021-12-04 15:40:09 +01:00
for (int i = this.amount / 2 + level.rand.nextInt(this.amount / 2); i >= 0; i--) {
BlockPos grassPos = new BlockPos(
2021-12-04 15:40:09 +01:00
pos.getX() + level.rand.nextGaussian() * this.dist,
pos.getY() + level.rand.nextGaussian() * this.dist,
pos.getZ() + level.rand.nextGaussian() * this.dist
);
2021-12-04 15:40:09 +01:00
if (grassPos.distanceSq(pos) <= this.dist * this.dist && level.isBlockLoaded(grassPos)) {
BlockState state = level.getBlockState(grassPos);
Block block = state.getBlock();
2019-10-20 22:30:49 +02:00
BlockState newState = null;
2020-01-21 23:02:39 +01:00
if (block instanceof LeavesBlock) {
newState = ModBlocks.DECAYED_LEAVES.getDefaultState();
2020-01-21 23:02:39 +01:00
} else if (block instanceof GrassBlock) {
newState = Blocks.COARSE_DIRT.getDefaultState();
} else if (block instanceof BushBlock) {
newState = Blocks.AIR.getDefaultState();
2020-02-26 19:32:42 +01:00
} else if (block == ModBlocks.NETHER_GRASS) {
newState = Blocks.NETHERRACK.getDefaultState();
}
if (newState != null)
2021-12-04 15:40:09 +01:00
level.setBlockState(grassPos, newState);
}
}
}
@Override
public boolean appliesHere(Chunk chunk, IAuraChunk auraChunk, IAuraType type) {
2020-02-26 19:32:42 +01:00
return ModConfig.instance.grassDieEffect.get() && (type.isSimilar(NaturesAuraAPI.TYPE_OVERWORLD) || type.isSimilar(NaturesAuraAPI.TYPE_NETHER));
}
@Override
public ResourceLocation getName() {
return NAME;
}
}