2018-12-04 13:38:46 +01:00
|
|
|
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;
|
2018-12-04 13:38:46 +01:00
|
|
|
|
|
|
|
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)
|
2018-12-04 13:38:46 +01:00
|
|
|
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;
|
2018-12-04 13:38:46 +01:00
|
|
|
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);
|
2018-12-04 13:56:48 +01:00
|
|
|
if (drain ? amount < bestAmount : amount > bestAmount) {
|
|
|
|
bestAmount = amount;
|
|
|
|
bestOffset = offset;
|
|
|
|
}
|
2018-12-04 13:38:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-21 23:02:39 +01:00
|
|
|
if (bestOffset == null)
|
2018-12-04 13:56:48 +01:00
|
|
|
break;
|
2018-12-04 13:38:46 +01:00
|
|
|
|
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);
|
2018-12-04 13:38:46 +01:00
|
|
|
|
|
|
|
int moved;
|
|
|
|
if (drain) {
|
2020-11-16 22:17:50 +01:00
|
|
|
moved = bestChunk.storeAura(bestPos, perSide);
|
2018-12-04 13:38:46 +01:00
|
|
|
auraChunk.drainAura(pos, moved);
|
|
|
|
} else {
|
2020-11-16 22:17:50 +01:00
|
|
|
moved = bestChunk.drainAura(bestPos, perSide);
|
2018-12-04 13:38:46 +01:00
|
|
|
auraChunk.storeAura(pos, moved);
|
|
|
|
}
|
|
|
|
toMove -= moved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-15 16:24:53 +01:00
|
|
|
public boolean appliesHere(LevelChunk chunk, IAuraChunk auraChunk, IAuraType type) {
|
2018-12-04 13:38:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
}
|