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

106 lines
4.1 KiB
Java
Raw Normal View History

2020-11-08 15:05:45 +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 net.minecraft.block.AbstractFireBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2020-11-08 15:05:45 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Direction;
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;
2020-11-08 15:05:45 +01:00
public class NetherDecayEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "nether_decay");
private int amount;
private int dist;
2021-12-04 15:40:09 +01:00
private boolean calcValues(Level level, BlockPos pos, Integer spot) {
2020-11-08 15:05:45 +01:00
if (spot >= 0)
return false;
2021-12-04 15:40:09 +01:00
Pair<Integer, Integer> auraAndSpots = IAuraChunk.getAuraAndSpotAmountInArea(level, pos, 50);
int aura = auraAndSpots.getLeft();
2020-11-08 15:05:45 +01:00
if (aura >= 0)
return false;
2021-12-04 19:17:21 +01:00
this.amount = Math.min(300, Mth.ceil(Math.abs(aura) / 50000F / auraAndSpots.getRight()));
2020-11-08 15:05:45 +01:00
if (this.amount <= 1)
return false;
2021-12-04 19:17:21 +01:00
this.dist = Mth.clamp(Math.abs(aura) / 50000, 5, 75);
2020-11-08 15:05:45 +01:00
return true;
}
@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-11-08 15:05:45 +01:00
return ActiveType.INACTIVE;
if (player.getDistanceSq(pos.getX(), pos.getY(), pos.getZ()) > this.dist * this.dist)
return ActiveType.INACTIVE;
return ActiveType.ACTIVE;
}
@Override
public ItemStack getDisplayIcon() {
return new ItemStack(Items.SOUL_SAND);
}
@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))
2020-11-08 15:05:45 +01:00
return;
2021-12-04 15:40:09 +01:00
for (int i = this.amount / 2 + level.rand.nextInt(this.amount / 2); i >= 0; i--) {
2020-11-08 15:05:45 +01:00
BlockPos offset = 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);
if (offset.distanceSq(pos) > this.dist * this.dist || !level.isBlockLoaded(offset))
2020-11-08 15:05:45 +01:00
continue;
// degrade blocks
Block degraded = null;
2021-12-04 15:40:09 +01:00
BlockState state = level.getBlockState(offset);
2020-11-08 15:05:45 +01:00
if (state.getBlock() == Blocks.GLOWSTONE) {
degraded = Blocks.NETHERRACK;
} else if (state.getBlock().isIn(BlockTags.NYLIUM) || state.getBlock() == Blocks.NETHERRACK) {
degraded = Blocks.SOUL_SOIL;
} else if (state.getBlock() == Blocks.SOUL_SOIL) {
degraded = Blocks.SOUL_SAND;
}
if (degraded != null) {
2021-12-04 15:40:09 +01:00
level.playEvent(2001, offset, Block.getStateId(state));
level.setBlockState(offset, degraded.getDefaultState());
2020-11-08 15:05:45 +01:00
}
// ignite blocks
2021-12-04 15:40:09 +01:00
if (AbstractFireBlock.canLightBlock(level, offset, Direction.NORTH)) {
BlockState fire = AbstractFireBlock.getFireForPlacement(level, offset);
level.setBlockState(offset, fire);
level.playEvent(1009, offset, 0);
2020-11-08 15:05:45 +01:00
}
}
}
@Override
public boolean appliesHere(Chunk chunk, IAuraChunk auraChunk, IAuraType type) {
return ModConfig.instance.netherDecayEffect.get() && type.isSimilar(NaturesAuraAPI.TYPE_NETHER);
}
@Override
public ResourceLocation getName() {
return NAME;
}
}