Uses Bukkit version method instead of class names (#2370)

* Uses Bukkit version method instead of class names

See https://forums.papermc.io/threads/important-dev-psa-future-removal-of-cb-package-relocation.1106/

* Fix tests

* Fix server compatibility reporting issue with Paper

* Remove unused import
This commit is contained in:
tastybento 2024-05-18 21:47:09 -07:00 committed by GitHub
parent 2fc3396a8f
commit d701b7e43c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 28 additions and 17 deletions

View File

@ -104,6 +104,8 @@ public class BentoBox extends JavaPlugin implements Listener {
@Override
public void onEnable(){
setInstance(this);
if (!ServerCompatibility.getInstance().checkCompatibility().isCanLaunch()) {
// The server's most likely incompatible.
// Show a warning
@ -125,7 +127,6 @@ public class BentoBox extends JavaPlugin implements Listener {
// Save the default config from config.yml
saveDefaultConfig();
setInstance(this);
// Load Flags
flagsManager = new FlagsManager(this);

View File

@ -120,7 +120,8 @@ public class IslandCache {
* associated per world.
*/
public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island.getUniqueId());
this.islandsById.put(island.getUniqueId(), island);
this.islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island.getUniqueId());
}
/**
@ -181,6 +182,7 @@ public class IslandCache {
public Island get(@NonNull World world, @NonNull UUID uuid) {
List<Island> islands = getIslands(world, uuid);
if (islands.isEmpty()) {
System.out.println("empty");
return null;
}
for (Island island : islands) {

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R1;
package world.bentobox.bentobox.nms.v1_20_0_R0_1_SNAPSHOT;
import java.util.concurrent.CompletableFuture;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R1;
package world.bentobox.bentobox.nms.v1_20_0_R0_1_SNAPSHOT;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R2;
package world.bentobox.bentobox.nms.v1_20_1_R0_1_SNAPSHOT;
import java.util.concurrent.CompletableFuture;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R2;
package world.bentobox.bentobox.nms.v1_20_1_R0_1_SNAPSHOT;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R3;
package world.bentobox.bentobox.nms.v1_20_4_R0_1_SNAPSHOT;
import java.util.concurrent.CompletableFuture;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R3;
package world.bentobox.bentobox.nms.v1_20_4_R0_1_SNAPSHOT;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R4;
package world.bentobox.bentobox.nms.v1_20_6_R0_1_SNAPSHOT;
import java.util.concurrent.CompletableFuture;

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.nms.v1_20_R4;
package world.bentobox.bentobox.nms.v1_20_6_R0_1_SNAPSHOT;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_20_R4.CraftWorld;

View File

@ -719,19 +719,21 @@ public class Util {
*/
public static WorldRegenerator getRegenerator() {
if (regenerator == null) {
String serverPackageName = Bukkit.getServer().getClass().getPackage().getName();
// Bukkit method that was added in 2011
// Example value: 1.20.4-R0.1-SNAPSHOT
String bukkitVersion = "v" + Bukkit.getServer().getBukkitVersion().replace('.', '_').replace('-', '_');
String pluginPackageName = plugin.getClass().getPackage().getName();
String version = serverPackageName.substring(serverPackageName.lastIndexOf('.') + 1);
WorldRegenerator handler;
try {
Class<?> clazz = Class.forName(pluginPackageName + ".nms." + version + ".WorldRegeneratorImpl");
Class<?> clazz = Class.forName(pluginPackageName + ".nms." + bukkitVersion + ".WorldRegeneratorImpl");
if (WorldRegenerator.class.isAssignableFrom(clazz)) {
handler = (WorldRegenerator) clazz.getConstructor().newInstance();
} else {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement WorldRegenerator");
}
} catch (Exception e) {
plugin.logWarning("No Regenerator found for " + version + ", falling back to Bukkit API.");
plugin.logWarning("No Regenerator found for " + bukkitVersion + ", falling back to Bukkit API.");
handler = new world.bentobox.bentobox.nms.fallback.WorldRegeneratorImpl();
}
setRegenerator(handler);

View File

@ -9,6 +9,8 @@ import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.util.Util;
/**
* Checks and ensures the current server software is compatible with BentoBox.
* @author Poslovitch
@ -281,7 +283,6 @@ public class ServerCompatibility {
if (result == null) {
// Check the server version first
ServerVersion version = getServerVersion();
if (version == null || version.getCompatibility().equals(Compatibility.INCOMPATIBLE)) {
// 'Version = null' means that it's not listed. And therefore, it's implicitly incompatible.
result = Compatibility.INCOMPATIBLE;
@ -323,9 +324,12 @@ public class ServerCompatibility {
*/
@NonNull
public ServerSoftware getServerSoftware() {
String[] parts = Bukkit.getServer().getVersion().split("-");
if (Util.isPaper()) {
return ServerSoftware.PAPER;
}
String[] parts = Bukkit.getServer().getBukkitVersion().split("-");
if (parts.length < 2) {
return ServerSoftware.UNKNOWN.setName(Bukkit.getServer().getVersion().toUpperCase(Locale.ENGLISH));
return ServerSoftware.UNKNOWN.setName(Bukkit.getServer().getBukkitVersion().toUpperCase(Locale.ENGLISH));
}
String serverSoftware = Bukkit.getServer().getVersion().split("-")[1];
try {

View File

@ -80,6 +80,7 @@ public class IslandDeletionManagerTest {
when(Bukkit.getPluginManager()).thenReturn(pim);
when(server.getPluginManager()).thenReturn(pim);
when(Bukkit.getScheduler()).thenReturn(scheduler);
when(server.getBukkitVersion()).thenReturn("1.20.6-R0.2-SNAPSHOT");
// Clear any remaining database
deleteAll(new File("database"));

View File

@ -81,6 +81,7 @@ public class IslandCacheTest {
// Island
when(island.getWorld()).thenReturn(world);
when(island.getUniqueId()).thenReturn("uniqueId");
@NonNull
String uniqueId = UUID.randomUUID().toString();
when(island.getUniqueId()).thenReturn(uniqueId);