Actually fix RedisBungee channel for 1.13+

This commit is contained in:
Drc-DEV 2019-10-16 19:33:42 +02:00 committed by filoghost
parent 8c09806c25
commit 7496bf8d4d

View File

@ -3,12 +3,12 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@ -36,48 +36,47 @@ import com.gmail.filoghost.holographicdisplays.util.NMSVersion;
public class BungeeChannel implements PluginMessageListener {
private static BungeeChannel instance;
public static BungeeChannel getInstance() {
if (instance == null) {
instance = new BungeeChannel(HolographicDisplays.getInstance());
}
return instance;
}
private BungeeChannel(Plugin plugin) {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "legacy:redisbungee");
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "legacy:redisbungee", this);
} else {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "RedisBungee");
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, "RedisBungee", this);
}
String targetChannel = "RedisBungee";
if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee";
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, targetChannel);
Bukkit.getMessenger().registerIncomingPluginChannel(plugin, targetChannel, this);
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord";
if (targetChannel.equalsIgnoreCase("RedisBungee") && NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee";
if (channel.equals(targetChannel)) {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
try {
String subChannel = in.readUTF();
if (subChannel.equals("PlayerCount")) {
String server = in.readUTF();
if (in.available() > 0) {
int online = in.readInt();
BungeeServerInfo serverInfo = BungeeServerTracker.getOrCreateServerInfo(server);
serverInfo.setOnlinePlayers(online);
}
}
} catch (EOFException e) {
// Do nothing.
} catch (IOException e) {
@ -86,8 +85,8 @@ public class BungeeChannel implements PluginMessageListener {
}
}
}
public void askPlayerCount(String server) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(b);
@ -103,7 +102,9 @@ public class BungeeChannel implements PluginMessageListener {
// OR, if you don't need to send it to a specific player
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
if (players.size() > 0) {
players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord", b.toByteArray());
String targetChannel = Configuration.useRedisBungee ? "RedisBungee" : "BungeeCord";
if (targetChannel.equalsIgnoreCase("RedisBungee") && NMSVersion.isGreaterEqualThan(NMSVersion.v1_13_R1)) targetChannel = "legacy:redisbungee";
players.iterator().next().sendPluginMessage(HolographicDisplays.getInstance(), targetChannel, b.toByteArray());
}
}
}