From 805523d0696355cba02cddd7c868d45e2d71bb8f Mon Sep 17 00:00:00 2001 From: RePixelatedMC <107539181+RePixelatedMC@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:07:02 +0200 Subject: [PATCH] Sync! --- .../com/volmit/iris/core/IrisSettings.java | 9 ++ .../iris/core/commands/CommandDeveloper.java | 9 +- .../com/volmit/iris/core/nms/INMSBinding.java | 5 + .../engine/service/EngineMobHandlerSVC.java | 98 ++++++++++++++----- .../iris/util/mobs/IrisMobDataHandler.java | 6 ++ .../volmit/iris/util/mobs/IrisMobPiece.java | 24 ++++- .../iris/core/nms/v1_20_R3/NMSBinding.java | 31 +++++- 7 files changed, 141 insertions(+), 41 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/core/IrisSettings.java b/core/src/main/java/com/volmit/iris/core/IrisSettings.java index bf330c389..57c547568 100644 --- a/core/src/main/java/com/volmit/iris/core/IrisSettings.java +++ b/core/src/main/java/com/volmit/iris/core/IrisSettings.java @@ -43,6 +43,7 @@ public class IrisSettings { private IrisSettingsStudio studio = new IrisSettingsStudio(); private IrisSettingsPerformance performance = new IrisSettingsPerformance(); private IrisWorldDump worldDump = new IrisWorldDump(); + private IrisWorldSettings irisWorldSettings = new IrisWorldSettings(); public static int getThreadCount(int c) { return switch (c) { @@ -199,4 +200,12 @@ public class IrisSettings { public static class IrisWorldDump { public int mcaCacheSize = 3; } + + // todo: Goal:Have these as the default world settings and when put in bukkit.yml it will again overwrite that world from these. + @Data + public static class IrisWorldSettings { + public boolean dynamicEntityAdjustments; + + + } } diff --git a/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java b/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java index f1d51edde..20296b8b3 100644 --- a/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java +++ b/core/src/main/java/com/volmit/iris/core/commands/CommandDeveloper.java @@ -19,6 +19,7 @@ package com.volmit.iris.core.commands; import com.volmit.iris.Iris; +import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.loader.IrisData; import com.volmit.iris.core.tools.IrisPackBenchmarking; import com.volmit.iris.core.tools.IrisToolbelt; @@ -130,15 +131,9 @@ public class CommandDeveloper implements DecreeExecutor { } @Decree(description = "test") - public void devtest() { + public void test() { try { - for (File mcafile : new File("rrtrender1/region").listFiles()) { - MCAFile mca = MCAUtil.read(mcafile); - int c = 0; - - } - } catch (Exception e) { e.printStackTrace(); } diff --git a/core/src/main/java/com/volmit/iris/core/nms/INMSBinding.java b/core/src/main/java/com/volmit/iris/core/nms/INMSBinding.java index c5802717d..0ae69bc2b 100644 --- a/core/src/main/java/com/volmit/iris/core/nms/INMSBinding.java +++ b/core/src/main/java/com/volmit/iris/core/nms/INMSBinding.java @@ -115,6 +115,11 @@ public interface INMSBinding { Vector3d getBoundingbox(org.bukkit.entity.EntityType entity); + default String getMobCategory(EntityType entityType) { + // todo: Update to other versions! + return null; + } + Entity spawnEntity(Location location, EntityType type, CreatureSpawnEvent.SpawnReason reason); Color getBiomeColor(Location location, BiomeColor type); 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 939760dd2..7eb9c3798 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 @@ -1,29 +1,50 @@ package com.volmit.iris.engine.service; -import com.google.common.util.concurrent.AtomicDouble; import com.volmit.iris.Iris; +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.adapters.BukkitEntity; +import it.unimi.dsi.fastutil.Hash; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerChangedWorldEvent; +import java.io.File; +import java.util.HashMap; +import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; -import java.util.function.Supplier; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDataHandler { private int id; public double energy; - private Supplier supplier; + private HashMap bukkitLimits; + private Function entityType; private ConcurrentLinkedQueue pieces; - + public enum Types { + monsters, + animals, + water_animals, + water_ambient, + water_underground_creature, + axolotls, + ambient + } public EngineMobHandlerSVC(Engine engine) { super(engine); @@ -34,7 +55,9 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat this.id = engine.getCacheID(); this.pieces = new ConcurrentLinkedQueue<>(); - this.supplier = null; + this.entityType = (entityType) -> Types.valueOf(INMS.get().getMobCategory(entityType)); + this.bukkitLimits = new HashMap<>(); + //new Ticker(); } @@ -42,23 +65,12 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat @Override public void onDisable(boolean hotload) { - } - - public Ticker tick() { - return new EngineMobHandlerSVC.Ticker(() -> { - - - - return null; - }); - - } private class Ticker extends Looper { private final CountDownLatch exit = new CountDownLatch(1); - private Ticker(Supplier supplier) { + private Ticker() { setPriority(Thread.NORM_PRIORITY); start(); Iris.debug("Started Mob Engine for: " + engine.getName()); @@ -76,10 +88,15 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat stopwatch.begin(); fixEnergy(); + Predicate shouldExecutePredicate = IrisMobPiece::shouldTick; + Consumer executeMethod = IrisMobPiece::tick; - + pieces.stream() + .filter(shouldExecutePredicate) + .forEach(executeMethod); stopwatch.end(); + Iris.info("Took: " + Form.f(stopwatch.getMilliseconds())); double millis = stopwatch.getMilliseconds(); int size = pieces.size(); wait = size == 0 ? 50L : (long) Math.max(50d / size - millis, 0); @@ -108,21 +125,52 @@ public class EngineMobHandlerSVC extends IrisEngineService implements IrisMobDat } if (event.getFrom().equals(world)) { -// pieces.removeIf(piece -> { -// if (piece.getOwner().equals(player.getUniqueId())) { -// piece.close(); -// return true; -// } -// return false; -// }); + pieces.removeIf(piece -> { + if (piece.getOwner().equals(player.getUniqueId())) { + piece.close(); + return true; + } + return false; + }); } } + private HashMap getBukkitLimits() { + HashMap temp = new HashMap<>(); + + + FileConfiguration fc = new YamlConfiguration(); + try { + fc.load(new File("bukkit.yml")); + ConfigurationSection section = fc.getConfigurationSection("spawn-limits"); + if (section == null) { + throw new NoSuchFieldException("spawn-limits not found!"); + } + + for (String s : section.getKeys(false)) { + try { + ConfigurationSection entry = section.getConfigurationSection(s); + } catch (Exception e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + Iris.reportError(e); + } + return null; + } + + private void fixEnergy() { energy = M.clip(energy, 1D, engine.getDimension().getEnergy().evaluate(null, engine.getData(), energy)); } + @Override + public Function getMobType() { + return entityType; + } + @Override public Engine getEngine() { return engine; 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 658d03f38..a69447532 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 @@ -2,9 +2,15 @@ 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.entity.EntityType; + +import java.util.function.Function; public interface IrisMobDataHandler { + Function getMobType(); + Engine getEngine(); 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 28154b46c..5d1e05ab5 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 @@ -14,23 +14,39 @@ public class IrisMobPiece { @Getter private final Player player; private IrisMobDataHandler dataHandler; - public long lastRanPlayer; + private long lastRanPlayer; public IrisMobPiece(Player player, IrisMobDataHandler dh) { this.player = player; this.dataHandler = dh; } + + /** + * Predict if it should tick the player or if it should skip it for this round. + * @return true = should tick + */ + public boolean shouldTick() { + + return true; + + } + + /** + * Ticks the current player + */ public void tick() { lastRanPlayer = M.ms(); - - // Use the engine instance as needed, but without a direct reference - // For example: engine.getDimension().getEnergy().evaluate(...) } + public UUID getOwner() { return player.getUniqueId(); } + + public void close() { + + } } \ No newline at end of file diff --git a/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/NMSBinding.java b/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/NMSBinding.java index 8797d3867..ec41a20f7 100644 --- a/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/NMSBinding.java +++ b/nms/v1_20_R3/src/main/java/com/volmit/iris/core/nms/v1_20_R3/NMSBinding.java @@ -59,11 +59,16 @@ import net.bytebuddy.implementation.bytecode.member.MethodInvocation; import net.bytebuddy.matcher.ElementMatchers; import net.minecraft.core.IdMapper; import net.minecraft.core.MappedRegistry; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.RegistryOps; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.progress.ChunkProgressListener; import net.minecraft.util.GsonHelper; import net.minecraft.world.RandomSequences; +import net.minecraft.world.entity.MobCategory; +import net.minecraft.world.entity.MobSpawnType; +import net.minecraft.world.entity.MobType; +import net.minecraft.world.entity.ai.behavior.TryLaySpawnOnWaterNearLand; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelHeightAccessor; @@ -98,6 +103,8 @@ import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_20_R3.util.CraftNamespacedKey; import org.bukkit.entity.Dolphin; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityCategory; +import org.bukkit.entity.Mob; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.generator.BiomeProvider; import org.bukkit.generator.ChunkGenerator; @@ -230,13 +237,13 @@ public class NMSBinding implements INMSBinding { } @Contract(value = "null, _, _ -> null", pure = true) - private Object convertFromTag(net.minecraft.nbt.Tag tag, int depth, int maxDepth) { + private Object convertFromTag(Tag tag, int depth, int maxDepth) { if (tag == null || depth > maxDepth) return null; if (tag instanceof CollectionTag collection) { KList list = new KList<>(); for (Object i : collection) { - if (i instanceof net.minecraft.nbt.Tag t) + if (i instanceof Tag t) list.add(convertFromTag(t, depth + 1, maxDepth)); else list.add(i); } @@ -303,7 +310,7 @@ public class NMSBinding implements INMSBinding { return tag; } if (object instanceof List list) { - var tag = new net.minecraft.nbt.ListTag(); + var tag = new ListTag(); for (var i : list) { tag.add(convertToTag(i, depth + 1, maxDepth)); } @@ -855,6 +862,20 @@ public class NMSBinding implements INMSBinding { return new Color(rgba, true); } + @Override + public String getMobCategory(org.bukkit.entity.EntityType bukkitEntity) { + var test = registry().registry(Registries.ENTITY_TYPE).orElse(null); + for (EntityType entity : test) { + MobCategory category = entity.getCategory(); + var name = entity.getDescriptionId().replaceAll("entity.minecraft.", ""); + var bukkit = bukkitEntity.name(); + if(name.equalsIgnoreCase(bukkit)) { + return category.getName(); + } + } + return null; + } + @Override public KList getStructureKeys() { KList keys = new KList<>(); @@ -965,12 +986,12 @@ public class NMSBinding implements INMSBinding { new ByteBuddy() .redefine(CraftServer.class) - .visit(Advice.to(CraftServerAdvice.class).on(ElementMatchers.isMethod().and(ElementMatchers.takesArguments(WorldCreator.class)))) + .visit(Advice.to(CraftServerAdvice.class).on(ElementMatchers.isMethod().and(takesArguments(WorldCreator.class)))) .make() .load(CraftServer.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent()); new ByteBuddy() .redefine(ServerLevel.class) - .visit(Advice.to(ServerLevelAdvice.class).on(ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(MinecraftServer.class, Executor.class, LevelStorageSource.LevelStorageAccess.class, + .visit(Advice.to(ServerLevelAdvice.class).on(ElementMatchers.isConstructor().and(takesArguments(MinecraftServer.class, Executor.class, LevelStorageSource.LevelStorageAccess.class, PrimaryLevelData.class, ResourceKey.class, LevelStem.class, ChunkProgressListener.class, boolean.class, long.class, List.class, boolean.class, RandomSequences.class, World.Environment.class, ChunkGenerator.class, BiomeProvider.class)))) .make()