2018-11-13 11:39:28 +01:00
|
|
|
package de.ellpeck.naturesaura.chunk.effect;
|
2018-10-25 18:49:42 +02:00
|
|
|
|
2018-11-13 18:21:41 +01:00
|
|
|
import de.ellpeck.naturesaura.ModConfig;
|
2018-11-13 11:39:28 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-11-12 01:29:33 +01:00
|
|
|
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;
|
2018-11-12 01:29:33 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
2018-10-25 18:49:42 +02:00
|
|
|
import de.ellpeck.naturesaura.blocks.ModBlocks;
|
2021-12-15 16:24:53 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
2023-02-15 23:52:52 +01:00
|
|
|
import net.minecraft.world.level.block.Blocks;
|
|
|
|
import net.minecraft.world.level.block.BushBlock;
|
|
|
|
import net.minecraft.world.level.block.GrassBlock;
|
|
|
|
import net.minecraft.world.level.block.LeavesBlock;
|
2021-12-15 16:24:53 +01:00
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.level.chunk.LevelChunk;
|
2018-10-25 18:49:42 +02:00
|
|
|
|
|
|
|
public class GrassDieEffect implements IDrainSpotEffect {
|
2018-11-13 11:39:28 +01:00
|
|
|
|
|
|
|
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "grass_die");
|
|
|
|
|
2019-02-09 21:55:40 +01:00
|
|
|
private int amount;
|
|
|
|
private int dist;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
|
2018-12-02 01:36:41 +01:00
|
|
|
if (spot < 0) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 50);
|
2021-06-28 14:31:37 +02:00
|
|
|
int aura = auraAndSpots.getLeft();
|
2018-10-25 18:49:42 +02:00
|
|
|
if (aura < 0) {
|
2021-12-04 19:17:21 +01:00
|
|
|
this.amount = Math.min(300, Mth.ceil(Math.abs(aura) / 100000F / auraAndSpots.getRight()));
|
2019-02-09 21:55:40 +01:00
|
|
|
if (this.amount > 1) {
|
2021-12-04 19:17:21 +01:00
|
|
|
this.dist = Mth.clamp(Math.abs(aura) / 75000, 5, 75);
|
2019-02-09 21:55:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public ActiveType isActiveHere(Player player, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(player.level, pos, spot))
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
2021-12-15 16:24:53 +01:00
|
|
|
if (player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ()) > this.dist * this.dist)
|
2020-01-23 16:05:52 +01:00
|
|
|
return ActiveType.INACTIVE;
|
|
|
|
return ActiveType.ACTIVE;
|
2019-02-09 21:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getDisplayIcon() {
|
|
|
|
return new ItemStack(ModBlocks.DECAYED_LEAVES);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public void update(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!this.calcValues(level, pos, spot))
|
2019-02-09 21:55:40 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = this.amount / 2 + level.random.nextInt(this.amount / 2); i >= 0; i--) {
|
|
|
|
var grassPos = new BlockPos(
|
2021-12-15 16:24:53 +01:00
|
|
|
pos.getX() + level.random.nextGaussian() * this.dist,
|
|
|
|
pos.getY() + level.random.nextGaussian() * this.dist,
|
|
|
|
pos.getZ() + level.random.nextGaussian() * this.dist
|
2019-02-09 21:55:40 +01:00
|
|
|
);
|
2021-12-15 16:24:53 +01:00
|
|
|
if (grassPos.distSqr(pos) <= this.dist * this.dist && level.isLoaded(grassPos)) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var state = level.getBlockState(grassPos);
|
|
|
|
var block = state.getBlock();
|
2018-10-25 18:49:42 +02:00
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState newState = null;
|
2020-01-21 23:02:39 +01:00
|
|
|
if (block instanceof LeavesBlock) {
|
2021-12-15 16:24:53 +01:00
|
|
|
newState = ModBlocks.DECAYED_LEAVES.defaultBlockState();
|
2020-01-21 23:02:39 +01:00
|
|
|
} else if (block instanceof GrassBlock) {
|
2021-12-15 16:24:53 +01:00
|
|
|
newState = Blocks.COARSE_DIRT.defaultBlockState();
|
2020-01-21 23:02:39 +01:00
|
|
|
} else if (block instanceof BushBlock) {
|
2021-12-15 16:24:53 +01:00
|
|
|
newState = Blocks.AIR.defaultBlockState();
|
2020-02-26 19:32:42 +01:00
|
|
|
} else if (block == ModBlocks.NETHER_GRASS) {
|
2021-12-15 16:24:53 +01:00
|
|
|
newState = Blocks.NETHERRACK.defaultBlockState();
|
2018-10-25 18:49:42 +02:00
|
|
|
}
|
2019-02-09 21:55:40 +01:00
|
|
|
if (newState != null)
|
2021-12-15 16:24:53 +01:00
|
|
|
level.setBlockAndUpdate(grassPos, newState);
|
2018-10-25 18:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 23:42:13 +01:00
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public boolean appliesHere(LevelChunk 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));
|
2018-11-07 23:42:13 +01:00
|
|
|
}
|
2018-11-13 11:39:28 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
2022-06-27 15:24:04 +02:00
|
|
|
return GrassDieEffect.NAME;
|
2018-11-13 11:39:28 +01:00
|
|
|
}
|
2018-10-25 18:49:42 +02:00
|
|
|
}
|