添加跨服聊天功能

This commit is contained in:
502647092 2016-11-01 20:35:05 +08:00
parent 0366ad42cf
commit 7d72c97c3b
6 changed files with 147 additions and 22 deletions

17
pom.xml
View File

@ -112,12 +112,17 @@
<type>jar</type>
<version>1.1</version>
</dependency>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>PlaceholderAPI</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/PlaceholderAPI.jar</systemPath>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>PlaceholderAPI</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/PlaceholderAPI.jar</systemPath>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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<String> offList = new HashSet<>();
private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
private final Queue<String> 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<? extends Player> 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<? extends Player> 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<? extends Entity> plist = Collections.emptyList();
Collection<? extends Entity> 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);
}

View File

@ -0,0 +1,5 @@
name: ${project.artifactId}
description: ${project.description}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}Bungee
version: ${project.version}
author: 喵♂呜