use of com.loohp.interactivechat.objectholders.ProcessSenderResult in project InteractiveChat by LOOHP.
the class OutChatPacket method processPacket.
private static void processPacket(Player receiver, PacketContainer packet, UUID messageUUID, boolean isFiltered) {
PacketContainer originalPacket = packet.deepClone();
try {
Component component = null;
ChatComponentType type = null;
int field = -1;
search: for (ChatComponentType t : ChatComponentType.byPriority()) {
for (int i = 0; i < packet.getModifier().size(); i++) {
if (!CustomArrayUtils.allNull(packet.getModifier().read(i)) && packet.getModifier().getField(i).getType().getName().matches(t.getMatchingRegex())) {
try {
component = t.convertFrom(packet.getModifier().read(i));
} catch (Throwable e) {
System.err.println(t.toString(packet.getModifier().read(i)));
e.printStackTrace();
service.send(packet, receiver, messageUUID);
return;
}
field = i;
type = t;
break search;
}
}
}
if (field < 0 || type == null || component == null) {
service.send(packet, receiver, messageUUID);
return;
}
String legacyText = LegacyComponentSerializer.legacySection().serializeOr(component, "");
try {
if (legacyText.equals("") || InteractiveChat.messageToIgnore.stream().anyMatch(each -> legacyText.matches(each))) {
service.send(packet, receiver, messageUUID);
return;
}
} catch (Exception e) {
service.send(packet, receiver, messageUUID);
return;
}
if (InteractiveChat.version.isOld() && JsonUtils.containsKey(InteractiveChatComponentSerializer.gson().serialize(component), "translate")) {
service.send(packet, receiver, messageUUID);
return;
}
boolean isCommand = false;
boolean isChat = false;
Optional<ICPlayer> sender = Optional.empty();
String rawMessageKey = PlainTextComponentSerializer.plainText().serializeOr(component, "");
InteractiveChat.keyTime.putIfAbsent(rawMessageKey, System.currentTimeMillis());
Long timeKey = InteractiveChat.keyTime.get(rawMessageKey);
long unix = timeKey == null ? System.currentTimeMillis() : timeKey;
ProcessSenderResult commandSender = ProcessCommands.process(component);
if (commandSender.getSender() != null) {
ICPlayer icplayer = ICPlayerFactory.getICPlayer(commandSender.getSender());
if (icplayer != null) {
sender = Optional.of(icplayer);
// noinspection UnusedAssignment
isCommand = true;
}
}
ProcessSenderResult chatSender = null;
if (!sender.isPresent()) {
if (InteractiveChat.useAccurateSenderFinder) {
chatSender = ProcessAccurateSender.process(component);
if (chatSender.getSender() != null) {
ICPlayer icplayer = ICPlayerFactory.getICPlayer(chatSender.getSender());
if (icplayer != null) {
sender = Optional.of(icplayer);
// noinspection UnusedAssignment
isChat = true;
}
}
}
}
if (!sender.isPresent() && !InteractiveChat.useAccurateSenderFinder) {
sender = SenderFinder.getSender(component, rawMessageKey);
}
if (sender.isPresent() && !sender.get().isLocal()) {
if (isFiltered) {
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> {
service.execute(() -> {
processPacket(receiver, packet, messageUUID, false);
}, receiver, messageUUID);
}, (int) Math.ceil((double) InteractiveChat.remoteDelay / 50));
return;
}
}
component = commandSender.getComponent();
if (chatSender != null) {
component = chatSender.getComponent();
}
if (sender.isPresent()) {
InteractiveChat.keyPlayer.put(rawMessageKey, sender.get());
}
component = ComponentModernizing.modernize(component);
component = component.replaceText(TextReplacementConfig.builder().match(Registry.ID_PATTERN).replacement("").build());
UUID preEventSenderUUID = sender.isPresent() ? sender.get().getUniqueId() : null;
PrePacketComponentProcessEvent preEvent = new PrePacketComponentProcessEvent(true, receiver, component, preEventSenderUUID);
Bukkit.getPluginManager().callEvent(preEvent);
if (preEvent.getSender() != null) {
Player newsender = Bukkit.getPlayer(preEvent.getSender());
if (newsender != null) {
sender = Optional.of(ICPlayerFactory.getICPlayer(newsender));
}
}
component = preEvent.getComponent();
if (InteractiveChat.translateHoverableItems && InteractiveChat.itemGUI) {
component = HoverableItemDisplay.process(component, receiver);
}
if (InteractiveChat.usePlayerName) {
component = PlayernameDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.allowMention && sender.isPresent()) {
PlayerData data = InteractiveChat.playerDataManager.getPlayerData(receiver);
if (data == null || !data.isMentionDisabled()) {
component = MentionDisplay.process(component, receiver, sender.get(), unix, true);
}
}
component = ComponentReplacing.replace(component, Registry.MENTION_TAG_CONVERTER.getReversePattern().pattern(), true, (result, components) -> {
return LegacyComponentSerializer.legacySection().deserialize(result.group(2));
});
component = CustomPlaceholderDisplay.process(component, sender, receiver, InteractiveChat.placeholderList.values(), unix);
if (InteractiveChat.useItem) {
component = ItemDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useInventory) {
component = InventoryDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useEnder) {
component = EnderchestDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.clickableCommands) {
component = CommandsDisplay.process(component);
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_16) && InteractiveChat.fontTags) {
if (!sender.isPresent() || (sender.isPresent() && PlayerUtils.hasPermission(sender.get().getUniqueId(), "interactivechat.customfont.translate", true, 250))) {
component = ComponentFont.parseFont(component);
}
}
if (PlayerUtils.getColorSettings(receiver).equals(ColorSettings.OFF)) {
component = ComponentStyling.stripColor(component);
}
PostPacketComponentProcessEvent postEvent = new PostPacketComponentProcessEvent(true, receiver, component, preEventSenderUUID);
Bukkit.getPluginManager().callEvent(postEvent);
component = postEvent.getComponent();
boolean legacyRGB = InteractiveChat.version.isLegacyRGB();
String json = legacyRGB ? InteractiveChatComponentSerializer.legacyGson().serialize(component) : InteractiveChatComponentSerializer.gson().serialize(component);
boolean longerThanMaxLength = InteractiveChat.sendOriginalIfTooLong && json.length() > InteractiveChat.packetStringMaxLength;
try {
packet.getModifier().write(field, type.convertTo(component, legacyRGB));
} catch (Throwable e) {
for (int i = 0; i < chatFieldsSize; i++) {
packet.getModifier().write(i, null);
}
packet.getChatComponents().write(0, WrappedChatComponent.fromJson(json));
}
UUID postEventSenderUUID = sender.isPresent() ? sender.get().getUniqueId() : new UUID(0, 0);
if (packet.getUUIDs().size() > 0) {
packet.getUUIDs().write(0, postEventSenderUUID);
}
PreChatPacketSendEvent sendEvent = new PreChatPacketSendEvent(true, receiver, packet, postEventSenderUUID, originalPacket, InteractiveChat.sendOriginalIfTooLong, longerThanMaxLength);
Bukkit.getPluginManager().callEvent(sendEvent);
Bukkit.getScheduler().runTaskLater(InteractiveChat.plugin, () -> {
InteractiveChat.keyTime.remove(rawMessageKey);
InteractiveChat.keyPlayer.remove(rawMessageKey);
}, 10);
if (sendEvent.isCancelled()) {
if (sendEvent.sendOriginalIfCancelled()) {
if (longerThanMaxLength && InteractiveChat.cancelledMessage) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[InteractiveChat] " + ChatColor.RED + "Cancelled a chat packet bounded to " + receiver.getName() + " that is " + json.length() + " characters long (Longer than maximum allowed in a chat packet), sending original unmodified message instead! [THIS IS NOT A BUG]");
}
PacketContainer originalPacketModified = sendEvent.getOriginal();
service.send(originalPacketModified, receiver, messageUUID);
return;
}
if (longerThanMaxLength && InteractiveChat.cancelledMessage) {
Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[InteractiveChat] " + ChatColor.RED + "Cancelled a chat packet bounded to " + receiver.getName() + " that is " + json.length() + " characters long (Longer than maximum allowed in a chat packet) [THIS IS NOT A BUG]");
}
service.discard(receiver.getUniqueId(), messageUUID);
return;
}
service.send(packet, receiver, messageUUID);
} catch (Exception e) {
e.printStackTrace();
service.send(originalPacket, receiver, messageUUID);
}
}
use of com.loohp.interactivechat.objectholders.ProcessSenderResult in project InteractiveChat by LOOHP.
the class ProcessAccurateSender method process.
public static ProcessSenderResult process(Component component) {
String text = InteractiveChatComponentSerializer.plainText().serialize(component);
UUID uuid = find(text);
Matcher matcher0 = PATTERN_0.matcher(text);
if (matcher0.find()) {
uuid = UUID.fromString(matcher0.group(1));
}
component = ComponentReplacing.replace(component, PATTERN_0.pattern(), false, (result, matchedComponents) -> {
String replacement = result.group(2);
if (replacement == null) {
return Component.empty();
} else {
return LegacyComponentSerializer.legacySection().deserialize(Registry.ID_UNESCAPE_PATTERN.matcher(replacement).replaceAll(">"));
}
});
if (uuid == null) {
Matcher matcher1 = PATTERN_1.matcher(text);
if (matcher1.find()) {
uuid = UUID.fromString(matcher1.group(1));
}
}
component = ComponentReplacing.replace(component, PATTERN_1.pattern(), Component.empty());
return new ProcessSenderResult(component, uuid);
}
use of com.loohp.interactivechat.objectholders.ProcessSenderResult in project InteractiveChat by LOOHP.
the class ProcessCommands method process.
public static ProcessSenderResult process(Component component) {
UUID uuid = null;
String text = InteractiveChatComponentSerializer.plainText().serialize(component);
Matcher matcher0 = PATTERN_0.matcher(text);
if (matcher0.find()) {
uuid = UUID.fromString(matcher0.group(1));
}
component = ComponentReplacing.replace(component, PATTERN_0.pattern(), false, (result, matchedComponents) -> {
String replacement = result.group(2);
if (replacement == null) {
return Component.empty();
} else {
return LegacyComponentSerializer.legacySection().deserialize(Registry.ID_UNESCAPE_PATTERN.matcher(replacement).replaceAll(">"));
}
});
if (uuid == null) {
Matcher matcher1 = PATTERN_1.matcher(text);
if (matcher1.find()) {
uuid = UUID.fromString(matcher1.group(1));
}
}
component = ComponentReplacing.replace(component, PATTERN_1.pattern(), Component.empty());
return new ProcessSenderResult(component, uuid);
}
use of com.loohp.interactivechat.objectholders.ProcessSenderResult in project InteractiveChat by LOOHP.
the class ProcessExternalMessage method processAndRespond0.
public String processAndRespond0(Player receiver, String json) throws Exception {
Component component = InteractiveChatComponentSerializer.gson().deserialize(json);
Component originalComponent = component;
try {
if (LegacyComponentSerializer.legacySection().serialize(component).isEmpty()) {
return json;
}
} catch (Exception e) {
return json;
}
if ((InteractiveChat.version.isOld()) && JsonUtils.containsKey(InteractiveChatComponentSerializer.gson().serialize(component), "translate")) {
return json;
}
Optional<ICPlayer> sender = Optional.empty();
String rawMessageKey = InteractiveChatComponentSerializer.plainText().serializeOr(component, "");
InteractiveChat.keyTime.putIfAbsent(rawMessageKey, System.currentTimeMillis());
Long timeKey = InteractiveChat.keyTime.get(rawMessageKey);
long unix = timeKey == null ? System.currentTimeMillis() : timeKey;
ProcessSenderResult commandSender = ProcessCommands.process(component);
if (commandSender.getSender() != null) {
sender = Optional.ofNullable(ICPlayerFactory.getICPlayer(commandSender.getSender()));
}
ProcessSenderResult chatSender = null;
if (!sender.isPresent()) {
if (InteractiveChat.useAccurateSenderFinder) {
chatSender = ProcessAccurateSender.process(component);
if (chatSender.getSender() != null) {
sender = Optional.ofNullable(ICPlayerFactory.getICPlayer(chatSender.getSender()));
}
}
}
if (!sender.isPresent() && !InteractiveChat.useAccurateSenderFinder) {
sender = SenderFinder.getSender(component, rawMessageKey);
}
component = commandSender.getComponent();
if (chatSender != null) {
component = chatSender.getComponent();
}
String text = LegacyComponentSerializer.legacySection().serialize(component);
if (InteractiveChat.messageToIgnore.stream().anyMatch(each -> text.matches(each))) {
return json;
}
if (sender.isPresent()) {
InteractiveChat.keyPlayer.put(rawMessageKey, sender.get());
}
String server;
if (sender.isPresent() && !sender.get().isLocal()) {
try {
TimeUnit.MILLISECONDS.sleep(InteractiveChat.remoteDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
server = sender.get().getServer();
} else {
server = ICPlayer.LOCAL_SERVER_REPRESENTATION;
}
component = ComponentModernizing.modernize(component);
component = ComponentReplacing.replace(component, Registry.ID_PATTERN.pattern(), Component.empty());
if (InteractiveChat.usePlayerName) {
component = PlayernameDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.allowMention && sender.isPresent()) {
PlayerData data = InteractiveChat.playerDataManager.getPlayerData(receiver);
if (data == null || !data.isMentionDisabled()) {
component = MentionDisplay.process(component, receiver, sender.get(), unix, !Bukkit.isPrimaryThread());
}
}
component = ComponentReplacing.replace(component, Registry.MENTION_TAG_CONVERTER.getReversePattern().pattern(), true, (result, components) -> {
return LegacyComponentSerializer.legacySection().deserialize(result.group(2));
});
Collection<ICPlaceholder> serverPlaceholderList = InteractiveChat.remotePlaceholderList.get(server);
if (server.equals(ICPlayer.LOCAL_SERVER_REPRESENTATION) || serverPlaceholderList == null) {
serverPlaceholderList = InteractiveChat.placeholderList.values();
}
component = CustomPlaceholderDisplay.process(component, sender, receiver, serverPlaceholderList, unix);
if (InteractiveChat.useItem) {
component = ItemDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useInventory) {
component = InventoryDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useEnder) {
component = EnderchestDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.clickableCommands) {
component = CommandsDisplay.process(component);
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_16) && InteractiveChat.fontTags) {
if (!sender.isPresent() || (sender.isPresent() && PlayerUtils.hasPermission(sender.get().getUniqueId(), "interactivechat.customfont.translate", true, 5))) {
component = ComponentFont.parseFont(component);
}
}
Bukkit.getScheduler().runTaskLater(InteractiveChat.plugin, () -> {
InteractiveChat.keyTime.remove(rawMessageKey);
InteractiveChat.keyPlayer.remove(rawMessageKey);
}, 5);
String newJson = InteractiveChatComponentSerializer.gson().serialize(component);
if (InteractiveChat.sendOriginalIfTooLong && newJson.length() > InteractiveChat.packetStringMaxLength) {
String originalJson = InteractiveChatComponentSerializer.gson().serialize(originalComponent);
if (originalJson.length() > InteractiveChat.packetStringMaxLength) {
return "{\"text\":\"\"}";
} else {
return originalJson;
}
}
return newJson;
}
Aggregations