com.velocitypowered.api.proxy.messages.ChannelIdentifier Java Examples

The following examples show how to use com.velocitypowered.api.proxy.messages.ChannelIdentifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginMessageEvent.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param source the source of the plugin message
 * @param target the destination of the plugin message
 * @param identifier the channel for this plugin message
 * @param data the payload of the plugin message
 */
public PluginMessageEvent(ChannelMessageSource source, ChannelMessageSink target,
    ChannelIdentifier identifier, byte[] data) {
  this.source = Preconditions.checkNotNull(source, "source");
  this.target = Preconditions.checkNotNull(target, "target");
  this.identifier = Preconditions.checkNotNull(identifier, "identifier");
  this.data = Preconditions.checkNotNull(data, "data");
  this.result = ForwardResult.forward();
}
 
Example #2
Source File: VelocityRegisteredServer.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) {
  for (ConnectedPlayer player : players) {
    ServerConnection connection = player.getConnectedServer();
    if (connection != null && connection.getServerInfo().equals(serverInfo)) {
      return connection.sendPluginMessage(identifier, data);
    }
  }

  return false;
}
 
Example #3
Source File: ConnectedPlayer.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) {
  Preconditions.checkNotNull(identifier, "identifier");
  Preconditions.checkNotNull(data, "data");
  PluginMessage message = new PluginMessage();
  message.setChannel(identifier.getId());
  message.setData(data);
  minecraftConnection.write(message);
  return true;
}
 
Example #4
Source File: VelocityServerConnection.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public boolean sendPluginMessage(ChannelIdentifier identifier, byte[] data) {
  Preconditions.checkNotNull(identifier, "identifier");
  Preconditions.checkNotNull(data, "data");

  MinecraftConnection mc = ensureConnected();

  PluginMessage message = new PluginMessage();
  message.setChannel(identifier.getId());
  message.setData(data);
  mc.write(message);
  return true;
}
 
Example #5
Source File: VelocityChannelRegistrar.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Returns all legacy channel IDs.
 *
 * @return all legacy channel IDs
 */
public Collection<String> getLegacyChannelIds() {
  Collection<String> ids = new HashSet<>();
  for (ChannelIdentifier value : identifierMap.values()) {
    ids.add(value.getId());
  }
  return ids;
}
 
Example #6
Source File: VelocityChannelRegistrar.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Returns all channel IDs (as strings) for use with Minecraft 1.13 and above.
 *
 * @return the channel IDs for Minecraft 1.13 and above
 */
public Collection<String> getModernChannelIds() {
  Collection<String> ids = new HashSet<>();
  for (ChannelIdentifier value : identifierMap.values()) {
    if (value instanceof MinecraftChannelIdentifier) {
      ids.add(value.getId());
    } else {
      ids.add(PluginMessageUtil.transformLegacyToModernChannel(value.getId()));
    }
  }
  return ids;
}
 
Example #7
Source File: VelocityUtil.java    From NuVotifier with GNU General Public License v3.0 5 votes vote down vote up
public static ChannelIdentifier getId(String channel) {
    if (channel.contains(":")) {
        String[] split = channel.split(":");
        return MinecraftChannelIdentifier.create(split[0], split[1]);
    }
    return new LegacyChannelIdentifier(channel);
}
 
Example #8
Source File: PluginMessageEvent.java    From Velocity with MIT License 4 votes vote down vote up
public ChannelIdentifier getIdentifier() {
  return identifier;
}
 
Example #9
Source File: ClientPlaySessionHandler.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public boolean handle(PluginMessage packet) {
  VelocityServerConnection serverConn = player.getConnectedServer();
  MinecraftConnection backendConn = serverConn != null ? serverConn.getConnection() : null;
  if (serverConn != null && backendConn != null) {
    if (backendConn.getState() != StateRegistry.PLAY) {
      logger.warn("A plugin message was received while the backend server was not "
          + "ready. Channel: {}. Packet discarded.", packet.getChannel());
    } else if (PluginMessageUtil.isRegister(packet)) {
      player.getKnownChannels().addAll(PluginMessageUtil.getChannels(packet));
      backendConn.write(packet);
    } else if (PluginMessageUtil.isUnregister(packet)) {
      player.getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet));
      backendConn.write(packet);
    } else if (PluginMessageUtil.isMcBrand(packet)) {
      backendConn.write(PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()));
    } else {
      if (serverConn.getPhase() == BackendConnectionPhases.IN_TRANSITION) {
        // We must bypass the currently-connected server when forwarding Forge packets.
        VelocityServerConnection inFlight = player.getConnectionInFlight();
        if (inFlight != null) {
          player.getPhase().handle(player, packet, inFlight);
        }
        return true;
      }

      if (!player.getPhase().handle(player, packet, serverConn)) {
        if (!player.getPhase().consideredComplete() || !serverConn.getPhase()
            .consideredComplete()) {
          // The client is trying to send messages too early. This is primarily caused by mods,
          // but further aggravated by Velocity. To work around these issues, we will queue any
          // non-FML handshake messages to be sent once the FML handshake has completed or the
          // JoinGame packet has been received by the proxy, whichever comes first.
          loginPluginMessages.add(packet);
        } else {
          ChannelIdentifier id = server.getChannelRegistrar().getFromId(packet.getChannel());
          if (id == null) {
            backendConn.write(packet);
          } else {
            PluginMessageEvent event = new PluginMessageEvent(player, serverConn, id,
                packet.getData());
            server.getEventManager().fire(event).thenAcceptAsync(pme -> backendConn.write(packet),
                backendConn.eventLoop());
          }
        }
      }
    }
  }

  return true;
}
 
Example #10
Source File: BackendPlaySessionHandler.java    From Velocity with MIT License 4 votes vote down vote up
@Override
public boolean handle(PluginMessage packet) {
  if (!serverConn.getPlayer().canForwardPluginMessage(serverConn.ensureConnected()
      .getProtocolVersion(), packet)) {
    return true;
  }

  // We need to specially handle REGISTER and UNREGISTER packets. Later on, we'll write them to
  // the client.
  if (PluginMessageUtil.isRegister(packet)) {
    serverConn.getPlayer().getKnownChannels().addAll(PluginMessageUtil.getChannels(packet));
    return false;
  } else if (PluginMessageUtil.isUnregister(packet)) {
    serverConn.getPlayer().getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet));
    return false;
  }

  if (PluginMessageUtil.isMcBrand(packet)) {
    PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet,
        server.getVersion());
    playerConnection.write(rewritten);
    return true;
  }

  if (serverConn.getPhase().handle(serverConn, serverConn.getPlayer(), packet)) {
    // Handled.
    return true;
  }

  ChannelIdentifier id = server.getChannelRegistrar().getFromId(packet.getChannel());
  if (id == null) {
    return false;
  }

  PluginMessageEvent event = new PluginMessageEvent(serverConn, serverConn.getPlayer(), id,
      packet.getData());
  server.getEventManager().fire(event)
      .thenAcceptAsync(pme -> {
        if (pme.getResult().isAllowed() && !playerConnection.isClosed()) {
          playerConnection.write(packet);
        }
      }, playerConnection.eventLoop());
  return true;
}
 
Example #11
Source File: VelocityChannelRegistrar.java    From Velocity with MIT License 4 votes vote down vote up
public @Nullable ChannelIdentifier getFromId(String id) {
  return identifierMap.get(id);
}