Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
feat: tipsコマンドの実装 (#845)
Browse files Browse the repository at this point in the history
* feat: tipsコマンドの実装

* fix: Call to 'printStackTrace()'
  • Loading branch information
book000 authored Jun 8, 2022
1 parent 4a2a0f2 commit ac4dfc9
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 3 deletions.
127 changes: 127 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/command/Cmd_Tips.java
Original file line number Diff line number Diff line change
@@ -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<CommandSender> 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.<CommandSender>newBuilder("name").withSuggestionsProvider(this::suggestTipNames), ArgumentDescription.of("Tipsの名前"))
.handler(this::broadcastTip)
.build()
);
}

void addTip(CommandContext<CommandSender> 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<CommandSender> 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<CommandSender> context) {
CommandSender sender = context.getSender();

SendMessage(sender, details(), "Tips 一覧");
for (Map.Entry<String, String> entry : Tips.getTips().entrySet()) {
SendMessage(sender, details(), "- %s: %s".formatted(entry.getKey(), entry.getValue()));
}
}

void broadcastTip(CommandContext<CommandSender> 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<String> suggestTipNames(final CommandContext<CommandSender> context, final String current) {
return Tips.getTips().keySet().stream()
.filter(name -> name.startsWith(current))
.collect(Collectors.toList());
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/jaoafa/mymaid4/lib/MyMaidLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

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

Expand All @@ -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),
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/lib/Tips.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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);
}
}
}

0 comments on commit ac4dfc9

Please sign in to comment.