4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-12-22 19:18:47 +00:00

feat: 通过内置的列表兼容更多插件

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-03-28 10:02:03 +08:00
parent f4713e06d9
commit f18647a2c6
6 changed files with 80 additions and 93 deletions

22
pom.xml
View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId> <artifactId>MiaoChat</artifactId>
<version>1.7.1</version> <version>1.8</version>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
<resources> <resources>
@ -16,7 +16,13 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>2.3</version> <version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar> <minimizeJar>true</minimizeJar>
@ -32,12 +38,6 @@
</relocation> </relocation>
</relocations> </relocations>
</configuration> </configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
@ -72,8 +72,8 @@
<properties> <properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
</properties> </properties>
<repositories> <repositories>
<repository> <repository>
@ -93,7 +93,7 @@
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<type>jar</type> <type>jar</type>
<version>1.6</version> <version>1.7</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -43,17 +43,17 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
return cfg; return cfg;
} }
@Cmd(permission = "MiaoChat.toggle") @Cmd(permission = "MiaoChat.toggle", executor = Cmd.Executor.PLAYER)
@Help("关闭聊天功能") @Help("关闭聊天功能")
public void off(CommandSender sender) { public void off(Player sender) {
ChatListener.offList.add(sender.getName()); ChatListener.offList.add(sender);
Log.sender(sender, "§c聊天功能已关闭!"); Log.sender(sender, "§c聊天功能已关闭!");
} }
@Cmd(permission = "MiaoChat.toggle") @Cmd(permission = "MiaoChat.toggle", executor = Cmd.Executor.PLAYER)
@Help("开启聊天功能") @Help("开启聊天功能")
public void on(CommandSender sender) { public void on(Player sender) {
ChatListener.offList.remove(sender.getName()); ChatListener.offList.remove(sender);
Log.sender(sender, "§a聊天功能已开启!"); Log.sender(sender, "§a聊天功能已开启!");
} }
@ -61,6 +61,12 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
public void onEnable() { public void onEnable() {
new ChatListener(); new ChatListener();
new CommandSub("MiaoChat", this); new CommandSub("MiaoChat", this);
enableBungeeCord();
hookPlaceholderAPI();
L10N.getName(new ItemStack(Material.AIR));
}
private void enableBungeeCord() {
if (getChatConfig().isBungeeCord()) { if (getChatConfig().isBungeeCord()) {
Log.i("已开启 BungeeCord 模式!"); Log.i("已开启 BungeeCord 模式!");
Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this); Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
@ -71,6 +77,9 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMALCHANNEL, this); Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMALCHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMALCHANNEL); Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMALCHANNEL);
} }
}
private void hookPlaceholderAPI() {
PlaceholderAPI.registerPlaceholderHook("mct", new PlaceholderHook() { PlaceholderAPI.registerPlaceholderHook("mct", new PlaceholderHook() {
@Override @Override
public String onPlaceholderRequest(Player player, String s) { public String onPlaceholderRequest(Player player, String s) {
@ -83,7 +92,6 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
return "未知的参数"; return "未知的参数";
} }
}); });
L10N.getName(new ItemStack(Material.AIR));
} }
@Override @Override
@ -112,16 +120,13 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
@EventHandler @EventHandler
public void onJoin(final PlayerJoinEvent e) { public void onJoin(final PlayerJoinEvent e) {
Bukkit.getScheduler().runTaskLater(this, new Runnable() { Bukkit.getScheduler().runTaskLater(this, () -> {
@Override
public void run() {
Player p = e.getPlayer(); Player p = e.getPlayer();
if (p.isOnline()) { if (p.isOnline()) {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("GetServer"); out.writeUTF("GetServer");
p.sendPluginMessage(MiaoChat.this, "BungeeCord", out.toByteArray()); p.sendPluginMessage(MiaoChat.this, "BungeeCord", out.toByteArray());
} }
}
}, 10); }, 10);
} }
@ -139,12 +144,9 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
ServerName = input.readUTF(); ServerName = input.readUTF();
Log.d("获取服务器名称: " + ServerName); Log.d("获取服务器名称: " + ServerName);
PlayerJoinEvent.getHandlerList().unregister((Listener) this); PlayerJoinEvent.getHandlerList().unregister((Listener) this);
Bukkit.getScheduler().runTaskLater(this, new Runnable() { Bukkit.getScheduler().runTaskLater(this, () -> {
@Override
public void run() {
Bukkit.getMessenger().unregisterIncomingPluginChannel(MiaoChat.this, "BungeeCord"); Bukkit.getMessenger().unregisterIncomingPluginChannel(MiaoChat.this, "BungeeCord");
Bukkit.getMessenger().unregisterOutgoingPluginChannel(MiaoChat.this, "BungeeCord"); Bukkit.getMessenger().unregisterOutgoingPluginChannel(MiaoChat.this, "BungeeCord");
}
}, 20); }, 20);
} }
} }

View File

@ -1,7 +1,11 @@
package pw.yumc.MiaoChat; package pw.yumc.MiaoChat;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.*; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
@ -22,11 +26,11 @@ public class MiaoChatBungee extends Plugin implements Listener {
public void handle(final PluginMessageEvent event) { public void handle(final PluginMessageEvent event) {
if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMALCHANNEL)) { if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMALCHANNEL)) {
InetSocketAddress origin = event.getSender().getAddress(); InetSocketAddress origin = event.getSender().getAddress();
for (ServerInfo server : groups.get(origin)) { groups.get(origin).forEach(server -> {
if (!server.getAddress().equals(origin) && server.getPlayers().size() > 0) { if (!server.getAddress().equals(origin) && server.getPlayers().size() > 0) {
server.sendData(event.getTag(), event.getData()); server.sendData(event.getTag(), event.getData());
} }
} });
} }
} }
@ -42,24 +46,20 @@ public class MiaoChatBungee extends Plugin implements Listener {
Set<ServerInfo> unused = new HashSet<>(); Set<ServerInfo> unused = new HashSet<>();
Configuration groupSel = config.getSection("Groups"); Configuration groupSel = config.getSection("Groups");
Collection<String> groupname = groupSel.getKeys(); Collection<String> groupname = groupSel.getKeys();
for (String gname : groupname) { groupname.forEach(gname -> {
Set<String> servers = new HashSet<>(groupSel.getStringList(gname)); Set<String> servers = new HashSet<>(groupSel.getStringList(gname));
Set<ServerInfo> sers = new HashSet<>(); Set<ServerInfo> sers = new HashSet<>();
for (String sname : servers) { servers.forEach(sname -> sers.add(temp.get(sname)));
sers.add(temp.get(sname));
}
sers.remove(null); sers.remove(null);
for (String sname : servers) { servers.forEach(sname -> {
ServerInfo isadd = temp.get(sname); ServerInfo isadd = temp.get(sname);
if (isadd != null) { if (isadd != null) {
unused.remove(isadd); unused.remove(isadd);
groups.put(isadd.getAddress(), sers); groups.put(isadd.getAddress(), sers);
} }
} });
} });
for (ServerInfo unser : unused) { unused.forEach(unser -> groups.put(unser.getAddress(), unused));
groups.put(unser.getAddress(), unused);
}
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package pw.yumc.MiaoChat.config; package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -20,7 +19,6 @@ import pw.yumc.YumCore.config.FileConfig;
public class ChatConfig { public class ChatConfig {
private static String F = "Formats"; private static String F = "Formats";
private Map<String, ChatMessagePart> formats; private Map<String, ChatMessagePart> formats;
private RuleComparator rulecomp;
private LinkedList<ChatRule> rules; private LinkedList<ChatRule> rules;
private FileConfig config; private FileConfig config;
private FileConfig format; private FileConfig format;
@ -31,7 +29,6 @@ public class ChatConfig {
config = P.getConfig(); config = P.getConfig();
BungeeCord = config.getBoolean("BungeeCord"); BungeeCord = config.getBoolean("BungeeCord");
format = new FileConfig("format.yml"); format = new FileConfig("format.yml");
rulecomp = new RuleComparator();
formats = new HashMap<>(); formats = new HashMap<>();
rules = new LinkedList<>(); rules = new LinkedList<>();
load(); load();
@ -64,18 +61,18 @@ public class ChatConfig {
public void load() { public void load() {
servername = config.getMessage("Server"); servername = config.getMessage("Server");
formats.clear(); formats.clear();
for (String name : format.getKeys(false)) { format.getKeys(false).forEach(name -> {
formats.put(name, new ChatMessagePart(format.getConfigurationSection(name))); formats.put(name, new ChatMessagePart(format.getConfigurationSection(name)));
Log.d("载入聊天格式: %s", name); Log.d("载入聊天格式: %s", name);
} });
rules.clear(); rules.clear();
if (config.isSet(F)) { if (config.isSet(F)) {
for (String rule : config.getConfigurationSection(F).getKeys(false)) { config.getConfigurationSection(F).getKeys(false).forEach(rule -> {
rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule))); rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule)));
Log.d("载入聊天规则: %s => \"%s\"", rule, rules.getLast().getFormat()); Log.d("载入聊天规则: %s => \"%s\"", rule, rules.getLast().getFormat());
});
} }
} rules.sort(Comparator.comparing(ChatRule::getIndex));
Collections.sort(rules, rulecomp);
} }
public String getServername() { public String getServername() {
@ -86,11 +83,4 @@ public class ChatConfig {
format.reload(); format.reload();
load(); load();
} }
private class RuleComparator implements Comparator<ChatRule> {
@Override
public int compare(ChatRule o1, ChatRule o2) {
return o1.getIndex().compareTo(o2.getIndex());
}
}
} }

View File

@ -95,7 +95,7 @@ public class ChatRule extends InjectConfigurationSection {
public Tellraw create(Player p) { public Tellraw create(Player p) {
Tellraw tr = Tellraw.create(); Tellraw tr = Tellraw.create();
for (String format : formats) { formats.forEach(format -> {
ChatMessagePart cmp = plugin.getChatConfig().getFormats().get(format); ChatMessagePart cmp = plugin.getChatConfig().getFormats().get(format);
if (cmp != null) { if (cmp != null) {
// Log.d("解析格式: %s", format); // Log.d("解析格式: %s", format);
@ -104,7 +104,7 @@ public class ChatRule extends InjectConfigurationSection {
// Log.d("追加文本: %s", format); // Log.d("追加文本: %s", format);
tr.then(PlaceholderAPI.setPlaceholders(p, format)); tr.then(PlaceholderAPI.setPlaceholders(p, format));
} }
} });
return tr; return tr;
} }

View File

@ -1,13 +1,15 @@
package pw.yumc.MiaoChat.listeners; package pw.yumc.MiaoChat.listeners;
import java.util.*; import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -21,14 +23,13 @@ import pw.yumc.MiaoChat.config.ChatConfig;
import pw.yumc.MiaoChat.config.ChatRule; import pw.yumc.MiaoChat.config.ChatRule;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
import pw.yumc.YumCore.global.L10N; import pw.yumc.YumCore.global.L10N;
import pw.yumc.YumCore.statistic.Statistics; import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.tellraw.Tellraw; import pw.yumc.YumCore.tellraw.Tellraw;
import pw.yumc.YumCore.update.SubscribeTask; import pw.yumc.YumCore.update.SubscribeTask;
public class ChatListener implements Listener { public class ChatListener implements Listener {
public static Set<String> offList = new HashSet<>(); public static Set<Player> offList = new HashSet<>();
private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
private final Queue<String> queue = new LinkedList<>(); private final Queue<String> queue = new LinkedList<>();
@ -50,13 +51,12 @@ public class ChatListener implements Listener {
// Log.d("玩家: %s 未发现可用ChatRule!", p.getName()); // Log.d("玩家: %s 未发现可用ChatRule!", p.getName());
return; return;
} }
e.setCancelled(true); handleChat(p, e.getRecipients(), cr, e.getMessage());
handleChat(p, cr, e.getMessage());
} }
private void handleChat(Player p, ChatRule cr, String message) { private void handleChat(Player p, Set<Player> receive, ChatRule cr, String message) {
// Log.d("玩家: %s 使用 %s 规则 解析 %s", p.getName(), cr.getName(), message); // Log.d("玩家: %s 使用 %s 规则 解析 %s", p.getName(), cr.getName(), message);
handleSend(p, handleTellraw(p, cr.create(p), cr, message), cr.getRange()); handleSend(p, receive, handleTellraw(p, cr.create(p), cr, message), cr.getRange());
} }
private LinkedList<String> handleMessage(LinkedList<String> il, String message) { private LinkedList<String> handleMessage(LinkedList<String> il, String message) {
@ -100,19 +100,16 @@ public class ChatListener implements Listener {
return ilist; return ilist;
} }
private void handleSend(Player p, Tellraw tr, int range) { private void handleSend(Player p, Set<Player> receive, Tellraw tr, int range) {
List<Player> plist = new ArrayList<>(); Set<Player> plist = new HashSet<>();
if (range != 0) { if (range != 0) {
for (Entity ent : p.getNearbyEntities(range, range, range)) { p.getNearbyEntities(range, range, range).stream().filter(entity -> entity instanceof Player).forEach(entity -> plist.add((Player) entity));
if (ent instanceof Player) {
plist.add((Player) ent);
}
}
plist.add(p); plist.add(p);
} else { } else {
plist.addAll(C.Player.getOnlinePlayers()); plist.addAll(receive);
if (cc.isBungeeCord()) { if (cc.isBungeeCord()) {
byte[] mm = MiaoMessage.encode(tr.toJsonString()); byte[] mm = MiaoMessage.encode(tr.toJsonString());
// 数据流等于NULL代表数据超长
if (mm == null) { if (mm == null) {
p.sendPluginMessage(P.instance, MiaoMessage.NORMALCHANNEL, MiaoMessage.encode(tr.toOldMessageFormat())); p.sendPluginMessage(P.instance, MiaoMessage.NORMALCHANNEL, MiaoMessage.encode(tr.toOldMessageFormat()));
} else { } else {
@ -120,11 +117,9 @@ public class ChatListener implements Listener {
} }
} }
} }
for (Player player : plist) { receive.clear();
if (!offList.contains(player.getName())) { plist.removeAll(offList);
tr.send(player); plist.forEach(tr::send);
}
}
Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat()); Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat());
} }