Skip to content

Commit a1d77b5

Browse files
committed
Use more reflection to directly access isIteratingOverLevels
1 parent d55af49 commit a1d77b5

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

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

+24-17
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
1818
import com.onarandombox.MultiverseCore.api.WorldPurger;
1919
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
20-
import org.bukkit.Bukkit;
21-
import org.bukkit.GameRule;
22-
import org.bukkit.Location;
23-
import org.bukkit.World;
20+
import org.bukkit.*;
2421
import org.bukkit.World.Environment;
25-
import org.bukkit.WorldCreator;
26-
import org.bukkit.WorldType;
2722
import org.bukkit.configuration.ConfigurationSection;
2823
import org.bukkit.configuration.file.FileConfiguration;
2924
import org.bukkit.configuration.file.YamlConfiguration;
@@ -37,6 +32,7 @@
3732
import java.io.File;
3833
import java.io.FilenameFilter;
3934
import java.io.IOException;
35+
import java.lang.reflect.Field;
4036
import java.lang.reflect.InvocationTargetException;
4137
import java.lang.reflect.Method;
4238
import java.util.ArrayList;
@@ -1054,19 +1050,30 @@ public void run() {
10541050
}
10551051

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

0 commit comments

Comments
 (0)