use of com.velocitypowered.proxy.protocol.packet.Chat in project InteractiveChat by LOOHP.
the class InteractiveChatVelocity method onServerConnected.
@Subscribe
public void onServerConnected(ServerPostConnectEvent event) {
Player player = event.getPlayer();
RegisteredServer to = player.getCurrentServer().get().getServer();
UUID uuid = player.getUniqueId();
if (!placeholderList.containsKey(to.getServerInfo().getName())) {
try {
PluginMessageSendingVelocity.requestPlaceholderList(to);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
PluginMessageSendingVelocity.sendPlayerListData();
} catch (IOException e1) {
e1.printStackTrace();
}
long universalTime = playerCooldownManager.getPlayerUniversalLastTimestamp(uuid);
if (universalTime >= 0) {
try {
PluginMessageSendingVelocity.sendPlayerUniversalCooldown(to, uuid, universalTime);
} catch (IOException e) {
e.printStackTrace();
}
}
List<ICPlaceholder> placeholders = placeholderList.get(to.getServerInfo().getName());
if (placeholders != null) {
for (ICPlaceholder placeholder : placeholders) {
long placeholderTime = playerCooldownManager.getPlayerPlaceholderLastTimestamp(uuid, placeholder.getInternalId());
if (placeholderTime >= 0) {
try {
PluginMessageSendingVelocity.sendPlayerPlaceholderCooldown(to, uuid, placeholder, placeholderTime);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
proxyServer.getScheduler().buildTask(plugin, () -> {
try {
PluginMessageSendingVelocity.sendDelayAndScheme();
} catch (IOException e) {
e.printStackTrace();
}
}).schedule();
proxyServer.getScheduler().buildTask(plugin, () -> {
if (event.getPlayer().getUsername().equals("LOOHP") || event.getPlayer().getUsername().equals("AppLEskakE")) {
sendMessage(event.getPlayer(), Component.text(TextColor.GOLD + "InteractiveChat (Velocity) " + getDescription().getVersion() + " is running!"));
}
}).delay(100, TimeUnit.MILLISECONDS).schedule();
VelocityServerConnection serverConnection = ((ConnectedPlayer) event.getPlayer()).getConnectedServer();
ChannelPipeline pipeline = serverConnection.ensureConnected().getChannel().pipeline();
pipeline.addBefore(Connections.HANDLER, "interactivechat_interceptor", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) throws Exception {
try {
if (obj instanceof Chat) {
Chat packet = (Chat) obj;
UUID uuid = player.getUniqueId();
String message = packet.getMessage();
byte position = packet.getType();
if ((position == 0 || position == 1) && uuid != null && message != null) {
Map<String, Long> list = forwardedMessages.get(uuid);
if (list != null) {
list.put(message, System.currentTimeMillis());
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
super.write(channelHandlerContext, obj, channelPromise);
}
});
}
use of com.velocitypowered.proxy.protocol.packet.Chat in project InteractiveChat by LOOHP.
the class InteractiveChatVelocity method onProxyInitialization.
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
plugin = this;
try {
JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("velocity-plugin.json"), StandardCharsets.UTF_8));
description = new VelocityPluginDescription(json);
} catch (IOException | ParseException e1) {
e1.printStackTrace();
}
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}
try {
Config.loadConfig(CONFIG_ID, new File(getDataFolder(), "bungeeconfig.yml"), getClass().getClassLoader().getResourceAsStream("config_proxy.yml"), getClass().getClassLoader().getResourceAsStream("config_proxy.yml"), true);
} catch (IOException e) {
e.printStackTrace();
return;
}
loadConfig();
CommandsVelocity.createBrigadierCommand();
proxyServer.getChannelRegistrar().register(ICChannelIdentifier.INSTANCE);
getLogger().info(TextColor.GREEN + "[InteractiveChat] Registered Plugin Messaging Channels!");
Metrics metrics = metricsFactory.make(this, BSTATS_PLUGIN_ID);
Charts.setup(metrics);
playerCooldownManager = new ProxyPlayerCooldownManager(placeholderList.values().stream().flatMap(each -> each.stream()).distinct().collect(Collectors.toList()));
messageForwardingHandler = new ProxyMessageForwardingHandler((info, component) -> {
Player player = proxyServer.getPlayer(info.getPlayer()).get();
ServerConnection server = player.getCurrentServer().get();
proxyServer.getScheduler().buildTask(plugin, () -> {
try {
if (player != null && server != null) {
PluginMessageSendingVelocity.requestMessageProcess(player, server.getServer(), component, info.getId());
}
} catch (IOException e) {
e.printStackTrace();
}
}).delay(delay + 50, TimeUnit.MILLISECONDS).schedule();
}, (info, component) -> {
Chat chatPacket = new Chat(component + "<QUxSRUFEWVBST0NFU1NFRA==>", info.getPosition(), null);
Optional<Player> optplayer = getServer().getPlayer(info.getPlayer());
if (optplayer.isPresent()) {
ConnectedPlayer userConnection = (ConnectedPlayer) optplayer.get();
userConnection.getConnection().getChannel().write(chatPacket);
}
}, uuid -> {
return proxyServer.getPlayer(uuid).isPresent();
}, uuid -> {
Optional<ServerConnection> optCurrentServer = proxyServer.getPlayer(uuid).get().getCurrentServer();
return optCurrentServer.isPresent() && hasInteractiveChat(optCurrentServer.get().getServer());
}, () -> (long) delay + 2000);
ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("InteractiveChatProxy Async PluginMessage Processing Thread #%d").build();
pluginMessageHandlingExecutor = new ThreadPoolExecutor(8, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(true), factory);
getLogger().info(TextColor.GREEN + "[InteractiveChat] InteractiveChat (Velocity) has been enabled!");
run();
}
use of com.velocitypowered.proxy.protocol.packet.Chat in project InteractiveChat by LOOHP.
the class InteractiveChatVelocity method onPlayerConnected.
@Subscribe
public void onPlayerConnected(PostLoginEvent event) {
if (!filtersAdded) {
addFilters();
}
Player player = event.getPlayer();
forwardedMessages.put(player.getUniqueId(), new ConcurrentHashMap<>());
if (player.hasPermission("interactivechat.backendinfo")) {
String proxyVersion = getDescription().getVersion();
for (BackendInteractiveChatData data : serverInteractiveChatInfo.values()) {
if (data.isOnline() && data.getProtocolVersion() != Registry.PLUGIN_MESSAGING_PROTOCOL_VERSION) {
String msg = TextColor.RED + "[InteractiveChat] Warning: Backend Server " + data.getServer() + " is not running a version of InteractiveChat which has the same plugin messaging protocol version as the proxy!";
HoverEvent<Component> hoverComponent = Component.text(TextColor.YELLOW + "Proxy Version: " + proxyVersion + " (" + Registry.PLUGIN_MESSAGING_PROTOCOL_VERSION + ")\n" + TextColor.RED + data.getServer() + " Version: " + data.getVersion() + " (" + data.getProtocolVersion() + ")").asHoverEvent();
TextComponent text = Component.text(msg).hoverEvent(hoverComponent);
sendMessage(player, text);
sendMessage(getServer().getConsoleCommandSource(), text);
}
}
}
ConnectedPlayer userConnection = (ConnectedPlayer) player;
ChannelPipeline pipeline = userConnection.getConnection().getChannel().pipeline();
pipeline.addBefore(Connections.HANDLER, "interactivechat_interceptor", new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext channelHandlerContext, Object obj, ChannelPromise channelPromise) throws Exception {
try {
if (obj instanceof Chat) {
Chat packet = (Chat) obj;
String message = packet.getMessage();
byte position = packet.getType();
if ((position == 0 || position == 1) && message != null) {
if (message.contains("<QUxSRUFEWVBST0NFU1NFRA==>")) {
message = message.replace("<QUxSRUFEWVBST0NFU1NFRA==>", "");
if (Registry.ID_PATTERN.matcher(message).find()) {
message = Registry.ID_PATTERN.matcher(message).replaceAll("").trim();
}
packet.setMessage(message);
} else if (player.getCurrentServer().isPresent() && hasInteractiveChat(player.getCurrentServer().get().getServer())) {
messageForwardingHandler.processMessage(player.getUniqueId(), message, position);
return;
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
super.write(channelHandlerContext, obj, channelPromise);
}
});
}
Aggregations