Hey guys so I am trying to make placeholders in my plugin that would grab the amount of players that are online on the specified server. I understand how to send the data output however receiving the data and trying to make it a public variable is where I'm having trouble. Heres what I currently have for receiving the data input: Code (Text): public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (!channel.equals("BungeeCord")) { return; } ByteArrayDataInput in = ByteStreams.newDataInput(message); ByteArrayDataOutput out = ByteStreams.newDataOutput(); String subchannel = in.readUTF(); if (subchannel.equals("ServerList")) { String[] serverList = in.readUTF().split(", "); for(String server : serverList){ out.writeUTF("ServerPlayerCount"); out.writeUTF("PlayerCount"); out.writeUTF(server); } } if (subchannel.equals("ServerPlayerCount")) { String server = in.readUTF(); // Name of server, as given in the arguments int playercount = in.readInt(); } } Data output code: Code (Text): ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF("ServerList"); out.writeUTF("GetServers"); Player player = Bukkit.getPlayerExact("Example"); player.sendPluginMessage(this, "BungeeCord", out.toByteArray()); So I'm trying to take the "server & "playercount" variable and make them a public variable so I can access it throughout my code, however don't really know where to start and how to do this. Thanks in advance.
Well, I do this on my lobby server, I have currently one runnable that keeps updating a HashMap. Runnable that gets info for the SkyWars server. Code (Text): @SuppressWarnings("deprecation") @Override public void run() { if(Bukkit.getOnlinePlayers().length >= 1) { Player[] players = Bukkit.getOnlinePlayers(); List<Player> pls = new ArrayList<Player>(); for(Player p: players) { pls.add(p); } getPlayers(pls.get(0), "SkyWars"); } } public void getPlayers(Player player, String targetServer) { ByteArrayOutputStream b = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(b); try { out.writeUTF("PlayerCount"); out.writeUTF(targetServer); } catch (Exception e) { e.printStackTrace(); } player.sendPluginMessage(Main.theClass, "BungeeCord", b.toByteArray()); } And this is the PluginMessageListener class, wich basically reads the data that was sent by the runnable and stores it on a static HashMap outside the class. ( You really don't need to do it with static ). Code (Text): @Override public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (!channel.equals("BungeeCord")) { return; } DataInputStream in = new DataInputStream(new ByteArrayInputStream(message)); try { String subchannel = in.readUTF(); if (subchannel.equalsIgnoreCase("PlayerCount")) { String server = in.readUTF(); Integer count = in.readInt(); BungeeCordAPI.playerCounts.put(server.toLowerCase(), count); } } catch (IOException e) { e.printStackTrace(); } } Hope you can understand how this works, you can also check the Plugin Messaging channel tutorials on the wiki.
@Fabricio20 why are you cloning the Player array/collection? Also, there's no need to use a runnable for this, you can do this on demand with callbacks (though this would require a player on the other server as well, or a BungeeCord plugin, but it's less load)