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.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.");
}

View File

@@ -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();
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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) {
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;
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;
}
}

View File

@@ -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文本"

View File

@@ -1,6 +1,6 @@
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 org.jetbrains.annotations.NotNull;
@@ -19,14 +19,14 @@ public interface CoreConstantObjects {
* GSON 工具
*/
Gson GSON = new GsonBuilder()
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE)
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
.create();
/**
* GSON 工具会使用格式化输出、且解析中包含null参数
*/
Gson GSON_HUMAN = new GsonBuilder()
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE)
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
.serializeNulls()
.setPrettyPrinting()
.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();
private MessageTypeAdapter() {
}
@Override
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Message().json(json);
public DisplayMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new DisplayMessage().fromJson(json);
}
@Override
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(DisplayMessage src, Type typeOfSrc, JsonSerializationContext context) {
return src.saveToJson();
}
}

View File

@@ -16,7 +16,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("unused")
public class Message {
public class DisplayMessage {
private Component message;
private Component actionbar;
private Title title;
@@ -25,7 +25,43 @@ public class Message {
*/
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) {
@@ -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) {
audience.sendMessage(message.compact().replaceText(replacement));
}
@@ -80,37 +116,37 @@ public class Message {
}
@NotNull
public Message message(@NotNull String message) {
public DisplayMessage setMessage(@NotNull String message) {
this.message = Component.text(message);
return this;
}
@NotNull
public Message message(@NotNull Component message) {
public DisplayMessage setMessage(@NotNull Component message) {
this.message = message;
return this;
}
@NotNull
public Message actionbar(@NotNull String message) {
public DisplayMessage setActionBar(@NotNull String message) {
this.actionbar = Component.text(message);
return this;
}
@NotNull
public Message actionbar(@NotNull Component message) {
public DisplayMessage setActionBar(@NotNull Component message) {
this.actionbar = message;
return this;
}
@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));
return this;
}
@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(
Component.text(title),
Component.text(subtitle),
@@ -124,13 +160,13 @@ public class Message {
}
@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);
return this;
}
@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(
title,
subtitle,
@@ -144,55 +180,55 @@ public class Message {
}
@NotNull
public Message title(@Nullable Title title) {
public DisplayMessage setTitle(@Nullable Title title) {
this.title = title;
return this;
}
@NotNull
@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);
return this;
}
@NotNull
@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);
return this;
}
@NotNull
@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);
return this;
}
@NotNull
@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);
return this;
}
@NotNull
@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);
return this;
}
@NotNull
public Message sound(@Nullable Sound sound) {
public DisplayMessage setSound(@Nullable Sound sound) {
this.sound = sound;
return this;
}
@NotNull
@SuppressWarnings("UnusedReturnValue")
public Message json(@NotNull JsonElement element) {
public DisplayMessage fromJson(@NotNull JsonElement element) {
if (!element.isJsonObject()) {
message = Component.text(element.toString());
return this;