mirror of
https://github.com/kaboomserver/extras.git
synced 2025-02-11 03:29:50 +00:00
Use a ChatRenderer and allow players to use MiniMessage in chat messages
This commit is contained in:
parent
e1b5b28860
commit
8296dc7075
1 changed files with 50 additions and 31 deletions
|
@ -1,30 +1,35 @@
|
|||
package pw.kaboom.extras.modules.player;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import io.papermc.paper.chat.ChatRenderer;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import pw.kaboom.extras.Main;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PlayerChat implements Listener {
|
||||
private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig();
|
||||
private static final FileConfiguration PREFIX_CONFIG = JavaPlugin
|
||||
.getPlugin(Main.class).getPrefixConfig();
|
||||
|
||||
private static final String OP_TAG = CONFIG.getString("opTag");
|
||||
private static final String DEOP_TAG = CONFIG.getString("deOpTag");
|
||||
private static final Component OP_TAG = LegacyComponentSerializer
|
||||
.legacySection().deserialize(CONFIG.getString("opTag", ""));
|
||||
private static final Component DEOP_TAG = LegacyComponentSerializer
|
||||
.legacySection().deserialize(CONFIG.getString("deOpTag", ""));
|
||||
private static final PlayerChatRenderer CHAT_RENDERER = new PlayerChatRenderer();
|
||||
|
||||
@EventHandler
|
||||
void onAsyncPlayerChat(final AsyncPlayerChatEvent event) {
|
||||
final Player player = event.getPlayer();
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
void onAsyncChatEventProcess(final AsyncChatEvent event) {
|
||||
final UUID playerUuid = event.getPlayer().getUniqueId();
|
||||
|
||||
if (PlayerCommand.getCommandMillisList().get(playerUuid) != null) {
|
||||
|
@ -37,25 +42,39 @@ public final class PlayerChat implements Listener {
|
|||
}
|
||||
|
||||
PlayerCommand.getCommandMillisList().put(playerUuid, System.currentTimeMillis());
|
||||
|
||||
if (event.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String name = player.getDisplayName().toString();
|
||||
String prefix = PREFIX_CONFIG.getString(player.getUniqueId().toString());
|
||||
|
||||
if (prefix != null) {
|
||||
prefix = ChatColor.translateAlternateColorCodes('&', prefix + " " + ChatColor.RESET);
|
||||
} else if (event.getPlayer().isOp()) {
|
||||
prefix = OP_TAG;
|
||||
} else {
|
||||
prefix = DEOP_TAG;
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
void onAsyncChatEventRenderer(final AsyncChatEvent event) {
|
||||
event.renderer(CHAT_RENDERER);
|
||||
}
|
||||
|
||||
event.setFormat(prefix + name + ChatColor.RESET + ": " + ChatColor.RESET + "%2$s");
|
||||
event.setMessage(
|
||||
ChatColor.translateAlternateColorCodes('&', event.getMessage())
|
||||
public static class PlayerChatRenderer implements ChatRenderer {
|
||||
|
||||
@Override
|
||||
public @NotNull Component render(@NotNull Player player,
|
||||
@NotNull Component displayName,
|
||||
@NotNull Component component,
|
||||
@NotNull Audience audience) {
|
||||
Component newComponent = Component.empty();
|
||||
final String legacyPrefix = PREFIX_CONFIG.getString(player.getUniqueId().toString());
|
||||
final Component prefix = legacyPrefix == null ?
|
||||
((player.isOp()) ? OP_TAG : DEOP_TAG)
|
||||
: LegacyComponentSerializer.legacyAmpersand().deserialize(legacyPrefix)
|
||||
.append(Component.space());
|
||||
|
||||
final String message = ((TextComponent) component).content();
|
||||
|
||||
newComponent = newComponent
|
||||
.append(prefix)
|
||||
.append(displayName)
|
||||
.append(Component.text(":"))
|
||||
.append(Component.space());
|
||||
|
||||
return newComponent.append(LegacyComponentSerializer
|
||||
.legacyAmpersand()
|
||||
.deserialize(message)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue