feat: 完善 message 相关代码
This commit is contained in:
@@ -3,6 +3,7 @@ package cn.hamster3.mc.plugin.core.bukkit;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.api.CoreBukkitAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.core.ParentCoreCommand;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.lore.ParentLoreCommand;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.hook.PointAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
@@ -34,8 +35,13 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
Logger logger = getLogger();
|
||||
long start = System.currentTimeMillis();
|
||||
logger.info("仓鼠核心正在初始化...");
|
||||
saveDefaultConfig();
|
||||
reloadConfig();
|
||||
logger.info("已读取配置文件.");
|
||||
CoreBukkitAPI.init();
|
||||
logger.info("CoreBukkitAPI 已初始化.");
|
||||
CoreMessage.init(this);
|
||||
logger.info("已初始化语言文本.");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms.");
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -14,10 +14,7 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
private final HikariDataSource datasource;
|
||||
|
||||
private CoreBukkitAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
|
||||
plugin.saveDefaultConfig();
|
||||
FileConfiguration config = plugin.getConfig();
|
||||
FileConfiguration config = HamsterCorePlugin.getInstance().getConfig();
|
||||
|
||||
ConfigurationSection datasourceConfig = config.getConfigurationSection("datasource");
|
||||
if (datasourceConfig == null) {
|
||||
@@ -47,7 +44,7 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull AudienceProvider getAudienceProvider() {
|
||||
public @NotNull BukkitAudiences getAudienceProvider() {
|
||||
return HamsterCorePlugin.getInstance().getAudienceProvider();
|
||||
}
|
||||
|
||||
|
@@ -52,7 +52,7 @@ public class LoreRemoveCommand extends ChildCommand {
|
||||
}
|
||||
int i = Integer.parseInt(args[0]);
|
||||
if (i <= 0) {
|
||||
CoreMessage.COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER_ERROR.show(sender);
|
||||
CoreMessage.COMMAND_LORE_REMOVE_INPUT_NUMBER_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
|
@@ -57,7 +57,7 @@ public class LoreSetCommand extends ChildCommand {
|
||||
}
|
||||
int i = Integer.parseInt(args[0]);
|
||||
if (i <= 0) {
|
||||
CoreMessage.COMMAND_LORE_SET_NOT_INPUT_NUMBER_ERROR.show(sender);
|
||||
CoreMessage.COMMAND_LORE_SET_INPUT_NUMBER_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
|
@@ -1,15 +1,21 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.constant;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.api.CoreBukkitAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public enum CoreMessage {
|
||||
COMMAND_NOT_FOUND,
|
||||
COMMAND_NOT_HAS_PERMISSION,
|
||||
COMMAND_MUST_USED_BY_PLAYER,
|
||||
|
||||
COMMAND_DEBUG_INFO_MODE_ON,
|
||||
COMMAND_DEBUG_INFO_MODE_OFF,
|
||||
|
||||
COMMAND_LORE_HAND_EMPTY,
|
||||
COMMAND_LORE_EMPTY_INPUT,
|
||||
COMMAND_LORE_ADD_SUCCESS,
|
||||
@@ -17,16 +23,52 @@ public enum CoreMessage {
|
||||
COMMAND_LORE_CLEAR_SUCCESS,
|
||||
COMMAND_LORE_NAME_SUCCESS,
|
||||
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER,
|
||||
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER_ERROR,
|
||||
COMMAND_LORE_REMOVE_INPUT_NUMBER_ERROR,
|
||||
COMMAND_LORE_REMOVE_INDEX_OUT_OF_RANGE,
|
||||
COMMAND_LORE_REMOVE_SUCCESS,
|
||||
COMMAND_LORE_SET_NOT_INPUT_NUMBER,
|
||||
COMMAND_LORE_SET_NOT_INPUT_NUMBER_ERROR,
|
||||
COMMAND_LORE_SET_INPUT_NUMBER_ERROR,
|
||||
COMMAND_LORE_SET_NOT_INPUT_TEXT,
|
||||
COMMAND_LORE_SET_INDEX_OUT_OF_RANGE,
|
||||
COMMAND_LORE_SET_SUCCESS;
|
||||
|
||||
private DisplayMessage message;
|
||||
|
||||
public static void init(@NotNull Plugin plugin) {
|
||||
ConfigurationSection config = plugin.getConfig().getConfigurationSection("messages");
|
||||
if (config == null) {
|
||||
plugin.getLogger().warning("加载消息失败: 配置文件中未找到 messages 节点!");
|
||||
return;
|
||||
}
|
||||
for (CoreMessage value : CoreMessage.values()) {
|
||||
try {
|
||||
value.message = BukkitUtils.getDisplayMessage(config.getConfigurationSection(value.name()));
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("加载消息设置 " + value.name() + " 时遇到了一个异常: ");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void show(CommandSender sender) {
|
||||
sender.sendMessage(name());
|
||||
if (message == null) {
|
||||
sender.sendMessage(name());
|
||||
}
|
||||
Audience audience = CoreBukkitAPI.getInstance().getAudienceProvider().sender(sender);
|
||||
message.show(audience);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void show(CommandSender sender, TextReplacementConfig config) {
|
||||
if (message == null) {
|
||||
sender.sendMessage(name());
|
||||
}
|
||||
Audience audience = CoreBukkitAPI.getInstance().getAudienceProvider().sender(sender);
|
||||
message.show(audience, config);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public DisplayMessage getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,11 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -170,4 +172,38 @@ public final class BukkitUtils {
|
||||
}
|
||||
return stack.getType().name();
|
||||
}
|
||||
|
||||
public static DisplayMessage getDisplayMessage(ConfigurationSection config) {
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
DisplayMessage displayMessage = new DisplayMessage();
|
||||
String message = config.getString("message");
|
||||
if (message != null) {
|
||||
displayMessage.setMessage(message);
|
||||
}
|
||||
String actionbar = config.getString("actionbar");
|
||||
if (actionbar != null) {
|
||||
displayMessage.setActionBar(actionbar);
|
||||
}
|
||||
String title = config.getString("title");
|
||||
String subtitle = config.getString("subtitle");
|
||||
if (title != null || subtitle != null) {
|
||||
displayMessage.setTitle(
|
||||
title == null ? "" : title,
|
||||
subtitle == null ? "" : subtitle,
|
||||
config.getInt("fade-in", 10),
|
||||
config.getInt("stay", 70),
|
||||
config.getInt("fade-out", 20)
|
||||
);
|
||||
}
|
||||
String sound = config.getString("sound");
|
||||
if (sound != null) {
|
||||
displayMessage.setSound(sound,
|
||||
(float) config.getDouble("volume", 1f),
|
||||
(float) config.getDouble("pitch", 1f)
|
||||
);
|
||||
}
|
||||
return displayMessage;
|
||||
}
|
||||
}
|
||||
|
@@ -24,3 +24,45 @@ datasource:
|
||||
# 单位:毫秒
|
||||
# 建议设置为比数据库上的 wait_timeout 参数少 30 秒
|
||||
max-lifetime: 1800000
|
||||
|
||||
messages:
|
||||
COMMAND_NOT_FOUND:
|
||||
message: "§c未找到该指令!"
|
||||
COMMAND_NOT_HAS_PERMISSION:
|
||||
message: "§c你没有这个权限!"
|
||||
COMMAND_MUST_USED_BY_PLAYER:
|
||||
message: "§c这个命令只能由玩家执行!"
|
||||
COMMAND_DEBUG_INFO_MODE_ON:
|
||||
message: "§a已开启信息查询模式!"
|
||||
COMMAND_DEBUG_INFO_MODE_OFF:
|
||||
message: "§a已关闭信息查询模式!"
|
||||
COMMAND_LORE_HAND_EMPTY:
|
||||
message: "§c你必须手持一个物品才能使用这个命令!"
|
||||
COMMAND_LORE_EMPTY_INPUT:
|
||||
message: "§c你没有输入lore文本!"
|
||||
COMMAND_LORE_ADD_SUCCESS:
|
||||
message: "§a已成功添加lore文本!"
|
||||
COMMAND_LORE_CLEAR_NOTHING:
|
||||
message: "§c这个物品没有lore文本!"
|
||||
COMMAND_LORE_CLEAR_SUCCESS:
|
||||
message: "§c已清理该物品的全部lore文本!"
|
||||
COMMAND_LORE_NAME_SUCCESS:
|
||||
message: "§a已成功设置物品名称!"
|
||||
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER:
|
||||
message: "§c请输入要删除的行号!"
|
||||
COMMAND_LORE_REMOVE_INPUT_NUMBER_ERROR:
|
||||
message: "§c行号必须是一个大于0的整数!"
|
||||
COMMAND_LORE_REMOVE_INDEX_OUT_OF_RANGE:
|
||||
message: "§c你的手持物品没有这么多行lore文本!"
|
||||
COMMAND_LORE_REMOVE_SUCCESS:
|
||||
message: "§a已成功删除lore文本!"
|
||||
COMMAND_LORE_SET_NOT_INPUT_NUMBER:
|
||||
message: "§c请输入要设置的行号!"
|
||||
COMMAND_LORE_SET_INPUT_NUMBER_ERROR:
|
||||
message: "§c行号必须是一个大于0的整数!"
|
||||
COMMAND_LORE_SET_NOT_INPUT_TEXT:
|
||||
message: "§c请输入要设置的lore文本!"
|
||||
COMMAND_LORE_SET_INDEX_OUT_OF_RANGE:
|
||||
message: "§c你的手持物品没有这么多行lore文本!"
|
||||
COMMAND_LORE_SET_SUCCESS:
|
||||
message: "§a已成功设置lore文本!"
|
||||
|
Reference in New Issue
Block a user