Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.31 KB | None | 0 0
  1. import java.io.ByteArrayOutputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.UUID;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.plugin.Plugin;
  14. import org.bukkit.plugin.messaging.PluginMessageListener;
  15.  
  16. import com.google.common.collect.Sets;
  17. import com.google.common.io.ByteArrayDataInput;
  18. import com.google.common.io.ByteArrayDataOutput;
  19. import com.google.common.io.ByteStreams;
  20.  
  21. /**
  22. *
  23. * @author Justis
  24. * Bungee wrapper for handling incoming and outgoing bungee messages, because plugin messages are a pain in the arse I refuse to work with them without a wrapper thank you. xD
  25. *
  26. */
  27. public class BungeeWrapper implements PluginMessageListener {
  28.  
  29. // The instance for this class, because there should only ever be one.
  30. private static BungeeWrapper com;
  31. private Plugin plugin;
  32.  
  33. /**
  34. * All of the below maps, sets, and strings will remain without data until you send a plugin message to update them.
  35. * This is to reduce overhead, and running plugin messages that don't need to be run.
  36. */
  37. // This server's server name as defined in the bungee config.
  38. private String serverName;
  39. // Set of servers.
  40. private Set<String> servers = new HashSet<>();
  41. // Map of player uuid's for the Key, and their IP for the value.
  42. private Map<UUID, String> playerIps = new HashMap<>();
  43. // Map of server names for the Key, and player count for the value.
  44. private Map<String, Integer> playerCount = new HashMap<>();
  45. // Map of server names for the Key, and player names for the value.
  46. private Map<String, String[]> playerList = new HashMap<>();
  47. // Map of server names for the Key, and the server IP for the value.
  48. private Map<String, String> serverIp = new HashMap<>();
  49. // Map of player names for the Key, and player UUIDs for the value.
  50. private Map<String, UUID> playerIds = new HashMap<>();
  51.  
  52. /**
  53. * Initiate the bungee com, and register channels.
  54. */
  55. public BungeeWrapper(Plugin plugin) {
  56. com = this;
  57. this.plugin = plugin;
  58. plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this);
  59. plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
  60. }
  61.  
  62. /**
  63. * Receive method, determines the incoming message and runs the appropriate updating method as a result.
  64. */
  65. public void onPluginMessageReceived(String channel, Player player, byte[] message) {
  66. if (!channel.equals("BungeeCord")) return;
  67. ByteArrayDataInput in = ByteStreams.newDataInput(message);
  68. String subchannel = in.readUTF();
  69. switch (Channel.fromString(subchannel)) {
  70. case GET_SERVER:
  71. receiveServerName(in.readUTF());
  72. break;
  73. case GET_SERVERS:
  74. receiveServers(in.readUTF().split(", "));
  75. break;
  76. case IP:
  77. receiveIP(player, in.readUTF(), in.readInt());
  78. break;
  79. case PLAYER_COUNT:
  80. receivePlayerCount(in.readUTF(), in.readInt());
  81. break;
  82. case PLAYER_LIST:
  83. receivePlayerList(in.readUTF(), in.readUTF().split(", "));
  84. break;
  85. case SERVER_IP:
  86. receiveServerIP(in.readUTF(), in.readUTF(), in.readShort());
  87. break;
  88. case UUID:
  89. receivePlayerUUID(player.getName(), in.readUTF());
  90. break;
  91. case UUID_OTHER:
  92. receivePlayerUUID(in.readUTF(), in.readUTF());
  93. break;
  94. default:
  95. byte[] data = new byte[in.readShort()];
  96. in.readFully(data);
  97. receiveForward(player, subchannel, new String(data).substring(2));
  98. }
  99. }
  100.  
  101. /**
  102. * Update server name
  103. * @param name to update it to.
  104. */
  105. private void receiveServerName(String name) {
  106. com.serverName = name;
  107. }
  108.  
  109. /**
  110. * @return server name
  111. */
  112. public static String getServerName() {
  113. return com.serverName;
  114. }
  115.  
  116. /**
  117. * Update server set
  118. * @param servers to update it to.
  119. */
  120. private void receiveServers(String[] servers) {
  121. com.servers = Sets.newHashSet(servers);
  122. }
  123.  
  124. /**
  125. * @return the server set.
  126. */
  127. public static Set<String> getServers() {
  128. return Collections.unmodifiableSet(com.servers);
  129. }
  130.  
  131. /**
  132. * Add/update a player to the ip list.
  133. * @param player
  134. * @param ip
  135. * @param port
  136. */
  137. private void receiveIP(Player player, String ip, int port) {
  138. com.playerIps.put(player.getUniqueId(), ip + ":" + port);
  139. }
  140.  
  141. /**
  142. * Get the ip of a player
  143. * @param name of the player
  144. * @return player's ip.
  145. * Null if player's ip has not been gathered.
  146. */
  147. public static String getPlayerIp(String name) {
  148. return (String) com.playerIps.get(name);
  149. }
  150.  
  151. /**
  152. * Update a server's player count.
  153. * @param server
  154. * @param count
  155. */
  156. private void receivePlayerCount(String server, int count) {
  157. com.playerCount.put(server, Integer.valueOf(count));
  158. }
  159.  
  160. /**
  161. * @param server
  162. * @return player count for given server.
  163. */
  164. public static int getPlayerCount(String server) {
  165. return ((Integer) com.playerCount.get(server)).intValue();
  166. }
  167.  
  168. /**
  169. * Receive list of player names for a given server
  170. * @param server
  171. * @param playerList
  172. */
  173. private void receivePlayerList(String server, String[] playerList) {
  174. com.playerList.put(server, playerList);
  175. }
  176.  
  177. /**
  178. * @param server to get player names from
  179. * @return list of player names for the given server.
  180. */
  181. public static String[] getPlayerList(String server) {
  182. return (String[]) com.playerList.get(server);
  183. }
  184.  
  185. /**
  186. * Update ip for the given server
  187. * @param server
  188. * @param ip
  189. * @param port
  190. */
  191. private void receiveServerIP(String server, String ip, short port) {
  192. com.serverIp.put(server, ip + ":" + port);
  193. }
  194.  
  195. /**
  196. * Update the UUID for a given player
  197. * @param player
  198. * @param id
  199. */
  200. private void receivePlayerUUID(String player, String id) {
  201. com.playerIds.put(player, UUID.fromString(id));
  202. }
  203.  
  204. /**
  205. * Do whatever you want here.
  206. *
  207. * @param player May be relevant if the forward was to that specific player
  208. * @param subChannel Sub channel name.
  209. * @param message The message received.
  210. *
  211. * TODO: Fill this in with stuff I wanna do. :P
  212. */
  213. private void receiveForward(Player player, String subChannel, String message) {
  214.  
  215. }
  216.  
  217. /**
  218. * Send a player to another server
  219. * @param player to send
  220. * @param server to send the player to
  221. */
  222. public static void sendConnect(Player player, String server) {
  223. out(Channel.CONNECT, player, server);
  224. }
  225.  
  226. /**
  227. * Send another player to a server
  228. * @param player to connect
  229. * @param server to connect the player to
  230. */
  231. public static void sendConnectOther(String player, String server) {
  232. out(Channel.CONNECT_OTHER, null, player, server);
  233. }
  234.  
  235. /**
  236. * Send for the player's ip
  237. * @param player who's ip to get
  238. */
  239. public static void sendIP(Player player) {
  240. out(Channel.IP, player);
  241. }
  242.  
  243. /**
  244. * Send for the player count to this server
  245. * @param server to get the player count from
  246. */
  247. public static void sendPlayerCount(String server) {
  248. out(Channel.PLAYER_COUNT, null, server);
  249. }
  250.  
  251. /**
  252. * Send for the total player count to all the servers
  253. */
  254. public static void sendPlayerCountAll() {
  255. out(Channel.PLAYER_COUNT, null, "ALL");
  256. }
  257.  
  258. /**
  259. * Send for the player list to a specific server
  260. * @param server to get the player list from
  261. */
  262. public static void sendPlayerList(String server) {
  263. out(Channel.PLAYER_LIST, null, server);
  264. }
  265.  
  266. /**
  267. * Send for player list from all the servers
  268. */
  269. public static void sendPlayerListAll() {
  270. out(Channel.PLAYER_LIST, null, "ALL");
  271. }
  272.  
  273. /**
  274. * Send for server list
  275. */
  276. public static void sendGetServers() {
  277. out(Channel.GET_SERVERS, null);
  278. }
  279.  
  280. /**
  281. * Send a player a message
  282. * @param player
  283. * @param message
  284. */
  285. public static void sendMessage(String player, String message) {
  286. out(Channel.MESSAGE, null, player, message);
  287. }
  288.  
  289. /**
  290. * Send for the server name
  291. */
  292. public static void sendGetServer() {
  293. out(Channel.GET_SERVER, null);
  294. }
  295.  
  296. /**
  297. * Send a specific server a message on a specific subchannel
  298. * Remember, the sending and receiving server(s) need to have a player online
  299. * @param server
  300. * @param subChannel
  301. * @param message
  302. */
  303. public static void sendForward(String server, String subChannel, String message) {
  304. out(Channel.FORWARD, null, server, subChannel, message);
  305. }
  306.  
  307. /**
  308. * Send a message on a specific subchannel to all online servers exept for the one sending the message
  309. * Remember, the sending and receiving server(s) need to have a player online
  310. * @param subChannel
  311. * @param message
  312. */
  313. public static void sendForwardOnline(String subChannel, String message) {
  314. out(Channel.FORWARD, null, "ONLINE", subChannel, message);
  315. }
  316.  
  317. /**
  318. * Send a message on a specific subchannel to all servers exept for the one sending the message
  319. * Remember, the sending and receiving server(s) need to have a player online
  320. * @param subChannel
  321. * @param message
  322. */
  323. public static void sendForwardAll(String subChannel, String message) {
  324. out(Channel.FORWARD, null, "ALL", subChannel, message);
  325. }
  326.  
  327. /**
  328. * Send a message on a specific subchannel to the server with a specific player
  329. * Remember, the sending and receiving server(s) need to have a player online
  330. * @param player
  331. * @param subChannel
  332. * @param message
  333. */
  334. public static void sendForwardToPlayer(String player, String subChannel, String message) {
  335. out(Channel.FORWARD_TO_PLAYER, null, player, subChannel, message);
  336. }
  337.  
  338. /**
  339. * Send for the uuid of a specific player
  340. * @param player
  341. */
  342. public static void sendUUID(Player player) {
  343. out(Channel.UUID, player);
  344. }
  345.  
  346. /**
  347. * Send for the UUID of a specific player
  348. * @param player
  349. */
  350. public static void sendUUIDOther(String player) {
  351. out(Channel.UUID_OTHER, null, player);
  352. }
  353.  
  354. /**
  355. * Send for the server ip of a specific server
  356. * @param server
  357. */
  358. public static void sendServerIP(String server) {
  359. out(Channel.SERVER_IP, null, server);
  360. }
  361.  
  362. /**
  363. * Kick a player for a specific reason.
  364. * @param player
  365. * @param reason
  366. */
  367. public static void sendKickPlayer(String player, String reason) {
  368. out(Channel.KICK_PLAYER, null, player, reason);
  369. }
  370.  
  371. /**
  372. * Handles the outgoing messages.
  373. * @param subChannel Subchannel of the message, if there is one.
  374. * @param p player to send the message to.
  375. * @param args
  376. */
  377. private static void out(Channel subChannel, Player p, String... args) {
  378. ByteArrayDataOutput out = ByteStreams.newDataOutput();
  379. out.writeUTF(subChannel.toString());
  380. if ((subChannel.equals(Channel.FORWARD)) || (subChannel.equals(Channel.FORWARD_TO_PLAYER))) {
  381. out.writeUTF(args[0]);
  382. out.writeUTF(args[1]);
  383. try {
  384. ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
  385. new DataOutputStream(msgbytes).writeUTF(args[2]);
  386. out.writeShort(msgbytes.toByteArray().length);
  387. out.write(msgbytes.toByteArray());
  388. } catch (IOException e) {
  389. e.printStackTrace();
  390. }
  391. } else {
  392. for (String arg : args)
  393. out.writeUTF(arg);
  394. }
  395. if (p == null)
  396. p = (Player) Bukkit.getOnlinePlayers().iterator().next();
  397. p.sendPluginMessage(com.plugin, "BungeeCord", out.toByteArray());
  398. }
  399.  
  400. /**
  401. *
  402. * @author Justis
  403. * Enum for all of the bungee channels.
  404. */
  405. static enum Channel {
  406.  
  407. CONNECT("Connect"),
  408.  
  409. CONNECT_OTHER("ConnectOther"),
  410.  
  411. IP("IP"),
  412.  
  413. PLAYER_COUNT("PlayerCount"),
  414.  
  415. PLAYER_LIST("PlayerList"),
  416.  
  417. GET_SERVERS("GetServers"),
  418.  
  419. MESSAGE("Message"),
  420.  
  421. GET_SERVER("GetServer"),
  422.  
  423. FORWARD("Forward"),
  424.  
  425. FORWARD_TO_PLAYER("ForwardToPlayer"),
  426.  
  427. UUID("UUID"),
  428.  
  429. UUID_OTHER("UUIDOther"),
  430.  
  431. SERVER_IP("ServerIP"),
  432.  
  433. KICK_PLAYER("KickPlayer"),
  434.  
  435. OTHER("");
  436.  
  437. private String string;
  438.  
  439. /**
  440. * You better know how an enum works.... -.-
  441. * @param string
  442. */
  443. private Channel(String string) {
  444. this.string = string;
  445. }
  446.  
  447. /**
  448. * Convert the channel to it's string value.
  449. */
  450. public String toString() {
  451. return this.string;
  452. }
  453.  
  454. /**
  455. * Get the enum value from a string
  456. * @param string to get the enum value from.
  457. * @return enum value of the string
  458. * OTHER if it is empty or does not match any of bungee's channels.
  459. */
  460. public static Channel fromString(String string) {
  461. for (Channel ch : values()) {
  462. if (ch.toString().equalsIgnoreCase(string))
  463. return ch;
  464. }
  465. return OTHER;
  466. }
  467. }
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement