feat: 完善 message 相关代码

This commit is contained in:
2022-10-29 05:31:19 +08:00
parent 57cecc1fc0
commit 9537e133eb
9 changed files with 198 additions and 39 deletions

View File

@@ -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.api.CoreBukkitAPI;
import cn.hamster3.mc.plugin.core.bukkit.command.core.ParentCoreCommand; 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.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.PointAPI;
import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI; import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener; import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
@@ -34,8 +35,13 @@ public class HamsterCorePlugin extends JavaPlugin {
Logger logger = getLogger(); Logger logger = getLogger();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
logger.info("仓鼠核心正在初始化..."); logger.info("仓鼠核心正在初始化...");
saveDefaultConfig();
reloadConfig();
logger.info("已读取配置文件.");
CoreBukkitAPI.init(); CoreBukkitAPI.init();
logger.info("CoreBukkitAPI 已初始化."); logger.info("CoreBukkitAPI 已初始化.");
CoreMessage.init(this);
logger.info("已初始化语言文本.");
long time = System.currentTimeMillis() - start; long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms."); logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms.");
} }

View File

@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; 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.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -14,10 +14,7 @@ public final class CoreBukkitAPI extends CoreAPI {
private final HikariDataSource datasource; private final HikariDataSource datasource;
private CoreBukkitAPI() { private CoreBukkitAPI() {
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance(); FileConfiguration config = HamsterCorePlugin.getInstance().getConfig();
plugin.saveDefaultConfig();
FileConfiguration config = plugin.getConfig();
ConfigurationSection datasourceConfig = config.getConfigurationSection("datasource"); ConfigurationSection datasourceConfig = config.getConfigurationSection("datasource");
if (datasourceConfig == null) { if (datasourceConfig == null) {
@@ -47,7 +44,7 @@ public final class CoreBukkitAPI extends CoreAPI {
} }
@Override @Override
public @NotNull AudienceProvider getAudienceProvider() { public @NotNull BukkitAudiences getAudienceProvider() {
return HamsterCorePlugin.getInstance().getAudienceProvider(); return HamsterCorePlugin.getInstance().getAudienceProvider();
} }

View File

@@ -52,7 +52,7 @@ public class LoreRemoveCommand extends ChildCommand {
} }
int i = Integer.parseInt(args[0]); int i = Integer.parseInt(args[0]);
if (i <= 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; return true;
} }
Player player = (Player) sender; Player player = (Player) sender;

View File

@@ -57,7 +57,7 @@ public class LoreSetCommand extends ChildCommand {
} }
int i = Integer.parseInt(args[0]); int i = Integer.parseInt(args[0]);
if (i <= 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; return true;
} }
Player player = (Player) sender; Player player = (Player) sender;

View File

@@ -1,15 +1,21 @@
package cn.hamster3.mc.plugin.core.bukkit.constant; 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.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
public enum CoreMessage { public enum CoreMessage {
COMMAND_NOT_FOUND, COMMAND_NOT_FOUND,
COMMAND_NOT_HAS_PERMISSION, COMMAND_NOT_HAS_PERMISSION,
COMMAND_MUST_USED_BY_PLAYER, COMMAND_MUST_USED_BY_PLAYER,
COMMAND_DEBUG_INFO_MODE_ON, COMMAND_DEBUG_INFO_MODE_ON,
COMMAND_DEBUG_INFO_MODE_OFF, COMMAND_DEBUG_INFO_MODE_OFF,
COMMAND_LORE_HAND_EMPTY, COMMAND_LORE_HAND_EMPTY,
COMMAND_LORE_EMPTY_INPUT, COMMAND_LORE_EMPTY_INPUT,
COMMAND_LORE_ADD_SUCCESS, COMMAND_LORE_ADD_SUCCESS,
@@ -17,16 +23,52 @@ public enum CoreMessage {
COMMAND_LORE_CLEAR_SUCCESS, COMMAND_LORE_CLEAR_SUCCESS,
COMMAND_LORE_NAME_SUCCESS, COMMAND_LORE_NAME_SUCCESS,
COMMAND_LORE_REMOVE_NOT_INPUT_NUMBER, 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_INDEX_OUT_OF_RANGE,
COMMAND_LORE_REMOVE_SUCCESS, COMMAND_LORE_REMOVE_SUCCESS,
COMMAND_LORE_SET_NOT_INPUT_NUMBER, 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_NOT_INPUT_TEXT,
COMMAND_LORE_SET_INDEX_OUT_OF_RANGE, COMMAND_LORE_SET_INDEX_OUT_OF_RANGE,
COMMAND_LORE_SET_SUCCESS; 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) { 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;
} }
} }

View File

@@ -1,9 +1,11 @@
package cn.hamster3.mc.plugin.core.bukkit.util; package cn.hamster3.mc.plugin.core.bukkit.util;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@@ -170,4 +172,38 @@ public final class BukkitUtils {
} }
return stack.getType().name(); 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;
}
} }

View File

@@ -24,3 +24,45 @@ datasource:
# 单位:毫秒 # 单位:毫秒
# 建议设置为比数据库上的 wait_timeout 参数少 30 秒 # 建议设置为比数据库上的 wait_timeout 参数少 30 秒
max-lifetime: 1800000 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文本"

View File

@@ -1,6 +1,6 @@
package cn.hamster3.mc.plugin.core.common.constant; package cn.hamster3.mc.plugin.core.common.constant;
import cn.hamster3.mc.plugin.core.common.data.Message; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import com.google.gson.*; import com.google.gson.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -19,14 +19,14 @@ public interface CoreConstantObjects {
* GSON 工具 * GSON 工具
*/ */
Gson GSON = new GsonBuilder() Gson GSON = new GsonBuilder()
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE) .registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
.create(); .create();
/** /**
* GSON 工具会使用格式化输出、且解析中包含null参数 * GSON 工具会使用格式化输出、且解析中包含null参数
*/ */
Gson GSON_HUMAN = new GsonBuilder() Gson GSON_HUMAN = new GsonBuilder()
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE) .registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
.serializeNulls() .serializeNulls()
.setPrettyPrinting() .setPrettyPrinting()
.create(); .create();
@@ -60,19 +60,19 @@ public interface CoreConstantObjects {
} }
} }
class MessageTypeAdapter implements JsonSerializer<Message>, JsonDeserializer<Message> { class MessageTypeAdapter implements JsonSerializer<DisplayMessage>, JsonDeserializer<DisplayMessage> {
public static final MessageTypeAdapter INSTANCE = new MessageTypeAdapter(); public static final MessageTypeAdapter INSTANCE = new MessageTypeAdapter();
private MessageTypeAdapter() { private MessageTypeAdapter() {
} }
@Override @Override
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public DisplayMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Message().json(json); return new DisplayMessage().fromJson(json);
} }
@Override @Override
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(DisplayMessage src, Type typeOfSrc, JsonSerializationContext context) {
return src.saveToJson(); return src.saveToJson();
} }
} }

View File

@@ -16,7 +16,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Message { public class DisplayMessage {
private Component message; private Component message;
private Component actionbar; private Component actionbar;
private Title title; private Title title;
@@ -25,7 +25,43 @@ public class Message {
*/ */
private Sound sound; private Sound sound;
public Message() { public DisplayMessage() {
}
public static DisplayMessage message(@NotNull String message) {
return new DisplayMessage().setMessage(message);
}
public static DisplayMessage message(@NotNull Component message) {
return new DisplayMessage().setMessage(message);
}
public static DisplayMessage actionbar(@NotNull String message) {
return new DisplayMessage().setActionBar(message);
}
public static DisplayMessage actionbar(@NotNull Component message) {
return new DisplayMessage().setActionBar(message);
}
public static DisplayMessage title(@NotNull String title, @NotNull String subtitle) {
return new DisplayMessage().setTitle(title, subtitle);
}
public static DisplayMessage title(@NotNull String title, @NotNull String subtitle, int fadeIn, int stay, int fadeOut) {
return new DisplayMessage().setTitle(title, subtitle, fadeIn, stay, fadeOut);
}
public static DisplayMessage sound(@NotNull String sound) {
return new DisplayMessage().setSound(sound);
}
public static DisplayMessage sound(@NotNull String sound, float volume, float pitch) {
return new DisplayMessage().setSound(sound, volume, pitch);
}
public static DisplayMessage sound(@NotNull Sound sound) {
return new DisplayMessage().setSound(sound);
} }
public void show(@NotNull Audience audience) { public void show(@NotNull Audience audience) {
@@ -43,7 +79,7 @@ public class Message {
} }
} }
public void show(@NotNull Audience audience, TextReplacementConfig replacement) { public void show(@NotNull Audience audience, @NotNull TextReplacementConfig replacement) {
if (message != null) { if (message != null) {
audience.sendMessage(message.compact().replaceText(replacement)); audience.sendMessage(message.compact().replaceText(replacement));
} }
@@ -80,37 +116,37 @@ public class Message {
} }
@NotNull @NotNull
public Message message(@NotNull String message) { public DisplayMessage setMessage(@NotNull String message) {
this.message = Component.text(message); this.message = Component.text(message);
return this; return this;
} }
@NotNull @NotNull
public Message message(@NotNull Component message) { public DisplayMessage setMessage(@NotNull Component message) {
this.message = message; this.message = message;
return this; return this;
} }
@NotNull @NotNull
public Message actionbar(@NotNull String message) { public DisplayMessage setActionBar(@NotNull String message) {
this.actionbar = Component.text(message); this.actionbar = Component.text(message);
return this; return this;
} }
@NotNull @NotNull
public Message actionbar(@NotNull Component message) { public DisplayMessage setActionBar(@NotNull Component message) {
this.actionbar = message; this.actionbar = message;
return this; return this;
} }
@NotNull @NotNull
public Message title(@NotNull String title, @NotNull String subtitle) { public DisplayMessage setTitle(@NotNull String title, @NotNull String subtitle) {
this.title = Title.title(Component.text(title), Component.text(subtitle)); this.title = Title.title(Component.text(title), Component.text(subtitle));
return this; return this;
} }
@NotNull @NotNull
public Message title(@NotNull String title, @NotNull String subtitle, int fadeIn, int stay, int fadeOut) { public DisplayMessage setTitle(@NotNull String title, @NotNull String subtitle, int fadeIn, int stay, int fadeOut) {
this.title = Title.title( this.title = Title.title(
Component.text(title), Component.text(title),
Component.text(subtitle), Component.text(subtitle),
@@ -124,13 +160,13 @@ public class Message {
} }
@NotNull @NotNull
public Message title(@NotNull Component title, @NotNull Component subtitle) { public DisplayMessage setTitle(@NotNull Component title, @NotNull Component subtitle) {
this.title = Title.title(title, subtitle); this.title = Title.title(title, subtitle);
return this; return this;
} }
@NotNull @NotNull
public Message title(@NotNull Component title, @NotNull Component subtitle, int fadeIn, int stay, int fadeOut) { public DisplayMessage setTitle(@NotNull Component title, @NotNull Component subtitle, int fadeIn, int stay, int fadeOut) {
this.title = Title.title( this.title = Title.title(
title, title,
subtitle, subtitle,
@@ -144,55 +180,55 @@ public class Message {
} }
@NotNull @NotNull
public Message title(@Nullable Title title) { public DisplayMessage setTitle(@Nullable Title title) {
this.title = title; this.title = title;
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public Message sound(@NotNull String sound) { public DisplayMessage setSound(@NotNull String sound) {
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, 1, 1); this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, 1, 1);
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public Message sound(@NotNull String namespace, @NotNull String path) { public DisplayMessage setSound(@NotNull String namespace, @NotNull String path) {
this.sound = Sound.sound(Key.key(namespace, path), Sound.Source.MASTER, 1, 1); this.sound = Sound.sound(Key.key(namespace, path), Sound.Source.MASTER, 1, 1);
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public Message sound(@NotNull String sound, float volume, float pitch) { public DisplayMessage setSound(@NotNull String sound, float volume, float pitch) {
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch); this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch);
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public Message sound(@NotNull String namespace, @NotNull String value, float volume, float pitch) { public DisplayMessage setSound(@NotNull String namespace, @NotNull String value, float volume, float pitch) {
this.sound = Sound.sound(Key.key(namespace, value), Sound.Source.MASTER, volume, pitch); this.sound = Sound.sound(Key.key(namespace, value), Sound.Source.MASTER, volume, pitch);
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("PatternValidation") @SuppressWarnings("PatternValidation")
public Message sound(@NotNull String namespace, @NotNull String value, @NotNull Sound.Source source, float volume, float pitch) { public DisplayMessage setSound(@NotNull String namespace, @NotNull String value, @NotNull Sound.Source source, float volume, float pitch) {
this.sound = Sound.sound(Key.key(namespace, value), source, volume, pitch); this.sound = Sound.sound(Key.key(namespace, value), source, volume, pitch);
return this; return this;
} }
@NotNull @NotNull
public Message sound(@Nullable Sound sound) { public DisplayMessage setSound(@Nullable Sound sound) {
this.sound = sound; this.sound = sound;
return this; return this;
} }
@NotNull @NotNull
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
public Message json(@NotNull JsonElement element) { public DisplayMessage fromJson(@NotNull JsonElement element) {
if (!element.isJsonObject()) { if (!element.isJsonObject()) {
message = Component.text(element.toString()); message = Component.text(element.toString());
return this; return this;