换行 + * 例如:"#RED#你输了!" -> "§c你输了!" + * @param string 带特殊颜色代码的文本 + * @return 转换后的文本 + */ + public static String translateColor(Object string) { + // 正则表达式替换 + String result = (String) string; // #RED#[红队] + Pattern regex = Pattern.compile("#[A-Z_-]+#"); + Matcher matcher = regex.matcher(result); + while (matcher.find()) { + String code = matcher.group(); // #RED# + result = result.replaceFirst( + code, // #RED# + "§" + ChatColor.valueOf(code.replaceAll("#", "")).getChar() // §c + ); + } + return result; + } + + /** + * 将带特殊颜色代码的文本转为带颜色代码的文本 (带占位符) + *
+ * 例如:"#RED#你输了!" -> "§c你输了!" + * @param string 带特殊颜色代码的文本 + * @param args 占位符替换 + * @return 转换后的文本 + */ + public static String translateColor(Object string, Object... args) { + return translateColor(String.format((String) string, args)); + } + + /** + * 全服公告消息(带占位符) + * @param message 要公告的消息 + * @param args 占位符替换 + */ + public static void broadcast(String message, Object... args) { + Bukkit.getOnlinePlayers().forEach(it -> PlayerUtils.send(it, ClickRailSpawnCar.normal + message, args)); + } + +} diff --git a/src/main/java/huihui/crsc/utils/LogUtils.java b/src/main/java/huihui/crsc/utils/LogUtils.java new file mode 100644 index 0000000..1810c7d --- /dev/null +++ b/src/main/java/huihui/crsc/utils/LogUtils.java @@ -0,0 +1,47 @@ +package huihui.crsc.utils; + + +import huihui.crsc.ClickRailSpawnCar; + +/** + * 日志工具库 + * @author X_huihui + */ +public class LogUtils { + // 定义instance + static ClickRailSpawnCar instance = ClickRailSpawnCar.instance; + + /** + * LogInfo + * @param log 要输出的info + */ + public static void info(Object log){ + instance.getLogger().info(ChatUtils.translateColor(log.toString())); + } + + /** + * LogInfo(WithArguments) + * @param log 要输出的info + * @param args 占位符 + */ + public static void info(Object log, Object... args){ + instance.getLogger().info(ChatUtils.translateColor(log.toString(), args)); + } + + /** + * LogWarning + * @param log 要输出的warning + */ + public static void warning(Object log){ + instance.getLogger().warning(ChatUtils.translateColor(log.toString())); + } + + /** + * LogWarning(WithArguments) + * @param log 要输出的warning + * @param args 占位符 + */ + public static void warning(Object log, Object... args){ + instance.getLogger().warning(ChatUtils.translateColor(log.toString(), args)); + } +} diff --git a/src/main/java/huihui/crsc/utils/PlayerUtils.java b/src/main/java/huihui/crsc/utils/PlayerUtils.java new file mode 100644 index 0000000..6684868 --- /dev/null +++ b/src/main/java/huihui/crsc/utils/PlayerUtils.java @@ -0,0 +1,33 @@ +package huihui.crsc.utils; + +import org.bukkit.command.CommandSender; + +import java.util.Collections; + +/** + * Player工具类 + * @author WindLeaf_qwq + */ +public class PlayerUtils { + + /** + * 给玩家发送信息 + * @param sender 玩家或控制台 + * @param message 要发送的消息 + */ + public static void send(CommandSender sender, Object message) { + StringBuilder sb = new StringBuilder(); + Collections.singletonList(message).forEach(it -> sb.append(it).append(" ")); + sender.sendMessage(ChatUtils.translateColor(sb.toString())); + } + + /** + * 给玩家发送消息(带占位符) + * @param sender 玩家或控制台 + * @param message 要发送的信息 + * @param args 占位符替换 + */ + public static void send(CommandSender sender, Object message, Object... args) { + send(sender, String.format((String) message, args)); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8a87159..4a22b60 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: ClickRailSpawnCar version: '${project.version}' main: huihui.crsc.ClickRailSpawnCar -api-version: 1.16 +api-version: 1.13