Fix Paper CraftBukkit relocation

This commit is contained in:
petersj 2024-07-28 12:51:19 +01:00
parent bf0873ba8b
commit 386962c3d7
7 changed files with 69 additions and 105 deletions

View File

@ -4,19 +4,19 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Supplier;
public class Api {
private static Plugin plugin;
private static NMSProvider nmsProvider;
public static NMSProvider init(Plugin plugin, Supplier<NMSProvider> defaultProvider) {
public static NMSProvider init(Plugin plugin) {
Api.plugin = plugin;
Values.init(plugin);
nmsProvider = setupNMSProvider();
if (nmsProvider == null)
nmsProvider = defaultProvider.get();
NMSProvider nmsProvider = setupNMSProvider();
if (nmsProvider == null) {
plugin.getLogger().severe("Disabling plugin: Failed to initialize NMS provider");
Bukkit.getPluginManager().disablePlugin(plugin);
}
return nmsProvider;
}
@ -27,14 +27,16 @@ public class Api {
private static NMSProvider setupNMSProvider() {
String packageName = NMSProvider.class.getPackage().getName();
String nmsVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
String nmsVersion = VersionMatcher.match();
String nmsProvider = packageName + "." + nmsVersion + ".NMSProviderImpl";
plugin.getLogger().info("Found API version: " + nmsVersion);
try {
return (NMSProvider) Class.forName(nmsProvider).getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
// Plugin now doesn't depend on NMS after 1.17.
// So NMSProviderDefault is used for all versions 1.17+
plugin.getLogger().severe("§c=======================================================");
plugin.getLogger().severe("§cThis version is not supported. The plugin most likely needs updating! ");
plugin.getLogger().severe("§c=======================================================");
return null;
}
}

View File

@ -0,0 +1,35 @@
package com.jamesdpeters.minecraft.chests;
import org.bukkit.Bukkit;
import java.util.HashMap;
import java.util.Map;
public class VersionMatcher {
private static final Map<String, String> VERSION_TO_REVISION = new HashMap<>() {
{
this.put("1.20", "v1_20_R1");
this.put("1.20.1", "v1_20_R1");
this.put("1.20.2", "v1_20_R2");
this.put("1.20.3", "v1_20_R3");
this.put("1.20.4", "v1_20_R3");
this.put("1.20.5", "v1_20_R4");
this.put("1.20.6", "v1_20_R4");
this.put("1.21", "v1_21_R1");
}
};
private static final String FALLBACK_REVISION = "v1_20_R1";
public static String match() {
String craftBukkitPackage = Bukkit.getServer().getClass().getPackage().getName();
String rVersion;
if (!craftBukkitPackage.contains(".v")) {
String version = Bukkit.getBukkitVersion().split("-")[0];
rVersion = VERSION_TO_REVISION.getOrDefault(version, FALLBACK_REVISION);
} else {
rVersion = craftBukkitPackage.split("\\.")[3];
}
return rVersion;
}
}

View File

@ -11,7 +11,7 @@
</parent>
<artifactId>ChestsPlusPlus-Master</artifactId>
<version>2.12-Release</version>
<version>2.13-Release</version>
<properties>
<maven.compiler.target>21</maven.compiler.target>

View File

@ -99,9 +99,9 @@ public class ChestsPlusPlus extends JavaPlugin {
Stats.addCharts(metrics);
//API initialisation
ApiSpecific.init(this);
if (!ApiSpecific.init(this)) {
return;
}
//Load storage
ServerType.init();

View File

@ -4,35 +4,25 @@ import com.jamesdpeters.minecraft.chests.Api;
import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import org.bukkit.Bukkit;
import lombok.Getter;
import org.bukkit.plugin.Plugin;
public class ApiSpecific {
@Getter
private static MaterialChecker materialChecker;
@Getter
private static ChestOpener chestOpener;
@Getter
private static NMSProvider nmsProvider;
public static void init(Plugin plugin) {
nmsProvider = Api.init(plugin, NMSProviderDefault::new);
materialChecker = nmsProvider.getMaterialChecker();
chestOpener = nmsProvider.getChestOpener();
}
public static boolean init(Plugin plugin) {
nmsProvider = Api.init(plugin);
if (nmsProvider != null) {
materialChecker = nmsProvider.getMaterialChecker();
chestOpener = nmsProvider.getChestOpener();
}
public static MaterialChecker getMaterialChecker() {
return materialChecker;
}
public static ChestOpener getChestOpener() {
return chestOpener;
}
public static NMSProvider getNmsProvider() {
return nmsProvider;
}
public static String getApiVersion() {
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
return (nmsProvider != null && materialChecker != null && chestOpener != null);
}
}

View File

@ -1,65 +0,0 @@
package com.jamesdpeters.minecraft.chests.api;
import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.CraftingProvider;
import com.jamesdpeters.minecraft.chests.EntityEventListener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import org.bukkit.Bukkit;
import org.bukkit.entity.ItemFrame;
public class NMSProviderDefault implements NMSProvider {
//Latest version at time of build is 1.19
NMSProvider defaultProvider;
public NMSProviderDefault() {
String NAME = Bukkit.getServer().getClass().getPackage().getName();
String VERSION = NAME.substring(NAME.lastIndexOf('.') + 1);
switch (VERSION) {
case "v1_17_R1" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_17_R1.NMSProviderImpl();
case "v1_18_R1" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_18_R1.NMSProviderImpl();
case "v1_18_R2" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_18_R2.NMSProviderImpl();
case "v1_19_R1" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_19_R1.NMSProviderImpl();
case "v1_20_R1" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_20_R1.NMSProviderImpl();
case "v1_20_R2" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_20_R2.NMSProviderImpl();
case "v1_20_R3" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_20_R3.NMSProviderImpl();
case "v1_21_R1" -> defaultProvider = new com.jamesdpeters.minecraft.chests.v1_21_R1.NMSProviderImpl();
default -> {
ChestsPlusPlus.PLUGIN.getLogger().severe("§c=======================================================");
ChestsPlusPlus.PLUGIN.getLogger().severe("§cThis version is not supported. Please update your server or the plugin!");
ChestsPlusPlus.PLUGIN.getLogger().severe("§c=======================================================");
defaultProvider = new com.jamesdpeters.minecraft.chests.v1_17_R1.NMSProviderImpl();
}
}
}
@Override
public ChestOpener getChestOpener() {
//1.16 ChestOpener contains lidded API!
return defaultProvider.getChestOpener();
}
@Override
public MaterialChecker getMaterialChecker() {
//Return the current latest MaterialChecker when an newer server implementation is found.
return defaultProvider.getMaterialChecker();
}
@Override
public CraftingProvider getCraftingProvider() {
return defaultProvider.getCraftingProvider();
}
@Override
public EntityEventListener getEntityEventListener() {
return defaultProvider.getEntityEventListener();
}
@Override
public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) {
//Not supported in Bukkit api 1.14.
defaultProvider.setItemFrameVisible(itemFrame, visible);
}
}

View File

@ -1,7 +1,7 @@
package com.jamesdpeters.minecraft.chests.commands;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
import com.jamesdpeters.minecraft.chests.VersionMatcher;
import com.jamesdpeters.minecraft.chests.maventemplates.BuildConstants;
import com.jamesdpeters.minecraft.chests.menus.PartyMenu;
import com.jamesdpeters.minecraft.chests.misc.Utils;
@ -11,6 +11,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.stream.Collectors;
@ -23,8 +24,9 @@ public class ChestsPlusPlusCommand extends ServerCommand {
PARTY("/chestsplusplus party", "Open the party menu to create, add, remove and invite parties and players."),
RELOAD("/chestsplusplus reload", "Reloads the plugin.");
String description, commandHelp;
static List<String> valuesList;
final String description;
final String commandHelp;
static final List<String> valuesList;
static {
valuesList = Stream.of(OPTIONS.values()).map(OPTIONS::toString).collect(Collectors.toList());
@ -48,7 +50,7 @@ public class ChestsPlusPlusCommand extends ServerCommand {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can use this command");
return false;
@ -59,7 +61,7 @@ public class ChestsPlusPlusCommand extends ServerCommand {
sender.sendMessage("ChestsPlusPlus Version: " + BuildConstants.VERSION);
sender.sendMessage("Server Version: " + Bukkit.getVersion());
sender.sendMessage("CraftBukkit Version: " + Bukkit.getBukkitVersion());
sender.sendMessage("Detected API Version: " + ApiSpecific.getApiVersion());
sender.sendMessage("Detected API Version: " + VersionMatcher.match());
return true;
case RELOAD:
@ -82,7 +84,7 @@ public class ChestsPlusPlusCommand extends ServerCommand {
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
if ((sender instanceof Player player)) {
if (args.length == 1) {
return OPTIONS.valuesList;