1
0

more mini message stuff

This commit is contained in:
Lee
2024-03-28 19:01:11 +00:00
parent adeaeb13c0
commit 9a599f319e
6 changed files with 84 additions and 29 deletions

View File

@ -2,32 +2,63 @@ package cc.fascinated.chat;
import cc.fascinated.account.Account; import cc.fascinated.account.Account;
import cc.fascinated.account.AccountManager; import cc.fascinated.account.AccountManager;
import cc.fascinated.config.Lang;
import cc.fascinated.utils.Manager; import cc.fascinated.utils.Manager;
import cc.fascinated.utils.Style;
import io.papermc.paper.event.player.AsyncChatEvent; import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
public class ChatManager extends Manager { public class ChatManager extends Manager {
private final HashMap<Account, String> lastMessage = new HashMap<>();
private final Pattern domainPattern = Pattern.compile("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,8}$");
@EventHandler @EventHandler
public void onChat(AsyncChatEvent event) { public void onChat(AsyncChatEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Account account = AccountManager.getAccount(player.getUniqueId()); Account account = AccountManager.getAccount(player.getUniqueId());
NamedTextColor color = account.getPlayerColorProfile().getColor(); NamedTextColor color = account.getPlayerColorProfile().getColor();
boolean blockedMessage = false;
TextComponent textComponent = (TextComponent) event.message();
String messageContent = textComponent.content();
// Check if the message contains a domain and is not the server's domain.
if (domainPattern.matcher(messageContent).find() && !messageContent.contains("aetheria.cc")) {
blockedMessage = true;
}
// Block the message if it is the same as the last message sent.
if (lastMessage.containsKey(account) && lastMessage.get(account).equalsIgnoreCase(messageContent)) {
blockedMessage = true;
}
if (blockedMessage) {
event.setCancelled(true);
account.sendMessage(Lang.BLOCKED_MESSAGE.getAsString());
return;
}
event.renderer((source, sourceDisplayName, message, viewer) -> { event.renderer((source, sourceDisplayName, message, viewer) -> {
return Component.text() MiniMessage miniMessage = Style.getMiniMessage();
.append(Component.text("<")) return miniMessage.deserialize(Lang.CHAT_FORMAT.getAsString()
.append(player.displayName().color(color)) .replace("%chatcolor%", color.toString())
.append(Component.text(">")) .replace("%name%", account.getName())
.append(Component.space()) .replace("%message%", miniMessage.stripTags(messageContent))
.append(message) );
.hoverEvent(Component.text("Click to message " + player.getName() + ".").hoverEvent())
.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player.getName() + " "))
.asComponent();
}); });
lastMessage.put(account, messageContent);
} }
} }

View File

@ -13,6 +13,6 @@ public class TotalJoinsCommand extends Command {
@Override @Override
public void execute(Account account, String[] args) { public void execute(Account account, String[] args) {
account.sendMessage(Lang.PREFIX.getAsString() + "§fTotal joins: §e" + Bukkit.getOfflinePlayers().length + "§f."); account.sendMessage(Lang.TOTAL_JOINS_COMMAND.getAsString());
} }
} }

View File

@ -5,8 +5,6 @@ import cc.fascinated.command.Command;
import cc.fascinated.config.Lang; import cc.fascinated.config.Lang;
import cc.fascinated.utils.ChatUtils; import cc.fascinated.utils.ChatUtils;
import cc.fascinated.utils.Style; import cc.fascinated.utils.Style;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import java.util.List; import java.util.List;
@ -18,10 +16,10 @@ public class VoteCommand extends Command {
@Override @Override
public void execute(Account account, String[] args) { public void execute(Account account, String[] args) {
account.sendMessage(ChatUtils.color(Lang.VOTE_HEADER.getAsString())); account.sendMessage(ChatUtils.color(Lang.VOTE_COMMAND_HEADER.getAsString()));
List<String> voteLinks = Lang.VOTE_LINKS.getAsStringList(); List<String> voteLinks = Lang.VOTE_COMMAND_LINKS.getAsStringList();
for (String link : voteLinks) { for (String link : voteLinks) {
account.sendMessage(Style.getMiniMessage().deserialize(Lang.VOTE_FORMAT.getAsString() account.sendMessage(Style.getMiniMessage().deserialize(Lang.VOTE_COMMAND_FORMAT.getAsString()
.replace("%link%", link) .replace("%link%", link)
.replace("%number%", String.valueOf(voteLinks.indexOf(link) + 1)) .replace("%number%", String.valueOf(voteLinks.indexOf(link) + 1))
)); ));

View File

@ -13,11 +13,17 @@ public enum Lang {
PREFIX("prefix"), PREFIX("prefix"),
HELP_COMMAND("help-command"), HELP_COMMAND("help-command"),
VOTE_HEADER("vote.header"),
VOTE_FORMAT("vote.format"),
VOTE_LINKS("vote.links"),
GIT_COMMAND("git-command"), GIT_COMMAND("git-command"),
SEED_COMMAND("seed-command"); SEED_COMMAND("seed-command"),
TOTAL_JOINS_COMMAND("total-joins-command"),
WORLD_SIZE_COMMAND_HEADER("world-size-command.header"),
WORLD_SIZE_COMMAND_FORMAT("world-size-command.format"),
WORLD_SIZE_COMMAND_FOOTER("world-size-command.footer"),
VOTE_COMMAND_HEADER("vote-command.header"),
VOTE_COMMAND_FORMAT("vote-command.format"),
VOTE_COMMAND_LINKS("vote-command.links"),
BLOCKED_MESSAGE("blocked-message"),
CHAT_FORMAT("chat-format");
/** /**
* The path of the lang in the lang.yml file. * The path of the lang in the lang.yml file.

View File

@ -9,6 +9,8 @@ import cc.fascinated.worldsize.WorldSizeManager;
import org.bukkit.World; import org.bukkit.World;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;
public class WorldSizeCommand extends Command { public class WorldSizeCommand extends Command {
@ -18,14 +20,23 @@ public class WorldSizeCommand extends Command {
@Override @Override
public void execute(Account account, String[] args) { public void execute(Account account, String[] args) {
account.sendMessage(Lang.PREFIX + "§fWorld information:"); account.sendMessage(Lang.WORLD_SIZE_COMMAND_HEADER.getAsString());
long totalSize = 0; AtomicLong totalSize = new AtomicLong();
for (Map.Entry<World, Long> entry : WorldSizeManager.getWorldSizes().entrySet()) { Stream<Map.Entry<World, Long>> sorted = WorldSizeManager.getWorldSizes().entrySet().stream()
long size = entry.getValue(); .sorted((o1, o2) -> Long.compare(o2.getValue(), o1.getValue()));
account.sendMessage(" §7- §f" + entry.getKey().getName() + ": §e" + FormatterUtils.formatBytes(size) + "§f."); sorted.forEach((entry) -> {
totalSize += size; account.sendMessage(Lang.WORLD_SIZE_COMMAND_FORMAT.getAsString()
.replace("%world%", entry.getKey().getName())
.replace("%size%", FormatterUtils.formatBytes(entry.getValue()))
);
totalSize.addAndGet(entry.getValue());
});
for (String line : Lang.WORLD_SIZE_COMMAND_FOOTER.getAsStringList()) {
account.sendMessage(line
.replace("%total%", FormatterUtils.formatBytes(totalSize.get()))
.replace("%time%", TimeUtils.format(System.currentTimeMillis() - WorldSizeManager.getLastUpdated()))
);
} }
account.sendMessage(" §fTotal size: §e" + FormatterUtils.formatBytes(totalSize) + "§f.");
account.sendMessage(" §fLast updated: §e" + TimeUtils.format(System.currentTimeMillis() - WorldSizeManager.getLastUpdated()) + " ago§f.");
} }
} }

View File

@ -1,5 +1,8 @@
prefix: "<gold><bold>AETHERIA</bold></gold> <gray>»</gray> <white>" prefix: "<gold><bold>AETHERIA</bold></gold> <gray>»</gray> <white>"
chat-format: "<<%chatcolor%>%name%</%chatcolor%>> <white>%message%"
blocked-message: "<prefix><red>Your message has been blocked."
help-command: help-command:
- "<prefix>Commands:" - "<prefix>Commands:"
- "<yellow>/kill <gray>- <white>Kills you" - "<yellow>/kill <gray>- <white>Kills you"
@ -12,8 +15,14 @@ git-command:
- " <yellow>Build: <white>%build%" - " <yellow>Build: <white>%build%"
- " <yellow>Build date: <white>%date%" - " <yellow>Build date: <white>%date%"
seed-command: "<prefix>The seed is: <hover:show_text:Click to copy the seed><click:copy_to_clipboard:%seed%><yellow>%seed%" seed-command: "<prefix>The seed is: <hover:show_text:Click to copy the seed><click:copy_to_clipboard:%seed%><yellow>%seed%"
total-joins-command: "<prefix>Total joins: <yellow>%joins%"
vote: world-size-command:
header: "<prefix>World information:"
format: " <yellow>%world%: <white>%size%"
footer:
- "<yellow>Total size: <white>%total%"
- "<yellow>Last updated: <white>%time% ago"
vote-command:
header: "<prefix>Vote for us on the following websites:" header: "<prefix>Vote for us on the following websites:"
format: "<yellow>%number%. <hover:show_text:Click to copy the url><click:open_url:%link%><white>%link%" format: "<yellow>%number%. <hover:show_text:Click to copy the url><click:open_url:%link%><white>%link%"
links: links: