DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
PluginMessageLibrary.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamMessagingChannel;
25
26import com.google.common.io.ByteArrayDataInput;
27import com.google.common.io.ByteStreams;
28import org.bukkit.entity.Player;
29import org.bukkit.plugin.java.JavaPlugin;
30import org.bukkit.plugin.messaging.PluginMessageListener;
31
32import java.io.ByteArrayInputStream;
33import java.io.DataInputStream;
34import java.util.logging.Level;
35
48public abstract class PluginMessageLibrary implements PluginMessageListener {
49
55 public String getChannelName(JavaPlugin plugin) {
57 }
58
72 @Override
73 public final void onPluginMessageReceived(String channel, Player player, byte[] message) {
74 final JavaPlugin plugin = getOwningPlugin();
75 if (plugin == null) return;
76
77 if (!getChannelName(plugin).equals(channel)) return;
78
79 try {
80 final ByteArrayDataInput in = ByteStreams.newDataInput(message);
81 final String sub = in.readUTF(); // e.g., "PlayerCount", "Forward", etc.
82
83 switch (sub) {
85 final String server = in.readUTF();
86 final int count = in.readInt();
87 onPlayerCount(server, count);
88 break;
89 }
90
92 final String subChannel = in.readUTF();
93 final short len = in.readShort();
94 final byte[] payload = new byte[len];
95 in.readFully(payload);
96 onForward(subChannel, payload, player);
97 break;
98 }
99
101 final String subChannel = in.readUTF();
102 final short len = in.readShort();
103 final byte[] payload = new byte[len];
104 in.readFully(payload);
105 onForwardToPlayer(subChannel, payload, player);
106 break;
107 }
108
109 default:
110 onUnknownSubchannel(sub, message, player);
111 break;
112 }
113 } catch (Throwable t) {
114 plugin.getLogger().log(Level.SEVERE, "Error handling plugin message on " + channel, t);
115 }
116 }
117
122 protected abstract JavaPlugin getOwningPlugin();
123
124 /* ===================== Overridable hooks ===================== */
125
139 protected void onPlayerCount(String server, int count) {
140 JavaPlugin plugin = getOwningPlugin();
141 if (plugin != null) {
142 plugin.getLogger().info("PlayerCount for " + server + ": " + count);
143 }
144 }
145
161 protected void onForward(String subChannel, byte[] payload, Player receiver) {
162 // no-op default
163 }
164
171 protected void onForwardToPlayer(String subChannel, byte[] payload, Player receiver) {
172 // no-op default
173 }
174
181 protected void onUnknownSubchannel(String sub, byte[] raw, Player player) {
182 JavaPlugin plugin = getOwningPlugin();
183 if (plugin != null) {
184 plugin.getLogger().fine("Unknown Bungee subchannel: " + sub);
185 }
186 }
187}
Utility class for handling BungeeCord plugin messaging channels in Paper/Spigot.
static final String BUNGEE_CHANNEL
Outer Bungee channel name (constant).
Base listener for BungeeCord plugin messages (channel: "BungeeCord").
void onPlayerCount(String server, int count)
Called when PlayerCount response is received.
abstract JavaPlugin getOwningPlugin()
Provide the owning plugin instance for logging and registration.
String getChannelName(JavaPlugin plugin)
Returns the plugin messaging channel name to listen on.
void onUnknownSubchannel(String sub, byte[] raw, Player player)
Called when an unknown subcommand is encountered.
final void onPluginMessageReceived(String channel, Player player, byte[] message)
Internal dispatcher for plugin messages; do not override.
void onForward(String subChannel, byte[] payload, Player receiver)
Called when a broadcast Forward payload is received.
void onForwardToPlayer(String subChannel, byte[] payload, Player receiver)
Called when a targeted ForwardToPlayer payload is received.