use of com.loohp.interactivechat.api.events.PrePacketComponentProcessEvent 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);
}
}
Aggregations