From ac4dfc913b3082b1d51338d2d229d3d995cca11e Mon Sep 17 00:00:00 2001 From: Tomachi <8929706+book000@users.noreply.github.com> Date: Wed, 8 Jun 2022 14:44:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20tips=E3=82=B3=E3=83=9E=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=81=AE=E5=AE=9F=E8=A3=85=20(#845)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: tipsコマンドの実装 * fix: Call to 'printStackTrace()' --- .../com/jaoafa/mymaid4/command/Cmd_Tips.java | 127 ++++++++++++++++++ .../com/jaoafa/mymaid4/lib/MyMaidLibrary.java | 7 +- .../java/com/jaoafa/mymaid4/lib/Tips.java | 107 +++++++++++++++ 3 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/jaoafa/mymaid4/command/Cmd_Tips.java create mode 100644 src/main/java/com/jaoafa/mymaid4/lib/Tips.java diff --git a/src/main/java/com/jaoafa/mymaid4/command/Cmd_Tips.java b/src/main/java/com/jaoafa/mymaid4/command/Cmd_Tips.java new file mode 100644 index 000000000..f825bf46d --- /dev/null +++ b/src/main/java/com/jaoafa/mymaid4/command/Cmd_Tips.java @@ -0,0 +1,127 @@ +/* + * jaoLicense + * + * Copyright (c) 2022 jao Minecraft Server + * + * The following license applies to this project: jaoLicense + * + * Japanese: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE.md + * English: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE-en.md + */ + +package com.jaoafa.mymaid4.command; + +import cloud.commandframework.ArgumentDescription; +import cloud.commandframework.Command; +import cloud.commandframework.arguments.standard.StringArgument; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.meta.CommandMeta; +import com.jaoafa.mymaid4.lib.*; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Cmd_Tips extends MyMaidLibrary implements CommandPremise { + @Override + public MyMaidCommand.Detail details() { + return new MyMaidCommand.Detail( + "tips", + "Tipsの定型文を投稿・管理します。" + ); + } + + @Override + public MyMaidCommand.Cmd register(Command.Builder builder) { + return new MyMaidCommand.Cmd( + builder + .meta(CommandMeta.DESCRIPTION, "Tipsを追加します。") + .literal("add", "a", "register", "reg") + .argument(StringArgument.of("name"), ArgumentDescription.of("Tipsの名前")) + .argument(StringArgument.greedy("text"), ArgumentDescription.of("Tipsの文章")) + .handler(this::addTip) + .build(), + builder + .meta(CommandMeta.DESCRIPTION, "Tipsを削除します。") + .literal("remove", "rem", "del", "r", "d", "unregister", "unreg") + .argument(StringArgument.of("name"), ArgumentDescription.of("Tipsの名前")) + .handler(this::removeTip) + .build(), + builder + .meta(CommandMeta.DESCRIPTION, "Tipsを表示します。") + .literal("list", "l", "view", "v") + .handler(this::listTip) + .build(), + builder + .meta(CommandMeta.DESCRIPTION, "Tipsを送信・発言します。") + .literal("broadcast", "b", "send", "s", "say") + .argument(StringArgument.newBuilder("name").withSuggestionsProvider(this::suggestTipNames), ArgumentDescription.of("Tipsの名前")) + .handler(this::broadcastTip) + .build() + ); + } + + void addTip(CommandContext context) { + CommandSender sender = context.getSender(); + String name = context.get("name"); + String text = context.get("text"); + + if (Tips.isExist(name)) { + SendMessage(sender, details(), "「%s」は既に存在しています。".formatted(name)); + return; + } + + Tips.addTip(name, text); + SendMessage(sender, details(), "「%s」を追加しました。".formatted(name)); + } + + void removeTip(CommandContext context) { + CommandSender sender = context.getSender(); + String name = context.get("name"); + + if (!Tips.isExist(name)) { + SendMessage(sender, details(), "「%s」は存在しません。".formatted(name)); + return; + } + + Tips.removeTip(name); + SendMessage(sender, details(), "「%s」を削除しました。".formatted(name)); + } + + void listTip(CommandContext context) { + CommandSender sender = context.getSender(); + + SendMessage(sender, details(), "Tips 一覧"); + for (Map.Entry entry : Tips.getTips().entrySet()) { + SendMessage(sender, details(), "- %s: %s".formatted(entry.getKey(), entry.getValue())); + } + } + + void broadcastTip(CommandContext context) { + CommandSender sender = context.getSender(); + String name = context.get("name"); + + TextColor color = NamedTextColor.GRAY; + if (sender instanceof Player) { + SKKColorManager.getPlayerColor((Player) sender); + } + + if (!Tips.isExist(name)) { + SendMessage(sender, details(), "「%s」は存在しません。".formatted(name)); + return; + } + + String text = Tips.getTip(name); + chatFake(color, sender.getName(), text); + } + + List suggestTipNames(final CommandContext context, final String current) { + return Tips.getTips().keySet().stream() + .filter(name -> name.startsWith(current)) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/com/jaoafa/mymaid4/lib/MyMaidLibrary.java b/src/main/java/com/jaoafa/mymaid4/lib/MyMaidLibrary.java index 2a533211f..ad8b53d55 100644 --- a/src/main/java/com/jaoafa/mymaid4/lib/MyMaidLibrary.java +++ b/src/main/java/com/jaoafa/mymaid4/lib/MyMaidLibrary.java @@ -23,6 +23,7 @@ import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.luckperms.api.LuckPerms; @@ -399,7 +400,7 @@ protected static String check4bytechars_DeleteMatchText(String str) { * @param name プレイヤー名 * @param text テキスト */ - public static void chatFake(NamedTextColor color, String name, String text) { + public static void chatFake(TextColor color, String name, String text) { chatFake(color, name, text, true); } @@ -411,7 +412,7 @@ public static void chatFake(NamedTextColor color, String name, String text) { * @param text テキスト * @param sendToDiscord Discordにも送信するか */ - public static void chatFake(NamedTextColor color, String name, String text, boolean sendToDiscord) { + public static void chatFake(TextColor color, String name, String text, boolean sendToDiscord) { chatFake(color, name, Component.text(text), sendToDiscord); } @@ -423,7 +424,7 @@ public static void chatFake(NamedTextColor color, String name, String text, bool * @param component テキスト * @param sendToDiscord Discordにも送信するか */ - public static void chatFake(NamedTextColor color, String name, Component component, boolean sendToDiscord) { + public static void chatFake(TextColor color, String name, Component component, boolean sendToDiscord) { Bukkit.getServer().sendMessage(Component.text().append( Component.text("[" + sdfTimeFormat(new Date()) + "]", NamedTextColor.GRAY), Component.text("■", color), diff --git a/src/main/java/com/jaoafa/mymaid4/lib/Tips.java b/src/main/java/com/jaoafa/mymaid4/lib/Tips.java new file mode 100644 index 000000000..b7855f56d --- /dev/null +++ b/src/main/java/com/jaoafa/mymaid4/lib/Tips.java @@ -0,0 +1,107 @@ +/* + * jaoLicense + * + * Copyright (c) 2022 jao Minecraft Server + * + * The following license applies to this project: jaoLicense + * + * Japanese: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE.md + * English: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE-en.md + */ + +package com.jaoafa.mymaid4.lib; + +import com.jaoafa.mymaid4.Main; +import org.json.JSONObject; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +public class Tips { + private static final Path path = Path.of(Main.getJavaPlugin().getDataFolder().getAbsolutePath(), "tips.json"); + private static final Map tips = new HashMap<>(); + + /** + * name から Tip を取得する + * + * @param name Tip の名前 + * + * @return Tip (存在しない場合は NULL) + */ + public static String getTip(String name) { + if (tips.isEmpty()) { + loadTips(); + } + return tips.get(name); + } + + /** + * すべての Tip を取得する + * + * @return Tip の Map + */ + public static Map getTips() { + if (tips.isEmpty()) { + loadTips(); + } + return tips; + } + + public static boolean isExist(String name) { + if (tips.isEmpty()) { + loadTips(); + } + return tips.containsKey(name); + } + + /** + * Tip を追加する + * + * @param name Tip の名前 + * @param text Tip のテキスト + */ + public static void addTip(String name, String text) { + tips.put(name, text); + saveTips(); + } + + /** + * Tip を削除する + * + * @param name Tip の名前 + */ + public static void removeTip(String name) { + tips.remove(name); + saveTips(); + } + + private static void loadTips() { + if (!Files.exists(path)) { + return; + } + tips.clear(); + try { + JSONObject object = new JSONObject(Files.readString(path)); + for (String key : object.keySet()) { + tips.put(key, object.getString(key)); + } + } catch (IOException e) { + MyMaidLibrary.reportError(Tips.class, e); + } + } + + private static void saveTips() { + JSONObject object = new JSONObject(); + for (String key : tips.keySet()) { + object.put(key, tips.get(key)); + } + try { + Files.writeString(path, object.toString()); + } catch (IOException e) { + MyMaidLibrary.reportError(Tips.class, e); + } + } +}