Skip to content

Commit

Permalink
fixed: getEntityTypeName & PlayerActor & ClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Reider745 committed Oct 26, 2024
1 parent 690fae9 commit 37d5386
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 139 deletions.
29 changes: 19 additions & 10 deletions src/main/java/com/reider745/entity/EntityMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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> T validateThen(long entityUid, Function<Entity, T> then, T defaultValue) {
Expand Down Expand Up @@ -420,14 +418,25 @@ public static int getEntityType(long entityUid) {
return validateThen(entityUid, entity -> getEntityTypeDirect(entity), -1);
}

private static final HashMap<Integer, String> 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reider745/entity/PlayerActorMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/reider745/event/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -567,10 +564,10 @@ public void onItemDespawn(ItemDespawnEvent event){
event.setCancelled();
}

private static final ArrayList<Integer> statesEvents = new ArrayList<>();
public static void addChunkStateChagedListener(int state) {
private static final HashMap<Integer, Boolean> 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
Expand All @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public boolean isInitialized() {
return this.initialized;
}

private static final List<URL> urls = new ArrayList<>();
private static URLClassLoader classLoader;

public void initialize() {
final List<URL> urls = new ArrayList<>();
for (File dexFile : this.dexFiles) {
try {
final String filePath = dexFile.getAbsolutePath();
Expand All @@ -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<String, Object> data = new HashMap<>();
for (String name : this.directory.getBootClassNames()) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/zhekasmirnov/innercore/api/NativeAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

Loading

0 comments on commit 37d5386

Please sign in to comment.