mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
91 lines
3.2 KiB
Java
91 lines
3.2 KiB
Java
package me.william278.huskhomes2;
|
|
|
|
import com.google.common.io.ByteArrayDataInput;
|
|
import com.google.common.io.ByteArrayDataOutput;
|
|
import com.google.common.io.ByteStreams;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.plugin.messaging.PluginMessageListener;
|
|
|
|
import java.io.*;
|
|
|
|
public class pluginMessageHandler implements PluginMessageListener {
|
|
|
|
private static final Main plugin = Main.getInstance();
|
|
|
|
// Move a player to a different server in the bungee network
|
|
public static void sendPlayer(Player p, String targetServer) {
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
out.writeUTF("Connect");
|
|
out.writeUTF(targetServer);
|
|
p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
|
}
|
|
|
|
// Send a plugin message
|
|
public static void sendPluginMessage(Player sender, String targetPlayerName, String messageType, String messageData) {
|
|
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
|
|
|
// Send a plugin message to the specified player name
|
|
out.writeUTF("ForwardToPlayer");
|
|
out.writeUTF(targetPlayerName);
|
|
|
|
// Send the HuskHomes message with a specific type
|
|
out.writeUTF("HuskHomes:" + messageType);
|
|
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
|
|
DataOutputStream messageOut = new DataOutputStream(messageBytes);
|
|
|
|
// Send the message data; output an exception if there's an error
|
|
try {
|
|
messageOut.writeUTF(messageData);
|
|
} catch (IOException e) {
|
|
Bukkit.getLogger().warning("An error occurred trying to send a plugin message (" + e.getCause() + ")");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
// Write the messages to the output packet
|
|
out.writeShort(messageBytes.toByteArray().length);
|
|
out.write(messageBytes.toByteArray());
|
|
|
|
// Send the constructed plugin message packet
|
|
sender.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
|
}
|
|
|
|
// When a plugin message is received
|
|
@Override
|
|
public void onPluginMessageReceived(String channel, Player p, byte[] message) {
|
|
// Return if the message is not a Bungee message
|
|
if (!channel.equals("BungeeCord")) {
|
|
return;
|
|
}
|
|
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
|
String messageType = input.readUTF();
|
|
|
|
// Return if the message was not sent by HuskHomes
|
|
if (!messageType.contains("HuskHomes:")) {
|
|
return;
|
|
}
|
|
|
|
// Get the HuskHomes message type
|
|
messageType = messageType.split(":")[1];
|
|
|
|
// Get the message data packets
|
|
String messageData;
|
|
short messageLength = input.readShort();
|
|
byte[] messageBytes = new byte[messageLength];
|
|
input.readFully(messageBytes);
|
|
DataInputStream messageIn = new DataInputStream(new ByteArrayInputStream(messageBytes));
|
|
|
|
// Get the message data string from the packets received
|
|
try {
|
|
messageData = messageIn.readUTF();
|
|
} catch (IOException e) {
|
|
Bukkit.getLogger().warning("An error occurred trying to read a plugin message (" + e.getCause() + ")");
|
|
e.printStackTrace();
|
|
}
|
|
|
|
/*switch (messageType) {
|
|
|
|
}*/
|
|
}
|
|
}
|