Support for kick message

This commit is contained in:
HaHaWTH
2024-07-23 00:51:48 +08:00
parent 8df6a008c8
commit 3b6160c91f
10 changed files with 73 additions and 11 deletions
@@ -1,9 +1,13 @@
package fr.xephi.authme.util.message;
import fr.xephi.authme.util.Utils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -11,7 +15,24 @@ public class MiniMessageUtils {
private static final MiniMessage miniMessage = MiniMessage.miniMessage();
private static final char SECTION_CHAR = '§';
private static final char AMPERSAND_CHAR = '&';
private static final boolean HEX_SUPPORTED = Utils.MAJOR_VERSION >= 16;
private static Method methodDisallow;
private static Method methodKick;
static {
try {
Class<?> componentClass = Class.forName("net{}kyori{}adventure{}text{}Component".replace("{}", "."));
methodDisallow = AsyncPlayerPreLoginEvent.class.getMethod("disallow", AsyncPlayerPreLoginEvent.Result.class, componentClass);
} catch (Exception e) {
methodDisallow = null;
}
try {
Class<?> componentClass = Class.forName("net{}kyori{}adventure{}text{}Component".replace("{}", "."));
methodKick = Player.class.getMethod("kick", componentClass);
} catch (Exception e) {
methodKick = null;
}
}
/**
* Parse a MiniMessage string into a legacy string.
*
@@ -30,7 +51,39 @@ public class MiniMessageUtils {
* @return The parsed message.
*/
public static Component parseMiniMessage(String message) {
return miniMessage.deserialize(convertLegacyToMiniMessage(message, false, SECTION_CHAR, true));
return miniMessage.deserialize(convertLegacyToMiniMessage(message, false, SECTION_CHAR, HEX_SUPPORTED));
}
public static void kickPlayer(Player player, Component message) {
if (methodKick != null) {
try {
methodKick.invoke(player, message);
} catch (Exception e) {
player.kickPlayer(LegacyComponentSerializer.legacySection().serialize(message));
}
} else {
player.kickPlayer(LegacyComponentSerializer.legacySection().serialize(message));
}
}
/**
* Disallows the login event with the given result and reason.
*
* @param event the event
* @param result the event result to set
* @param message the denial message
*/
public static void disallowPreLoginEvent(AsyncPlayerPreLoginEvent event,
AsyncPlayerPreLoginEvent.Result result, Component message) {
if (methodDisallow != null) {
try {
methodDisallow.invoke(event, result, message);
} catch (Exception e) {
event.disallow(result, LegacyComponentSerializer.legacySection().serialize(message));
}
} else {
event.disallow(result, LegacyComponentSerializer.legacySection().serialize(message));
}
}
@SuppressWarnings("all")