From e5c818cf7b45be39b1f625ed3fa2589b16ad4048 Mon Sep 17 00:00:00 2001 From: RePixelatedMC <107539181+RePixelatedMC@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:23:58 +0200 Subject: [PATCH] save --- .../engine/service/EngineMobHandlerSVC.java | 67 +++++++++++++++++-- .../iris/util/mobs/IrisMobDataHandler.java | 4 ++ .../volmit/iris/util/mobs/IrisMobPiece.java | 59 ++++++---------- 3 files changed, 84 insertions(+), 46 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/engine/service/EngineMobHandlerSVC.java b/core/src/main/java/com/volmit/iris/engine/service/EngineMobHandlerSVC.java index f8aad661d..83d1db961 100644 --- a/core/src/main/java/com/volmit/iris/engine/service/EngineMobHandlerSVC.java +++ b/core/src/main/java/com/volmit/iris/engine/service/EngineMobHandlerSVC.java @@ -5,11 +5,12 @@ import com.volmit.iris.core.nms.INMS; import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.object.IrisEngineService; import com.volmit.iris.util.format.Form; -import com.volmit.iris.util.math.M; import com.volmit.iris.util.mobs.IrisMobDataHandler; import com.volmit.iris.util.mobs.IrisMobPiece; import com.volmit.iris.util.scheduling.Looper; import com.volmit.iris.util.scheduling.PrecisionStopwatch; +import io.lumine.mythic.bukkit.utils.lib.jooq.impl.QOM; +import org.bukkit.Chunk; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.EntityType; @@ -17,17 +18,21 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerChangedWorldEvent; -import java.util.HashMap; +import java.util.*; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.stream.Collectors; public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDataHandler { private int id; + public double energyMax; public double energy; + private HashSet loadedChunks; private HashMap bukkitLimits; private Function entityType; private ConcurrentLinkedQueue pieces; @@ -42,6 +47,7 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat this.id = engine.getCacheID(); this.pieces = new ConcurrentLinkedQueue<>(); this.entityType = (entityType) -> Types.valueOf(INMS.get().getMobCategory(entityType)); + this.loadedChunks = new HashSet<>(); this.bukkitLimits = getBukkitLimits(); new Ticker(); @@ -72,9 +78,21 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat PrecisionStopwatch stopwatch = new PrecisionStopwatch(); stopwatch.begin(); + loadedChunks = Arrays.stream(getEngine().getWorld().realWorld().getLoadedChunks()) + .collect(Collectors.toCollection(HashSet::new)); + fixEnergy(); + Predicate shouldTick = IrisMobPiece::shouldTick; - Consumer tick = IrisMobPiece::tick; + Function> tickCosts = piece -> piece.getTickCosts(1); + + Map> pieceTickCostsMap = pieces.stream() + .collect(Collectors.toMap( + Function.identity(), + tickCosts + )); + + Consumer tick = piece -> piece.tick(42); pieces.stream() .filter(shouldTick) @@ -93,6 +111,36 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat } } + private void assignEnergyToPieces(LinkedHashMap> map) { + Supplier>> sortedMapSupplier = new Supplier<>() { + private LinkedHashMap> cachedMap; + + @Override + public LinkedHashMap> get() { + if (cachedMap == null) { + cachedMap = map.entrySet() + .stream() + .sorted(Map.Entry.>comparingByValue( + Comparator.comparingInt(list -> list.stream().mapToInt(Integer::intValue).sum()) + ).reversed()) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (e1, e2) -> e1, // Handle potential duplicates by keeping the first entry + LinkedHashMap::new + )); + } + return cachedMap; + } + }; + + Function viewHistory = (history) -> map.values().stream() + .mapToInt(list -> list.isEmpty() ? 0 : list.get(history)) // Extract the first element or use 0 if the list is empty + .sum(); + + + } + @EventHandler(priority = EventPriority.NORMAL) public void on(PlayerChangedWorldEvent event) { if (!engine.getWorld().tryGetRealWorld()) { @@ -120,17 +168,17 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat } } - private HashMap getBukkitLimits() { HashMap temp = new HashMap<>(); FileConfiguration fc = new YamlConfiguration(); - fc.getConfigurationSection("spawn-limits").getKeys(false).forEach(key -> temp.put(Types.valueOf(key), fc.getInt(key))); + var section = fc.getConfigurationSection("spawn-limits"); + if (section != null) + section.getKeys(false).forEach(key -> temp.put(Types.valueOf(key), section.getInt(key))); return temp; } - private void fixEnergy() { - energy = M.clip(energy, 1D, engine.getDimension().getEnergy().evaluate(null, engine.getData(), energy)); + energyMax = engine.getDimension().getEnergy().evaluate(null, engine.getData(), energy); } @Override @@ -143,6 +191,11 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat return engine; } + @Override + public HashSet getChunks() { + return loadedChunks; + } + @Override public HashMap bukkitLimits() { return bukkitLimits; diff --git a/core/src/main/java/com/volmit/iris/util/mobs/IrisMobDataHandler.java b/core/src/main/java/com/volmit/iris/util/mobs/IrisMobDataHandler.java index 05c607e07..d8ad44b07 100644 --- a/core/src/main/java/com/volmit/iris/util/mobs/IrisMobDataHandler.java +++ b/core/src/main/java/com/volmit/iris/util/mobs/IrisMobDataHandler.java @@ -3,9 +3,11 @@ package com.volmit.iris.util.mobs; import com.google.common.util.concurrent.AtomicDouble; import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.service.EngineMobHandlerSVC; +import org.bukkit.Chunk; import org.bukkit.entity.EntityType; import java.util.HashMap; +import java.util.HashSet; import java.util.function.Function; public interface IrisMobDataHandler { @@ -26,6 +28,8 @@ public interface IrisMobDataHandler { Engine getEngine(); + HashSet getChunks(); + HashMap bukkitLimits(); double getEnergy(); diff --git a/core/src/main/java/com/volmit/iris/util/mobs/IrisMobPiece.java b/core/src/main/java/com/volmit/iris/util/mobs/IrisMobPiece.java index 0f77d247f..30e6470b6 100644 --- a/core/src/main/java/com/volmit/iris/util/mobs/IrisMobPiece.java +++ b/core/src/main/java/com/volmit/iris/util/mobs/IrisMobPiece.java @@ -12,6 +12,8 @@ import jakarta.activation.DataHandler; import lombok.Getter; import org.bukkit.entity.Player; +import java.util.Collections; +import java.util.List; import java.util.UUID; @@ -29,6 +31,8 @@ public class IrisMobPiece { /** * Predict if it should tick the player or if it should skip it for this round. + * + This method should be very fast. + * Its supposed to be a faster alternative to the getTickCosts method. * @return true = should tick */ public boolean shouldTick() { @@ -38,12 +42,25 @@ public class IrisMobPiece { } /** - * Ticks the current player + * Returns the estimated Energy cost to run this tick. + * Handy for if you are on a resource limit and need to prioritize who gets ticked and who not and what to expect. + * @param predict > The Prediction size on how far it should predict + * @return The Predictions it made. */ - public void tick() { - lastRanPlayer = M.ms(); + public List getTickCosts(int predict) { + return Collections.singletonList(0); + + } + + /** + * Ticks the current player + * @param energy the energy given for the tick + */ + public void tick(int energy) { + lastRanPlayer = M.ms(); + } @@ -54,40 +71,4 @@ public class IrisMobPiece { public void close() { } - -// private void spawn(IrisPosition c, IrisEntitySpawn i) { -// boolean allow = true; -// -// if (!i.getReferenceSpawner().getMaximumRatePerChunk().isInfinite()) { -// allow = false; -// IrisEngineChunkData cd = dataHandler.getEngine().getEngineData().getChunk(c.getX() >> 4, c.getZ() >> 4); -// IrisEngineSpawnerCooldown sc = null; -// for (IrisEngineSpawnerCooldown j : cd.getCooldowns()) { -// if (j.getSpawner().equals(i.getReferenceSpawner().getLoadKey())) { -// sc = j; -// break; -// } -// } -// -// if (sc == null) { -// sc = new IrisEngineSpawnerCooldown(); -// sc.setSpawner(i.getReferenceSpawner().getLoadKey()); -// cd.getCooldowns().add(sc); -// } -// -// if (sc.canSpawn(i.getReferenceSpawner().getMaximumRatePerChunk())) { -// sc.spawn(dataHandler.getEngine()); -// allow = true; -// } -// } -// -// if (allow) { -// int s = i.spawn(dataHandler.getEngine(), c, RNG.r); -// actuallySpawned += s; -// if (s > 0) { -// getCooldown(i.getReferenceSpawner()).spawn(dataHandler.getEngine()); -// energy -= s * ((i.getEnergyMultiplier() * i.getReferenceSpawner().getEnergyMultiplier() * 1)); -// } -// } -// } } \ No newline at end of file