Java源码示例:com.velocitypowered.api.proxy.messages.ChannelIdentifier

示例1
/**
 * 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();
}
 
示例2
@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;
}
 
示例3
@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;
}
 
示例4
@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;
}
 
示例5
/**
 * 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;
}
 
示例6
/**
 * 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;
}
 
示例7
public static ChannelIdentifier getId(String channel) {
    if (channel.contains(":")) {
        String[] split = channel.split(":");
        return MinecraftChannelIdentifier.create(split[0], split[1]);
    }
    return new LegacyChannelIdentifier(channel);
}
 
示例8
public ChannelIdentifier getIdentifier() {
  return identifier;
}
 
示例9
@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;
}
 
示例10
@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;
}
 
示例11
public @Nullable ChannelIdentifier getFromId(String id) {
  return identifierMap.get(id);
}