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

67 lines
2.5 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.chunk.effect;
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.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.LevelChunk;
public class SpreadEffect implements IDrainSpotEffect {
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "spread");
@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 (Math.abs(spot) < 500000 || Math.abs(IAuraChunk.getAuraInArea(level, pos, 25)) < 2000000)
return;
2021-12-15 16:30:22 +01:00
var drain = spot > 0;
var toMove = Mth.ceil(Math.abs(spot) * 0.72F);
var perSide = toMove / 6;
while (toMove > 0) {
BlockPos bestOffset = null;
2021-12-15 16:30:22 +01:00
var bestAmount = drain ? Integer.MAX_VALUE : Integer.MIN_VALUE;
for (var facing : Direction.values()) {
var offset = pos.relative(facing, 15);
2021-12-15 16:24:53 +01:00
if (level.isLoaded(offset) && offset.getY() >= 0 && offset.getY() <= level.getHeight()) {
2021-12-15 16:30:22 +01:00
var amount = IAuraChunk.getAuraInArea(level, offset, 14);
if (drain ? amount < bestAmount : amount > bestAmount) {
bestAmount = amount;
bestOffset = offset;
}
}
}
2020-01-21 23:02:39 +01:00
if (bestOffset == null)
break;
2021-12-15 16:30:22 +01:00
var bestPos = drain ? IAuraChunk.getLowestSpot(level, bestOffset, 14, bestOffset)
2021-12-04 15:40:09 +01:00
: IAuraChunk.getHighestSpot(level, bestOffset, 14, bestOffset);
2021-12-15 16:30:22 +01:00
var bestChunk = IAuraChunk.getAuraChunk(level, bestPos);
int moved;
if (drain) {
moved = bestChunk.storeAura(bestPos, perSide);
auraChunk.drainAura(pos, moved);
} else {
moved = bestChunk.drainAura(bestPos, perSide);
auraChunk.storeAura(pos, moved);
}
toMove -= moved;
}
}
@Override
2021-12-15 16:24:53 +01:00
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
return true;
}
@Override
public ResourceLocation getName() {
return NAME;
}
}