Skip to content

Commit

Permalink
msg: 添加消息
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvigarabis committed Jan 4, 2024
1 parent 1ad3463 commit 3261efa
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void onDisable(){

@Override
public void reloadConfig(){
logger.info("正在加载配置文件");

saveDefaultConfig();
super.reloadConfig();
ESplitterConfig.cleanConfig();
Expand All @@ -95,5 +97,16 @@ public void reloadConfig(){
logger.warning("插件配置有误,请检查配置文件是否正确。");
logger.warning("在确认无误后,可以使用/esplitter reload重新加载");
}

logger.info("正在加载聊天消息");
saveResource("message.yml", false);
var messageConfigFile = new File(getDataDir().getPath(), "message.yml");
Messages.cleanMessageConfig();
Messages.loadMessageConfig(messageConfigFile);
if (Messages.countMissingMessage() == 0){
logger.info("聊天消息已加载成功!");
} else {
logger.warning("缺少" + Messages.countMissingMessage() + "聊天消息");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.Bukkit;
import io.github.silvigarabis.esplitter.Messages.MessageKeys;

import java.util.logging.Logger;

Expand All @@ -24,16 +25,14 @@ public class MainCommandExecutor implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
ESplitterPlugin.getPlugin().getLogger().info("正在处理指令……");
if (!ESplitterPlugin.isConfigured()){
sender.sendMessage("警告!插件配置错误!");
sender.sendMessage("查看控制台日志以获得详情");

Messages.send(sender, MessageKeys.INVALID_PLUGIN_CONFIG);
ESplitterPlugin.getPlugin().getLogger().warning("插件的配置文件没有正确配置");
ESplitterPlugin.getPlugin().getLogger().warning("这可能是因为其中含有语法错误,或者缺失了必要的配置");
ESplitterPlugin.getPlugin().getLogger().warning("尝试修正配置,并在这之后使用 /esplitter reload 重新载入配置文件");
}

if (!Permissions.MAIN_COMMAND.check(sender)){
sender.sendMessage("没有使用权限");
Messages.send(sender, MessageKeys.COMMAND_NO_PERMISSION);
return true;
}

Expand Down Expand Up @@ -64,7 +63,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
helpCmd(sender, label, args);
break;
default:
sender.sendMessage("未知的命令格式");
Messages.send(sender, MessageKeys.COMMAND_UNKNOWN);
helpCmd(sender, label, args);
break;
}
Expand All @@ -82,71 +81,76 @@ private static Player getPlayer(String playerName){

private void guiCmd(CommandSender sender, String label, String[] args){
if (!ESplitterPlugin.isConfigured()){
sender.sendMessage("插件配置错误!");
Messages.send(sender, MessageKeys.INVALID_PLUGIN_CONFIG);
return;
}
if (!Permissions.COMMAND_GUI.check(sender)){
sender.sendMessage("没有权限打开GUI");
Messages.send(sender, MessageKeys.COMMAND_GUI_NO_PERMISSION);
return;
}

if (args.length == 2){
var player = getPlayer(args[1]);
if (!sender.equals(player) && !Permissions.COMMAND_GUI_OTHERS.check(sender)){
sender.sendMessage("没有权限为其他玩家打开GUI");
Messages.send(sender, MessageKeys.COMMAND_GUI_NO_PERMISSION_FOR_OTHERS);
return;
}
if (player != null){
openGui(player);
} else {
sender.sendMessage("错误!未找到玩家");
Messages.send(sender, MessageKeys.COMMAND_GUI_OTHER_PLAYER_NOTFOUND, args[1]);
}
} else {
if (sender instanceof Player){
openGui((Player)sender);
} else {
sender.sendMessage("仅玩家可用");
Messages.send(sender, MessageKeys.COMMAND_GUI_PLAYER_ONLY);
}
}
}

private void debugCmd(CommandSender sender, String label, String[] args){
if (!Permissions.COMMAND_DEBUG.check(sender)){
sender.sendMessage("没有使用权限");
Messages.send(sender, MessageKeys.COMMAND_DEBUG_NO_PERMISSION);
return;
}
if (args.length == 2){
boolean mode = "true".equals(args[1]);
ESplitterPlugin.getPlugin().setDebugMode(mode);
sender.sendMessage("Debug 模式被设置为 " + mode);
Messages.send(sender,
MessageKeys.COMMAND_DEBUG_STATUS,
"" + ESplitterPlugin.getPlugin().isDebugMode()
);
} else {
sender.sendMessage("Debug 模式:" + ESplitterPlugin.getPlugin().isDebugMode());
Messages.send(sender,
MessageKeys.COMMAND_DEBUG_STATUS,
"" + ESplitterPlugin.getPlugin().isDebugMode()
);
}
}

private void reloadCmd(CommandSender sender, String label, String[] args){
if (!Permissions.COMMAND_RELOAD.check(sender)){
sender.sendMessage("没有使用权限");
Messages.send(sender, MessageKeys.COMMAND_RELOAD_NO_PERMISSION);
return;
}

sender.sendMessage("重新载入配置文件中");
Messages.send(sender, MessageKeys.COMMAND_RELOAD_PROCESSING);

ESplitterPlugin.getPlugin().reloadConfig();

if (ESplitterPlugin.isConfigured()){
sender.sendMessage("已成功加载配置文件!");
Messages.send(sender, MessageKeys.COMMAND_RELOAD_SUCCESS);
} else {
sender.sendMessage("载入配置文件失败!请检查服务器控制台日志");
Messages.send(sender, MessageKeys.COMMAND_RELOAD_FAIL);
}
}

private void helpCmd(CommandSender sender, String label, String[] args){
if (! "esgui".equals(label)){
sender.sendMessage("用法:/" + label + " [gui|help|reload|debug]");
sender.sendMessage("用法:/esgui [player]");
if ("esgui".equals(label)){
Messages.send(sender, MessageKeys.COMMAND_HELP_ESGUI, label);
} else {
sender.sendMessage("用法:/" + label + " [player]");
Messages.send(sender, MessageKeys.COMMAND_HELP_GENERAL, label);
}
}

Expand Down
74 changes: 61 additions & 13 deletions src/main/java/io/github/silvigarabis/esplitter/Messages.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
package io.github.silvigarabis.esplitter;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.YamlConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.util.Map;
import java.util.EnumMap;

public class Messages {
public enum MessageKey {
COMMAND_NO_PERMISSION_COMMAND("command-no-permission.command"),
COMMAND_NO_PERMISSION_GUI("command-no-permission.gui"),
COMMAND_NO_PERMISSION_GUI_OTHERS("command-no-permission.gui.others"),
COMMAND_NO_PERMISSION_RELOAD("command-no-permission.reload"),
COMMAND_NO_PERMISSION_DEBUG("command-no-permission.debug");

COMMAND_NO_PERMISSION("command-no-permission"),
COMMAND_UNKNOWN_COMMAND("command-unknown"),

COMMAND_GUI_NO_PERMISSION("command-gui.no-permission"),
COMMAND_GUI_NO_PERMISSION_OTHERS("command-gui.no-permission-for-others"),
COMMAND_GUI_OTHER_PLAYER_NOTFOUND("command-gui.other-player-not-found"),
COMMAND_GUI_PLAYER_ONLY("command-gui.player-only"),

COMMAND_DEBUG_NO_PERMISSION("command-debug.no-permission"),
COMMAND_DEBUG_ENABLED("command-debug.debug-enable"),
COMMAND_DEBUG_DISABLED("command-debug.debug-disable"),
COMMAND_DEBUG_STATUS("command-debug.debug-status"),

COMMAND_RELOAD_NO_PERMISSION("command-reload.no-permission"),
COMMAND_RELOAD_PROCESSING("command-reload.reload-processing"),
COMMAND_RELOAD_SUCCESS("command-reload.success"),
COMMAND_RELOAD_FAIL("command-reload.fail"),

COMMAND_HELP_NO_PERMISSION("command-help.no-permission"),
COMMAND_HELP_GENERAL("command-help.general"),
COMMAND_HELP_ESGUI("command-help.esgui"),

GUI_TITLE("gui.title"),
GUI_UNEXPECTED_CLOSE("gui.unexpected-error"),

INVALID_PLUGIN_CONFIG("invalid-plugin-config"),
CHAT_PREFIX("chat-prefix");

private String messageKey;
public String getMessageKey(){
return messageKey;
Expand All @@ -28,33 +51,58 @@ public void send(CommandSender sender){
public void send(CommandSender sender, String... replacements){
Messages.send(sender, this, replacements);
}
public String getMessageString(){
return Messages.getMessageString(this);
}
}

private static Map<String, String> messages = new EnumMap<>();
public void cleanMessageConfig(){
public static int countMissingMessage(){
return MessageKey.values() - messages.size();
}
public static void cleanMessageConfig(){
messages.clear();
}
public void loadMessageConfig(ConfigurationSection messageConfig){
public static void loadMessageConfig(ConfigurationSection messageConfig){
for (var messageKey : MessageKey.values()){
var key = messageKey.getMessageKey();
var messageString = messageConfig.get(key);
if (messageString == null){
messageString = "";
continue;
}
messages.put(messageKey, messageString);
}
}
public static void loadMessageConfig(File yamlConfigFile){
var messageConfig = YamlConfiguration.loadConfiguration(yamlConfigFile);
loadMessageConfig(messageConfig);
}
public static String getMessageString(MessageKey messageKey){
return messages.contains(messageKey) ? messages.get(messageKey) : "";
return messages.contains(messageKey) ? messages.get(messageKey) : messageKey.getMessageKey();
}
public static void send(CommandSender sender, MessageKeys key, String[] replacements){
var messageString = getMessageString(key);
for (var replacement : replacements){
messageString = messageString.replaceFirst("\\{\\}", replacement);
}
sender.sendMessage(messageString);
send(sender, messageString);
}
public static void send(CommandSender sender, MessageKeys key){
sender.sendMessage(getMessageString(key));
send(sender, getMessageString(key));
}
public static void send(CommandSender, sender, String message){
message = getMessageString(CHAT_PREFIX) + " " + message;
message = message.replaceAll("&([0-9a-fmnol])", "§$1");
message = message.replaceAll("&&", "&");
sender.sendMessage(message);
}
public static void sendConsole(MessageKeys key, String[] replacements){
send(Bukkit.getConsoleSender(), key, replacements);
}
public static void sendConsole(MessageKeys key){
send(Bukkit.getConsoleSender(), key);
}
public static void sendConsole(String message){
send(Bukkit.getConsoleSender(), message);
}
}
34 changes: 34 additions & 0 deletions src/main/resources/message.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
chat-prefix: "[ESplitter]"
invalid-plugin-config: ""

gui:
title:
unexpected-error:

command-no-permission: "&c没有使用权限"
command-unknown: "&e未知的命令"

command-help:
general: |-
用法:/{} [gui|help|reload|debug]
用法:/esgui [player]
esgui: |-
用法:/{} [player]
command-gui:
no-permission: "没有权限打开GUI"
no-permission-for-others: "没有权限为其他玩家打开GUI"
other-player-not-found: "错误!未找到玩家 {}"
player-only: "仅玩家可用"

command-reload:
no-permission: "没有权限重载插件"
processing-reload: "重新载入配置文件中"
success: "已成功加载配置文件!"
fail: "载入配置文件失败!请检查服务器控制台日志"

command-debug:
no-permission: "没有权限使用此功能"
debug-enable: "Debug 模式被设置为 true"
debug-disable: "Debug 模式被设置为 false"
debug-status: "Debug 模式: {}"

0 comments on commit 3261efa

Please sign in to comment.