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

106 lines
4.5 KiB
Java
Raw Normal View History

2020-02-26 19:32:42 +01:00
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
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 de.ellpeck.naturesaura.chunk.AuraChunk;
2020-02-26 19:32:42 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
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;
import net.minecraft.world.level.chunk.LevelChunk;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.common.Tags;
2020-02-26 19:32:42 +01:00
public class NetherGrassEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "nether_grass");
private int amount;
private int dist;
2021-12-04 15:40:09 +01:00
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
2020-02-26 19:32:42 +01:00
if (spot <= 0)
return false;
2021-12-15 16:30:22 +01:00
var auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 30);
int aura = auraAndSpots.getLeft();
2020-02-26 19:32:42 +01:00
if (aura < 1500000)
return false;
2021-12-04 19:17:21 +01:00
this.amount = Math.min(20, Mth.ceil(Math.abs(aura) / 100000F / auraAndSpots.getRight()));
2020-02-26 19:32:42 +01:00
if (this.amount <= 1)
return false;
2021-12-04 19:17:21 +01:00
this.dist = Mth.clamp(Math.abs(aura) / 100000, 5, 35);
2020-02-26 19:32:42 +01:00
return true;
}
@Override
2021-12-15 16:24:53 +01:00
public ActiveType isActiveHere(Player player, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
2023-07-08 12:32:27 +02:00
if (!this.calcValues(player.level(), pos, spot))
2020-02-26 19:32:42 +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-02-26 19:32:42 +01:00
return ActiveType.INACTIVE;
2023-07-08 12:32:27 +02:00
if (NaturesAuraAPI.instance().isEffectPowderActive(player.level(), player.blockPosition(), NetherGrassEffect.NAME))
2020-02-26 19:32:42 +01:00
return ActiveType.INHIBITED;
return ActiveType.ACTIVE;
}
@Override
public ItemStack getDisplayIcon() {
return new ItemStack(ModBlocks.NETHER_GRASS);
}
@Override
public void update(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot, AuraChunk.DrainSpot actualSpot) {
2021-12-04 15:40:09 +01:00
if (level.getGameTime() % 40 != 0)
return;
2021-12-04 15:40:09 +01:00
if (!this.calcValues(level, pos, spot))
2020-02-26 19:32:42 +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 x = Mth.floor(pos.getX() + level.random.nextGaussian() * this.dist);
var y = Mth.floor(pos.getY() + level.random.nextGaussian() * this.dist);
var z = Mth.floor(pos.getZ() + level.random.nextGaussian() * this.dist);
2020-02-26 19:32:42 +01:00
2021-12-15 16:30:22 +01:00
for (var yOff = -5; yOff <= 5; yOff++) {
var goalPos = new BlockPos(x, y + yOff, z);
2021-12-15 16:24:53 +01:00
if (goalPos.distSqr(pos) <= this.dist * this.dist && level.isLoaded(goalPos)) {
2022-06-27 15:24:04 +02:00
if (NaturesAuraAPI.instance().isEffectPowderActive(level, goalPos, NetherGrassEffect.NAME))
2020-02-26 19:32:42 +01:00
continue;
2021-12-15 16:30:22 +01:00
var up = goalPos.above();
2021-12-15 16:24:53 +01:00
if (level.getBlockState(up).isFaceSturdy(level, up, Direction.DOWN))
2020-02-26 19:32:42 +01:00
continue;
2021-12-15 16:30:22 +01:00
var state = level.getBlockState(goalPos);
2022-03-04 15:59:04 +01:00
if (state.is(Tags.Blocks.NETHERRACK)) {
2021-12-15 16:24:53 +01:00
level.setBlockAndUpdate(goalPos, ModBlocks.NETHER_GRASS.defaultBlockState());
2020-02-26 19:32:42 +01:00
2021-12-15 16:30:22 +01:00
var closestSpot = IAuraChunk.getHighestSpot(level, goalPos, 25, pos);
2021-12-04 15:40:09 +01:00
IAuraChunk.getAuraChunk(level, closestSpot).drainAura(closestSpot, 500);
2020-02-26 19:32:42 +01:00
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(level, goalPos, 32,
2020-02-26 19:32:42 +01:00
new PacketParticles(goalPos.getX(), goalPos.getY() + 0.5F, goalPos.getZ(), PacketParticles.Type.PLANT_BOOST));
break;
}
}
}
}
}
@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.netherGrassEffect.get() && type.isSimilar(NaturesAuraAPI.TYPE_NETHER);
}
@Override
public ResourceLocation getName() {
2022-06-27 15:24:04 +02:00
return NetherGrassEffect.NAME;
2020-02-26 19:32:42 +01:00
}
}