Created
August 10, 2015 20:55
-
-
Save justinoboyle/7445c9371ab09f9e67f8 to your computer and use it in GitHub Desktop.
Talk to BungeeCord easily!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.plugin.messaging.PluginMessageListener; | |
import com.google.common.collect.Iterables; | |
import com.google.common.io.ByteArrayDataInput; | |
import com.google.common.io.ByteArrayDataOutput; | |
import com.google.common.io.ByteStreams; | |
public class BungeeTalk implements PluginMessageListener { | |
private Map<String, String> responses; | |
private JavaPlugin plugin; | |
public BungeeTalk(JavaPlugin plugin) { | |
this.plugin = plugin; | |
responses = new ConcurrentHashMap<String, String>(); | |
Bukkit.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord"); | |
Bukkit.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", this); | |
} | |
public void ask(Player p, String channel, String... messages) { | |
ByteArrayDataOutput out = ByteStreams.newDataOutput(); | |
for(String s : messages) out.writeUTF(s); | |
p.sendPluginMessage(plugin, channel, out.toByteArray()); | |
} | |
public void ask(String channel, String... messages) { | |
Player p = Iterables.getFirst(Bukkit.getOnlinePlayers(), null); | |
if(p == null) return; | |
ask(p, channel, messages); | |
} | |
public void ask(String... messages) { | |
ask("BungeeCord", messages); | |
} | |
@Override | |
public void onPluginMessageReceived(String channel, Player player, byte[] message) { | |
if (!channel.equals("BungeeCord")) | |
return; | |
ByteArrayDataInput in = ByteStreams.newDataInput(message); | |
String subchannel = in.readUTF(); | |
String msg = in.readUTF(); | |
if(responses.containsKey(subchannel)) responses.remove(subchannel); | |
responses.put(subchannel, msg); | |
} | |
public Map<String, String> getResponses() { | |
return responses; | |
} | |
public String getResponse(String key) { | |
if(!responses.containsKey(key)) return null; | |
return responses.get(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment