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;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Direction;
|
2018-12-04 13:38:46 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2020-11-16 22:17:50 +01:00
|
|
|
import net.minecraft.util.math.MathHelper;
|
2018-12-04 13:38:46 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
|
|
|
|
public class SpreadEffect implements IDrainSpotEffect {
|
|
|
|
|
|
|
|
public static final ResourceLocation NAME = new ResourceLocation(NaturesAura.MOD_ID, "spread");
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update(World world, Chunk chunk, IAuraChunk auraChunk, BlockPos pos, Integer spot) {
|
2020-11-16 22:17:50 +01:00
|
|
|
if (Math.abs(spot) < 500000 || Math.abs(IAuraChunk.getAuraInArea(world, pos, 25)) < 2000000)
|
2018-12-04 13:38:46 +01:00
|
|
|
return;
|
|
|
|
boolean drain = spot > 0;
|
2020-11-16 22:17:50 +01:00
|
|
|
int toMove = MathHelper.ceil(Math.abs(spot) * 0.72F);
|
|
|
|
int perSide = toMove / 6;
|
2018-12-04 13:38:46 +01:00
|
|
|
while (toMove > 0) {
|
|
|
|
BlockPos bestOffset = null;
|
|
|
|
int bestAmount = drain ? Integer.MAX_VALUE : Integer.MIN_VALUE;
|
2020-01-21 23:02:39 +01:00
|
|
|
for (Direction facing : Direction.values()) {
|
2018-12-04 13:38:46 +01:00
|
|
|
BlockPos offset = pos.offset(facing, 15);
|
2018-12-04 16:17:50 +01:00
|
|
|
if (world.isBlockLoaded(offset) && offset.getY() >= 0 && offset.getY() <= world.getHeight()) {
|
2018-12-04 13:56:48 +01:00
|
|
|
int amount = IAuraChunk.getAuraInArea(world, offset, 14);
|
|
|
|
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
|
|
|
|
|
|
|
BlockPos bestPos = drain ? IAuraChunk.getLowestSpot(world, bestOffset, 14, bestOffset)
|
|
|
|
: IAuraChunk.getHighestSpot(world, bestOffset, 14, bestOffset);
|
|
|
|
IAuraChunk bestChunk = IAuraChunk.getAuraChunk(world, bestPos);
|
|
|
|
|
|
|
|
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
|
|
|
|
public boolean appliesHere(Chunk chunk, IAuraChunk auraChunk, IAuraType type) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getName() {
|
|
|
|
return NAME;
|
|
|
|
}
|
|
|
|
}
|