2019-12-17 12:37:59 +00:00
|
|
|
package pw.kaboom.extras.modules.player;
|
2019-07-30 17:14:24 +00:00
|
|
|
|
2022-05-21 18:41:44 +00:00
|
|
|
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;
|
2023-04-02 00:11:43 +00:00
|
|
|
import net.kyori.adventure.text.TextReplacementConfig;
|
|
|
|
import net.kyori.adventure.text.event.ClickEvent;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
|
import net.kyori.adventure.text.format.TextDecoration;
|
2022-05-21 18:41:44 +00:00
|
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
2019-07-30 17:14:24 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2022-05-21 18:41:44 +00:00
|
|
|
import org.bukkit.event.EventPriority;
|
2019-07-30 17:14:24 +00:00
|
|
|
import org.bukkit.event.Listener;
|
2022-05-21 18:41:44 +00:00
|
|
|
|
2023-04-02 00:11:43 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2019-12-21 14:12:26 +00:00
|
|
|
public final class PlayerChat implements Listener {
|
2022-05-21 18:41:44 +00:00
|
|
|
private static final PlayerChatRenderer CHAT_RENDERER = new PlayerChatRenderer();
|
2022-05-20 03:57:17 +00:00
|
|
|
|
2022-05-21 18:41:44 +00:00
|
|
|
@EventHandler(priority = EventPriority.HIGHEST)
|
|
|
|
void onAsyncChatEventProcess(final AsyncChatEvent event) {
|
2022-05-20 02:35:48 +00:00
|
|
|
final UUID playerUuid = event.getPlayer().getUniqueId();
|
|
|
|
|
|
|
|
if (PlayerCommand.getCommandMillisList().get(playerUuid) != null) {
|
2022-05-20 03:07:38 +00:00
|
|
|
final long lastCommandTime = PlayerCommand.getCommandMillisList().get(playerUuid);
|
|
|
|
final long millisDifference = System.currentTimeMillis() - lastCommandTime;
|
2022-05-20 02:35:48 +00:00
|
|
|
|
|
|
|
if (millisDifference < 50) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerCommand.getCommandMillisList().put(playerUuid, System.currentTimeMillis());
|
2022-05-21 18:41:44 +00:00
|
|
|
}
|
2022-05-20 02:35:48 +00:00
|
|
|
|
2022-05-21 18:41:44 +00:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
void onAsyncChatEventRenderer(final AsyncChatEvent event) {
|
|
|
|
event.renderer(CHAT_RENDERER);
|
|
|
|
}
|
2022-05-20 02:35:48 +00:00
|
|
|
|
2022-05-21 18:41:44 +00:00
|
|
|
public static class PlayerChatRenderer implements ChatRenderer {
|
2023-04-02 00:11:43 +00:00
|
|
|
private static final TextReplacementConfig URL_REPLACEMENT_CONFIG =
|
|
|
|
TextReplacementConfig
|
|
|
|
.builder()
|
|
|
|
.match(Pattern
|
2023-04-08 15:49:46 +00:00
|
|
|
.compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
|
|
|
|
+ "\\.[a-zA-Z0-9]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))"))
|
2023-04-02 00:11:43 +00:00
|
|
|
.replacement((b, c) -> {
|
|
|
|
if (c == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.groupCount() < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String content = b.group(1);
|
|
|
|
final String url;
|
|
|
|
|
|
|
|
/*
|
2023-04-08 15:49:46 +00:00
|
|
|
Minecraft doesn't accept "www.google.com" as a URL
|
2023-04-02 00:11:43 +00:00
|
|
|
in click events
|
|
|
|
*/
|
|
|
|
if (content.contains("://")) {
|
|
|
|
url = content;
|
|
|
|
} else {
|
|
|
|
url = "https://" + content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Component.text(content, NamedTextColor.BLUE)
|
|
|
|
.decorate(TextDecoration.UNDERLINED)
|
|
|
|
.clickEvent(ClickEvent.openUrl(url));
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
|
|
|
|
private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER =
|
|
|
|
LegacyComponentSerializer
|
|
|
|
.legacyAmpersand();
|
2022-05-20 02:35:48 +00:00
|
|
|
|
2022-05-21 18:41:44 +00:00
|
|
|
@Override
|
2022-05-21 19:28:42 +00:00
|
|
|
public @Nonnull Component render(@Nonnull Player player,
|
|
|
|
@Nonnull Component displayName,
|
|
|
|
@Nonnull Component component,
|
|
|
|
@Nonnull Audience audience) {
|
2022-05-21 18:41:44 +00:00
|
|
|
Component newComponent = Component.empty();
|
2022-12-27 17:50:26 +00:00
|
|
|
final Component prefix;
|
|
|
|
Component prefix1;
|
|
|
|
|
|
|
|
try {
|
|
|
|
prefix1 = PlayerPrefix.getPrefix(player);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
prefix1 = PlayerPrefix.getDefaultPrefix(player);
|
|
|
|
}
|
2022-05-20 02:35:48 +00:00
|
|
|
|
2022-12-27 17:50:26 +00:00
|
|
|
prefix = prefix1;
|
2022-05-21 18:41:44 +00:00
|
|
|
final String message = ((TextComponent) component).content();
|
|
|
|
|
|
|
|
newComponent = newComponent
|
|
|
|
.append(prefix)
|
|
|
|
.append(displayName)
|
|
|
|
.append(Component.text(":"))
|
|
|
|
.append(Component.space());
|
|
|
|
|
2023-04-02 00:11:43 +00:00
|
|
|
final Component messageWithColorCodes = LEGACY_COMPONENT_SERIALIZER
|
|
|
|
.deserialize(message);
|
|
|
|
final Component completedMessage = messageWithColorCodes
|
|
|
|
.replaceText(URL_REPLACEMENT_CONFIG);
|
|
|
|
|
|
|
|
return newComponent.append(completedMessage);
|
2022-05-21 18:41:44 +00:00
|
|
|
}
|
2022-05-20 02:35:48 +00:00
|
|
|
}
|
2019-07-30 17:14:24 +00:00
|
|
|
}
|