Skip to content

Commit

Permalink
Use reflection to test for Paper and check if it is safe to load or u…
Browse files Browse the repository at this point in the history
…nload worlds

Note: this code was designed under the assumption that PaperMC/Paper#8316 will be accepted, so it won't work unless the commit from that PR is included in the Paper build.
  • Loading branch information
willkroboth committed Apr 11, 2023
1 parent 9ea2037 commit 200a461
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -1035,8 +1037,7 @@ public Collection<String> getPotentialWorlds() {
* {@inheritDoc}
*/
public void addOrRemoveWorldSafely(String worldName, String operationName, Runnable worldModification) {
// TODO: Find real way to tell if worlds are being ticked
if (!worldName.equals("testWorld")) {
if (safeToAddOrRemoveWorld()) {
// Operation is fine to do now
worldModification.run();
} else {
Expand All @@ -1050,4 +1051,21 @@ public void run() {
}.runTask(plugin);
}
}

private boolean safeToAddOrRemoveWorld(){
Method isTickingWorlds;
try {
isTickingWorlds = Bukkit.class.getMethod("isTickingWorlds");
} catch (NoSuchMethodException e) {
// Paper fixes aren't active, so it is always considered safe to proceed
return true;
}
// Paper fixes are active, so we need to and can check Bukkit.isTickingWorlds
try {
return !(boolean) isTickingWorlds.invoke(null);
} catch (InvocationTargetException | IllegalAccessException e) {
// Shouldn't happen, I know I'm using the method correctly
throw new RuntimeException(e);
}
}
}

0 comments on commit 200a461

Please sign in to comment.