com.velocitypowered.api.event.connection.PluginMessageEvent Java Examples

The following examples show how to use com.velocitypowered.api.event.connection.PluginMessageEvent. 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: PluginMessageMessenger.java    From LuckPerms with MIT License 6 votes vote down vote up
@Subscribe
public void onPluginMessage(PluginMessageEvent e) {
    // compare the underlying text representation of the channel
    // the namespaced representation is used by legacy servers too, so we
    // are able to support both. :)
    if (!e.getIdentifier().getId().equals(CHANNEL.getId())) {
        return;
    }

    e.setResult(ForwardResult.handled());

    if (e.getSource() instanceof Player) {
        return;
    }

    ByteArrayDataInput in = e.dataAsDataStream();
    String msg = in.readUTF();

    if (this.consumer.consumeIncomingMessageAsString(msg)) {
        // Forward to other servers
        this.plugin.getBootstrap().getScheduler().executeAsync(() -> dispatchMessage(e.getData()));
    }
}
 
Example #2
Source File: PluginMessenger.java    From TAB with Apache License 2.0 5 votes vote down vote up
@Subscribe
public void on(PluginMessageEvent event){
	if (!event.getIdentifier().getId().equalsIgnoreCase(Shared.CHANNEL_NAME)) return;
	ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
	String subChannel = in.readUTF();
	if (event.getTarget() instanceof Player && subChannel.equalsIgnoreCase("Placeholder")){
		event.setResult(ForwardResult.handled());
		ITabPlayer receiver = Shared.getPlayer(((Player) event.getTarget()).getUniqueId());
		if (receiver == null) return;
		String placeholder = in.readUTF();
		String output = in.readUTF();
		long cpu = in.readLong();
		PlayerPlaceholder pl = (PlayerPlaceholder) Placeholders.getPlaceholder(placeholder); //all bridge placeholders are marked as player
		if (pl != null) {
			pl.lastValue.put(receiver.getName(), output);
			pl.lastValue.put("null", output);
			Set<Refreshable> update = PlaceholderManager.getPlaceholderUsage(pl.getIdentifier());
			Shared.featureCpu.runTask("refreshing", new Runnable() {

				@Override
				public void run() {
					for (Refreshable r : update) {
						long startTime = System.nanoTime();
						r.refresh(receiver, false);
						Shared.featureCpu.addTime(r.getRefreshCPU(), System.nanoTime()-startTime);
					}
				}
			});
			Shared.bukkitBridgePlaceholderCpu.addTime(pl.getIdentifier(), cpu);
		} else {
			Shared.debug("Received output for unknown placeholder " + placeholder);
		}
	}
}
 
Example #3
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 #4
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 #5
Source File: OnlineForwardPluginMessagingForwardingSource.java    From NuVotifier with GNU General Public License v3.0 4 votes vote down vote up
@Subscribe
public void onPluginMessage(PluginMessageEvent e) {
    if (e.getIdentifier().equals(velocityChannelId)) {
        e.setResult(PluginMessageEvent.ForwardResult.handled());
    }
}
 
Example #6
Source File: PluginMessagingForwardingSource.java    From NuVotifier with GNU General Public License v3.0 4 votes vote down vote up
@Subscribe
public void onPluginMessage(PluginMessageEvent e) {
    if (e.getIdentifier().equals(velocityChannelId)) {
        e.setResult(PluginMessageEvent.ForwardResult.handled());
    }
}