4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-11-21 14:28:46 +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>
<groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId>
<version>1.7.1</version>
<version>1.8</version>
<build>
<finalName>${project.name}</finalName>
<resources>
@ -16,7 +16,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<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>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
@ -32,12 +38,6 @@
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
@ -72,8 +72,8 @@
<properties>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
@ -93,7 +93,7 @@
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>
<type>jar</type>
<version>1.6</version>
<version>1.7</version>
</dependency>
</dependencies>
</project>

View File

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

View File

@ -1,7 +1,11 @@
package pw.yumc.MiaoChat;
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.config.ServerInfo;
@ -22,11 +26,11 @@ public class MiaoChatBungee extends Plugin implements Listener {
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 : groups.get(origin)) {
groups.get(origin).forEach(server -> {
if (!server.getAddress().equals(origin) && server.getPlayers().size() > 0) {
server.sendData(event.getTag(), event.getData());
}
}
});
}
}
@ -42,24 +46,20 @@ public class MiaoChatBungee extends Plugin implements Listener {
Set<ServerInfo> unused = new HashSet<>();
Configuration groupSel = config.getSection("Groups");
Collection<String> groupname = groupSel.getKeys();
for (String gname : groupname) {
groupname.forEach(gname -> {
Set<String> servers = new HashSet<>(groupSel.getStringList(gname));
Set<ServerInfo> sers = new HashSet<>();
for (String sname : servers) {
sers.add(temp.get(sname));
}
servers.forEach(sname -> sers.add(temp.get(sname)));
sers.remove(null);
for (String sname : servers) {
servers.forEach(sname -> {
ServerInfo isadd = temp.get(sname);
if (isadd != null) {
unused.remove(isadd);
groups.put(isadd.getAddress(), sers);
}
}
}
for (ServerInfo unser : unused) {
groups.put(unser.getAddress(), unused);
}
});
});
unused.forEach(unser -> groups.put(unser.getAddress(), unused));
}
@Override

View File

@ -1,6 +1,5 @@
package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
@ -20,7 +19,6 @@ import pw.yumc.YumCore.config.FileConfig;
public class ChatConfig {
private static String F = "Formats";
private Map<String, ChatMessagePart> formats;
private RuleComparator rulecomp;
private LinkedList<ChatRule> rules;
private FileConfig config;
private FileConfig format;
@ -31,7 +29,6 @@ public class ChatConfig {
config = P.getConfig();
BungeeCord = config.getBoolean("BungeeCord");
format = new FileConfig("format.yml");
rulecomp = new RuleComparator();
formats = new HashMap<>();
rules = new LinkedList<>();
load();
@ -64,18 +61,18 @@ public class ChatConfig {
public void load() {
servername = config.getMessage("Server");
formats.clear();
for (String name : format.getKeys(false)) {
format.getKeys(false).forEach(name -> {
formats.put(name, new ChatMessagePart(format.getConfigurationSection(name)));
Log.d("载入聊天格式: %s", name);
}
});
rules.clear();
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)));
Log.d("载入聊天规则: %s => \"%s\"", rule, rules.getLast().getFormat());
});
}
}
Collections.sort(rules, rulecomp);
rules.sort(Comparator.comparing(ChatRule::getIndex));
}
public String getServername() {
@ -86,11 +83,4 @@ public class ChatConfig {
format.reload();
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) {
Tellraw tr = Tellraw.create();
for (String format : formats) {
formats.forEach(format -> {
ChatMessagePart cmp = plugin.getChatConfig().getFormats().get(format);
if (cmp != null) {
// Log.d("解析格式: %s", format);
@ -104,7 +104,7 @@ public class ChatRule extends InjectConfigurationSection {
// Log.d("追加文本: %s", format);
tr.then(PlaceholderAPI.setPlaceholders(p, format));
}
}
});
return tr;
}

View File

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