From 62e5d2391427d4783ca70650d931e74032bcec58 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 23 Dec 2016 16:14:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E5=88=86=E7=BB=84?= =?UTF-8?q?=E5=8F=91=E9=80=81=20=E5=AE=8C=E5=96=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- proguard.conf | 32 +++++----- src/main/java/pw/yumc/MiaoChat/MiaoChat.java | 58 ++++++++++++++++++- .../java/pw/yumc/MiaoChat/MiaoChatBungee.java | 41 +++++++------ .../pw/yumc/MiaoChat/bungee/FileConfig.java | 11 ++++ .../pw/yumc/MiaoChat/config/ChatConfig.java | 6 ++ .../pw/yumc/MiaoChat/config/ChatRule.java | 3 +- src/main/resources/config.yml | 4 +- 7 files changed, 114 insertions(+), 41 deletions(-) diff --git a/proguard.conf b/proguard.conf index 444289e..f70d1bb 100644 --- a/proguard.conf +++ b/proguard.conf @@ -6,7 +6,7 @@ -dontnote # -----混淆时应用侵入式重载----- --overloadaggressively +-overloadaggressively # -----启用混淆字典----- -obfuscationdictionary obf.dict @@ -16,38 +16,36 @@ # -----保留所有属性 -keepattributes ** -# -----公共数据不混淆----- --keep class cn.citycraft.CommonData.** {*;} - # -----保护所有实体中的字段名称----- --keepclassmembers class * implements java.io.Serializable { ; } +-keepclassmembers class * implements java.io.Serializable {;} # -----保护监听方法不被清理----- --keepclassmembers class * implements org.bukkit.event.Listener { - @org.bukkit.event.EventHandler ; +-keepclassmembers class * implements org.bukkit.event.Listener { + @org.bukkit.event.EventHandler ; } -keepclassmembers class * implements net.md_5.bungee.api.plugin.Listener { @net.md_5.bungee.event.EventHandler ; } + # -----保护枚举方法的完整性----- --keepclassmembers enum * { - public static **[] values(); - public static ** valueOf(java.lang.String); +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); } # -----保护配置注入不被清理----- --keepclassmembers class * extends **.config**Inject** { - ; +-keepclassmembers class * extends **.config.inject.Inject** { + ; } # -----保护注解命令方法不被清理----- --keepclassmembers class **.commands.annotation.** { ; } --keepclassmembers class * implements **.commands**CommandExecutor { ; } +-keepclassmembers class **.commands.annotation.** {;} +-keepclassmembers class * implements **.commands.interfaces.Executor {;} # -----保护注解NotProguard标记----- -keep class **.NotProguard -keep @**.NotProguard class * {*;} --keepclassmembers class * { - @**.NotProguard ; - @**.NotProguard ; +-keepclassmembers class * { + @**.NotProguard ; + @**.NotProguard ; } \ 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 a24c5c6..13b56a0 100644 --- a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java @@ -5,10 +5,19 @@ import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; + +import me.clip.placeholderapi.PlaceholderAPI; +import me.clip.placeholderapi.PlaceholderHook; import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.listeners.ChatListener; import pw.yumc.YumCore.bukkit.Log; @@ -20,9 +29,10 @@ import pw.yumc.YumCore.commands.interfaces.Executor; import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.global.L10N; -public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListener { +public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListener, Listener { private FileConfig cfg; private ChatConfig chatConfig; + private String ServerName; public ChatConfig getChatConfig() { return chatConfig; @@ -52,12 +62,27 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe new ChatListener(); new CommandSub("MiaoChat", this); if (getChatConfig().isBungeeCord()) { - Log.info("已开启 BUngeeCord 模式!"); + Log.info("已开启 BungeeCord 模式!"); + Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this); + Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); + Bukkit.getPluginManager().registerEvents(this, this); 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); } + PlaceholderAPI.registerPlaceholderHook("mct", new PlaceholderHook() { + @Override + public String onPlaceholderRequest(Player player, String s) { + switch (s.toLowerCase()) { + case "server": + return getChatConfig().getServername(); + case "bserver": + return ServerName; + } + return "未知的参数"; + } + }); L10N.getName(new ItemStack(Material.AIR)); } @@ -85,6 +110,21 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe } } + @EventHandler + public void onJoin(final PlayerJoinEvent e) { + Bukkit.getScheduler().runTaskLater(this, new Runnable() { + @Override + public void run() { + Player p = e.getPlayer(); + if (p.isOnline()) { + ByteArrayDataOutput out = ByteStreams.newDataOutput(); + out.writeUTF("GetServer"); + p.sendPluginMessage(MiaoChat.this, "BungeeCord", out.toByteArray()); + } + } + }, 10); + } + @Override public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (MiaoMessage.CHANNEL.equals(channel)) { @@ -93,6 +133,20 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe for (Player p : C.Player.getOnlinePlayers()) { p.sendMessage(MiaoMessage.decode(message).getJson()); } + } else if ("BungeeCord".equals(channel)) { + final ByteArrayDataInput input = ByteStreams.newDataInput(message); + if ("GetServer".equals(input.readUTF())) { + ServerName = input.readUTF(); + Log.d("获取服务器名称: " + ServerName); + PlayerJoinEvent.getHandlerList().unregister((Listener) this); + Bukkit.getScheduler().runTaskLater(this, new Runnable() { + @Override + public void run() { + Bukkit.getMessenger().unregisterIncomingPluginChannel(MiaoChat.this, "BungeeCord"); + Bukkit.getMessenger().unregisterOutgoingPluginChannel(MiaoChat.this, "BungeeCord"); + } + }, 20); + } } } } diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java b/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java index fd3f6ed..6b3222d 100644 --- a/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChatBungee.java @@ -12,16 +12,17 @@ import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.config.Configuration; import net.md_5.bungee.event.EventHandler; import pw.yumc.MiaoChat.bungee.FileConfig; +import pw.yumc.MiaoChat.bungee.Log; public class MiaoChatBungee extends Plugin implements Listener { - private Map> groups; + private Map> groups; private FileConfig config; @EventHandler public void handle(final PluginMessageEvent event) { if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMALCHANNEL)) { InetSocketAddress origin = event.getSender().getAddress(); - for (ServerInfo server : getProxy().getServers().values()) { + for (ServerInfo server : groups.get(origin)) { if (!server.getAddress().equals(origin) && server.getPlayers().size() > 0) { server.sendData(event.getTag(), event.getData()); } @@ -31,55 +32,57 @@ public class MiaoChatBungee extends Plugin implements Listener { @Override public void onLoad() { + Log.init(this); config = new FileConfig(this, "group.yml"); } public void loadGroup() { - Map temp = new HashMap<>(); - Set unused = new HashSet<>(); - for (ServerInfo server : getProxy().getServers().values()) { - temp.put(server.getName(), server.getAddress()); - } + groups = new HashMap<>(); + Map temp = getProxy().getServers(); + Set unused = new HashSet<>(); Configuration groupSel = config.getSection("Groups"); Collection groupname = groupSel.getKeys(); for (String gname : groupname) { Set servers = new HashSet<>(groupSel.getStringList(gname)); - Set serISA = new HashSet<>(); + Set sers = new HashSet<>(); for (String sname : servers) { - serISA.add(temp.get(sname)); + sers.add(temp.get(sname)); } - serISA.remove(null); + sers.remove(null); for (String sname : servers) { - InetSocketAddress isadd = temp.get(sname); + ServerInfo isadd = temp.get(sname); if (isadd != null) { unused.remove(isadd); - groups.put(isadd, serISA); + groups.put(isadd.getAddress(), sers); } } } - for (InetSocketAddress unser : unused) { - groups.put(unser, unused); + for (ServerInfo unser : unused) { + groups.put(unser.getAddress(), unused); } } @Override public void onEnable() { + loadGroup(); getProxy().registerChannel(MiaoMessage.CHANNEL); getProxy().registerChannel(MiaoMessage.NORMALCHANNEL); getProxy().getPluginManager().registerListener(this, this); getProxy().getPluginManager().registerCommand(this, new Command("MiaoChat", "MiaoChat.admin", "mct") { @Override public void execute(CommandSender commandSender, String[] args) { - if (args.length > 1) { + if (args.length > 0) { switch (args[0].toLowerCase()) { case "reload": - onLoad(); - break; + config.reload(); + loadGroup(); + commandSender.sendMessage("§a配置文件已重载!"); + return; case "version": - commandSender.sendMessage(getDescription().getVersion()); - break; + default: } } + commandSender.sendMessage("§6插件版本: §av" + getDescription().getVersion()); } }); getLogger().info("注意: 通过BC转发的聊天信息将不会在控制台显示 仅客户端可见!"); diff --git a/src/main/java/pw/yumc/MiaoChat/bungee/FileConfig.java b/src/main/java/pw/yumc/MiaoChat/bungee/FileConfig.java index e29ed8d..05a800c 100644 --- a/src/main/java/pw/yumc/MiaoChat/bungee/FileConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/bungee/FileConfig.java @@ -171,6 +171,9 @@ public class FileConfig { this.file = new File(plugin.getDataFolder(), name); try { if (!file.exists()) { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } Files.copy(plugin.getResourceAsStream(name), file.toPath()); } this.config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); @@ -191,7 +194,15 @@ public class FileConfig { Log.w("配置文件保存失败!"); e.printStackTrace(); } + } + public void reload() { + try { + this.config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); + } catch (IOException e) { + Log.w("配置文件读取失败!"); + e.printStackTrace(); + } } } diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java index 6a8da2f..e32dcfa 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java @@ -20,6 +20,7 @@ public class ChatConfig { private LinkedList rules; private FileConfig config; private FileConfig format; + private String servername; private boolean BungeeCord; public ChatConfig() { @@ -58,6 +59,7 @@ public class ChatConfig { } public void load() { + servername = config.getMessage("Server"); formats.clear(); for (String name : format.getKeys(false)) { formats.put(name, new ChatMessagePart(format.getConfigurationSection(name))); @@ -73,6 +75,10 @@ public class ChatConfig { Collections.sort(rules, rulecomp); } + public String getServername() { + return servername; + } + public void reload() { format.reload(); load(); diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java index 1544f5b..15a394a 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java @@ -23,7 +23,6 @@ import pw.yumc.YumCore.tellraw.Tellraw; */ public class ChatRule extends InjectConfigurationSection { private transient static MiaoChat plugin = P.getPlugin(); - private transient static ChatConfig cc = plugin.getChatConfig(); private transient static Pattern FORMAT_PATTERN = Pattern.compile("[\\[]([^\\[\\]]+)[]]"); private transient String name; @Default("50") @@ -97,7 +96,7 @@ public class ChatRule extends InjectConfigurationSection { public Tellraw create(Player p) { Tellraw tr = Tellraw.create(); for (String format : formats) { - ChatMessagePart cmp = cc.getFormats().get(format); + ChatMessagePart cmp = plugin.getChatConfig().getFormats().get(format); if (cmp != null) { // Log.d("解析格式: %s", format); cmp.then(tr, p); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e67f9b4..9270479 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,8 +1,10 @@ #配置文件版本号 请勿修改 -Version: 1.3 +Version: 1.6 #BC跨服模式 BungeeCord: true +#当前服务器名称(变量为%mct_server%) +Server: '§a生存服' #格式列表 Formats: #格式名称 对应当前文件夹下的default.yml