use of com.velocitypowered.api.proxy.messages.ChannelMessageSource in project InteractiveChat by LOOHP.
the class InteractiveChatVelocity method onReceive.
@Subscribe
public void onReceive(PluginMessageEvent event) {
if (!event.getIdentifier().getId().equals("interchat:main")) {
return;
}
ChannelMessageSource source = event.getSource();
if (!(source instanceof ServerConnection)) {
return;
}
event.setResult(ForwardResult.handled());
RegisteredServer server = ((ServerConnection) source).getServer();
String senderServer = server.getServerInfo().getName();
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 (RegisteredServer eachServer : getServer().getAllServers()) {
if (!eachServer.getServerInfo().getName().equals(senderServer) && eachServer.getPlayersConnected().size() > 0) {
eachServer.sendPluginMessage(ICChannelIdentifier.INSTANCE, 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 isBuiltIn = input.readBoolean();
if (isBuiltIn) {
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(server.getServerInfo().getName(), list);
playerCooldownManager.reloadPlaceholders(placeholderList.values().stream().flatMap(each -> each.stream()).distinct().collect(Collectors.toList()));
PluginMessageSendingVelocity.forwardPlaceholderList(list, server);
break;
case 0x0D:
UUID uuid2 = DataTypeIO.readUUID(input);
PluginMessageSendingVelocity.reloadPlayerData(uuid2, server);
break;
case 0x10:
UUID requestUUID = DataTypeIO.readUUID(input);
int requestType = input.readByte();
switch(requestType) {
case 0:
PluginMessageSendingVelocity.respondPlayerListRequest(requestUUID, server);
break;
default:
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
});
} else {
pluginMessageHandlingExecutor.submit(() -> {
for (RegisteredServer eachServer : getServer().getAllServers()) {
if (!eachServer.getServerInfo().getName().equals(senderServer) && eachServer.getPlayersConnected().size() > 0) {
eachServer.sendPluginMessage(ICChannelIdentifier.INSTANCE, event.getData());
pluginMessagesCounter.incrementAndGet();
}
}
});
}
}
use of com.velocitypowered.api.proxy.messages.ChannelMessageSource in project Floodgate by GeyserMC.
the class VelocityPluginMessageUtils method onPluginMessage.
@Subscribe
public void onPluginMessage(PluginMessageEvent event) {
String channelId = event.getIdentifier().getId();
PluginMessageChannel channel = pluginMessageManager.getChannel(channelId);
if (channel == null) {
return;
}
UUID targetUuid = null;
String targetUsername = null;
Identity targetIdentity = Identity.UNKNOWN;
ChannelMessageSink target = event.getTarget();
if (target instanceof Player) {
Player player = (Player) target;
targetUuid = player.getUniqueId();
targetUsername = player.getUsername();
targetIdentity = Identity.PLAYER;
} else if (target instanceof ServerConnection) {
targetIdentity = Identity.SERVER;
}
UUID sourceUuid = null;
String sourceUsername = null;
Identity sourceIdentity = Identity.UNKNOWN;
ChannelMessageSource source = event.getSource();
if (source instanceof Player) {
Player player = (Player) source;
sourceUuid = player.getUniqueId();
sourceUsername = player.getUsername();
sourceIdentity = Identity.PLAYER;
} else if (source instanceof ServerConnection) {
sourceIdentity = Identity.SERVER;
}
Result result = channel.handleProxyCall(event.getData(), targetUuid, targetUsername, targetIdentity, sourceUuid, sourceUsername, sourceIdentity);
event.setResult(result.isAllowed() ? ForwardResult.forward() : ForwardResult.handled());
if (!result.isAllowed() && result.getReason() != null) {
logKick(source, result.getReason());
}
}
Aggregations