add the ability to overide the chat

This commit is contained in:
Taah 2022-05-05 21:27:32 -07:00
parent 048518dc49
commit 38b18e5867
7 changed files with 68 additions and 18 deletions

View File

@ -0,0 +1,8 @@
package dev.plex.api.chat;
import io.papermc.paper.event.player.AsyncChatEvent;
public interface IChatHandler
{
void doChat(AsyncChatEvent event);
}

View File

@ -0,0 +1,28 @@
package dev.plex.api.plugin;
import dev.plex.api.chat.IChatHandler;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
@Setter
public abstract class PlexPlugin extends JavaPlugin
{
@Setter(AccessLevel.NONE)
private static PlexPlugin plugin;
private IChatHandler chatHandler;
@Override
public void onLoad()
{
plugin = this;
}
public static PlexPlugin get()
{
return plugin;
}
}

View File

@ -13,6 +13,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -23,7 +24,7 @@ public class ServerListener extends PlexListener
@Subscribe(order = PostOrder.FIRST) @Subscribe(order = PostOrder.FIRST)
public void onPing(ProxyPingEvent event) public void onPing(ProxyPingEvent event)
{ {
String baseMotd = plugin.getConfig().as(ServerSettings.class).getServer().getMotd(); String baseMotd = plugin.getConfig().as(ServerSettings.class).getServer().getMotd().get(ThreadLocalRandom.current().nextInt(plugin.getConfig().as(ServerSettings.class).getServer().getMotd().size()));
baseMotd = baseMotd.replace("\\n", "\n"); baseMotd = baseMotd.replace("\\n", "\n");
baseMotd = baseMotd.replace("%servername%", plugin.getConfig().as(ServerSettings.class).getServer().getName()); baseMotd = baseMotd.replace("%servername%", plugin.getConfig().as(ServerSettings.class).getServer().getName());
baseMotd = baseMotd.replace("%mcversion%", plugin.getServer().getVersion().getVersion().split(" ")[0]); baseMotd = baseMotd.replace("%mcversion%", plugin.getServer().getVersion().getVersion().split(" ")[0]);

View File

@ -15,7 +15,7 @@ public class ServerSettings
@Data @Data
public static class Server { public static class Server {
private String name = "Server"; private String name = "Server";
private String motd = "%randomgradient%%servername% - %mcversion%"; private List<String> motd = Lists.newArrayList("%randomgradient%%servername% - %mcversion%", "Another motd");
private boolean colorizeMotd = false; private boolean colorizeMotd = false;
private boolean debug = false; private boolean debug = false;
private final List<String> sample = Lists.newArrayList("example", "example"); private final List<String> sample = Lists.newArrayList("example", "example");

View File

@ -13,7 +13,7 @@
# %servername% - The name provided above # %servername% - The name provided above
# %randomgradient% - Creates a random gradient every ping of two random colors for the whole string # %randomgradient% - Creates a random gradient every ping of two random colors for the whole string
# Supports MiniMessage strings, no legacy & and § # Supports MiniMessage strings, no legacy & and §
motd = "%randomgradient%%servername% - %mcversion%" motd = ["%randomgradient%%servername% - %mcversion%", "Another motd"]
colorizeMotd = false colorizeMotd = false
# Enables debug messages # Enables debug messages

View File

@ -7,8 +7,10 @@ import dev.plex.cache.PlayerCache;
import dev.plex.config.Config; import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler; import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler; import dev.plex.handlers.ListenerHandler;
import dev.plex.listener.impl.ChatListener;
import dev.plex.module.ModuleManager; import dev.plex.module.ModuleManager;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.api.plugin.PlexPlugin;
import dev.plex.punishment.PunishmentManager; import dev.plex.punishment.PunishmentManager;
import dev.plex.rank.RankManager; import dev.plex.rank.RankManager;
import dev.plex.services.ServiceManager; import dev.plex.services.ServiceManager;
@ -34,11 +36,10 @@ import net.milkbowl.vault.permission.Permission;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
@Getter @Getter
@Setter @Setter
public class Plex extends JavaPlugin public class Plex extends PlexPlugin
{ {
private static Plex plugin; private static Plex plugin;
@ -83,6 +84,7 @@ public class Plex extends JavaPlugin
@Override @Override
public void onLoad() public void onLoad()
{ {
super.onLoad();
plugin = this; plugin = this;
config = new Config(this, "config.yml"); config = new Config(this, "config.yml");
messages = new Config(this, "messages.yml"); messages = new Config(this, "messages.yml");
@ -99,6 +101,8 @@ public class Plex extends JavaPlugin
moduleManager = new ModuleManager(); moduleManager = new ModuleManager();
moduleManager.loadAllModules(); moduleManager.loadAllModules();
moduleManager.loadModules(); moduleManager.loadModules();
this.setChatHandler(new ChatListener.ChatHandlerImpl());
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package dev.plex.listener.impl; package dev.plex.listener.impl;
import dev.plex.api.chat.IChatHandler;
import dev.plex.cache.PlayerCache; import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggleable; import dev.plex.listener.annotation.Toggleable;
@ -24,26 +25,34 @@ import org.jetbrains.annotations.NotNull;
public class ChatListener extends PlexListener public class ChatListener extends PlexListener
{ {
private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder().match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]").replacement((matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent(ClickEvent.openUrl(matchResult.group()))).build(); private final static TextReplacementConfig URL_REPLACEMENT_CONFIG = TextReplacementConfig.builder().match("(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]").replacement((matchResult, builder) -> Component.empty().content(matchResult.group()).clickEvent(ClickEvent.openUrl(matchResult.group()))).build();
private final PlexChatRenderer renderer = new PlexChatRenderer();
@EventHandler @EventHandler
public void onChat(AsyncChatEvent event) public void onChat(AsyncChatEvent event)
{ {
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); plugin.getChatHandler().doChat(event);
Component prefix = plugin.getRankManager().getPrefix(plexPlayer); }
if (prefix != null) public static class ChatHandlerImpl implements IChatHandler
{
private final PlexChatRenderer renderer = new PlexChatRenderer();
@Override
public void doChat(AsyncChatEvent event)
{ {
renderer.hasPrefix = true; PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId());
renderer.prefix = prefix; Component prefix = plugin.getRankManager().getPrefix(plexPlayer);
}
else
{
renderer.hasPrefix = false;
renderer.prefix = null;
}
event.renderer(renderer); if (prefix != null)
{
renderer.hasPrefix = true;
renderer.prefix = prefix;
} else
{
renderer.hasPrefix = false;
renderer.prefix = null;
}
event.renderer(renderer);
}
} }
public static class PlexChatRenderer implements ChatRenderer public static class PlexChatRenderer implements ChatRenderer