From b71fec632900203199caaa2e96ddc46d32a9cf42 Mon Sep 17 00:00:00 2001 From: zhangyuheng Date: Sat, 10 Aug 2024 20:22:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86prefix=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=88=E6=9C=AA=E6=B5=8B=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++ .../cn/lunadeer/essentialsd/EssentialsD.java | 10 ++- .../essentialsd/events/ChatPrefixEvent.java | 19 ++++++ .../essentialsd/managers/ConfigManager.java | 63 +++++++++++++++++++ src/main/resources/config.yml | 5 ++ src/main/resources/plugin.yml | 1 + 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/lunadeer/essentialsd/events/ChatPrefixEvent.java diff --git a/pom.xml b/pom.xml index a77388a..bdda6b6 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,10 @@ lunadeer-repo https://ssl.lunadeer.cn:14454/repository/maven-snapshots/ + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + @@ -90,5 +94,11 @@ 1.7 provided + + me.clip + placeholderapi + 2.11.6 + provided + diff --git a/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java b/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java index 2a3c603..0323a45 100644 --- a/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java +++ b/src/main/java/cn/lunadeer/essentialsd/EssentialsD.java @@ -38,7 +38,6 @@ public final class EssentialsD extends JavaPlugin { instance = this; new XLogger(instance); config = new ConfigManager(instance); - XLogger.setDebug(config.isDebug()); new Notification(instance); database = new DatabaseManager(this, DatabaseManager.TYPE.valueOf(config.getDbType().toUpperCase()), @@ -51,6 +50,15 @@ public final class EssentialsD extends JavaPlugin { new Scheduler(this); tpManager = new TeleportManager(); + if (config.getPrefixEnable()) { + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + Bukkit.getPluginManager().registerEvents(new ChatPrefixEvent(), this); + } else { + XLogger.warn("未找到 PlaceholderAPI 插件, 无法使用聊天前缀功能, 已自动关闭前缀功能"); + config.setPrefixEnable(false); + } + } + Bukkit.getPluginManager().registerEvents(new InvisibleItemFrameEvent(), this); Bukkit.getPluginManager().registerEvents(new ChairEvent(), this); Bukkit.getPluginManager().registerEvents(new ArmorStandHandsEvent(), this); diff --git a/src/main/java/cn/lunadeer/essentialsd/events/ChatPrefixEvent.java b/src/main/java/cn/lunadeer/essentialsd/events/ChatPrefixEvent.java new file mode 100644 index 0000000..b9b68ab --- /dev/null +++ b/src/main/java/cn/lunadeer/essentialsd/events/ChatPrefixEvent.java @@ -0,0 +1,19 @@ +package cn.lunadeer.essentialsd.events; + +import cn.lunadeer.essentialsd.EssentialsD; +import io.papermc.paper.event.player.AsyncChatEvent; +import me.clip.placeholderapi.PlaceholderAPI; +import net.kyori.adventure.text.Component; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class ChatPrefixEvent implements Listener { + + @EventHandler + public void onChat(AsyncChatEvent event) { + event.setCancelled(true); + String formated = PlaceholderAPI.setPlaceholders(event.getPlayer(), EssentialsD.config.getPrefixFormat()); + EssentialsD.instance.getServer().sendMessage(Component.text(formated).append(event.message())); + } + +} diff --git a/src/main/java/cn/lunadeer/essentialsd/managers/ConfigManager.java b/src/main/java/cn/lunadeer/essentialsd/managers/ConfigManager.java index bbad922..416af05 100644 --- a/src/main/java/cn/lunadeer/essentialsd/managers/ConfigManager.java +++ b/src/main/java/cn/lunadeer/essentialsd/managers/ConfigManager.java @@ -23,6 +23,7 @@ public class ConfigManager { _plugin.reloadConfig(); _file = _plugin.getConfig(); _debug = _file.getBoolean("Debug", false); + XLogger.setDebug(isDebug()); _check_update = _file.getBoolean("CheckUpdate", true); _exp_bottle_ratio = (float) _file.getDouble("ExpBottleRatio", 1.0); _combine_exp_orbs_enable = _file.getBoolean("CombineExpOrbs.Enable", false); @@ -55,6 +56,44 @@ public class ConfigManager { _db_name = _file.getString("Database.Name", "dominion"); _db_user = _file.getString("Database.User", "postgres"); _db_pass = _file.getString("Database.Pass", "postgres"); + _prefix_enable = _file.getBoolean("Prefix.Enable", true); + _prefix_format = _file.getString("Prefix.Format", "<%player_name%> "); + saveAll(); // save all to make sure all values are valid + } + + private void saveAll() { + _file.set("Debug", _debug); + _file.set("CheckUpdate", _check_update); + _file.set("ExpBottleRatio", _exp_bottle_ratio); + _file.set("CombineExpOrbs.Enable", _combine_exp_orbs_enable); + _file.set("CombineExpOrbs.Radius", _combine_exp_orbs_radius); + _file.set("NoExpCoolDown", _no_exp_cool_down); + _file.set("ForceLoadChunks", _force_load_chunks); + _file.set("ChunkOperateDelay", _chunk_operate_delay); + _file.set("Teleport.Delay", _tp_delay); + _file.set("Teleport.CoolDown", _tp_cool_down); + _file.set("Teleport.TpaExpire", _tp_tpa_expire); + _file.set("Teleport.RtpRadius", _tp_rtp_radius); + _file.set("Teleport.WorldBlackList", _tp_world_blacklist); + _file.set("Chair.Enable", _chair_enable); + _file.set("Chair.MaxWidth", _chair_max_width); + _file.set("Chair.SignCheck", _chair_sign_check); + _file.set("Chair.SitHeight", _chair_sit_height); + _file.set("Recipes.CrowBar", _recipes_crowbar); + _file.set("Recipes.InvisibleItemFrame", _recipes_invisible_item_frame); + _file.set("Recipes.LightBlock", _recipes_light_block); + _file.set("Recipes.StackedEnchantBook", _recipes_stacked_enchant_book); + _file.set("HomeLimit.Amount", _home_limit_amount); + _file.set("HomeLimit.WorldBlacklist", _home_world_blacklist); + _file.set("Database.Type", _db_type); + _file.set("Database.Host", _db_host); + _file.set("Database.Port", _db_port); + _file.set("Database.Name", _db_name); + _file.set("Database.User", _db_user); + _file.set("Database.Pass", _db_pass); + _file.set("Prefix.Enable", _prefix_enable); + _file.set("Prefix.Format", _prefix_format); + _plugin.saveConfig(); } public Boolean isDebug() { @@ -274,6 +313,26 @@ public class ConfigManager { return _db_pass; } + public Boolean getPrefixEnable() { + return _prefix_enable; + } + + public void setPrefixEnable(Boolean enable) { + _prefix_enable = enable; + _file.set("Prefix.Enable", enable); + _plugin.saveConfig(); + } + + public String getPrefixFormat() { + return _prefix_format; + } + + public void setPrefixFormat(String format) { + _prefix_format = format; + _file.set("Prefix.Format", format); + _plugin.saveConfig(); + } + private final EssentialsD _plugin; private FileConfiguration _file; @@ -311,4 +370,8 @@ public class ConfigManager { private String _db_user; private String _db_pass; private String _db_name; + + // prefix + private Boolean _prefix_enable; + private String _prefix_format; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 685dfb8..0ca06c1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -55,6 +55,11 @@ HomeLimit: Amount: 5 # 数量限制 WorldBlackList: [ ] # 不允许设置 home 的世界 +# prefix (需要 PlaceholderAPI 支持) +Prefix: + Enable: false + Format: '<%player_name%> ' + Debug: false CheckUpdate: true \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c537273..9e72272 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -8,6 +8,7 @@ load: STARTUP folia-supported: true softdepend: - LuckPerms + - PlaceholderAPI permissions: essd.*: default: op