This commit is contained in:
RePixelatedMC
2024-08-22 20:23:58 +02:00
parent 6b4575e75d
commit e5c818cf7b
3 changed files with 84 additions and 46 deletions

View File

@@ -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<Chunk> loadedChunks;
private HashMap<Types, Integer> bukkitLimits;
private Function<EntityType, Types> entityType;
private ConcurrentLinkedQueue<IrisMobPiece> 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<IrisMobPiece> shouldTick = IrisMobPiece::shouldTick;
Consumer<IrisMobPiece> tick = IrisMobPiece::tick;
Function<IrisMobPiece, List<Integer>> tickCosts = piece -> piece.getTickCosts(1);
Map<IrisMobPiece, List<Integer>> pieceTickCostsMap = pieces.stream()
.collect(Collectors.toMap(
Function.identity(),
tickCosts
));
Consumer<IrisMobPiece> tick = piece -> piece.tick(42);
pieces.stream()
.filter(shouldTick)
@@ -93,6 +111,36 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat
}
}
private void assignEnergyToPieces(LinkedHashMap<IrisMobPiece, List<Integer>> map) {
Supplier<LinkedHashMap<IrisMobPiece, List<Integer>>> sortedMapSupplier = new Supplier<>() {
private LinkedHashMap<IrisMobPiece, List<Integer>> cachedMap;
@Override
public LinkedHashMap<IrisMobPiece, List<Integer>> get() {
if (cachedMap == null) {
cachedMap = map.entrySet()
.stream()
.sorted(Map.Entry.<IrisMobPiece, List<Integer>>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<Integer,Integer> 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<Types, Integer> getBukkitLimits() {
HashMap<Types, Integer> 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<Chunk> getChunks() {
return loadedChunks;
}
@Override
public HashMap<Types, Integer> bukkitLimits() {
return bukkitLimits;

View File

@@ -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<Chunk> getChunks();
HashMap<Types, Integer> bukkitLimits();
double getEnergy();

View File

@@ -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<Integer> 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));
// }
// }
// }
}