|
| 1 | +package com.jaimemartz.playerbalanceraddon; |
| 2 | + |
| 3 | +import com.google.common.collect.ArrayListMultimap; |
| 4 | +import com.google.common.collect.Iterables; |
| 5 | +import com.google.common.collect.LinkedListMultimap; |
| 6 | +import com.google.common.collect.Multimap; |
| 7 | +import com.google.common.io.ByteArrayDataInput; |
| 8 | +import com.google.common.io.ByteArrayDataOutput; |
| 9 | +import com.google.common.io.ByteStreams; |
| 10 | +import org.bukkit.Bukkit; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.plugin.messaging.Messenger; |
| 13 | +import org.bukkit.plugin.messaging.PluginMessageListener; |
| 14 | + |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.DataInputStream; |
| 17 | +import java.util.*; |
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +public class PluginMessageManager implements PluginMessageListener { |
| 21 | + private final Multimap<MessageContext, Consumer<ByteArrayDataInput>> contexts = LinkedListMultimap.create(); |
| 22 | + private final PlayerBalancerAddon plugin; |
| 23 | + |
| 24 | + public PluginMessageManager(PlayerBalancerAddon plugin) { |
| 25 | + this.plugin = plugin; |
| 26 | + |
| 27 | + plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "PlayerBalancer", this); |
| 28 | + plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "PlayerBalancer"); |
| 29 | + |
| 30 | + //In case we need to use BungeeCord channels |
| 31 | + plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this); |
| 32 | + plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onPluginMessageReceived(String channel, Player player, byte[] message) { |
| 37 | + if (channel.equals("PlayerBalancer")) { |
| 38 | + ByteArrayDataInput in = ByteStreams.newDataInput(message); |
| 39 | + String subchannel = in.readUTF(); |
| 40 | + |
| 41 | + contexts.get(new MessageContext(channel, subchannel, player)) |
| 42 | + .stream().findFirst().ifPresent(a -> a.accept(in)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void connectPlayer(Player player, String section) { |
| 47 | + ByteArrayDataOutput out = ByteStreams.newDataOutput(); |
| 48 | + out.writeUTF("Connect"); |
| 49 | + out.writeUTF(section); |
| 50 | + player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray()); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean getSectionByName(String section, Consumer<String> consumer) { |
| 54 | + Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null); |
| 55 | + if (player == null) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + ByteArrayDataOutput out = ByteStreams.newDataOutput(); |
| 60 | + out.writeUTF("GetSectionByName"); |
| 61 | + out.writeUTF(section); |
| 62 | + player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray()); |
| 63 | + |
| 64 | + contexts.put(new MessageContext( |
| 65 | + "PlayerBalancer", |
| 66 | + "GetSectionByName", |
| 67 | + player |
| 68 | + ), ByteArrayDataInput::readUTF); |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean getSectionByServer(String server, Consumer<String> consumer) { |
| 74 | + Player player = Iterables.getFirst(plugin.getServer().getOnlinePlayers(), null); |
| 75 | + if (player == null) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + ByteArrayDataOutput out = ByteStreams.newDataOutput(); |
| 80 | + out.writeUTF("GetSectionByServer"); |
| 81 | + out.writeUTF(server); |
| 82 | + player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray()); |
| 83 | + |
| 84 | + contexts.put(new MessageContext( |
| 85 | + "PlayerBalancer", |
| 86 | + "GetSectionByServer", |
| 87 | + player |
| 88 | + ), ByteArrayDataInput::readUTF); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + public void getSectionOfPlayer(Player player, Consumer<String> consumer) { |
| 93 | + ByteArrayDataOutput out = ByteStreams.newDataOutput(); |
| 94 | + out.writeUTF("GetSectionOfPlayer"); |
| 95 | + out.writeUTF(player.getName()); |
| 96 | + player.sendPluginMessage(plugin, "PlayerBalancer", out.toByteArray()); |
| 97 | + |
| 98 | + contexts.put(new MessageContext( |
| 99 | + "PlayerBalancer", |
| 100 | + "GetSectionOfPlayer", |
| 101 | + player |
| 102 | + ), ByteArrayDataInput::readUTF); |
| 103 | + } |
| 104 | + |
| 105 | + private final class MessageContext { |
| 106 | + private final String channel; |
| 107 | + private final String subchannel; |
| 108 | + private final Player player; |
| 109 | + |
| 110 | + public MessageContext(String channel, String subchannel, Player player) { |
| 111 | + this.channel = channel; |
| 112 | + this.subchannel = subchannel; |
| 113 | + this.player = player; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean equals(Object o) { |
| 118 | + if (this == o) return true; |
| 119 | + if (o == null || getClass() != o.getClass()) return false; |
| 120 | + |
| 121 | + MessageContext that = (MessageContext) o; |
| 122 | + |
| 123 | + if (channel != null ? !channel.equals(that.channel) : that.channel != null) return false; |
| 124 | + if (subchannel != null ? !subchannel.equals(that.subchannel) : that.subchannel != null) return false; |
| 125 | + return player != null ? player.equals(that.player) : that.player == null; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int hashCode() { |
| 130 | + int result = channel != null ? channel.hashCode() : 0; |
| 131 | + result = 31 * result + (subchannel != null ? subchannel.hashCode() : 0); |
| 132 | + result = 31 * result + (player != null ? player.hashCode() : 0); |
| 133 | + return result; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments