-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBungeeMessageSender.java
More file actions
51 lines (40 loc) · 1.75 KB
/
BungeeMessageSender.java
File metadata and controls
51 lines (40 loc) · 1.75 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
package at.helpch.chatchat.cs.sender;
import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.util.Constants;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
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 BungeeMessageSender implements RemoteMessageSender {
private final @NotNull ChatChatPlugin plugin;
public BungeeMessageSender(final @NotNull ChatChatPlugin plugin) {
this.plugin = plugin;
}
@Override
public boolean send(String channel, String message) {
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("Forward");
out.writeUTF("ALL");
out.writeUTF(CROSS_SERVER_SUB_CHANNEL);
final ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
final DataOutputStream messageOut = new DataOutputStream(messageBytes);
try {
messageOut.writeUTF(Constants.PUBLIC_MESSAGE_TYPE);
messageOut.writeUTF(channel);
messageOut.writeUTF(message);
} catch (final IOException exception){
plugin.getLogger().log(Level.WARNING, "Could not write cross server message.", exception);
return false;
}
out.writeShort(messageBytes.toByteArray().length);
out.write(messageBytes.toByteArray());
Bukkit.getServer().sendPluginMessage(plugin, BUNGEE_CROSS_SERVER_CHANNEL, out.toByteArray());
return true;
}
}