From 07f457662a504ee8145a24cb64b8f0b51142a669 Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Fri, 5 Aug 2022 13:01:11 +0800 Subject: [PATCH 1/8] feat: Add /msg command --- build.gradle | 2 +- gradle.properties | 1 + src/main/java/luminous/safechat/Main.java | 42 ++++++++++++++++++++++- src/main/resources/plugin.yml | 7 ++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 gradle.properties diff --git a/build.gradle b/build.gradle index afb3ee1..b891837 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'luminous' -version '1.0.0' +version plugin_version repositories { mavenCentral() diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..181900b --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +plugin_version = 1.0.0 diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index 9f34626..fdba5b6 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -3,17 +3,22 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import java.util.UUID; + public class Main extends JavaPlugin { @Override public void onEnable() { Bukkit.getPluginCommand("safechat").setExecutor(this); + Bukkit.getPluginCommand("msg").setExecutor(new ChatListener()); Bukkit.getPluginManager().registerEvents(new ChatListener(), this); } @@ -27,7 +32,42 @@ public boolean onCommand(@NotNull CommandSender sender, } } -class ChatListener implements Listener { +class ChatListener implements Listener, CommandExecutor { + @Override + public boolean onCommand(@NotNull CommandSender sender, + @NotNull Command command, + @NotNull String label, + @NotNull String[] args) { + if (!(sender instanceof Player)) { + return false; + } + if (args.length < 2) { + return false; + } + + Player player = Bukkit.getPlayerExact(args[0]); + if (player == null) { + try { + Bukkit.getPlayer(UUID.fromString(args[0])); + } catch (IllegalArgumentException e) { + sender.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } + } + if (player == null) { + sender.sendMessage(ChatColor.RED + "Player not Found!"); + return true; + } + + StringBuilder message = new StringBuilder(); + for (int i = 1; i < args.length; i++) { + message.append(args[i]); + } + + sender.sendMessage(ChatColor.GRAY + sender.getName() + " -> " + player.getName() + ": " + message); + return true; + } + @EventHandler public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) { String sender = event.getPlayer().getName(); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 57c5d2a..42c1f08 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,6 @@ name: SafeChat version: 1.0.0 +author: Luminous-Coder main: luminous.safechat.Main api-version: 1.19 @@ -7,3 +8,9 @@ api-version: 1.19 commands: safechat: description: Test whether the plugin is enabled. + msg: + aliases: + - tell + - w + description: Send a private message to another player. + usage: /msg From 9783eba3a20ff48edc3707af54358d41cd52ee1e Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Fri, 5 Aug 2022 16:44:16 +0800 Subject: [PATCH 2/8] fix: Not sending direct message to the receiver --- src/main/java/luminous/safechat/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index fdba5b6..02bf7d8 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -64,7 +64,8 @@ public boolean onCommand(@NotNull CommandSender sender, message.append(args[i]); } - sender.sendMessage(ChatColor.GRAY + sender.getName() + " -> " + player.getName() + ": " + message); + sender.sendMessage(ChatColor.GRAY + "YOU -> " + player.getName() + ": " + ChatColor.WHITE + message); + player.sendMessage(ChatColor.GRAY + sender.getName() + " -> YOU: " + ChatColor.WHITE + message); return true; } From afb08efc7292b0dc7e762022a19029833b93a1ad Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Fri, 5 Aug 2022 17:56:20 +0800 Subject: [PATCH 3/8] docs: Update the readme file --- CHANGELOG.md | 5 +++++ README.md | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f1bb0..f83bbae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. +## Unreleased +Released on: +### Added +- Direct message protection. + ## v1.0.0 Released on: 2022-08-04 ### Added diff --git a/README.md b/README.md index cdbe214..d41c671 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # Safe-Chat -No more reportable message without the "unsafe" mark. +No more reportable message and "unsafe" marks. -Whispers are still reportable now. -It'll be solved in the future. +## Features + +All the messages in the server are protected, so you cannot report anyone. + +![Screenshot](https://truth.bahamut.com.tw/s01/202208/2d76aca3213c8848910a35327962c83b.JPG?w=1000) + +And there's no any "unsafe" mark, too! + +The direct messages (using command such as `/w`) are also protected now. From 96e9bff3a693648594bb7672be40819f28c1e09e Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Thu, 11 Aug 2022 15:41:05 +0800 Subject: [PATCH 4/8] feat: Add message format configuration --- CHANGELOG.md | 1 + src/main/java/luminous/safechat/Main.java | 68 +++++++++++++++++-- src/main/java/luminous/safechat/util/Fmt.java | 17 +++++ src/main/resources/config.yml | 3 + 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/main/java/luminous/safechat/util/Fmt.java create mode 100644 src/main/resources/config.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index f83bbae..f993b93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. Released on: ### Added - Direct message protection. +- Message format configuration. ## v1.0.0 Released on: 2022-08-04 diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index 02bf7d8..1735789 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -1,10 +1,12 @@ package luminous.safechat; +import luminous.safechat.util.Fmt; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -12,14 +14,45 @@ import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; public class Main extends JavaPlugin { + public static FileConfiguration config = null; + + public static final Map CHAT_COLOR_SHEET = Map.ofEntries( + Map.entry("_BLACK", ChatColor.BLACK), + Map.entry("_DARK_BLUE", ChatColor.DARK_BLUE), + Map.entry("_DARK_GREEN", ChatColor.DARK_GREEN), + Map.entry("_DARK_AQUA", ChatColor.DARK_AQUA), + Map.entry("_DARK_RED", ChatColor.DARK_RED), + Map.entry("_DARK_PURPLE", ChatColor.DARK_PURPLE), + Map.entry("_GOLD", ChatColor.GOLD), + Map.entry("_GRAY", ChatColor.GRAY), + Map.entry("_DARK_GRAY", ChatColor.DARK_GRAY), + Map.entry("_BLUE", ChatColor.BLUE), + Map.entry("_GREEN", ChatColor.GREEN), + Map.entry("_AQUA", ChatColor.AQUA), + Map.entry("_RED", ChatColor.RED), + Map.entry("_LIGHT_PURPLE", ChatColor.LIGHT_PURPLE), + Map.entry("_YELLOW", ChatColor.YELLOW), + Map.entry("_WHITE", ChatColor.WHITE), + Map.entry("_MAGIC", ChatColor.MAGIC), + Map.entry("_BOLD", ChatColor.BOLD), + Map.entry("_STRIKETHROUGH", ChatColor.STRIKETHROUGH), + Map.entry("_UNDERLINE", ChatColor.UNDERLINE), + Map.entry("_ITALIC", ChatColor.ITALIC), + Map.entry("_RESET", ChatColor.RESET) + ); + @Override public void onEnable() { Bukkit.getPluginCommand("safechat").setExecutor(this); Bukkit.getPluginCommand("msg").setExecutor(new ChatListener()); Bukkit.getPluginManager().registerEvents(new ChatListener(), this); + + config = getConfig(); } @Override @@ -45,8 +78,8 @@ public boolean onCommand(@NotNull CommandSender sender, return false; } - Player player = Bukkit.getPlayerExact(args[0]); - if (player == null) { + Player target = Bukkit.getPlayerExact(args[0]); + if (target == null) { try { Bukkit.getPlayer(UUID.fromString(args[0])); } catch (IllegalArgumentException e) { @@ -54,7 +87,7 @@ public boolean onCommand(@NotNull CommandSender sender, return true; } } - if (player == null) { + if (target == null) { sender.sendMessage(ChatColor.RED + "Player not Found!"); return true; } @@ -64,8 +97,21 @@ public boolean onCommand(@NotNull CommandSender sender, message.append(args[i]); } - sender.sendMessage(ChatColor.GRAY + "YOU -> " + player.getName() + ": " + ChatColor.WHITE + message); - player.sendMessage(ChatColor.GRAY + sender.getName() + " -> YOU: " + ChatColor.WHITE + message); + Map formatArgs = new HashMap<>(Map.of( + "sender", sender.getName(), + "target", target.getName(), + "message", message.toString() + )); + formatArgs.putAll(Main.CHAT_COLOR_SHEET); + + sender.sendMessage(Fmt.format( + Main.config.getString("direct-message-sender-format"), + formatArgs + )); + target.sendMessage(Fmt.format( + Main.config.getString("direct-message-target-format"), + formatArgs + )); return true; } @@ -74,6 +120,16 @@ public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) { String sender = event.getPlayer().getName(); String message = event.getMessage(); event.setCancelled(true); - Bukkit.broadcastMessage("<" + sender + "> " + message); + + Map formatArgs = new HashMap<>(Map.of( + "sender", sender, + "message", message.toString() + )); + formatArgs.putAll(Main.CHAT_COLOR_SHEET); + + Bukkit.broadcastMessage(Fmt.format( + Main.config.getString("global-message-format"), + formatArgs + )); } } diff --git a/src/main/java/luminous/safechat/util/Fmt.java b/src/main/java/luminous/safechat/util/Fmt.java new file mode 100644 index 0000000..68acba6 --- /dev/null +++ b/src/main/java/luminous/safechat/util/Fmt.java @@ -0,0 +1,17 @@ +package luminous.safechat.util; + +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class Fmt { + public static String format(@NotNull String fmt, @NotNull Map<@NotNull String, Object> args) { + String result = fmt; + for (var arg : args.entrySet()) { + if (arg.getValue() == null) continue; + + result = result.replaceAll("\\{" + arg.getKey() + "}", arg.getValue().toString()); + } + return result; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..c3b2e84 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,3 @@ +global-message-format: '{_GRAY}<{sender}>{_WHITE} {message}' +direct-message-sender-format: '{_GRAY} {target}>{_WHITE} {message}' +direct-message-target-format: '{_GRAY}<{sender} -> YOU>{_WHITE} {message}' From 73ad6936ed467145ab0d312548948276cdc8473d Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Mon, 15 Aug 2022 15:59:55 +0800 Subject: [PATCH 5/8] fix: Saving config now --- src/main/java/luminous/safechat/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index 1735789..b5708d7 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -53,6 +53,7 @@ public void onEnable() { Bukkit.getPluginManager().registerEvents(new ChatListener(), this); config = getConfig(); + saveConfig(); } @Override From 2a58a1959d23e376bd85938e8ca425ebe589aed1 Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Mon, 15 Aug 2022 19:31:49 +0800 Subject: [PATCH 6/8] fix: Not saving default config if have no one --- src/main/java/luminous/safechat/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index b5708d7..5b68d3b 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -52,8 +52,8 @@ public void onEnable() { Bukkit.getPluginCommand("msg").setExecutor(new ChatListener()); Bukkit.getPluginManager().registerEvents(new ChatListener(), this); + saveDefaultConfig(); config = getConfig(); - saveConfig(); } @Override From af3649da369d74a73725c292afa6153a0f41126f Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Tue, 16 Aug 2022 12:25:47 +0800 Subject: [PATCH 7/8] docs: Update the readme and the changelog --- CHANGELOG.md | 4 ++-- README.md | 43 +++++++++++++++++++++++++++++++---- gradle.properties | 2 +- src/main/resources/config.yml | 13 +++++++++++ src/main/resources/plugin.yml | 2 +- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f993b93..d14a600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Changelog All notable changes to this project will be documented in this file. -## Unreleased -Released on: +## v1.1.0 +Released on: 2022-08-16 ### Added - Direct message protection. - Message format configuration. diff --git a/README.md b/README.md index d41c671..3748d07 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,46 @@ -# Safe-Chat +# Safe Chat -No more reportable message and "unsafe" marks. +Safe Chat is a Minecraft plugin that protects players' messages. ## Features -All the messages in the server are protected, so you cannot report anyone. +The messages will be protected so no one is able to report while enable the plugin. +Because of the way this plugin uses, the game won't mark messages as unverified or unsafe, too. ![Screenshot](https://truth.bahamut.com.tw/s01/202208/2d76aca3213c8848910a35327962c83b.JPG?w=1000) -And there's no any "unsafe" mark, too! +## Configuration -The direct messages (using command such as `/w`) are also protected now. +It's able to configure the format of the messages. +When the first time the plugin enabled, it'll automatically generate the configuration file, +you may change it with the description below: + +The format may contain strings inside a pair of curly brackets that will be replaced. +The table below shows the general ones. + +| Name | Description | +|------------------|----------------------------------------------| +| `_BLACK` | Make the following text black. | +| `_DARK_BLUE` | Make the following text dark blue. | +| `_DARK_GREEN` | Make the following text dark green. | +| `_DARK_AQUA` | Make the following text dark aqua. | +| `_DARK_RED` | Make the following text dark red. | +| `_DARK_PURPLE` | Make the following text dark purple. | +| `_GOLD` | Make the following text gold. | +| `_GRAY` | Make the following text gray. | +| `_DARK_GRAY` | Make the following text dark gray. | +| `_BLUE` | Make the following text blue. | +| `_GREEN` | Make the following text green. | +| `_AQUA` | Make the following text aqua. | +| `_RED` | Make the following text red. | +| `_LIGHT_PURPLE` | Make the following text light purple. | +| `_YELLOW` | Make the following text yellow. | +| `_WHITE` | Make the following text white. | +| `_MAGIC` | Make the following text "magic". | +| `_BOLD` | Make the following text bold. | +| `_STRIKETHROUGH` | Make the following text strikethrough. | +| `_UNDERLINE` | Make the following text underline. | +| `_ITALIC` | Make the following text italic. | +| `_RESET` | Set the following text to the default style. | + +You can find specific ones for every entry in the generated configuration file. diff --git a/gradle.properties b/gradle.properties index 181900b..77e97ff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -plugin_version = 1.0.0 +plugin_version = 1.1.0 diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c3b2e84..3075ad4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,3 +1,16 @@ +# Control the chat format +# +# Format: {sender} - The message sender. global-message-format: '{_GRAY}<{sender}>{_WHITE} {message}' + +# Control the direct message format the sender see. +# +# Format {sender} - The message sender. +# {target} - The message receiver. direct-message-sender-format: '{_GRAY} {target}>{_WHITE} {message}' + +# Control the direct message format the receiver see. +# +# Format {sender} - The message sender. +# {target} - The message receiver. direct-message-target-format: '{_GRAY}<{sender} -> YOU>{_WHITE} {message}' diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 42c1f08..9c60bbc 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SafeChat -version: 1.0.0 +version: 1.1.0 author: Luminous-Coder main: luminous.safechat.Main From 97737216e494657a0e7a6252ad498c862b5208c4 Mon Sep 17 00:00:00 2001 From: Luminous-Coder Date: Tue, 16 Aug 2022 12:26:33 +0800 Subject: [PATCH 8/8] chore: Remove the GitHub workflow --- .github/workflows/build.yml | 41 ------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 6be4e55..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: build -on: - push: - branches: - - main -jobs: - build-1-19-1: - name: Build for Minecraft 1.19.1 - runs-on: ubuntu-22.04 - steps: - - name: Checkout. - id: checkout - uses: actions/checkout@v3 - - name: Setup Java. - id: setup-java - uses: actions/setup-java@v3 - with: - distribution: temurin - java-version: 17 - cache: gradle - - name: Build the plugin. - id: build - uses: SpraxDev/Action-SpigotMC@v4 - with: - versions: 1.19.1 - - name: Create GitHub release. - id: release - uses: CupOfTea696/gh-action-auto-release@v1 - with: - title: $version - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - name: Upload to GitHub release. - id: upload - uses: Fhwang0926/upload-release-asset@v1 - with: - upload_url: ${{ steps.release.outputs.upload_url }} - asset_name: '*.jar' - asset_label: 1.19.1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}