From 37d538676014e7d13a7b5674d5f6d90173060ecc Mon Sep 17 00:00:00 2001 From: Reider745 <70357814+Reider745@users.noreply.github.com> Date: Sat, 26 Oct 2024 11:10:44 +0300 Subject: [PATCH] fixed: getEntityTypeName & PlayerActor & ClassLoader --- .../com/reider745/entity/EntityMethod.java | 29 ++++-- .../reider745/entity/PlayerActorMethods.java | 2 +- .../com/reider745/event/EventListener.java | 16 ++-- .../launcher/env/ClassLoaderPatch.java | 4 +- .../horizon/modloader/JavaEnvironment.java | 10 ++ .../horizon/modloader/java/JavaLibrary.java | 17 +++- .../zhekasmirnov/innercore/api/NativeAPI.java | 5 + .../innercore/api/NativeCallback.java | 4 +- .../mod/adaptedscript/AdaptedScriptAPI.java | 15 +-- .../innercore/api/runtime/ChunkManager.java | 95 ------------------- .../innercore/coreengine/core-engine.dev.js | 74 ++++++++++++++- src/main/resources/zotecore.yml | 2 +- 12 files changed, 134 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/com/zhekasmirnov/innercore/api/runtime/ChunkManager.java diff --git a/src/main/java/com/reider745/entity/EntityMethod.java b/src/main/java/com/reider745/entity/EntityMethod.java index f04e48792..e3b5301c5 100644 --- a/src/main/java/com/reider745/entity/EntityMethod.java +++ b/src/main/java/com/reider745/entity/EntityMethod.java @@ -22,6 +22,7 @@ import cn.nukkit.math.Vector3; import cn.nukkit.nbt.NBTIO; import cn.nukkit.nbt.tag.CompoundTag; +import cn.nukkit.network.protocol.AddEntityPacket; import cn.nukkit.network.protocol.MovePlayerPacket; import cn.nukkit.potion.Effect; import cn.nukkit.utils.Identifier; @@ -38,10 +39,7 @@ import com.zhekasmirnov.innercore.api.constants.EntityType; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.function.Consumer; import java.util.function.Function; @@ -110,7 +108,7 @@ public static Player[] getPlayersByIds(long[] entityUids) { } public static boolean isValid(Entity entity) { - return entity != null && entity.isValid() && !entity.isClosed(); + return entity != null && entity.isValid(); } private static T validateThen(long entityUid, Function then, T defaultValue) { @@ -420,14 +418,25 @@ public static int getEntityType(long entityUid) { return validateThen(entityUid, entity -> getEntityTypeDirect(entity), -1); } + private static final HashMap ids = new HashMap<>(); + + static { + AddEntityPacket.setupLegacyIdentifiers(ids, InnerCoreServer.PROTOCOL); + } + public static String getEntityTypeName(long entityUid) { - return validateThen(entityUid, entity -> { + final Entity entity = getEntityById(entityUid); + if(entity != null){ Identifier identifier = entity.getIdentifier(); - if (identifier == null && entity.isPlayer) { - identifier = Identifier.of("minecraft", "player"); + if (identifier == null) { + final String result = ids.get(entity.getNetworkId()); + if(result != null) + return result; + return ""; } - return identifier != null ? identifier + "<>" : null; - }, null); + return identifier + "<>"; + } + return ""; } public static CompoundTag getEntityCompoundTag(long entityUid) { diff --git a/src/main/java/com/reider745/entity/PlayerActorMethods.java b/src/main/java/com/reider745/entity/PlayerActorMethods.java index aee5538c9..176f990af 100644 --- a/src/main/java/com/reider745/entity/PlayerActorMethods.java +++ b/src/main/java/com/reider745/entity/PlayerActorMethods.java @@ -22,7 +22,7 @@ public class PlayerActorMethods { public static boolean isValid(Player player, boolean checkOnline) { - if (!EntityMethod.isValid(player)) { + if (!EntityMethod.isValid(player) || player.getInventory() == null) { return false; } if (checkOnline) { diff --git a/src/main/java/com/reider745/event/EventListener.java b/src/main/java/com/reider745/event/EventListener.java index e5801eea3..f35e10206 100644 --- a/src/main/java/com/reider745/event/EventListener.java +++ b/src/main/java/com/reider745/event/EventListener.java @@ -51,10 +51,7 @@ import com.zhekasmirnov.apparatus.multiplayer.server.ModdedServer; import oshi.hardware.NetworkIF; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.UUID; +import java.util.*; import com.reider745.api.CallbackHelper; import com.reider745.api.CustomManager; @@ -567,10 +564,10 @@ public void onItemDespawn(ItemDespawnEvent event){ event.setCancelled(); } - private static final ArrayList statesEvents = new ArrayList<>(); - public static void addChunkStateChagedListener(int state) { + private static final HashMap statesEvents = new HashMap<>(); + public static void setChunkStateChangedCallbackEnabled(int state, boolean enabled) { if(state < -1 || state > 9) return; - if(!statesEvents.contains(state)) statesEvents.add(state); + statesEvents.put(state, enabled); } @EventHandler @@ -581,13 +578,14 @@ public void onChunkLoad(ChunkLoadEvent event){ final int x = chunk.getX(), z = chunk.getZ(); for(int i = 1, preState = 0;i < 10;i+=1, preState++){ - NativeCallback.onChunkStateChaged(dimension, x, z, i, preState, false); + if(statesEvents.containsKey(i)) + NativeCallback.onChunkStateChaged(dimension, x, z, i, preState, false); } } @EventHandler public void onChunkUnload(ChunkUnloadEvent event){ - if(statesEvents.contains(-1) || statesEvents.contains(0)){ + if(statesEvents.containsKey(-1) || statesEvents.containsKey(0)){ final FullChunk chunk = event.getChunk(); NativeCallback.onChunkStateChaged( FakeDimensions.getFakeIdForLevel(event.getLevel()), diff --git a/src/main/java/com/zhekasmirnov/horizon/launcher/env/ClassLoaderPatch.java b/src/main/java/com/zhekasmirnov/horizon/launcher/env/ClassLoaderPatch.java index 472fa6393..51dfc70ac 100644 --- a/src/main/java/com/zhekasmirnov/horizon/launcher/env/ClassLoaderPatch.java +++ b/src/main/java/com/zhekasmirnov/horizon/launcher/env/ClassLoaderPatch.java @@ -127,13 +127,13 @@ public static synchronized void addDexPath(ClassLoader classLoader, File file, b Dex2jar.from(file).to(jarFile.toPath()); } } - addClasspath(classLoader, jarFile); + //addClasspath(classLoader, jarFile); return; } catch (CannotCompileException | NotFoundException | IOException | BadBytecode e) { Logger.error("ClassLoaderPath", "Java library is broken or it class file is not supported yet!"); Logger.error("ClassLoaderPath", e); } - addClasspath(classLoader, file); + //addClasspath(classLoader, file); } @SuppressWarnings("unused") diff --git a/src/main/java/com/zhekasmirnov/horizon/modloader/JavaEnvironment.java b/src/main/java/com/zhekasmirnov/horizon/modloader/JavaEnvironment.java index cf48d6c72..95511206e 100644 --- a/src/main/java/com/zhekasmirnov/horizon/modloader/JavaEnvironment.java +++ b/src/main/java/com/zhekasmirnov/horizon/modloader/JavaEnvironment.java @@ -36,5 +36,15 @@ public void build() { } } } + + for (JavaLibrary javaLibrary : javaLibraries) { + if (!javaLibrary.isInitialized()) { + try { + javaLibrary.postInitialize(); + } catch (Throwable err) { + Logger.error("LOAD", "failed to load java library " + javaLibrary.getDirectory(), err); + } + } + } } } diff --git a/src/main/java/com/zhekasmirnov/horizon/modloader/java/JavaLibrary.java b/src/main/java/com/zhekasmirnov/horizon/modloader/java/JavaLibrary.java index e7f4e7729..cf9871e1b 100644 --- a/src/main/java/com/zhekasmirnov/horizon/modloader/java/JavaLibrary.java +++ b/src/main/java/com/zhekasmirnov/horizon/modloader/java/JavaLibrary.java @@ -43,8 +43,10 @@ public boolean isInitialized() { return this.initialized; } + private static final List urls = new ArrayList<>(); + private static URLClassLoader classLoader; + public void initialize() { - final List urls = new ArrayList<>(); for (File dexFile : this.dexFiles) { try { final String filePath = dexFile.getAbsolutePath(); @@ -54,11 +56,16 @@ public void initialize() { throw new RuntimeException(e); } } - - final URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0]), InnerCoreServer.class.getClassLoader()); - RhinoOverrides.ClassLoaderPool.addClassLoader(classLoader); + for (File dexFile : this.dexFiles) - ClassLoaderPatch.addDexPath(classLoader, dexFile, true); + ClassLoaderPatch.addDexPath(null, dexFile, true); + } + + public void postInitialize() { + if(classLoader == null){ + classLoader = new URLClassLoader(urls.toArray(new URL[0]), InnerCoreServer.class.getClassLoader()); + RhinoOverrides.ClassLoaderPool.addClassLoader(classLoader); + } HashMap data = new HashMap<>(); for (String name : this.directory.getBootClassNames()) { diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeAPI.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeAPI.java index a0b1bf3db..73046a70b 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeAPI.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeAPI.java @@ -10,6 +10,7 @@ import com.reider745.api.CallbackHelper; import com.reider745.api.ClientOnly; import com.reider745.entity.EntityMethod; +import com.reider745.event.EventListener; import com.reider745.item.ItemMethod; import com.reider745.world.BiomesMethods; import com.reider745.world.WorldMethod; @@ -1087,4 +1088,8 @@ public static long getTarget(long entity) { public static void setTarget(long entity, long target) { EntityMethod.setTarget(entity, target); } + + public static void setChunkStateChangedCallbackEnabled(int state, boolean b) { + EventListener.setChunkStateChangedCallbackEnabled(state, b); + } } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java b/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java index 338a283e3..43213e3fb 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/NativeCallback.java @@ -854,12 +854,12 @@ public static void onCustomDimensionTransfer(long entity, int from, int to) { } public static void onChunkStateChaged(int dimension, int chunkX, int chunkZ, int state, int preState, boolean discarded) { - ChunkManager.onChunkStateChaged(dimension, chunkX, chunkZ, state, preState, discarded); + Callback.invokeAPICallback("ChunkLoadingStateChanged", chunkX, chunkZ, dimension, preState, state, discarded); } @ClientOnly public static void onLocalChunkStateChaged(int dimension, int chunkX, int chunkZ, int state, int preState, boolean discarded) { - ChunkManager.onLocalChunkStateChaged(dimension, chunkX, chunkZ, state, preState, discarded); + Callback.invokeAPICallback("LocalChunkLoadingStateChanged", chunkX, chunkZ, dimension, preState, state, discarded); } /* NETWORK CALLBACKS */ diff --git a/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java b/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java index 72c9256da..a59dbd6f0 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/mod/adaptedscript/AdaptedScriptAPI.java @@ -39,7 +39,6 @@ import com.zhekasmirnov.innercore.api.nbt.NativeListTag; import com.zhekasmirnov.innercore.api.nbt.NbtDataType; import com.zhekasmirnov.innercore.api.particles.ParticleRegistry; -import com.zhekasmirnov.innercore.api.runtime.ChunkManager; import com.zhekasmirnov.innercore.api.runtime.LevelInfo; import com.zhekasmirnov.innercore.api.runtime.MainThreadQueue; import com.zhekasmirnov.innercore.api.runtime.other.ArmorRegistry; @@ -223,18 +222,14 @@ public static void error(String tag, String message, Object error) { } } - @JSStaticFunction - public static void addListenerChunkStateChaged(Function listener, NativeArray states){ - ChunkManager.addListenerChunkStateChaged(listener, states); - } - - @JSStaticFunction - public static void addLocalListenerChunkStateChaged(Function listener, NativeArray states){ - ChunkManager.addLocalListenerChunkStateChaged(listener, states); - } @APIStaticModule public static class Level { + @JSStaticFunction + public static void setChunkStateChangeCallbackEnabled(int state) { + NativeAPI.setChunkStateChangedCallbackEnabled(state, true); + } + @JSStaticFunction public static void setBlockChangeCallbackEnabled(int id, boolean enabled) { NativeAPI.setBlockChangeCallbackEnabled(id, enabled); diff --git a/src/main/java/com/zhekasmirnov/innercore/api/runtime/ChunkManager.java b/src/main/java/com/zhekasmirnov/innercore/api/runtime/ChunkManager.java deleted file mode 100644 index 5bd1d2005..000000000 --- a/src/main/java/com/zhekasmirnov/innercore/api/runtime/ChunkManager.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.zhekasmirnov.innercore.api.runtime; - -import com.reider745.event.EventListener; -import com.zhekasmirnov.apparatus.util.Java8BackComp; -import com.zhekasmirnov.innercore.mod.executable.Compiler; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Function; -import org.mozilla.javascript.NativeArray; -import org.mozilla.javascript.Scriptable; - -import java.util.HashMap; -import java.util.LinkedList; - -public class ChunkManager { - public static void addChunkStateChagedListener(int state){ - EventListener.addChunkStateChagedListener(state); - } - - public interface IChunkStateChanged { - void onChunkStateChaged(int chunkX, int chunkZ, int dimension, int preState, int state, boolean discarded); - } - - private static final HashMap> serverStatesListener = new HashMap<>(); - private static final HashMap> localStatesListener = new HashMap<>(); - - private static final HashMap> serverJsStatesListener = new HashMap<>(); - private static final HashMap> localJsStatesListener = new HashMap<>(); - - public static void addListenerChunkStateChaged(IChunkStateChanged changed, int[] states){ - for(int state : states) - Java8BackComp.computeIfAbsent(serverStatesListener, state, _state -> { - addChunkStateChagedListener(state); - return new LinkedList<>(); - }).add(changed); - } - - public static void addListenerChunkStateChaged(Function changed, NativeArray states){ - for(int i = 0;i < states.size();i++) { - int state = ((Number) states.get(i)).intValue(); - - Java8BackComp.computeIfAbsent(serverJsStatesListener, state, _state -> { - addChunkStateChagedListener(state); - return new LinkedList<>(); - }).add(changed); - } - } - - public static void addLocalListenerChunkStateChaged(IChunkStateChanged changed, int[] states){ - for(int state : states) - Java8BackComp.computeIfAbsent(localStatesListener, state, _state -> { - addChunkStateChagedListener(state); - return new LinkedList<>(); - }).add(changed); - } - - public static void addLocalListenerChunkStateChaged(Function changed, NativeArray states){ - for(int i = 0;i < states.size();i++) { - int state = ((Number) states.get(i)).intValue(); - - Java8BackComp.computeIfAbsent(localJsStatesListener, state, _state -> { - addChunkStateChagedListener(state); - return new LinkedList<>(); - }).add(changed); - } - } - - private static final LinkedList EMPTY_LISTENERS = new LinkedList<>(); - private static final LinkedList EMPTY_FUNCTION = new LinkedList<>(); - - public static void onChunkStateChaged(int dimension, int chunkX, int chunkZ, int state, int preState, boolean discarded){ - for(IChunkStateChanged changed : serverStatesListener.getOrDefault(state, EMPTY_LISTENERS)) - changed.onChunkStateChaged(chunkX, chunkZ, dimension, preState, state, discarded); - - final Context context = Compiler.assureContextForCurrentThread(); - final Object[] args = new Object[]{chunkX, chunkZ, dimension, preState, state, discarded}; - - for(Function changed : serverJsStatesListener.getOrDefault(state, EMPTY_FUNCTION)) { - final Scriptable parent = changed.getParentScope(); - changed.call(context, parent, parent, args); - } - } - - public static void onLocalChunkStateChaged(int dimension, int chunkX, int chunkZ, int state, int preState, boolean discarded){ - for(IChunkStateChanged changed : localStatesListener.getOrDefault(state, EMPTY_LISTENERS)) - changed.onChunkStateChaged(chunkX, chunkZ, dimension, preState, state, discarded); - - final Context context = Compiler.assureContextForCurrentThread(); - final Object[] args = new Object[]{chunkX, chunkZ, dimension, preState, state, discarded}; - - for(Function changed : localJsStatesListener.getOrDefault(state, EMPTY_FUNCTION)) { - final Scriptable parent = changed.getParentScope(); - changed.call(context, parent, parent, args); - } - } -} \ No newline at end of file diff --git a/src/main/resources/innercore/coreengine/core-engine.dev.js b/src/main/resources/innercore/coreengine/core-engine.dev.js index 6ae88ec89..91ed65ffe 100755 --- a/src/main/resources/innercore/coreengine/core-engine.dev.js +++ b/src/main/resources/innercore/coreengine/core-engine.dev.js @@ -3384,11 +3384,64 @@ var WorldAPI = { Level.setBlockChangeCallbackEnabled(id, enabled); }, blockChangeCallbacks: [], - addListenerChunkStateChaged: function(listener, states) { - Level.addListenerChunkStateChaged(listener, states); + serverStateChangeCallbacks: [], + localStateChangeCallbacks: [], + + addListenerChunkStateChanged: function(listener, states) { + for(let i in states){ + let state = states[i]; + + let listeners = this.serverStateChangeCallbacks[state] || []; + listeners.push(listener); + this.serverStateChangeCallbacks[state] = listeners; + + Level.setChunkStateChangeCallbackEnabled(state); + } + }, + onChunkLoadingStateChanged: function(chunkX, chunkZ, dimension, preState, state, discarded) { + let listeners = this.serverStateChangeCallbacks[state]; + + for(let i in listeners) + listeners[i](chunkX, chunkZ, dimension, preState, state, discarded); }, - addLocalListenerChunkStateChaged: function(listener, states) { - Level.addLocalListenerChunkStateChaged(listener, states); + + addLocalListenerChunkStateChanged: function(listener, states) { + for(let i in states){ + let state = states[i]; + + let listeners = this.localStateChangeCallbacks[state] || []; + listeners.push(listener); + this.localStateChangeCallbacks[state] = listeners; + + Level.setChunkStateChangeCallbackEnabled(state); + } + }, + onLocalChunkLoadingStateChanged: function(chunkX, chunkZ, dimension, preState, state, discarded) { + let listeners = this.localStateChangeCallbacks[state]; + + for(let i in listeners) + listeners[i](chunkX, chunkZ, dimension, preState, state, discarded); + }, + + registerBlockChangeCallback: function(ids, callback) { + if (!Array.isArray(ids)) { + ids = [ids]; + } + for (var i in ids) { + var id = ids[i]; + if (typeof(id) == "string") { + var numericID = BlockRegistry.getNumericId(id); + if (numericID == -1) { + Logger.Log("invalid block name id " + id); + continue; + } + id = numericID; + } + Level.setBlockChangeCallbackEnabled(id, true); + var callbacks = this.blockChangeCallbacks[id] || []; + callbacks.push(callback); + this.blockChangeCallbacks[id] = callbacks; + } }, registerBlockChangeCallback: function(ids, callback) { if (!Array.isArray(ids)) { @@ -3701,6 +3754,19 @@ WorldAPI.setLoaded(true); // always keep it loaded, for God sake, native will ha Callback.addCallback("BlockChanged", function(coords, block1, block2, int1, int2, region) { WorldAPI.onBlockChanged(coords, block1, block2, region, int1, int2); }); + +Callback.addCallback("ChunkLoadingStateChanged", function(chunkX, chunkZ, dimension, preState, state, discarded){ + if(discarded) + WorldAPI.onChunkLoadingStateChanged(chunkX, chunkZ, dimension, preState, -1, discarded); + WorldAPI.onChunkLoadingStateChanged(chunkX, chunkZ, dimension, preState, state, discarded); +}); + +Callback.addCallback("LocalChunkLoadingStateChanged", function(chunkX, chunkZ, dimension, preState, state, discarded){ + if(discarded) + WorldAPI.onLocalChunkLoadingStateChanged(chunkX, chunkZ, dimension, preState, -1, discarded); + WorldAPI.onLocalChunkLoadingStateChanged(chunkX, chunkZ, dimension, preState, state, discarded); +}); + var AnimatorToken = { __current: 1, genToken: function() { diff --git a/src/main/resources/zotecore.yml b/src/main/resources/zotecore.yml index b20aae2a7..285357a0a 100644 --- a/src/main/resources/zotecore.yml +++ b/src/main/resources/zotecore.yml @@ -2,7 +2,7 @@ # However, no deprecated or unimplemented methods will be added. pack: Inner Core pack-version: 2.3.0b122 -pack-version-code: 162 +pack-version-code: 163 # Sets the list of mods and configs to load, you can specify # name, folder name of a modpack from /modpacks or a path.