mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 12:29:15 +00:00
拦截指令和消息的tag
This commit is contained in:
@@ -2,6 +2,8 @@ package net.momirealms.craftengine.bukkit.font;
|
||||
|
||||
import io.papermc.paper.event.player.AsyncChatCommandDecorateEvent;
|
||||
import io.papermc.paper.event.player.AsyncChatDecorateEvent;
|
||||
import io.papermc.paper.event.player.PlayerSignCommandPreprocessEvent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||
@@ -15,8 +17,10 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.SignChangeEvent;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class BukkitFontManager extends AbstractFontManager implements Listener {
|
||||
@@ -55,7 +59,6 @@ public class BukkitFontManager extends AbstractFontManager implements Listener {
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
public void onCommand(PlayerCommandPreprocessEvent event) {
|
||||
if (!Config.filterCommand()) return;
|
||||
if (!this.isDefaultFontInUse()) return;
|
||||
if (event.getPlayer().hasPermission(FontManager.BYPASS_COMMAND)) {
|
||||
return;
|
||||
}
|
||||
@@ -66,42 +69,55 @@ public class BukkitFontManager extends AbstractFontManager implements Listener {
|
||||
private void processChatEvent(AsyncChatDecorateEvent event) {
|
||||
Player player = event.player();
|
||||
if (player == null) return;
|
||||
if (!this.isDefaultFontInUse()) return;
|
||||
if (player.hasPermission(FontManager.BYPASS_CHAT)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Object originalMessage = Reflections.field$AsyncChatDecorateEvent$originalMessage.get(event);
|
||||
runIfContainsIllegalCharacter(ComponentUtils.paperAdventureToJson(originalMessage), (json) -> {
|
||||
Object component = ComponentUtils.jsonToPaperAdventure(json);
|
||||
try {
|
||||
Reflections.method$AsyncChatDecorateEvent$result.invoke(event, component);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
String jsonMessage = ComponentUtils.paperAdventureToJson(originalMessage);
|
||||
if (!player.hasPermission(FontManager.BYPASS_CHAT)) {
|
||||
runIfContainsIllegalCharacter(jsonMessage, (json) -> {
|
||||
Object component = ComponentUtils.jsonToPaperAdventure(json);
|
||||
try {
|
||||
Reflections.method$AsyncChatDecorateEvent$result.invoke(event, component);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void runIfContainsIllegalCharacter(String string, Consumer<String> callback) {
|
||||
//noinspection DuplicatedCode
|
||||
char[] chars = string.toCharArray();
|
||||
int[] codepoints = CharacterUtils.charsToCodePoints(chars);
|
||||
int[] newCodepoints = new int[codepoints.length];
|
||||
private void runIfContainsIllegalCharacter(String raw, Consumer<String> callback) {
|
||||
boolean hasIllegal = false;
|
||||
for (int i = 0; i < codepoints.length; i++) {
|
||||
int codepoint = codepoints[i];
|
||||
if (!isIllegalCodepoint(codepoint)) {
|
||||
newCodepoints[i] = codepoint;
|
||||
} else {
|
||||
newCodepoints[i] = '*';
|
||||
// replace illegal image usage
|
||||
Map<String, Component> tokens = matchTags(raw);
|
||||
if (!tokens.isEmpty()) {
|
||||
for (Map.Entry<String, Component> entry : tokens.entrySet()) {
|
||||
raw = raw.replace(entry.getKey(), "*");
|
||||
hasIllegal = true;
|
||||
}
|
||||
}
|
||||
if (hasIllegal) {
|
||||
callback.accept(new String(newCodepoints, 0, newCodepoints.length));
|
||||
|
||||
if (this.isDefaultFontInUse()) {
|
||||
// replace illegal codepoint
|
||||
char[] chars = raw.toCharArray();
|
||||
int[] codepoints = CharacterUtils.charsToCodePoints(chars);
|
||||
int[] newCodepoints = new int[codepoints.length];
|
||||
|
||||
for (int i = 0; i < codepoints.length; i++) {
|
||||
int codepoint = codepoints[i];
|
||||
if (!isIllegalCodepoint(codepoint)) {
|
||||
newCodepoints[i] = codepoint;
|
||||
} else {
|
||||
newCodepoints[i] = '*';
|
||||
hasIllegal = true;
|
||||
}
|
||||
}
|
||||
if (hasIllegal) {
|
||||
callback.accept(new String(newCodepoints, 0, newCodepoints.length));
|
||||
}
|
||||
} else if (hasIllegal) {
|
||||
callback.accept(raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user