4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-11-14 13:28:46 +00:00

feat: 完善BungeeCord模式

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-11-03 20:31:01 +08:00
parent 7d72c97c3b
commit cfe7f8c919
9 changed files with 50 additions and 50 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId>
<version>1.3.3</version>
<version>1.5</version>
<build>
<finalName>${project.name}</finalName>
<resources>
@ -62,6 +62,9 @@
<options>
<option>-repackageclasses \ʼ.ʽ.ʾ.${project.artifactId}</option>
<option>-keep class ${project.groupId}.${project.artifactId}.${project.artifactId}</option>
<option>-keep class
${project.groupId}.${project.artifactId}.${project.artifactId}Bungee
</option>
</options>
<libs>
<lib>${java.home}/lib/rt.jar</lib>

View File

@ -26,7 +26,9 @@
-keepclassmembers class * implements org.bukkit.event.Listener {
@org.bukkit.event.EventHandler <methods>;
}
-keepclassmembers class * implements net.md_5.bungee.api.plugin.Listener {
@net.md_5.bungee.event.EventHandler <methods>;
}
# -----保护枚举方法的完整性-----
-keepclassmembers enum * {
public static **[] values();

View File

@ -50,8 +50,13 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor, PluginMessa
public void onEnable() {
new ChatListener();
new CommandManager("MiaoChat", this);
if (getChatConfig().isBungeeCord()) {
Log.info("已开启 BUngeeCord 模式!");
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.CHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.CHANNEL);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMALCHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMALCHANNEL);
}
L10N.getName(new ItemStack(Material.AIR));
}
@ -83,6 +88,10 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor, PluginMessa
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (MiaoMessage.CHANNEL.equals(channel)) {
send(message);
} else if (MiaoMessage.NORMALCHANNEL.equals(channel)) {
for (Player p : C.Player.getOnlinePlayers()) {
p.sendMessage(MiaoMessage.decode(message).getJson());
}
}
}
}

View File

@ -11,11 +11,11 @@ import java.net.InetSocketAddress;
public class MiaoChatBungee extends Plugin implements Listener {
@EventHandler
public void handle(final PluginMessageEvent event) {
if (event.getTag().equals(MiaoMessage.CHANNEL)) {
if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMALCHANNEL)) {
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());
server.sendData(event.getTag(), event.getData());
}
}
}
@ -24,6 +24,8 @@ public class MiaoChatBungee extends Plugin implements Listener {
@Override
public void onEnable() {
getProxy().registerChannel(MiaoMessage.CHANNEL);
getProxy().registerChannel(MiaoMessage.NORMALCHANNEL);
getProxy().getPluginManager().registerListener(this, this);
getLogger().info("注意: 通过BC转发的聊天信息将不会在控制台显示 仅客户端可见!");
}
}

View File

@ -10,6 +10,7 @@ import com.google.common.io.ByteStreams;
public class MiaoMessage {
public static final String CHANNEL = "MiaoChat";
public static final String NORMALCHANNEL = "MiaoChatNM";
private String json;
private MiaoMessage(String json) {
@ -30,6 +31,7 @@ public class MiaoMessage {
}
public byte[] encode() {
if (json.getBytes().length > 32000) { return null; }
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(json);
return out.toByteArray();

View File

@ -3,5 +3,5 @@ package pw.yumc.MiaoChat.config;
public enum CLICKTYPE {
COMMAND,
SUGGEST,
OPENURL;
OPENURL
}

View File

@ -1,17 +1,12 @@
package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.config.FileConfig;
import java.util.*;
/**
*
* @since 2016年9月9日 下午4:40:50
@ -24,9 +19,11 @@ public class ChatConfig {
private LinkedList<ChatRule> rules;
private FileConfig config;
private FileConfig format;
private boolean BungeeCord;
public ChatConfig() {
config = P.getConfig();
BungeeCord = config.getBoolean("BungeeCord");
format = new FileConfig("format.yml");
rulecomp = new RuleComparator();
formats = new HashMap<>();
@ -34,6 +31,13 @@ public class ChatConfig {
load();
}
/**
* @return 是否为BC模式
*/
public boolean isBungeeCord() {
return BungeeCord;
}
/**
* 获得玩家可用的消息处理
*
@ -43,9 +47,7 @@ public class ChatConfig {
public ChatRule getChatRule(Player player) {
for (ChatRule cr : rules) {
Log.debug(cr.getName());
if (cr.check(player)) {
return cr;
}
if (cr.check(player)) { return cr; }
}
return null;
}

View File

@ -43,38 +43,7 @@ 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)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent e) {
Player p = e.getPlayer();
ChatRule cr = cc.getChatRule(e.getPlayer());
@ -158,8 +127,17 @@ public class ChatListener implements Listener {
tr.send(p);
} else {
plist = C.Player.getOnlinePlayers();
if (cc.isBungeeCord()) {
byte[] mm = MiaoMessage.encode(tr.toJsonString());
if (mm == null) {
p.sendPluginMessage(P.instance, MiaoMessage.NORMALCHANNEL, MiaoMessage.encode(tr.toOldMessageFormat()));
} else {
p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, mm);
}
}
}
for (Entity ne : plist) {
// 此处必须进行强制转换 老版本服务器的Entity没有getName()
if (ne instanceof Player && !offList.contains(((Player) ne).getName())) {
tr.send(ne);
}

View File

@ -1,6 +1,8 @@
#配置文件版本号 请勿修改
Version: 1.2
Version: 1.3
#BC跨服模式
BungeeCord: true
#格式列表
Formats:
#格式名称 对应当前文件夹下的default.yml