some more caching for some more performance improvements!

This commit is contained in:
Ell 2022-05-20 20:29:40 +02:00
parent 5bee48bdfc
commit 8770ac6cd7
2 changed files with 38 additions and 8 deletions

View file

@ -152,10 +152,11 @@ public class InternalHooks implements NaturesAuraAPI.IInternalHooks {
public BlockPos getLowestAuraDrainSpot(Level level, BlockPos pos, int radius, BlockPos defaultSpot) {
var lowestAmount = new MutableInt(Integer.MAX_VALUE);
var lowestSpot = new MutableObject<BlockPos>();
this.getAuraSpotsInArea(level, pos, radius, (blockPos, drainSpot) -> {
if (drainSpot < lowestAmount.intValue()) {
lowestAmount.setValue(drainSpot);
lowestSpot.setValue(blockPos);
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, c -> {
var spot = c.getLowestAndHighestSpots(pos, radius)[0];
if (spot.getRight() < lowestAmount.intValue()) {
lowestAmount.setValue(spot.getRight());
lowestSpot.setValue(spot.getLeft());
}
});
var lowest = lowestSpot.getValue();
@ -168,10 +169,11 @@ public class InternalHooks implements NaturesAuraAPI.IInternalHooks {
public BlockPos getHighestAuraDrainSpot(Level level, BlockPos pos, int radius, BlockPos defaultSpot) {
var highestAmount = new MutableInt(Integer.MIN_VALUE);
var highestSpot = new MutableObject<BlockPos>();
this.getAuraSpotsInArea(level, pos, radius, (blockPos, drainSpot) -> {
if (drainSpot > highestAmount.intValue()) {
highestAmount.setValue(drainSpot);
highestSpot.setValue(blockPos);
Helper.getAuraChunksWithSpotsInArea(level, pos, radius, c -> {
var spot = c.getLowestAndHighestSpots(pos, radius)[1];
if (spot.getRight() > highestAmount.intValue()) {
highestAmount.setValue(spot.getRight());
highestSpot.setValue(spot.getLeft());
}
});
var highest = highestSpot.getValue();

View file

@ -20,6 +20,7 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.chunk.LevelChunk;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
@ -34,6 +35,7 @@ public class AuraChunk implements IAuraChunk {
private final IAuraType type;
private final Map<BlockPos, MutableInt> drainSpots = new ConcurrentHashMap<>();
private final Table<BlockPos, Integer, Pair<Integer, Integer>> auraAndSpotAmountCache = HashBasedTable.create();
private final Table<BlockPos, Integer, Pair<BlockPos, Integer>[]> limitSpotCache = HashBasedTable.create();
private final List<IDrainSpotEffect> effects = new ArrayList<>();
private boolean needsSync;
@ -143,6 +145,7 @@ public class AuraChunk implements IAuraChunk {
this.chunk.setUnsaved(true);
this.needsSync = true;
this.auraAndSpotAmountCache.clear();
this.limitSpotCache.clear();
this.addOrRemoveAsActive();
}
@ -194,6 +197,31 @@ public class AuraChunk implements IAuraChunk {
return ret;
}
public Pair<BlockPos, Integer>[] getLowestAndHighestSpots(BlockPos pos, int radius) {
var ret = this.limitSpotCache.get(pos, radius);
if (ret == null) {
var lowestSpot = new MutableObject<BlockPos>();
var highestSpot = new MutableObject<BlockPos>();
var lowestAmount = new MutableInt(Integer.MAX_VALUE);
var highestAmount = new MutableInt(Integer.MIN_VALUE);
this.getSpots(pos, radius, (p, i) -> {
if (i > highestAmount.intValue()) {
highestAmount.setValue(i);
highestSpot.setValue(p);
}
if (i < lowestAmount.intValue()) {
lowestAmount.setValue(i);
lowestSpot.setValue(p);
}
});
ret = new Pair[]{
Pair.of(lowestSpot.getValue(), lowestAmount.intValue()),
Pair.of(highestSpot.getValue(), highestAmount.intValue())};
this.limitSpotCache.put(pos, radius, ret);
}
return ret;
}
public void getActiveEffectIcons(Player player, Map<ResourceLocation, Tuple<ItemStack, Boolean>> icons) {
for (var effect : this.effects) {
var alreadyThere = icons.get(effect.getName());