From eb2848e5d780cf24e6df1c8812a2035d7bc2c3b8 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 15 Aug 2021 13:58:56 +0100 Subject: [PATCH] Fix bad method names etc for chunk/world listener --- .../bukkit/listener/ChunkListener.java | 57 +++++++++++++------ .../bukkit/listener/SingleWorldListener.java | 13 +++-- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java index ca81edbbe..60bfceedd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ChunkListener.java @@ -26,6 +26,7 @@ package com.plotsquared.bukkit.listener; import com.google.inject.Inject; +import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.location.Location; import com.plotsquared.core.plot.Plot; @@ -37,6 +38,8 @@ import com.plotsquared.core.util.task.PlotSquaredTask; import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskTime; import io.papermc.lib.PaperLib; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Material; @@ -55,8 +58,6 @@ import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.world.ChunkUnloadEvent; import org.checkerframework.checker.nullness.qual.NonNull; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.lang.reflect.Method; import java.util.HashSet; @@ -72,26 +73,42 @@ public class ChunkListener implements Listener { private final PlotAreaManager plotAreaManager; private RefMethod methodGetHandleChunk; + private RefMethod methodGetHandleWorld; private RefField mustSave; private Chunk lastChunk; private boolean ignoreUnload = false; + private boolean isTrueForNotSave = true; @Inject public ChunkListener(final @NonNull PlotAreaManager plotAreaManager) { this.plotAreaManager = plotAreaManager; - if (Settings.Chunk_Processor.AUTO_TRIM) { - try { - RefClass classChunk = getRefClass("{nms}.Chunk"); - RefClass classCraftChunk = getRefClass("{cb}.CraftChunk"); - this.mustSave = classChunk.getField("mustSave"); - this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle"); - } catch (Throwable ignored) { - Settings.Chunk_Processor.AUTO_TRIM = false; - } - } if (!Settings.Chunk_Processor.AUTO_TRIM) { return; } + try { + this.methodGetHandleWorld = getRefClass("{cb}.CraftWorld").getMethod("getHandle"); + RefClass classCraftChunk = getRefClass("{cb}.CraftChunk"); + this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle"); + try { + if (PlotSquared.platform().serverVersion()[1] < 17) { + RefClass classChunk = getRefClass("{nms}.Chunk"); + RefClass worldServer = getRefClass("{nms}.WorldServer"); + if (PlotSquared.platform().serverVersion()[1] == 13) { + this.mustSave = classChunk.getField("mustSave"); + this.isTrueForNotSave = false; + } else { + this.mustSave = classChunk.getField("mustNotSave"); + } + } else { + RefClass classChunk = getRefClass("net.minecraft.world.level.chunk"); + this.mustSave = classChunk.getField("mustNotSave"); + } + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } catch (Throwable ignored) { + Settings.Chunk_Processor.AUTO_TRIM = false; + } for (World world : Bukkit.getWorlds()) { world.setAutoSave(false); } @@ -103,13 +120,17 @@ public class ChunkListener implements Listener { if (!this.plotAreaManager.hasPlotArea(worldName)) { continue; } - Object w = world.getClass().getDeclaredMethod("getHandle").invoke(world); - Object chunkMap = w.getClass().getDeclaredMethod("getPlayerChunkMap").invoke(w); - Method methodIsChunkInUse = - chunkMap.getClass().getDeclaredMethod("isChunkInUse", int.class, int.class); + Method methodIsChunkInUse = null; + Object chunkMap = null; + if (PlotSquared.platform().serverVersion()[1] == 13) { + Object w = methodGetHandleWorld.of(world).call(); + chunkMap = w.getClass().getDeclaredMethod("getPlayerChunkMap").invoke(w); + methodIsChunkInUse = + chunkMap.getClass().getDeclaredMethod("isChunkInUse", int.class, int.class); + } Chunk[] chunks = world.getLoadedChunks(); for (Chunk chunk : chunks) { - if ((boolean) methodIsChunkInUse + if (methodIsChunkInUse != null && (boolean) methodIsChunkInUse .invoke(chunkMap, chunk.getX(), chunk.getZ())) { continue; } @@ -145,7 +166,7 @@ public class ChunkListener implements Listener { Object c = this.methodGetHandleChunk.of(chunk).call(); RefField.RefExecutor field = this.mustSave.of(c); if ((Boolean) field.get()) { - field.set(false); + field.set(isTrueForNotSave); if (chunk.isLoaded()) { ignoreUnload = true; chunk.unload(false); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/SingleWorldListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/SingleWorldListener.java index 40abdb0ec..c0072d1e8 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/SingleWorldListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/SingleWorldListener.java @@ -50,14 +50,19 @@ public class SingleWorldListener implements Listener { private boolean isTrueForNotSave = true; public SingleWorldListener() throws Exception { - ReflectionUtils.RefClass classChunk = getRefClass("{nms}.Chunk"); ReflectionUtils.RefClass classCraftChunk = getRefClass("{cb}.CraftChunk"); this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle").getRealMethod(); try { - if (PlotSquared.platform().serverVersion()[1] == 13) { - this.mustSave = classChunk.getField("mustSave").getRealField(); - this.isTrueForNotSave = false; + if (PlotSquared.platform().serverVersion()[1] < 17) { + ReflectionUtils.RefClass classChunk = getRefClass("{nms}.Chunk"); + if (PlotSquared.platform().serverVersion()[1] == 13) { + this.mustSave = classChunk.getField("mustSave").getRealField(); + this.isTrueForNotSave = false; + } else { + this.mustSave = classChunk.getField("mustNotSave").getRealField(); + } } else { + ReflectionUtils.RefClass classChunk = getRefClass("net.minecraft.world.level.chunk.Chunk"); this.mustSave = classChunk.getField("mustNotSave").getRealField(); } } catch (NoSuchFieldException e) {