Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lumynou5 committed Aug 16, 2022
2 parents 77addf6 + 9773721 commit 945678c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 49 deletions.
41 changes: 0 additions & 41 deletions .github/workflows/build.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'luminous'
version '1.0.0'
version plugin_version

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin_version = 1.1.0
102 changes: 100 additions & 2 deletions src/main/java/luminous/safechat/Main.java
Original file line number Diff line number Diff line change
@@ -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<String, ChatColor> 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
Expand All @@ -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<String, Object> 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<String, Object> 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
));
}
}
17 changes: 17 additions & 0 deletions src/main/java/luminous/safechat/util/Fmt.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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}<YOU -> {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}'
9 changes: 8 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
name: SafeChat
version: 1.0.0
version: 1.1.0
author: Luminous-Coder

main: luminous.safechat.Main
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 <player name or UUID> <message>

0 comments on commit 945678c

Please sign in to comment.