1
mirror of https://github.com/CarmJos/UserPrefix.git synced 2026-06-04 23:43:29 +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
+5 -17
View File
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<properties> <properties>
<project.jdk.version>21</project.jdk.version> <project.jdk.version>8</project.jdk.version>
<project.package>cc.carm.plugin.userprefix</project.package> <project.package>cc.carm.plugin.userprefix</project.package>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source> <maven.compiler.source>${project.jdk.version}</maven.compiler.source>
@@ -63,7 +63,7 @@
<repository> <repository>
<id>maven-central</id> <id>maven-central</id>
<url>https://repo1.maven.org/maven2/</url> <url>https://repo.nju.edu.cn/maven/</url>
</repository> </repository>
<repository> <repository>
@@ -76,11 +76,6 @@
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository> <repository>
<id>luck-repo</id> <id>luck-repo</id>
<url>https://repo.lucko.me/</url> <url>https://repo.lucko.me/</url>
@@ -190,17 +185,10 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.spigotmc</groupId>-->
<!-- <artifactId>spigot-api</artifactId>-->
<!-- <version>1.17-R0.1-SNAPSHOT</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>dev.folia</groupId> <groupId>org.spigotmc</groupId>
<artifactId>folia-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.21.4-R0.1-SNAPSHOT</version> <version>1.17-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
@@ -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.ConfiguredSound;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem; import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.plugin.userprefix.conf.gui.GUIItems; import cc.carm.plugin.userprefix.conf.gui.GUIItems;
import cc.carm.plugin.userprefix.folia.MajorUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
public class PluginConfig implements Configuration { public class PluginConfig implements Configuration {
@@ -163,7 +163,7 @@ public class PluginConfig implements Configuration {
@HeaderComments({"当选择了默认前缀时显示的物品"}) @HeaderComments({"当选择了默认前缀时显示的物品"})
public static final ConfiguredItem USING = ConfiguredItem.create() public static final ConfiguredItem USING = ConfiguredItem.create()
.defaultType(Material.NAME_TAG) .defaultType(Material.NAME_TAG)
.defaultEnchant(Enchantment.PROTECTION, 1) .defaultEnchant(MajorUtil.getEnchantProtection(), 1) // 附魔改过名
.defaultFlags(ItemFlag.HIDE_ENCHANTS) .defaultFlags(ItemFlag.HIDE_ENCHANTS)
.defaultName("&f默认玩家前缀") .defaultName("&f默认玩家前缀")
.defaultLore("", "&a✔ 您正在使用该前缀") .defaultLore("", "&a✔ 您正在使用该前缀")
@@ -1,14 +1,18 @@
package cc.carm.plugin.userprefix.folia; package cc.carm.plugin.userprefix.folia;
import cc.carm.plugin.userprefix.Main;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer;
public class FoliaScheduler { public class FoliaScheduler {
private final Plugin plugin; private final Main plugin;
private final boolean folia; private final boolean folia;
public FoliaScheduler(Plugin plugin, boolean folia) { public FoliaScheduler(Main plugin, boolean folia) {
this.plugin = plugin; this.plugin = plugin;
this.folia = folia; this.folia = folia;
} }
@@ -26,7 +30,20 @@ public class FoliaScheduler {
} }
private void runAsyncFolia(Runnable task) { 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) { 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) { 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) { 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);
}
}
}