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

83 lines
3.1 KiB
Java
Raw Normal View History

2018-11-21 17:45:06 +01:00
package de.ellpeck.naturesaura.chunk.effect;
import de.ellpeck.naturesaura.ModConfig;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.chunk.IDrainSpotEffect;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
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.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.levelgen.Heightmap;
2018-11-21 17:45:06 +01:00
public class ExplosionEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "explosions");
private float strength;
private int dist;
2021-12-04 15:40:09 +01:00
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
if (spot >= 0)
return false;
2021-12-15 16:30:22 +01:00
var aura = IAuraChunk.getAuraInArea(level, pos, 85);
if (aura > -5000000)
return false;
2021-12-15 16:30:22 +01:00
var chance = 140 - Math.abs(aura) / 200000;
2021-12-15 16:24:53 +01:00
if (chance > 1 && level.random.nextInt(chance) != 0)
return false;
this.strength = Math.min(Math.abs(aura) / 5000000F, 5F);
if (this.strength <= 0)
return false;
2021-12-04 19:17:21 +01:00
this.dist = Mth.clamp(Math.abs(aura) / 200000, 25, 100);
return true;
}
@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;
}
@Override
public ItemStack getDisplayIcon() {
return new ItemStack(Blocks.TNT);
}
@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 (level.getGameTime() % 40 != 0)
2018-11-21 17:45:06 +01:00
return;
2021-12-04 15:40:09 +01:00
if (!this.calcValues(level, pos, spot))
2018-11-21 17:45:06 +01:00
return;
2021-12-15 16:30:22 +01:00
var x = Mth.floor(pos.getX() + level.random.nextGaussian() * this.dist);
var z = Mth.floor(pos.getZ() + level.random.nextGaussian() * this.dist);
var chosenPos = new BlockPos(x, level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z), z);
2021-12-15 16:24:53 +01:00
if (chosenPos.distSqr(pos) <= this.dist * this.dist && level.isLoaded(chosenPos)) {
level.explode(null,
2018-11-21 17:45:06 +01:00
chosenPos.getX() + 0.5, chosenPos.getY() + 0.5, chosenPos.getZ() + 0.5,
2021-12-15 16:24:53 +01:00
this.strength, false, Explosion.BlockInteraction.DESTROY);
2018-11-21 17:45:06 +01:00
}
}
@Override
2021-12-15 16:24:53 +01:00
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
2020-01-24 17:05:41 +01:00
return ModConfig.instance.explosionEffect.get();
2018-11-21 17:45:06 +01:00
}
@Override
public ResourceLocation getName() {
return NAME;
}
}