|
17 | 17 | import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
|
18 | 18 | import com.onarandombox.MultiverseCore.api.WorldPurger;
|
19 | 19 | 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.*; |
24 | 21 | import org.bukkit.World.Environment;
|
25 |
| -import org.bukkit.WorldCreator; |
26 |
| -import org.bukkit.WorldType; |
27 | 22 | import org.bukkit.configuration.ConfigurationSection;
|
28 | 23 | import org.bukkit.configuration.file.FileConfiguration;
|
29 | 24 | import org.bukkit.configuration.file.YamlConfiguration;
|
|
37 | 32 | import java.io.File;
|
38 | 33 | import java.io.FilenameFilter;
|
39 | 34 | import java.io.IOException;
|
| 35 | +import java.lang.reflect.Field; |
40 | 36 | import java.lang.reflect.InvocationTargetException;
|
41 | 37 | import java.lang.reflect.Method;
|
42 | 38 | import java.util.ArrayList;
|
@@ -1054,19 +1050,30 @@ public void run() {
|
1054 | 1050 | }
|
1055 | 1051 |
|
1056 | 1052 | private boolean safeToAddOrRemoveWorld(){
|
1057 |
| - Method isTickingWorlds; |
| 1053 | + Server server = Bukkit.getServer(); |
| 1054 | + Logging.finest("Using reflection to test for Paper build after PR #7653"); |
1058 | 1055 | 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"); |
1062 | 1076 | 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); |
1070 | 1077 | }
|
1071 | 1078 | }
|
1072 | 1079 | }
|
0 commit comments