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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f1bb0..d14a600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog All notable changes to this project will be documented in this file. +## v1.1.0 +Released on: 2022-08-16 +### Added +- Direct message protection. +- Message format configuration. + ## v1.0.0 Released on: 2022-08-04 ### Added diff --git a/README.md b/README.md index cdbe214..3748d07 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,46 @@ -# Safe-Chat +# Safe Chat -No more reportable message without the "unsafe" mark. +Safe Chat is a Minecraft plugin that protects players' messages. -Whispers are still reportable now. -It'll be solved in the future. +## Features + +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) + +## Configuration + +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/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..77e97ff --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +plugin_version = 1.1.0 diff --git a/src/main/java/luminous/safechat/Main.java b/src/main/java/luminous/safechat/Main.java index 9f34626..5b68d3b 100644 --- a/src/main/java/luminous/safechat/Main.java +++ b/src/main/java/luminous/safechat/Main.java @@ -1,20 +1,59 @@ 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; import org.bukkit.event.player.AsyncPlayerChatEvent; 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); + + saveDefaultConfig(); + config = getConfig(); } @Override @@ -27,12 +66,71 @@ 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 target = Bukkit.getPlayerExact(args[0]); + if (target == null) { + try { + Bukkit.getPlayer(UUID.fromString(args[0])); + } catch (IllegalArgumentException e) { + sender.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } + } + if (target == 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]); + } + + 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; + } + @EventHandler 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..3075ad4 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +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 57c5d2a..9c60bbc 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,6 @@ name: SafeChat -version: 1.0.0 +version: 1.1.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