From a7381cd66bfb0b49104d65d5059f718633df5222 Mon Sep 17 00:00:00 2001 From: Reider745 <70357814+Reider745@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:45:05 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D1=80=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=82=D0=BB=D0=B0=D0=B4=D0=BA=D0=B0?= =?UTF-8?q?=20&=20=D0=B8=D1=81=D0=BF=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20BlockSource=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20EntityRemoved=20&=20=D0=B8=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D0=BE?= =?UTF-8?q?=D0=BC=D0=BA=D0=B0=20SocketServer(not=20tested)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/reider745/api/CallbackHelper.java | 17 +++++++------ .../com/reider745/event/EventListener.java | 2 +- .../world/GenerationUtilsMethods.java | 3 ++- .../apparatus/mcpe/NativeBlockSource.java | 2 +- .../multiplayer/socket/SocketServer.java | 6 ++--- .../innercore/api/runtime/other/WorldGen.java | 25 +++++++++---------- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/reider745/api/CallbackHelper.java b/src/main/java/com/reider745/api/CallbackHelper.java index 071cd185a..837349a66 100644 --- a/src/main/java/com/reider745/api/CallbackHelper.java +++ b/src/main/java/com/reider745/api/CallbackHelper.java @@ -1,5 +1,6 @@ package com.reider745.api; +import cn.nukkit.Server; import cn.nukkit.event.Event; import cn.nukkit.level.Level; import com.reider745.api.hooks.HookController; @@ -43,11 +44,9 @@ public void add(ICallbackApply apply) { @Override public void run() { - while (true) { + while (Server.getInstance().isRunning()) { synchronized (applys) { - Iterator it = applys.iterator(); - while (it.hasNext()) - it.next().apply(); + for (ICallbackApply apply : applys) apply.apply(); applys.clear(); } try { @@ -55,6 +54,8 @@ public void run() { } catch (InterruptedException e) { } } + + Logger.warning("Disable thread: "+getName()); } } @@ -173,7 +174,7 @@ public static boolean isPrevent() { @SuppressWarnings("unchecked") public static void setValueForCurrent(Object value) { - Thread thread = Thread.currentThread(); + final Thread thread = Thread.currentThread(); if (thread instanceof ThreadCustomValue custom) { try { custom.value = value; @@ -184,9 +185,9 @@ public static void setValueForCurrent(Object value) { } public static Level getForCurrentThread() { - Thread thread = Thread.currentThread(); - if (thread instanceof ThreadCustomValue threadCallback && threadCallback.getValue() instanceof Level) { - return (Level) threadCallback.getValue(); + final Thread thread = Thread.currentThread(); + if (thread instanceof ThreadCustomValue threadCallback && threadCallback.getValue() instanceof Level level) { + return level; } return null; } diff --git a/src/main/java/com/reider745/event/EventListener.java b/src/main/java/com/reider745/event/EventListener.java index 37d857cf5..10e438590 100644 --- a/src/main/java/com/reider745/event/EventListener.java +++ b/src/main/java/com/reider745/event/EventListener.java @@ -239,7 +239,7 @@ public void onEntityDespawn(EntityDespawnEvent event) { final Entity entity = event.getEntity(); EntityMotion.remove(entity); - NativeCallback.onEntityRemoved(EntityMethod.getIdForEntity(entity)); + if(entity.level != null) NativeCallback.onEntityRemoved(EntityMethod.getIdForEntity(entity)); } public static int convertDamageCauseToEnum(DamageCause cause) { diff --git a/src/main/java/com/reider745/world/GenerationUtilsMethods.java b/src/main/java/com/reider745/world/GenerationUtilsMethods.java index dea5fcc35..1d0eb8d08 100644 --- a/src/main/java/com/reider745/world/GenerationUtilsMethods.java +++ b/src/main/java/com/reider745/world/GenerationUtilsMethods.java @@ -10,6 +10,7 @@ import com.reider745.api.CallbackHelper; import com.reider745.world.dimensions.NoiseOctaveMethods; import com.zhekasmirnov.apparatus.mcpe.NativeBlockSource; + import java.util.ArrayList; public class GenerationUtilsMethods { @@ -64,7 +65,7 @@ private static boolean isReplace(int[] ids, int id, boolean whitelist){ private static NukkitRandom rand = null; public static void generateOreNative(int x, int y, int z, int id, int data, int clusterSize, boolean whitelist, int[] blockIds, int seed){ - Level level = CallbackHelper.getForCurrentThread(); + final Level level = CallbackHelper.getForCurrentThread(); if(level == null) throw new RuntimeException("Region null"); if(rand == null) rand = new NukkitRandom(); diff --git a/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeBlockSource.java b/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeBlockSource.java index c8d98a2e2..9ac9e1e7c 100644 --- a/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeBlockSource.java +++ b/src/main/java/com/zhekasmirnov/apparatus/mcpe/NativeBlockSource.java @@ -65,7 +65,7 @@ public static NativeBlockSource getDefaultForLevel(String name) { public static NativeBlockSource getCurrentWorldGenRegion() { Level level = CallbackHelper.getForCurrentThread(); - if(level == null) Server.getInstance().getDefaultLevel(); + if(level == null) level = Server.getInstance().getDefaultLevel(); return level != null ? new NativeBlockSource(level, false) : null; } diff --git a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/socket/SocketServer.java b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/socket/SocketServer.java index 0b6734cc6..7558a7e88 100644 --- a/src/main/java/com/zhekasmirnov/apparatus/multiplayer/socket/SocketServer.java +++ b/src/main/java/com/zhekasmirnov/apparatus/multiplayer/socket/SocketServer.java @@ -86,10 +86,10 @@ public void start(int port) throws IOException { } } } catch (IOException exception) { - Logger.error("socket server error", "error in creating channel", exception); + Logger.error("socket server error", "error in creating channel, sleep socket", exception); try { - socket.close(); - } catch (IOException e) { + Thread.sleep(1000L); + } catch (InterruptedException e) { e.printStackTrace(); } } diff --git a/src/main/java/com/zhekasmirnov/innercore/api/runtime/other/WorldGen.java b/src/main/java/com/zhekasmirnov/innercore/api/runtime/other/WorldGen.java index 76bf707cc..9cb42ad7d 100644 --- a/src/main/java/com/zhekasmirnov/innercore/api/runtime/other/WorldGen.java +++ b/src/main/java/com/zhekasmirnov/innercore/api/runtime/other/WorldGen.java @@ -83,16 +83,12 @@ private static void generateChunkByLevelAndMode(int chunkX, int chunkZ, int dime } private static int dimensionIdToGenerationMode(int id) { - switch (id) { - case 0: - return MODE_SURFACE; - case 1: - return MODE_NETHER; - case 2: - return MODE_END; - default: - return MODE_CUSTOM; - } + return switch (id) { + case 0 -> MODE_SURFACE; + case 1 -> MODE_NETHER; + case 2 -> MODE_END; + default -> MODE_CUSTOM; + }; } public static void generateChunk(int x, int z, int level) { @@ -103,15 +99,18 @@ public static void generateChunk(int x, int z, int level) { return; } + for (int lvl : allLevels) { + generateChunk(x, z, dimension, lvl); + } + // if feature is not supported, call every generation level at once - if (!MinecraftVersions.getCurrent() - .isFeatureSupported(MinecraftVersion.FEATURE_VANILLA_WORLD_GENERATION_LEVELS)) { + /*if (!MinecraftVersions.getCurrent().isFeatureSupported(MinecraftVersion.FEATURE_VANILLA_WORLD_GENERATION_LEVELS)) { for (int lvl : allLevels) { generateChunk(x, z, dimension, lvl); } } else { generateChunk(x, z, dimension, level); - } + }*/ } public static void generateChunk(int x, int z, int dimensionId, int level) {