From d54349da987398f272a3596c5a0c4a5dfb3fa2f4 Mon Sep 17 00:00:00 2001 From: flowerinsnow Date: Sun, 11 May 2025 20:41:38 +0800 Subject: [PATCH] feat: use reflection instead folia dependencies, java 8 supported --- pom.xml | 22 ++------ .../plugin/userprefix/conf/PluginConfig.java | 4 +- .../userprefix/folia/FoliaScheduler.java | 51 +++++++++++++++++-- .../plugin/userprefix/folia/MajorUtil.java | 23 +++++++++ 4 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 src/main/java/cc/carm/plugin/userprefix/folia/MajorUtil.java diff --git a/pom.xml b/pom.xml index 79f116e..6fd47a8 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 - 21 + 8 cc.carm.plugin.userprefix ${project.jdk.version} @@ -63,7 +63,7 @@ maven-central - https://repo1.maven.org/maven2/ + https://repo.nju.edu.cn/maven/ @@ -76,11 +76,6 @@ https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - papermc - https://repo.papermc.io/repository/maven-public/ - - luck-repo https://repo.lucko.me/ @@ -190,17 +185,10 @@ provided - - - - - - - - dev.folia - folia-api - 1.21.4-R0.1-SNAPSHOT + org.spigotmc + spigot-api + 1.17-R0.1-SNAPSHOT provided diff --git a/src/main/java/cc/carm/plugin/userprefix/conf/PluginConfig.java b/src/main/java/cc/carm/plugin/userprefix/conf/PluginConfig.java index 0cf2457..7ad3086 100644 --- a/src/main/java/cc/carm/plugin/userprefix/conf/PluginConfig.java +++ b/src/main/java/cc/carm/plugin/userprefix/conf/PluginConfig.java @@ -8,8 +8,8 @@ import cc.carm.lib.configuration.value.standard.ConfiguredValue; import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredSound; import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem; import cc.carm.plugin.userprefix.conf.gui.GUIItems; +import cc.carm.plugin.userprefix.folia.MajorUtil; import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; public class PluginConfig implements Configuration { @@ -163,7 +163,7 @@ public class PluginConfig implements Configuration { @HeaderComments({"当选择了默认前缀时显示的物品"}) public static final ConfiguredItem USING = ConfiguredItem.create() .defaultType(Material.NAME_TAG) - .defaultEnchant(Enchantment.PROTECTION, 1) + .defaultEnchant(MajorUtil.getEnchantProtection(), 1) // 附魔改过名 .defaultFlags(ItemFlag.HIDE_ENCHANTS) .defaultName("&f默认玩家前缀") .defaultLore("", "&a✔ 您正在使用该前缀") diff --git a/src/main/java/cc/carm/plugin/userprefix/folia/FoliaScheduler.java b/src/main/java/cc/carm/plugin/userprefix/folia/FoliaScheduler.java index d3b0d82..5b80c08 100644 --- a/src/main/java/cc/carm/plugin/userprefix/folia/FoliaScheduler.java +++ b/src/main/java/cc/carm/plugin/userprefix/folia/FoliaScheduler.java @@ -1,14 +1,18 @@ package cc.carm.plugin.userprefix.folia; +import cc.carm.plugin.userprefix.Main; import org.bukkit.Bukkit; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; +import java.lang.reflect.InvocationTargetException; +import java.util.function.Consumer; + public class FoliaScheduler { - private final Plugin plugin; + private final Main plugin; private final boolean folia; - public FoliaScheduler(Plugin plugin, boolean folia) { + public FoliaScheduler(Main plugin, boolean folia) { this.plugin = plugin; this.folia = folia; } @@ -26,7 +30,20 @@ public class FoliaScheduler { } private void runAsyncFolia(Runnable task) { - Bukkit.getAsyncScheduler().runNow(this.plugin, t -> task.run()); +// AsyncScheduler asyncScheduler = Bukkit.getAsyncScheduler(); +// asyncScheduler.runNow(this.plugin, t -> task.run()); + + try { + Object asyncScheduler = Bukkit.class.getMethod("getAsyncScheduler") + .invoke(null); + asyncScheduler.getClass().getMethod("runNow", Plugin.class, Consumer.class) + .invoke(asyncScheduler, this.plugin, (Consumer) t -> task.run()); + } catch (IllegalAccessException | NoSuchMethodException e) { + Main.severe("unexpected exception during reflection (#runAsyncFolia), it should never happen!"); + e.printStackTrace(); + } catch (InvocationTargetException e) { + throw (RuntimeException) e.getTargetException(); + } } /** @@ -47,7 +64,19 @@ public class FoliaScheduler { } private void runOnEntityFolia(Entity entity, Runnable task) { - entity.getScheduler().run(this.plugin, t -> task.run(), null); +// EntityScheduler entityScheduler = entity.getScheduler(); +// entityScheduler.run(this.plugin, t -> task.run(), null); + try { + Object entityScheduler = Entity.class.getMethod("getScheduler") + .invoke(entity); + entityScheduler.getClass().getMethod("run", Plugin.class, Consumer.class, Runnable.class) + .invoke(entityScheduler, this.plugin, (Consumer) t -> task.run(), null); + } catch (IllegalAccessException | NoSuchMethodException e) { + Main.severe("unexpected exception during reflection (#runOnEntityFolia), it should never happen!"); + e.printStackTrace(); + } catch (InvocationTargetException e) { + throw (RuntimeException) e.getTargetException(); + } } /** @@ -67,7 +96,19 @@ public class FoliaScheduler { } private void runGlobalFolia(Runnable task) { - Bukkit.getGlobalRegionScheduler().execute(this.plugin, task); +// GlobalRegionScheduler globalRegionScheduler = Bukkit.getGlobalRegionScheduler(); +// globalRegionScheduler.execute(this.plugin, task); + try { + Object globalRegionScheduler = Bukkit.class.getMethod("getGlobalRegionScheduler") + .invoke(null); + globalRegionScheduler.getClass().getMethod("execute", Plugin.class, Runnable.class) + .invoke(globalRegionScheduler, this.plugin, task); + } catch (IllegalAccessException | NoSuchMethodException e) { + Main.severe("unexpected exception during reflection (#runGlobalFolia), it should never happen!"); + e.printStackTrace(); + } catch (InvocationTargetException e) { + throw (RuntimeException) e.getTargetException(); + } } private void runBukkit(boolean forceSync, Runnable task) { diff --git a/src/main/java/cc/carm/plugin/userprefix/folia/MajorUtil.java b/src/main/java/cc/carm/plugin/userprefix/folia/MajorUtil.java new file mode 100644 index 0000000..433f080 --- /dev/null +++ b/src/main/java/cc/carm/plugin/userprefix/folia/MajorUtil.java @@ -0,0 +1,23 @@ +package cc.carm.plugin.userprefix.folia; + +import cc.carm.plugin.userprefix.Main; +import org.bukkit.enchantments.Enchantment; + +public interface MajorUtil { + static Enchantment getEnchantProtection() { + Class enchantmentClass = Enchantment.class; + try { + return (Enchantment) enchantmentClass.getField("PROTECTION_ENVIRONMENTAL").get(null); + } catch (NoSuchFieldException e1) { + try { + return (Enchantment) enchantmentClass.getField("PROTECTION").get(null); + } catch (NoSuchFieldException | IllegalAccessException e2) { + Main.severe("unexpected exception during reflection (#getEnchantProtection), it should never happen!"); + throw new RuntimeException(e2); + } + } catch (IllegalAccessException e) { + Main.severe("unexpected exception during reflection (#getEnchantProtection), it should never happen!"); + throw new RuntimeException(e); + } + } +}