From 7d72c97c3b236484a24850d681785860873c2a97 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 1 Nov 2016 20:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B7=A8=E6=9C=8D=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 ++++-- src/main/java/pw/yumc/MiaoChat/MiaoChat.java | 26 ++++++++- .../java/pw/yumc/MiaoChat/MiaoChatBungee.java | 29 ++++++++++ .../java/pw/yumc/MiaoChat/MiaoMessage.java | 37 +++++++++++++ .../yumc/MiaoChat/listeners/ChatListener.java | 55 ++++++++++++++----- src/main/resources/bungee.yml | 5 ++ 6 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java create mode 100644 src/main/java/pw/yumc/MiaoChat/MiaoMessage.java create mode 100644 src/main/resources/bungee.yml diff --git a/pom.xml b/pom.xml index 4c22dd0..0e0a834 100644 --- a/pom.xml +++ b/pom.xml @@ -112,12 +112,17 @@ jar 1.1 - - pw.yumc - PlaceholderAPI - 1.0 - system - ${project.basedir}/lib/PlaceholderAPI.jar + + net.md-5 + bungeecord-api + 1.10-SNAPSHOT + + + pw.yumc + PlaceholderAPI + 1.0 + system + ${project.basedir}/lib/PlaceholderAPI.jar \ No newline at end of file diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java index 767d724..451fb4e 100644 --- a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java @@ -1,14 +1,17 @@ package pw.yumc.MiaoChat; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; - +import org.bukkit.plugin.messaging.PluginMessageListener; import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.listeners.ChatListener; import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.commands.CommandManager; import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Help; @@ -16,7 +19,7 @@ import pw.yumc.YumCore.commands.interfaces.CommandExecutor; import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.global.L10N; -public class MiaoChat extends JavaPlugin implements CommandExecutor { +public class MiaoChat extends JavaPlugin implements CommandExecutor, PluginMessageListener { private FileConfig cfg; private ChatConfig chatConfig; @@ -47,6 +50,8 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { public void onEnable() { new ChatListener(); new CommandManager("MiaoChat", this); + Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.CHANNEL, this); + Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.CHANNEL); L10N.getName(new ItemStack(Material.AIR)); } @@ -63,4 +68,21 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { chatConfig.reload(); Log.toSender(sender, "§a配置文件已重载!"); } + + public static void send(byte[] in) { + send(MiaoMessage.decode(in).getJson()); + } + + public static void send(String json) { + for (Player player : C.Player.getOnlinePlayers()) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + json); + } + } + + @Override + public void onPluginMessageReceived(String channel, Player player, byte[] message) { + if (MiaoMessage.CHANNEL.equals(channel)) { + send(message); + } + } } diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java b/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java new file mode 100644 index 0000000..5f8cf6c --- /dev/null +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java @@ -0,0 +1,29 @@ +package pw.yumc.MiaoChat; + +import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.api.event.PluginMessageEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.event.EventHandler; + +import java.net.InetSocketAddress; + +public class MiaoChatBungee extends Plugin implements Listener { + @EventHandler + public void handle(final PluginMessageEvent event) { + if (event.getTag().equals(MiaoMessage.CHANNEL)) { + InetSocketAddress origin = event.getSender().getAddress(); + for (ServerInfo server : getProxy().getServers().values()) { + if (!server.getAddress().equals(origin) && server.getPlayers().size() > 0) { + server.sendData(MiaoMessage.CHANNEL, event.getData()); + } + } + } + } + + @Override + public void onEnable() { + getProxy().registerChannel(MiaoMessage.CHANNEL); + getProxy().getPluginManager().registerListener(this, this); + } +} diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoMessage.java b/src/main/java/pw/yumc/MiaoChat/MiaoMessage.java new file mode 100644 index 0000000..2e2ab27 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoChat/MiaoMessage.java @@ -0,0 +1,37 @@ +package pw.yumc.MiaoChat; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; + +/** + * Created on 16-9-8. + */ +public class MiaoMessage { + + public static final String CHANNEL = "MiaoChat"; + private String json; + + private MiaoMessage(String json) { + this.json = json; + } + + public static byte[] encode(String in) { + return new MiaoMessage(in).encode(); + } + + public static MiaoMessage decode(byte[] in) { + final ByteArrayDataInput input = ByteStreams.newDataInput(in); + return new MiaoMessage(input.readUTF()); + } + + public String getJson() { + return json; + } + + public byte[] encode() { + final ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF(json); + return out.toByteArray(); + } +} \ No newline at end of file diff --git a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java index 20f5911..578fc23 100644 --- a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java +++ b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java @@ -1,13 +1,6 @@ package pw.yumc.MiaoChat.listeners; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - +import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -18,9 +11,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.ItemStack; - -import me.clip.placeholderapi.PlaceholderAPI; import pw.yumc.MiaoChat.MiaoChat; +import pw.yumc.MiaoChat.MiaoMessage; import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.config.ChatMessagePart; import pw.yumc.MiaoChat.config.ChatRule; @@ -32,10 +24,16 @@ import pw.yumc.YumCore.statistic.Statistics; import pw.yumc.YumCore.tellraw.Tellraw; import pw.yumc.YumCore.update.SubscribeTask; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class ChatListener implements Listener { public static Set offList = new HashSet<>(); private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); + private final Queue queue = new LinkedList<>(); + private MiaoChat plugin = P.getPlugin(); private ChatConfig cc = plugin.getChatConfig(); @@ -45,6 +43,37 @@ public class ChatListener implements Listener { new SubscribeTask(true, true); } + public void execute(final String commandList) { + execute(commandList, false); + } + + public void execute(final String command, final boolean queued) { + final Iterator it = C.Player.getOnlinePlayers().iterator(); + if (it.hasNext()) { + final Player p = it.next(); + p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, MiaoMessage.encode(command)); + } else if (queued) { + queue.offer(command); + } else { + throw new RuntimeException("None player channel registered! Use queued"); + } + } + + private boolean processQueued() { + if (!queue.isEmpty()) { + final Iterator it = C.Player.getOnlinePlayers().iterator(); + if (!it.hasNext()) { return false; } + final Player p = it.next(); + String command = queue.poll(); + while (command != null) { + p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, MiaoMessage.encode(command)); + command = queue.poll(); + } + return true; + } + return false; + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onChat(AsyncPlayerChatEvent e) { Player p = e.getPlayer(); @@ -123,7 +152,7 @@ public class ChatListener implements Listener { } private void handleSend(Player p, Tellraw tr, int range) { - Collection plist = Collections.emptyList(); + Collection plist; if (range != 0) { plist = p.getNearbyEntities(range, range, range); tr.send(p); @@ -139,9 +168,7 @@ public class ChatListener implements Listener { } private void handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) { - if (message.isEmpty()) { - return; - } + if (message.isEmpty()) { return; } if (player.hasPermission("MiaoChat.color")) { message = ChatColor.translateAlternateColorCodes('&', message); } diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml new file mode 100644 index 0000000..8351ad5 --- /dev/null +++ b/src/main/resources/bungee.yml @@ -0,0 +1,5 @@ +name: ${project.artifactId} +description: ${project.description} +main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee +version: ${project.version} +author: 喵♂呜 \ No newline at end of file