Fix bad method names etc for chunk/world listener

This commit is contained in:
dordsor21 2021-08-15 13:58:56 +01:00
parent da4ae9f4f5
commit eb2848e5d7
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
2 changed files with 48 additions and 22 deletions

View File

@ -26,6 +26,7 @@
package com.plotsquared.bukkit.listener; package com.plotsquared.bukkit.listener;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot; 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.TaskManager;
import com.plotsquared.core.util.task.TaskTime; import com.plotsquared.core.util.task.TaskTime;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Material; 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.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent; import org.bukkit.event.world.ChunkUnloadEvent;
import org.checkerframework.checker.nullness.qual.NonNull; 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.lang.reflect.Method;
import java.util.HashSet; import java.util.HashSet;
@ -72,26 +73,42 @@ public class ChunkListener implements Listener {
private final PlotAreaManager plotAreaManager; private final PlotAreaManager plotAreaManager;
private RefMethod methodGetHandleChunk; private RefMethod methodGetHandleChunk;
private RefMethod methodGetHandleWorld;
private RefField mustSave; private RefField mustSave;
private Chunk lastChunk; private Chunk lastChunk;
private boolean ignoreUnload = false; private boolean ignoreUnload = false;
private boolean isTrueForNotSave = true;
@Inject @Inject
public ChunkListener(final @NonNull PlotAreaManager plotAreaManager) { public ChunkListener(final @NonNull PlotAreaManager plotAreaManager) {
this.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) { if (!Settings.Chunk_Processor.AUTO_TRIM) {
return; 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()) { for (World world : Bukkit.getWorlds()) {
world.setAutoSave(false); world.setAutoSave(false);
} }
@ -103,13 +120,17 @@ public class ChunkListener implements Listener {
if (!this.plotAreaManager.hasPlotArea(worldName)) { if (!this.plotAreaManager.hasPlotArea(worldName)) {
continue; continue;
} }
Object w = world.getClass().getDeclaredMethod("getHandle").invoke(world); Method methodIsChunkInUse = null;
Object chunkMap = w.getClass().getDeclaredMethod("getPlayerChunkMap").invoke(w); Object chunkMap = null;
Method methodIsChunkInUse = if (PlotSquared.platform().serverVersion()[1] == 13) {
chunkMap.getClass().getDeclaredMethod("isChunkInUse", int.class, int.class); 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(); Chunk[] chunks = world.getLoadedChunks();
for (Chunk chunk : chunks) { for (Chunk chunk : chunks) {
if ((boolean) methodIsChunkInUse if (methodIsChunkInUse != null && (boolean) methodIsChunkInUse
.invoke(chunkMap, chunk.getX(), chunk.getZ())) { .invoke(chunkMap, chunk.getX(), chunk.getZ())) {
continue; continue;
} }
@ -145,7 +166,7 @@ public class ChunkListener implements Listener {
Object c = this.methodGetHandleChunk.of(chunk).call(); Object c = this.methodGetHandleChunk.of(chunk).call();
RefField.RefExecutor field = this.mustSave.of(c); RefField.RefExecutor field = this.mustSave.of(c);
if ((Boolean) field.get()) { if ((Boolean) field.get()) {
field.set(false); field.set(isTrueForNotSave);
if (chunk.isLoaded()) { if (chunk.isLoaded()) {
ignoreUnload = true; ignoreUnload = true;
chunk.unload(false); chunk.unload(false);

View File

@ -50,14 +50,19 @@ public class SingleWorldListener implements Listener {
private boolean isTrueForNotSave = true; private boolean isTrueForNotSave = true;
public SingleWorldListener() throws Exception { public SingleWorldListener() throws Exception {
ReflectionUtils.RefClass classChunk = getRefClass("{nms}.Chunk");
ReflectionUtils.RefClass classCraftChunk = getRefClass("{cb}.CraftChunk"); ReflectionUtils.RefClass classCraftChunk = getRefClass("{cb}.CraftChunk");
this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle").getRealMethod(); this.methodGetHandleChunk = classCraftChunk.getMethod("getHandle").getRealMethod();
try { try {
if (PlotSquared.platform().serverVersion()[1] == 13) { if (PlotSquared.platform().serverVersion()[1] < 17) {
this.mustSave = classChunk.getField("mustSave").getRealField(); ReflectionUtils.RefClass classChunk = getRefClass("{nms}.Chunk");
this.isTrueForNotSave = false; if (PlotSquared.platform().serverVersion()[1] == 13) {
this.mustSave = classChunk.getField("mustSave").getRealField();
this.isTrueForNotSave = false;
} else {
this.mustSave = classChunk.getField("mustNotSave").getRealField();
}
} else { } else {
ReflectionUtils.RefClass classChunk = getRefClass("net.minecraft.world.level.chunk.Chunk");
this.mustSave = classChunk.getField("mustNotSave").getRealField(); this.mustSave = classChunk.getField("mustNotSave").getRealField();
} }
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {