Skip to content

Commit 11c9a06

Browse files
committed
Use original safeToAddOrRemoveWorld method
(PaperMC/Paper#8316 was accepted, so this works now) Also some more logging messages
1 parent cc4fbbd commit 11c9a06

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java

+16-27
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
import com.onarandombox.MultiverseCore.api.WorldPurger;
1919
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
2020
import org.bukkit.Bukkit;
21-
import org.bukkit.ChatColor;
2221
import org.bukkit.GameRule;
2322
import org.bukkit.Location;
24-
import org.bukkit.Server;
2523
import org.bukkit.World;
2624
import org.bukkit.World.Environment;
2725
import org.bukkit.WorldCreator;
@@ -39,7 +37,6 @@
3937
import java.io.File;
4038
import java.io.FilenameFilter;
4139
import java.io.IOException;
42-
import java.lang.reflect.Field;
4340
import java.lang.reflect.InvocationTargetException;
4441
import java.lang.reflect.Method;
4542
import java.util.ArrayList;
@@ -54,7 +51,6 @@
5451
import java.util.Stack;
5552
import java.util.concurrent.Callable;
5653
import java.util.concurrent.ConcurrentHashMap;
57-
import java.util.regex.Pattern;
5854
import java.util.stream.Collectors;
5955

6056
/**
@@ -1041,12 +1037,13 @@ public Collection<String> getPotentialWorlds() {
10411037
*/
10421038
@Override
10431039
public void addOrRemoveWorldSafely(String worldName, String operationName, Runnable worldModification) {
1040+
Logging.finest("Checking if it is safe to perform world modification");
10441041
if (safeToAddOrRemoveWorld()) {
10451042
// Operation is fine to do now
1043+
Logging.finest("Clear to modify worlds");
10461044
worldModification.run();
10471045
} else {
10481046
// Operation needs to be delayed until worlds are not being ticked
1049-
10501047
Logging.fine("Worlds were being ticked while attempting to %s %s. Trying again in the next tick", operationName, worldName);
10511048
new BukkitRunnable() {
10521049
public void run() {
@@ -1057,30 +1054,22 @@ public void run() {
10571054
}
10581055

10591056
private boolean safeToAddOrRemoveWorld(){
1060-
Server server = Bukkit.getServer();
1061-
Logging.finest("Using reflection to test for Paper build after PR #7653");
1057+
Logging.finest("Checking for Paper");
1058+
Method isTickingWorlds;
10621059
try {
1063-
// basically doing ((CraftServer) Bukkit.getServer()).getServer().isIteratingOverLevels;
1064-
Method getConsole = server.getClass().getMethod("getServer");
1065-
Object console = getConsole.invoke(server);
1066-
1067-
Field isTickingWorlds = console.getClass().getField("isIteratingOverLevels");
1068-
boolean isTicking = isTickingWorlds.getBoolean(console);
1069-
1070-
Logging.finest("Paper fix active");
1071-
return !isTicking;
1072-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
1073-
Logging.finest("%sUnexpected exception: %s", ChatColor.RED, e.getMessage());
1074-
Logging.finest("Assuming Paper fix is inactive");
1075-
// If the Paper fix actually is active it should become obvious when Paper complains
1076-
// about a world being loaded/unloaded while being ticked
1077-
// If that happens, this method needs to be fixed
1078-
return true;
1079-
} catch (NoSuchFieldException ignored) {
1080-
// Expected to fail when field isIteratingOverLevels doesn't exist
1081-
// Therefore, Paper fixes aren't active, so it is always considered safe to proceed
1082-
Logging.finest("Paper fix inactive");
1060+
isTickingWorlds = Bukkit.class.getMethod("isTickingWorlds");
1061+
} catch (NoSuchMethodException e) {
1062+
// Paper fixes aren't active, so it is always considered safe to proceed
1063+
Logging.finest("Paper fixes inactive");
10831064
return true;
10841065
}
1066+
// Paper fixes are active, and Paper wants us to check Bukkit.isTickingWorlds()
1067+
Logging.finest("Paper fixes active");
1068+
try {
1069+
return !(boolean) isTickingWorlds.invoke(null);
1070+
} catch (InvocationTargetException | IllegalAccessException e) {
1071+
// Shouldn't happen, I know I'm using the method correctly
1072+
throw new RuntimeException(e);
1073+
}
10851074
}
10861075
}

0 commit comments

Comments
 (0)