-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBungeeMessageReceiver.java
More file actions
60 lines (44 loc) · 2.26 KB
/
BungeeMessageReceiver.java
File metadata and controls
60 lines (44 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package at.helpch.chatchat.cs.receiver;
import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.processor.RemoteToLocalMessageProcessor;
import at.helpch.chatchat.util.Constants;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.logging.Level;
import static at.helpch.chatchat.util.Constants.BUNGEE_CROSS_SERVER_CHANNEL;
import static at.helpch.chatchat.util.Constants.CROSS_SERVER_SUB_CHANNEL;
public class BungeeMessageReceiver implements RemoteMessageReceiver {
private final @NotNull ChatChatPlugin plugin;
public BungeeMessageReceiver(final @NotNull ChatChatPlugin plugin) {
this.plugin = plugin;
}
@Override
public void onPluginMessageReceived(final @NotNull String channel, @NotNull Player player, final byte[] message) {
if (!channel.equals(BUNGEE_CROSS_SERVER_CHANNEL)) return;
final ByteArrayDataInput in = ByteStreams.newDataInput(message);
final String subChannel = in.readUTF();
if (!subChannel.equals(CROSS_SERVER_SUB_CHANNEL)) return;
final short length = in.readShort();
final byte[] messageBytes = new byte[length];
in.readFully(messageBytes);
final DataInputStream messageIn = new DataInputStream(new ByteArrayInputStream(messageBytes));
try {
final String messageType = messageIn.readUTF();
if (!messageType.equals(Constants.PUBLIC_MESSAGE_TYPE) && !messageType.equals(Constants.PRIVATE_MESSAGE_TYPE)) {
plugin.getLogger().warning("Got cross server message but the message type was invalid: " + messageType);
return;
}
// TODO: Add private message support
final String channelName = messageIn.readUTF();
final String messageContent = messageIn.readUTF();
RemoteToLocalMessageProcessor.processRemoteMessageEvent(plugin, channelName, messageContent);
} catch (final IOException exception) {
plugin.getLogger().log(Level.WARNING, "Got cross server message but could not read it.", exception);
}
}
}