Skip to content

Commit

Permalink
Refactor/minimessages (#27)
Browse files Browse the repository at this point in the history
* refactor: use AsyncChatEvent for modifying the format

* refactor: remove old event handling for deprecated chat event

* refactor: use minimessages and miniplaceholders instead of legacy chat

* chore: replace player name with constant

* chore: try to reduce complexicity

* chore: move to separate method
  • Loading branch information
rexlManu authored Jun 10, 2023
1 parent 6e625c0 commit a99fa40
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ repositories {

dependencies {
compileOnly 'io.papermc.paper:paper-api:1.20-R0.1-SNAPSHOT'
compileOnly 'me.clip:placeholderapi:2.11.3'
compileOnly 'io.github.miniplaceholders:miniplaceholders-api:2.1.0'
}

processResources {
Expand Down
39 changes: 3 additions & 36 deletions src/main/java/net/simplyvanilla/simplychat/PlayerListener.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package net.simplyvanilla.simplychat;

import io.papermc.paper.chat.ChatRenderer;
import io.github.miniplaceholders.api.MiniPlaceholders;
import io.papermc.paper.event.player.AsyncChatEvent;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.simplyvanilla.simplychat.database.Cache;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;

public class PlayerListener implements Listener {

Expand Down Expand Up @@ -47,8 +39,8 @@ public void handleAsyncChatEvent(AsyncChatEvent event) {
(source, sourceDisplayName, message, viewer) -> MiniMessage.miniMessage()
.deserialize(
this.plugin.getFormat(),
this.papiTag(source),
Placeholder.component("message", message)
Placeholder.component("message", message),
MiniPlaceholders.getAudiencePlaceholders(source)
));
}

Expand All @@ -57,30 +49,5 @@ public void handlePlayerJoinEvent(PlayerJoinEvent event) {
plugin.getCache().loadPlayerIgnoreList(event.getPlayer());
}

/**
* Creates a tag resolver capable of resolving PlaceholderAPI tags for a given player.
*
* @param player the player
* @return the tag resolver
*/
public @NotNull TagResolver papiTag(final @NotNull Player player) {
return TagResolver.resolver("papi", (argumentQueue, context) -> {
// Get the string placeholder that they want to use.
final String papiPlaceholder =
argumentQueue.popOr("papi tag requires an argument").value();

// Then get PAPI to parse the placeholder for the given player.
final String parsedPlaceholder =
PlaceholderAPI.setPlaceholders(player, '%' + papiPlaceholder + '%');

// We need to turn this ugly legacy string into a nice component.
final Component componentPlaceholder =
LegacyComponentSerializer.legacySection().deserialize(parsedPlaceholder);

// Finally, return the tag instance to insert the placeholder!
return Tag.selfClosingInserting(componentPlaceholder);
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,85 @@

public class IgnoreCommandExecutor implements CommandExecutor {

public static final String PLAYER_NAME = "[player_name]";
SimplyChatPlugin plugin = SimplyChatPlugin.getInstance();
Cache cache = plugin.getCache();
MYSQL database = plugin.getDatabase();

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (handleMultipleArguments(sender, command, args)) {
return true;
}

if (command.getName().equals("ignorelist")) {
List<String> ignoredPlayers = cache.getPlayerIgnoreInfo((Player) sender);
if (ignoredPlayers.isEmpty() || ignoredPlayers.get(0).length() == 0) {
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.notIgnoredAnyPlayerMessage"));
return true;
}
StringBuilder sb = new StringBuilder();
for (String ignoredPlayer : ignoredPlayers) {
OfflinePlayer offlinePlayer =
Bukkit.getOfflinePlayer(UUID.fromString(ignoredPlayer));
String newFormat =
plugin.getColorCodeTranslatedConfigString(
"command.ignore.ignoredPlayerDisplayMessage").replace(PLAYER_NAME,
offlinePlayer.getName());
sb.append(newFormat);
}
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.ignoredPlayerListMessage") + sb.toString());
}
return true;
}

private boolean handleMultipleArguments(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String[] args) {
if (args.length == 1) {
Player targetPlayer = Bukkit.getPlayer(args[0]);
if (targetPlayer == null) {
sender.sendMessage(MessageFormat.expandInternalPlaceholders ( "[player_name]",
args[0], plugin.getColorCodeTranslatedConfigString("command.ignore.playerCannotFoundMessage")));
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.playerCannotFoundMessage").replace(PLAYER_NAME,
args[0]));
return true;
} else if (targetPlayer == sender)
}

if (targetPlayer == sender) {
return true;
}

if (command.getName().equals("ignore")) {
if (cache.getPlayerIgnoreInfo((Player) sender).contains(targetPlayer.getUniqueId().toString())) {
sender.sendMessage(MessageFormat.expandInternalPlaceholders("[player_name]",
args[0], plugin.getColorCodeTranslatedConfigString("command.ignore.playerAlreadyIgnoredMessage")));
if (cache.getPlayerIgnoreInfo((Player) sender)
.contains(targetPlayer.getUniqueId().toString())) {
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.playerAlreadyIgnoredMessage").replace(PLAYER_NAME,
args[0]));
return true;
}
database.addIgnoredPlayer((Player) sender, targetPlayer);
sender.sendMessage(MessageFormat.expandInternalPlaceholders("[player_name]",
args[0], plugin.getColorCodeTranslatedConfigString("command.ignore.playerIgnoredMessage")));
} else if (command.getName().equals("unignore")) {
if (!cache.getPlayerIgnoreInfo((Player) sender).contains(targetPlayer.getUniqueId().toString())) {
sender.sendMessage(MessageFormat.expandInternalPlaceholders("[player_name]",
args[0], plugin.getColorCodeTranslatedConfigString("command.ignore.playerIsNotIgnoredMessage")));
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.playerIgnoredMessage").replace(PLAYER_NAME,
args[0]));
}

if (command.getName().equals("unignore")) {
if (!cache.getPlayerIgnoreInfo((Player) sender)
.contains(targetPlayer.getUniqueId().toString())) {
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.playerIsNotIgnoredMessage").replace(PLAYER_NAME,
args[0]));
return true;
}
database.removeIgnoredPlayer((Player) sender, targetPlayer);
sender.sendMessage(MessageFormat.expandInternalPlaceholders("[player_name]",
args[0], plugin.getColorCodeTranslatedConfigString("command.ignore.playerUnignoredMessage")));
}
return true;
}

if (command.getName().equals("ignorelist")) {
List<String> ignoredPlayers = cache.getPlayerIgnoreInfo((Player) sender);
if (ignoredPlayers.isEmpty() || ignoredPlayers.get(0).length() == 0) {
sender.sendMessage(plugin.getColorCodeTranslatedConfigString("command.ignore.notIgnoredAnyPlayerMessage"));
return true;
sender.sendMessage(plugin.getColorCodeTranslatedConfigString(
"command.ignore.playerUnignoredMessage").replace(PLAYER_NAME,
args[0]));
}
StringBuilder sb = new StringBuilder();
for (String ignoredPlayer : ignoredPlayers) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(ignoredPlayer));
String newFormat =
MessageFormat.expandInternalPlaceholders("[player_name]",
offlinePlayer.getName(), plugin.getColorCodeTranslatedConfigString("command.ignore.ignoredPlayerDisplayMessage"));
sb.append(newFormat);
}
sender.sendMessage(plugin.getColorCodeTranslatedConfigString("command.ignore.ignoredPlayerListMessage") + sb.toString());
return true;
}
return true;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.simplyvanilla.simplychat.command;

import me.clip.placeholderapi.PlaceholderAPI;
import static net.kyori.adventure.text.minimessage.MiniMessage.miniMessage;

import java.util.Arrays;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.simplyvanilla.simplychat.SimplyChatPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand All @@ -10,15 +14,13 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.stream.Collectors;

public class MessageCommandExecutor implements CommandExecutor {

private final SimplyChatPlugin plugin = SimplyChatPlugin.getInstance();

@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) {
if (!(commandSender instanceof Player sender)) {
commandSender.sendMessage("This command is only for players.");
return false;
Expand All @@ -31,7 +33,8 @@ public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command
Player receiver = Bukkit.getPlayer(args[0]);

if (receiver == null) {
String message = plugin.getColorCodeTranslatedConfigString("command.message.receiverNotFoundMessage");
String message = plugin.getColorCodeTranslatedConfigString(
"command.message.receiverNotFoundMessage");
message = message.replace("[receiver]", args[0]);
sender.sendMessage(message);
return true;
Expand All @@ -49,22 +52,33 @@ public void message(Player sender, Player receiver, String message) {
boolean receiverIgnoredSender = plugin.getCache().isPlayerIgnored(receiver, sender);

if (receiverIgnoredSender) {
sender.sendMessage(MessageFormat.expandInternalPlaceholders("[receiver_name]", receiver.getName(),
plugin.getColorCodeTranslatedConfigString("command.message.senderIgnoreErrorMessage")));
sender.sendMessage(
plugin.getColorCodeTranslatedConfigString(
"command.message.senderIgnoreErrorMessage")
.replace("[receiver_name]", receiver.getName()));
return;
}

String senderMessageFormat = plugin.getColorCodeTranslatedConfigString("command.message.senderMessageFormat");
senderMessageFormat = PlaceholderAPI.setPlaceholders(sender, senderMessageFormat);
String senderMessage = MessageFormat.expandInternalPlaceholders(sender.getName(), receiver.getName(), message, senderMessageFormat);
sender.sendMessage(Component.text(senderMessage));
String senderMessageFormat =
plugin.getColorCodeTranslatedConfigString("command.message.senderMessageFormat");

sender.sendMessage(miniMessage().deserialize(senderMessageFormat,
Placeholder.component("sender", sender.displayName()),
Placeholder.component("receiver", receiver.displayName()),
Placeholder.component("message", Component.text(message)))
);

String receiverMessageFormat =
plugin.getColorCodeTranslatedConfigString("command.message.receiverMessageFormat");

String receiverMessageFormat = plugin.getColorCodeTranslatedConfigString("command.message.receiverMessageFormat");
receiverMessageFormat = PlaceholderAPI.setPlaceholders(sender, receiverMessageFormat);
String receiverMessage = MessageFormat.expandInternalPlaceholders(sender.getName(), receiver.getName(), message, receiverMessageFormat);
receiver.sendMessage(Component.text(receiverMessage));
sender.sendMessage(miniMessage().deserialize(receiverMessageFormat,
Placeholder.component("sender", sender.displayName()),
Placeholder.component("receiver", receiver.displayName()),
Placeholder.component("message", Component.text(message)))
);

plugin.getPlayerStateManager().getPlayerState(receiver.getUniqueId()).setLastMessageSender(sender.getUniqueId());
plugin.getPlayerStateManager().getPlayerState(receiver.getUniqueId())
.setLastMessageSender(sender.getUniqueId());
}

}

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ command:
# Available placeholders: [receiver)
receiverNotFoundMessage: "[receiver] could not be found."
# Available placeholders: Everything from PlaceholderAPI and [sender, receiver, message]
senderMessageFormat: "You -> [receiver] >> [message]"
senderMessageFormat: "You -> <receiver> >> <message>"
# Available placeholders: Everything from PlaceholderAPI and [sender, receiver, message]
receiverMessageFormat: "[sender] -> You >> [message]"
receiverMessageFormat: "<sender> -> You >> <message>"
# Available placeholders: Everything from PlaceholderAPI and [receiver_name]
senderIgnoreErrorMessage: "[receiver_name] is ignoring you!"
# The reply command is simply a shortcut for the message command. thus, it uses the same formats.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ author: Simply Vanilla
website: https://github.com/SimplyVanilla/SimplyChat

depend:
- PlaceholderAPI
- MiniPlaceholders

commands:
msg:
Expand Down

0 comments on commit a99fa40

Please sign in to comment.