Skip to content

Instantly share code, notes, and snippets.

@Xernium
Created January 8, 2021 23:30
Show Gist options
  • Save Xernium/95c9262c5f70b8791557861bbc09be1b to your computer and use it in GitHub Desktop.
Save Xernium/95c9262c5f70b8791557861bbc09be1b to your computer and use it in GitHub Desktop.
Velocity API 1.1.0 Plugin messaging channel example
package com.velocityuser.exampleplugin;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
public class MessageListener {
private final ChannelIdentifier identifier;
public MessageListener(ChannelIdentifier identifier){
this.identifier = identifier;
}
@Subscribe
public void onPluginMessageEvent(PluginMessageEvent event){
// Received plugin message, check channel identifier matches
if(event.getIdentifier().equals(identifier)){
// Since this message was meant for this listener set it to handled
// We do this so the message doesn't get routed through.
event.setResult(PluginMessageEvent.ForwardResult.handled());
// Important! Check the origin of the plugin message
// Depending on what the message does its critical that a player
// is not able to spoof it
if(event.getSource() instanceof Player){
// Most of the time you want to defer the player
// from being able to interact with this handler.
// You could just return from here or if you're feeling
// especially evil you could kick them
}
// Could also instead be
if(event.getSource() instanceof ServerConnection){
// Read the data written to the message
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
// Example:
String message = in.readUTF();
}
}
}
}
package com.velocityuser.exampleplugin;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.messages.ChannelIdentifier;
import com.velocitypowered.api.proxy.messages.ChannelMessageSink;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import org.slf4j.Logger;
@Plugin(
id = "exampleplugin",
name = "ExamplePlugin",
version = "@version@",
description = "Plugin channel example plugin",
authors = {"VelocityContributor"}
)
public class VelocityPlugin {
@Inject
private Logger logger;
@Inject
private ProxyServer proxy;
// The messaging channel you're going to use as namespaced:key
private final ChannelIdentifier customChannel =
MinecraftChannelIdentifier.from("customplugin:mychannel");
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
// Register the custom messaging channel
proxy.getChannelRegistrar().register(customChannel);
// Register an event handler to catch messages for it
proxy.getEventManager().register(this, new MessageListener(customChannel));
}
public void sendExampleMessage(ChannelMessageSink receiver, String message){
// Write whatever you want to send to a buffer
ByteArrayDataOutput buf = ByteStreams.newDataOutput();
buf.writeUTF(message);
// Send it
receiver.sendPluginMessage(customChannel, buf.toByteArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment