ChatManager now uses ChatRenderer properly

This commit is contained in:
Video 2023-04-05 17:39:52 -06:00
parent 4b3c31f0e9
commit 5ba81de118

View File

@ -1,15 +1,15 @@
package me.totalfreedom.totalfreedommod; package me.totalfreedom.totalfreedommod;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.api.event.AdminChatEvent; import me.totalfreedom.totalfreedommod.api.event.AdminChatEvent;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.rank.Displayable;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
@ -24,6 +24,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
@ -74,12 +75,6 @@ public class ChatManager extends FreedomService
return; return;
} }
// Tag
Component tag = fPlayer.getTag().append(Component.space());
// Nickname
Component nickname = player.displayName();
// Splitter // Splitter
Component splitter = Component.text("»", NamedTextColor.DARK_GRAY); Component splitter = Component.text("»", NamedTextColor.DARK_GRAY);
@ -92,23 +87,6 @@ public class ChatManager extends FreedomService
steamrolled = steamrolled.substring(0, 256); steamrolled = steamrolled.substring(0, 256);
} }
// Pinging
Arrays.stream(steamrolled.split(" ")).filter(string -> string.startsWith("@")).forEach(possiblePlayer ->
{
Player potential = server.getPlayer(possiblePlayer.replace("@", ""));
// Ping only that particular player
if (potential != null)
{
ping(player);
}
// Ping everyone (if the person pinging is an admin)
else if (possiblePlayer.equalsIgnoreCase("@everyone") && plugin.al.isAdmin(player))
{
server.getOnlinePlayers().forEach(this::ping);
}
});
// Did this because sonarlint was complaining about doing the unboxing in the if statement. // Did this because sonarlint was complaining about doing the unboxing in the if statement.
// Something about returning null because it was boxed... I'm not sure. // Something about returning null because it was boxed... I'm not sure.
boolean unboxed = ConfigEntry.FOURCHAN_ENABLED.getBoolean(); boolean unboxed = ConfigEntry.FOURCHAN_ENABLED.getBoolean();
@ -133,12 +111,31 @@ public class ChatManager extends FreedomService
// This simply filters out shit like &k in a simple but stupid way. // This simply filters out shit like &k in a simple but stupid way.
Component filtered = FUtil.miniMessage(FUtil.miniMessage(message.build())); Component filtered = FUtil.miniMessage(FUtil.miniMessage(message.build()));
event.setCancelled(true); // Pinging
server.broadcast(FUtil.miniMessage("<tag><nickname> <splitter> <message>", String steamrolledFiltered = FUtil.steamroll(filtered);
Placeholder.component("tag", tag), Arrays.stream(steamrolledFiltered.split(" ")).filter(string -> string.startsWith("@")).forEach(possiblePlayer ->
Placeholder.component("nickname", nickname), {
Player potential = server.getPlayer(possiblePlayer.replace("@", ""));
// Ping only that particular player
if (potential != null)
{
ping(potential);
}
// Ping everyone (if the person pinging is an admin)
else if (possiblePlayer.equalsIgnoreCase("@everyone") && plugin.al.isAdmin(player))
{
server.getOnlinePlayers().forEach(this::ping);
}
});
event.message(filtered);
event.renderer((source, displayName, msg, viewer) -> FUtil.miniMessage("<tag><nickname> <splitter> <message>",
Placeholder.component("tag", plugin.pl.getPlayer(source).getTag().append(Component.space())),
Placeholder.component("nickname", displayName),
Placeholder.component("splitter", splitter), Placeholder.component("splitter", splitter),
Placeholder.component("message", filtered))); Placeholder.component("message", msg)));
} }
@EventHandler @EventHandler