use of com.loohp.interactivechat.objectholders.ICPlaceholder in project InteractiveChat by LOOHP.
the class InteractiveChatVelocity method handleChat.
private void handleChat(PlayerChatEvent event) {
if (!event.getResult().isAllowed()) {
return;
}
try {
Field messageField = event.getClass().getDeclaredField("message");
messageField.setAccessible(true);
messageField.set(event, Registry.ID_PATTERN.matcher(event.getMessage()).replaceAll(""));
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
String message = event.getMessage();
if (!player.getCurrentServer().isPresent()) {
return;
}
String newMessage = event.getMessage();
boolean hasInteractiveChat = false;
BackendInteractiveChatData data = serverInteractiveChatInfo.get(player.getCurrentServer().get().getServerInfo().getName());
if (data != null) {
hasInteractiveChat = data.hasInteractiveChat();
}
boolean usage = false;
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
if (icplaceholder.getKeyword().matcher(message).find()) {
usage = true;
break outer;
}
}
}
if (newMessage.startsWith("/")) {
if (usage && hasInteractiveChat) {
for (String parsecommand : InteractiveChatVelocity.parseCommands) {
if (newMessage.matches(parsecommand)) {
String command = newMessage.trim();
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
Pattern placeholder = icplaceholder.getKeyword();
Matcher matcher = placeholder.matcher(command);
if (matcher.find()) {
String uuidmatch = "<cmd=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(command.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
command = command.substring(0, matcher.start()) + uuidmatch + command.substring(matcher.end());
if (command.length() > 256) {
command = command.substring(0, 256);
}
event.setResult(ChatResult.message(command));
break outer;
}
}
}
break;
}
}
}
} else {
if (usage && InteractiveChatVelocity.useAccurateSenderFinder && hasInteractiveChat) {
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
Pattern placeholder = icplaceholder.getKeyword();
Matcher matcher = placeholder.matcher(message);
if (matcher.find()) {
String uuidmatch = "<chat=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(message.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
message = message.substring(0, matcher.start()) + uuidmatch + message.substring(matcher.end());
if (message.length() > 256) {
message = message.substring(0, 256);
}
event.setResult(ChatResult.message(message));
break outer;
}
}
}
}
try {
Field messageField = event.getClass().getDeclaredField("message");
messageField.setAccessible(true);
messageField.set(event, message);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
proxyServer.getScheduler().buildTask(plugin, () -> {
Map<String, Long> messages = forwardedMessages.get(uuid);
if (messages != null && messages.remove(newMessage) != null) {
try {
PluginMessageSendingVelocity.sendMessagePair(uuid, newMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
}).delay(100, TimeUnit.MILLISECONDS).schedule();
}
}
use of com.loohp.interactivechat.objectholders.ICPlaceholder in project InteractiveChat by LOOHP.
the class PluginMessageSendingVelocity method forwardPlaceholderList.
@SuppressWarnings("deprecation")
public static void forwardPlaceholderList(List<ICPlaceholder> serverPlaceholderList, RegisteredServer serverFrom) throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
DataTypeIO.writeString(output, serverFrom.getServerInfo().getName(), StandardCharsets.UTF_8);
output.writeInt(serverPlaceholderList.size());
for (ICPlaceholder placeholder : serverPlaceholderList) {
boolean isBuiltIn = placeholder.isBuildIn();
output.writeBoolean(isBuiltIn);
if (isBuiltIn) {
DataTypeIO.writeString(output, placeholder.getKeyword().pattern(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, placeholder.getName(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, placeholder.getDescription(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, placeholder.getPermission(), StandardCharsets.UTF_8);
output.writeLong(placeholder.getCooldown());
} else {
CustomPlaceholder customPlaceholder = (CustomPlaceholder) placeholder;
output.writeInt(customPlaceholder.getPosition());
output.writeByte(customPlaceholder.getParsePlayer().getOrder());
DataTypeIO.writeString(output, customPlaceholder.getKeyword().pattern(), StandardCharsets.UTF_8);
output.writeBoolean(customPlaceholder.getParseKeyword());
output.writeLong(customPlaceholder.getCooldown());
CustomPlaceholderHoverEvent hover = customPlaceholder.getHover();
output.writeBoolean(hover.isEnabled());
DataTypeIO.writeString(output, hover.getText(), StandardCharsets.UTF_8);
CustomPlaceholderClickEvent click = customPlaceholder.getClick();
output.writeBoolean(click.isEnabled());
DataTypeIO.writeString(output, click.getAction() == null ? "" : click.getAction().name(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, click.getValue(), StandardCharsets.UTF_8);
CustomPlaceholderReplaceText replace = customPlaceholder.getReplace();
output.writeBoolean(replace.isEnabled());
DataTypeIO.writeString(output, replace.getReplaceText(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, placeholder.getName(), StandardCharsets.UTF_8);
DataTypeIO.writeString(output, placeholder.getDescription(), StandardCharsets.UTF_8);
}
}
int packetNumber = InteractiveChatVelocity.random.nextInt();
int packetId = 0x09;
byte[] data = output.toByteArray();
byte[][] dataArray = CustomArrayUtils.divideArray(data, 32700);
for (int i = 0; i < dataArray.length; i++) {
byte[] chunk = dataArray[i];
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeInt(packetNumber);
out.writeShort(packetId);
out.writeBoolean(i == (dataArray.length - 1));
out.write(chunk);
for (RegisteredServer server : getServer().getAllServers()) {
if (!server.getServerInfo().getName().equals(serverFrom.getServerInfo().getName())) {
server.sendPluginMessage(ICChannelIdentifier.INSTANCE, out.toByteArray());
InteractiveChatVelocity.pluginMessagesCounter.incrementAndGet();
}
}
}
}
use of com.loohp.interactivechat.objectholders.ICPlaceholder 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;
}
use of com.loohp.interactivechat.objectholders.ICPlaceholder in project InteractiveChat by LOOHP.
the class InteractiveChatBungee method handleChat.
private void handleChat(ChatEvent event) {
if (event.isCancelled()) {
return;
}
event.setMessage(Registry.ID_PATTERN.matcher(event.getMessage()).replaceAll(""));
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
UUID uuid = player.getUniqueId();
String message = event.getMessage();
String newMessage = event.getMessage();
boolean hasInteractiveChat = false;
Server server = player.getServer();
if (server != null) {
BackendInteractiveChatData data = serverInteractiveChatInfo.get(server.getInfo().getName());
if (data != null) {
hasInteractiveChat = data.hasInteractiveChat();
}
}
boolean usage = false;
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
Matcher matcher = icplaceholder.getKeyword().matcher(message);
if (matcher.find()) {
int start = matcher.start();
if ((start < 1 || message.charAt(start - 1) != '\\') || (start > 1 && message.charAt(start - 1) == '\\' && message.charAt(start - 2) == '\\')) {
usage = true;
break outer;
}
}
}
}
if (newMessage.startsWith("/")) {
if (usage && hasInteractiveChat) {
for (String parsecommand : InteractiveChatBungee.parseCommands) {
if (newMessage.matches(parsecommand)) {
String command = newMessage.trim();
if (tagEveryIdentifiableMessage) {
String uuidmatch = " <cmd=" + uuid + ">";
if (command.length() > 256 - uuidmatch.length()) {
command = command.substring(0, 256 - uuidmatch.length());
}
command = command + uuidmatch;
event.setMessage(command);
} else {
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
Pattern placeholder = icplaceholder.getKeyword();
Matcher matcher = placeholder.matcher(command);
if (matcher.find()) {
int start = matcher.start();
if ((start < 1 || command.charAt(start - 1) != '\\') || (start > 1 && command.charAt(start - 1) == '\\' && command.charAt(start - 2) == '\\')) {
String uuidmatch = "<cmd=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(command.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
command = command.substring(0, matcher.start()) + uuidmatch + command.substring(matcher.end());
if (command.length() > 256) {
command = command.substring(0, 256);
}
event.setMessage(command);
break outer;
}
}
}
}
}
break;
}
}
}
} else {
if (usage && useAccurateSenderFinder && hasInteractiveChat) {
if (tagEveryIdentifiableMessage) {
String uuidmatch = " <cmd=" + uuid + ">";
if (message.length() > 256 - uuidmatch.length()) {
message = message.substring(0, 256 - uuidmatch.length());
}
message = message + uuidmatch;
event.setMessage(message);
} else {
outer: for (List<ICPlaceholder> serverPlaceholders : placeholderList.values()) {
for (ICPlaceholder icplaceholder : serverPlaceholders) {
Pattern placeholder = icplaceholder.getKeyword();
Matcher matcher = placeholder.matcher(message);
if (matcher.find()) {
int start = matcher.start();
if ((start < 1 || message.charAt(start - 1) != '\\') || (start > 1 && message.charAt(start - 1) == '\\' && message.charAt(start - 2) == '\\')) {
String uuidmatch = "<chat=" + uuid + ":" + Registry.ID_ESCAPE_PATTERN.matcher(message.substring(matcher.start(), matcher.end())).replaceAll("\\>") + ":>";
message = message.substring(0, matcher.start()) + uuidmatch + message.substring(matcher.end());
if (message.length() > 256) {
message = message.substring(0, 256);
}
event.setMessage(message);
break outer;
}
}
}
}
}
}
ProxyServer.getInstance().getScheduler().schedule(plugin, () -> {
Map<String, Long> messages = forwardedMessages.get(uuid);
if (messages != null && messages.remove(newMessage) != null) {
try {
PluginMessageSendingBungee.sendMessagePair(uuid, newMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
}, 100, TimeUnit.MILLISECONDS);
}
}
use of com.loohp.interactivechat.objectholders.ICPlaceholder in project InteractiveChat by LOOHP.
the class InteractiveChatBungee method onReceive.
@EventHandler
public void onReceive(PluginMessageEvent event) {
if (!event.getTag().equals("interchat:main")) {
return;
}
event.setCancelled(true);
Server senderServer = (Server) event.getSender();
SocketAddress senderServerAddress = event.getSender().getSocketAddress();
byte[] packet = Arrays.copyOf(event.getData(), event.getData().length);
ByteArrayDataInput in = ByteStreams.newDataInput(packet);
int packetNumber = in.readInt();
int packetId = in.readShort();
if (!Registry.PROXY_PASSTHROUGH_RELAY_PACKETS.contains(packetId)) {
boolean isEnding = in.readBoolean();
byte[] data = new byte[packet.length - 7];
in.readFully(data);
byte[] chain = incomming.remove(packetNumber);
if (chain != null) {
ByteBuffer buff = ByteBuffer.allocate(chain.length + data.length);
buff.put(chain);
buff.put(data);
data = buff.array();
}
if (!isEnding) {
incomming.put(packetNumber, data);
return;
}
byte[] finalData = data;
pluginMessageHandlingExecutor.submit(() -> {
try {
ByteArrayDataInput input = ByteStreams.newDataInput(finalData);
switch(packetId) {
case 0x07:
int cooldownType = input.readByte();
switch(cooldownType) {
case 0:
UUID uuid = DataTypeIO.readUUID(input);
long time = input.readLong();
playerCooldownManager.setPlayerUniversalLastTimestamp(uuid, time);
break;
case 1:
uuid = DataTypeIO.readUUID(input);
UUID internalId = DataTypeIO.readUUID(input);
time = input.readLong();
playerCooldownManager.setPlayerPlaceholderLastTimestamp(uuid, internalId, time);
break;
}
for (ServerInfo server : getProxy().getServers().values()) {
if (!server.getSocketAddress().equals(senderServerAddress) && server.getPlayers().size() > 0) {
server.sendData("interchat:main", finalData);
pluginMessagesCounter.incrementAndGet();
}
}
break;
case 0x08:
UUID messageId = DataTypeIO.readUUID(input);
String component = DataTypeIO.readString(input, StandardCharsets.UTF_8);
messageForwardingHandler.receivedProcessedMessage(messageId, component);
break;
case 0x09:
loadConfig();
break;
case 0x0B:
int id = input.readInt();
boolean permissionValue = input.readBoolean();
permissionChecks.put(id, permissionValue);
break;
case 0x0C:
int size1 = input.readInt();
List<ICPlaceholder> list = new ArrayList<>(size1);
for (int i = 0; i < size1; i++) {
boolean isBulitIn = input.readBoolean();
if (isBulitIn) {
String keyword = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String name = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String description = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String permission = DataTypeIO.readString(input, StandardCharsets.UTF_8);
long cooldown = input.readLong();
list.add(new BuiltInPlaceholder(Pattern.compile(keyword), name, description, permission, cooldown));
} else {
int customNo = input.readInt();
ParsePlayer parseplayer = ParsePlayer.fromOrder(input.readByte());
String placeholder = DataTypeIO.readString(input, StandardCharsets.UTF_8);
boolean parseKeyword = input.readBoolean();
long cooldown = input.readLong();
boolean hoverEnabled = input.readBoolean();
String hoverText = DataTypeIO.readString(input, StandardCharsets.UTF_8);
boolean clickEnabled = input.readBoolean();
String clickAction = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String clickValue = DataTypeIO.readString(input, StandardCharsets.UTF_8);
boolean replaceEnabled = input.readBoolean();
String replaceText = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String name = DataTypeIO.readString(input, StandardCharsets.UTF_8);
String description = DataTypeIO.readString(input, StandardCharsets.UTF_8);
list.add(new CustomPlaceholder(customNo, parseplayer, Pattern.compile(placeholder), parseKeyword, cooldown, new CustomPlaceholderHoverEvent(hoverEnabled, hoverText), new CustomPlaceholderClickEvent(clickEnabled, clickEnabled ? ClickEventAction.valueOf(clickAction) : null, clickValue), new CustomPlaceholderReplaceText(replaceEnabled, replaceText), name, description));
}
}
placeholderList.put(senderServer.getInfo().getName(), list);
playerCooldownManager.reloadPlaceholders(placeholderList.values().stream().flatMap(each -> each.stream()).distinct().collect(Collectors.toList()));
PluginMessageSendingBungee.forwardPlaceholderList(list, senderServer.getInfo());
break;
case 0x0D:
UUID uuid2 = DataTypeIO.readUUID(input);
PluginMessageSendingBungee.reloadPlayerData(uuid2, senderServer.getInfo());
break;
case 0x10:
UUID requestUUID = DataTypeIO.readUUID(input);
int requestType = input.readByte();
switch(requestType) {
case 0:
PluginMessageSendingBungee.respondPlayerListRequest(requestUUID, senderServer.getInfo());
break;
default:
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
} else {
pluginMessageHandlingExecutor.submit(() -> {
for (ServerInfo server : getProxy().getServers().values()) {
if (!server.getSocketAddress().equals(senderServerAddress) && server.getPlayers().size() > 0) {
server.sendData("interchat:main", event.getData());
pluginMessagesCounter.incrementAndGet();
}
}
});
}
}
Aggregations