1
mirror of https://github.com/CarmJos/UserPrefix.git synced 2026-06-04 15:28:21 +08:00

feat: use reflection instead folia dependencies, java 8 supported

This commit is contained in:
flowerinsnow
2025-05-11 20:41:38 +08:00
committed by Carm
parent eab9b2385c
commit d54349da98
4 changed files with 76 additions and 24 deletions
@@ -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✔ 您正在使用该前缀")
@@ -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) {
@@ -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<Enchantment> 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);
}
}
}