Skip to content

Commit

Permalink
common chat formatter and new way of blocking colors via perms
Browse files Browse the repository at this point in the history
  • Loading branch information
Majekdor committed Dec 30, 2021
1 parent 628fd52 commit 57e09bd
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 72 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Plugin Changelog

# 2.2.1 - No Duplicate Nicknames

- New config option for preventing players from having the same nickname.
- Added ability to block color usage with permissions, read [here](https://github.com/MajekDev/HexNicks/wiki/Permissions#blocking-certain-color-usage).
- PlaceholderAPI placeholders will now be parsed in chat format.
- Chat format can now contain legacy codes on Paper if legacy support is enabled.
- Fix typo permission issue.
- Standard code cleanup.

This is a very minor update that adds a lot of small new features and squashes some bugs.

# 2.2.0 - Async Everything

- Switched access to MySQL database from synchronous to asynchronous.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/majek/hexnicks/command/CommandNick.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
String nickInput = String.join(" ", args);

Component nickname = MiniMessageWrapper.builder()
.gradients(player.hasPermission("hexnicks.nick.gradient"))
.hexColors(player.hasPermission("hexnicks.nick.hex"))
.standardColors(player.hasPermission("hexnicks.nick.color"))
.gradients(player.hasPermission("hexnicks.color.gradient"))
.hexColors(player.hasPermission("hexnicks.color.hex"))
.standardColors(true)
.legacyColors(Nicks.config().LEGACY_COLORS)
.removeTextDecorations(Nicks.config().DISABLED_DECORATIONS.toArray(new TextDecoration[0]))
.removeColors(Nicks.utils().blockedColors(player).toArray(new NamedTextColor[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

MiniMessageWrapper wrapper = MiniMessageWrapper.builder()
.gradients(player.hasPermission("hexnicks.nick.gradient"))
.hexColors(player.hasPermission("hexnicks.nick.hex"))
.standardColors(player.hasPermission("hexnicks.nick.color"))
.gradients(player.hasPermission("hexnicks.color.gradient"))
.hexColors(player.hasPermission("hexnicks.color.hex"))
.standardColors(true)
.legacyColors(Nicks.config().LEGACY_COLORS)
.removeTextDecorations(Nicks.config().DISABLED_DECORATIONS.toArray(new TextDecoration[0]))
.removeColors(Nicks.utils().blockedColors(player).toArray(new NamedTextColor[0]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

Component nickname = MiniMessageWrapper.builder()
.gradients(target.hasPermission("hexnicks.nick.gradient"))
.hexColors(target.hasPermission("hexnicks.nick.hex"))
.standardColors(target.hasPermission("hexnicks.nick.color"))
.gradients(target.hasPermission("hexnicks.color.gradient"))
.hexColors(target.hasPermission("hexnicks.color.hex"))
.standardColors(true)
.legacyColors(Nicks.config().LEGACY_COLORS)
.removeTextDecorations(Nicks.config().DISABLED_DECORATIONS.toArray(new TextDecoration[0]))
.build().mmParse(nickInput);
Expand Down
28 changes: 1 addition & 27 deletions src/main/java/dev/majek/hexnicks/server/PaperServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
package dev.majek.hexnicks.server;

import dev.majek.hexnicks.Nicks;
import dev.majek.hexnicks.util.MiniMessageWrapper;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -85,31 +82,8 @@ public void sendMessage(@NotNull CommandSender sender, @NotNull Component messag
public void onChat(final AsyncChatEvent event) {
if (Nicks.config().CHAT_FORMATTER) {
Nicks.debug("paper chat event fired");
final LegacyComponentSerializer legacySerializer = LegacyComponentSerializer.builder().hexColors()
.useUnusualXRepeatedCharacterHexFormat().build();
event.renderer((source, sourceDisplayName, message, viewer) ->
// Format entire configured chat format with legacy and mini messages codes
MiniMessageWrapper.legacy().mmParse(Nicks.hooks().applyPlaceHolders(source, Nicks.config().CHAT_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(Nicks.core().getDisplayName(source)).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(legacySerializer.deserialize(Nicks.hooks().vaultPrefix(source))).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(legacySerializer.deserialize(Nicks.hooks().vaultSuffix(source))).build()
)
// Replace message placeholder with the formatted message from the event
.replaceText(TextReplacementConfig.builder().matchLiteral("{message}")
.replacement(
MiniMessageWrapper.builder().legacyColors(Nicks.config().LEGACY_COLORS)
.advancedTransformations(source.hasPermission("hexnicks.chat.advanced")).build()
.mmParse(PlainTextComponentSerializer.plainText().serialize(message))
).build()
)
formatChat(source, PlainTextComponentSerializer.plainText().serialize(message))
);
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/dev/majek/hexnicks/server/ServerSoftware.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
*/
package dev.majek.hexnicks.server;

import dev.majek.hexnicks.Nicks;
import dev.majek.hexnicks.util.MiniMessageWrapper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -71,4 +77,49 @@ public interface ServerSoftware extends Listener {
* @return name
*/
@NotNull String softwareName();

/**
* Instance of the {@link LegacyComponentSerializer} used by server implementations.
*/
LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.builder().hexColors()
.useUnusualXRepeatedCharacterHexFormat().character('§').build();

/**
* Format the chat for all server implementations.
*
* @param source the chatter
* @param message the message
* @return formatted chat
*/
default @NotNull Component formatChat(final @NotNull Player source, final @NotNull String message) {
final MiniMessageWrapper miniMessageWrapper = MiniMessageWrapper.builder()
.gradients(source.hasPermission("hexnicks.color.gradient"))
.hexColors(source.hasPermission("hexnicks.color.hex"))
.legacyColors(Nicks.config().LEGACY_COLORS)
.removeTextDecorations(Nicks.config().DISABLED_DECORATIONS.toArray(new TextDecoration[0]))
.removeColors(Nicks.utils().blockedColors(source).toArray(new NamedTextColor[0]))
.build();

return miniMessageWrapper.mmParse(Nicks.hooks().applyPlaceHolders(source, Nicks.config().CHAT_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(Nicks.core().getDisplayName(source)).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(LEGACY_COMPONENT_SERIALIZER.deserialize(Nicks.hooks().vaultPrefix(source))).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(LEGACY_COMPONENT_SERIALIZER.deserialize(Nicks.hooks().vaultSuffix(source))).build()
)
// Replace message placeholder with the formatted message from the event
.replaceText(TextReplacementConfig.builder().matchLiteral("{message}")
.replacement(
miniMessageWrapper.mmParse(
Nicks.config().LEGACY_COLORS ? Nicks.utils().legacyToMini(message) : message
)
).build()
);
}
}
28 changes: 5 additions & 23 deletions src/main/java/dev/majek/hexnicks/server/SpigotServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import dev.majek.hexnicks.Nicks;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand All @@ -41,21 +39,14 @@
@SuppressWarnings("deprecation")
public final class SpigotServer implements ServerSoftware {

private final LegacyComponentSerializer legacyComponentSerializer;

public SpigotServer() {
legacyComponentSerializer = LegacyComponentSerializer.builder().hexColors()
.useUnusualXRepeatedCharacterHexFormat().build();
}

@Override
public @NotNull Component getNick(@NotNull Player player) {
return legacyComponentSerializer.deserialize(player.getDisplayName());
return LEGACY_COMPONENT_SERIALIZER.deserialize(player.getDisplayName());
}

@Override
public void setNick(@NotNull Player player, @NotNull Component nickname) {
String nick = legacyComponentSerializer.serialize(nickname) + "§r";
String nick = LEGACY_COMPONENT_SERIALIZER.serialize(nickname) + "§r";
Nicks.core().getNickMap().put(player.getUniqueId(), nickname);
player.setDisplayName(nick);
if (Nicks.config().TAB_NICKS) {
Expand Down Expand Up @@ -87,18 +78,9 @@ public void sendMessage(@NotNull CommandSender sender, @NotNull Component messag
@EventHandler(priority = EventPriority.LOWEST)
public void onChat(AsyncPlayerChatEvent event) {
if (Nicks.config().CHAT_FORMATTER) {
String format = Nicks.hooks().applyPlaceHolders(event.getPlayer(), Nicks.config().CHAT_FORMAT);
String message = event.getMessage();
if (Nicks.config().LEGACY_COLORS) {
message = Nicks.utils().legacyToMini(message);
}
message = legacyComponentSerializer.serialize(MiniMessage.miniMessage().parse(message));
format = Nicks.utils().miniToLegacy(format)
.replace("{displayname}", legacyComponentSerializer.serialize(Nicks.core().getDisplayName(event.getPlayer())))
.replace("{message}", message)
.replace("{prefix}", Nicks.hooks().vaultPrefix(event.getPlayer()))
.replace("{suffix}", Nicks.hooks().vaultSuffix(event.getPlayer()));
event.setFormat(Nicks.utils().applyLegacyColors(format));
event.setFormat(
LEGACY_COMPONENT_SERIALIZER.serialize(formatChat(event.getPlayer(), event.getMessage()))
);
Nicks.debug("spigot chat event - " + event.getFormat());
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/dev/majek/hexnicks/util/NicksUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ public boolean preventDuplicates(@NotNull Component nickname, @NotNull CommandSe
} else if (WHITE.equals(color)) {
return 'f';
}
System.out.println("What the fuck");
return null;
}

public Set<NamedTextColor> blockedColors(@NotNull CommandSender sender) {
Set<NamedTextColor> set = new HashSet<>();
for (NamedTextColor color : NamedTextColor.NAMES.values()) {
if (sender.hasPermission("hexnicks.color." + color.toString().toLowerCase(Locale.ROOT) + ".block")) {
if (!sender.hasPermission("hexnicks.color." + color.toString().toLowerCase(Locale.ROOT))) {
set.add(color);
}
}
Expand Down
55 changes: 44 additions & 11 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@ commands:

permissions:
hexnicks.nick:
description: Permission to change nicknames.
default: true
hexnicks.nick.color:
description: Permission to use standard color codes in nicknames.
default: true
hexnicks.nick.hex:
description: Permission to use hex color codes in nicknames.
default: true
hexnicks.nick.gradient:
description: Permission to use gradients in nicknames.
description: Permission to change nicknames with /nick.
default: true
hexnicks.nick.other:
description: Permission to change other player's nicknames.
Expand All @@ -64,4 +55,46 @@ permissions:
default: true
hexnicks.chat.advanced:
description: Permission to inject things like click events into chat messages via MiniMessage.
default: op
default: op

hexnicks.color.*:
description: Permission to use all standard color codes.
default: true
hexnicks.color.hex:
description: Permission to use hex color codes.
default: true
hexnicks.color.gradient:
description: Permission to use gradient colors.
default: true
hexnicks.color.black:
default: true
hexnicks.color.dark_blue:
default: true
hexnicks.color.dark_green:
default: true
hexnicks.color.dark_aqua:
default: true
hexnicks.color.dark_red:
default: true
hexnicks.color.dark_purple:
default: true
hexnicks.color.gold:
default: true
hexnicks.color.gray:
default: true
hexnicks.color.dark_gray:
default: true
hexnicks.color.blue:
default: true
hexnicks.color.green:
default: true
hexnicks.color.aqua:
default: true
hexnicks.color.red:
default: true
hexnicks.color.light_purple:
default: true
hexnicks.color.yellow:
default: true
hexnicks.color.white:
default: true

0 comments on commit 57e09bd

Please sign in to comment.