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

feat: 1.1版本发布

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-12 01:42:23 +08:00
parent 55b420f11c
commit f43af0b3a1
8 changed files with 114 additions and 102 deletions

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.0</version> <version>1.1</version>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
<resources> <resources>

View File

@ -1,9 +1,11 @@
package pw.yumc.MiaoChat; package pw.yumc.MiaoChat;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import pw.yumc.MiaoChat.config.Config; import pw.yumc.MiaoChat.config.ChatConfig;
import pw.yumc.MiaoChat.listeners.ChatListener; import pw.yumc.MiaoChat.listeners.ChatListener;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.commands.CommandArgument; import pw.yumc.YumCore.commands.CommandArgument;
@ -12,20 +14,21 @@ import pw.yumc.YumCore.commands.CommandManager;
import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.misc.L10N;
public class MiaoChat extends JavaPlugin implements CommandExecutor { public class MiaoChat extends JavaPlugin implements CommandExecutor {
private FileConfig cfg; private FileConfig cfg;
private Config config; private ChatConfig chatConfig;
public ChatConfig getChatConfig() {
return chatConfig;
}
@Override @Override
public FileConfiguration getConfig() { public FileConfiguration getConfig() {
return cfg; return cfg;
} }
public Config getConfigExt() {
return config;
}
@Cmd(permission = "MiaoChat.toggle") @Cmd(permission = "MiaoChat.toggle")
@Help("关闭聊天功能") @Help("关闭聊天功能")
public void off(final CommandArgument e) { public void off(final CommandArgument e) {
@ -44,19 +47,20 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
public void onEnable() { public void onEnable() {
new ChatListener(); new ChatListener();
new CommandManager("MiaoChat", this); new CommandManager("MiaoChat", this);
L10N.getItemFullName(new ItemStack(Material.DIAMOND_SWORD));
} }
@Override @Override
public void onLoad() { public void onLoad() {
cfg = new FileConfig(); cfg = new FileConfig();
config = new Config(); chatConfig = new ChatConfig();
} }
@Cmd(permission = "MiaoChat.reload") @Cmd(permission = "MiaoChat.reload")
@Help("重载配置文件") @Help("重载配置文件")
public void reload(final CommandArgument e) { public void reload(final CommandArgument e) {
cfg.reload(); cfg.reload();
config.reload(); chatConfig.reload();
Log.toSender(e.getSender(), "§a配置文件已重载!"); Log.toSender(e.getSender(), "§a配置文件已重载!");
} }
} }

View File

@ -1,28 +1,78 @@
package pw.yumc.MiaoChat.config; package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; 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 pw.yumc.YumCore.config.FileConfig;
/**
*
* @since 2016年9月9日 下午4:40:50
* @author
*/
public class ChatConfig { public class ChatConfig {
private final FileConfig config; private static final String F = "Formats";
private final Map<String, ChatMessagePart> formats; private final Map<String, ChatMessagePart> formats;
private final RuleComparator rulecomp;
private final List<ChatRule> rules;
private final FileConfig config;
private final FileConfig format;
public ChatConfig(final FileConfig cfg) { public ChatConfig() {
config = cfg; config = P.getConfig();
format = new FileConfig("format.yml");
rulecomp = new RuleComparator();
formats = new HashMap<>(); formats = new HashMap<>();
rules = new LinkedList<>();
reload(); reload();
} }
/**
* 获得玩家可用的消息处理
*
* @param player
* @return {@link ChatConfig}
*/
public ChatRule getChatRule(final Player player) {
for (final ChatRule cr : rules) {
Log.debug(cr.getName());
if (cr.check(player)) {
return cr;
}
}
return null;
}
public ChatMessagePart getFormat(final String name) { public ChatMessagePart getFormat(final String name) {
return formats.get(name); return formats.get(name);
} }
public void reload() { public void reload() {
formats.clear(); formats.clear();
for (final String name : config.getKeys(false)) { for (final String name : format.getKeys(false)) {
formats.put(name, new ChatMessagePart(config.getConfigurationSection(name))); formats.put(name, new ChatMessagePart(format.getConfigurationSection(name)));
}
rules.clear();
if (config.isSet(F)) {
for (final String rule : config.getConfigurationSection(F).getKeys(false)) {
rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule)));
}
}
Collections.sort(rules, rulecomp);
}
private class RuleComparator implements Comparator<ChatRule> {
@Override
public int compare(final ChatRule o1, final ChatRule o2) {
return o1.getIndex().compareTo(o2.getIndex());
} }
} }
} }

View File

@ -17,13 +17,13 @@ import pw.yumc.YumCore.config.InjectConfigurationSection;
* @author * @author
*/ */
public class ChatRule extends InjectConfigurationSection { public class ChatRule extends InjectConfigurationSection {
private transient static final Pattern FORMAT_PATTERN = Pattern.compile("[#]([^#]+)[#]"); private transient static final Pattern FORMAT_PATTERN = Pattern.compile("[\\[]([^\\[\\]]+)[\\]]");
private transient String name; private transient String name;
@Default("50") @Default("50")
private Integer index; private Integer index;
@Default("MiaoChat.default") @Default("MiaoChat.default")
private String permission; private String permission;
@Default("#world#player: ") @Default("#world##player#: ")
private String format; private String format;
@Default("0") @Default("0")
private Integer range; private Integer range;
@ -40,10 +40,7 @@ public class ChatRule extends InjectConfigurationSection {
permission = String.format("MiaoChat.%s", name); permission = String.format("MiaoChat.%s", name);
} }
formats = new LinkedList<>(); formats = new LinkedList<>();
final Matcher m = FORMAT_PATTERN.matcher(format); load();
while (m.find()) {
formats.add(m.group(1));
}
} }
public boolean check(final Player player) { public boolean check(final Player player) {
@ -85,4 +82,24 @@ public class ChatRule extends InjectConfigurationSection {
public boolean isItem() { public boolean isItem() {
return item; return item;
} }
private void load() {
final Matcher m = FORMAT_PATTERN.matcher(format);
final LinkedList<String> temp = new LinkedList<>();
while (m.find()) {
temp.add(m.group(1));
}
String tempvar = format;
if (!temp.isEmpty()) {
for (final String var : temp) {
final String[] args = tempvar.split("\\[" + var + "\\]", 2);
formats.add(args[0]);
formats.add(var);
tempvar = args[1];
}
if (!tempvar.isEmpty()) {
formats.add(tempvar);
}
}
}
} }

View File

@ -1,64 +0,0 @@
package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.config.FileConfig;
/**
*
* @since 2016年9月9日 下午4:40:50
* @author
*/
public class Config {
private static final String F = "Formats";
private final RuleComparator rulecomp;
private final List<ChatRule> rules;
private final FileConfig config;
public Config() {
config = P.getConfig();
rulecomp = new RuleComparator();
rules = new LinkedList<>();
reload();
}
/**
* 获得玩家可用的消息处理
*
* @param player
* @return {@link ChatConfig}
*/
public ChatRule getChatRule(final Player player) {
for (final ChatRule cr : rules) {
Log.debug(cr.getName());
if (cr.check(player)) {
return cr;
}
}
return null;
}
public void reload() {
rules.clear();
if (config.isSet(F)) {
for (final String rule : config.getConfigurationSection(F).getKeys(false)) {
rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule)));
}
}
Collections.sort(rules, rulecomp);
}
private class RuleComparator implements Comparator<ChatRule> {
@Override
public int compare(final ChatRule o1, final ChatRule o2) {
return o1.getIndex().compareTo(o2.getIndex());
}
}
}

View File

@ -20,19 +20,23 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import pw.yumc.MiaoChat.MiaoChat; import pw.yumc.MiaoChat.MiaoChat;
import pw.yumc.MiaoChat.config.ChatConfig;
import pw.yumc.MiaoChat.config.ChatMessagePart;
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.bukkit.compatible.C;
import pw.yumc.YumCore.misc.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<String> offList = new HashSet<>();
static final Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); private static final Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
MiaoChat plugin = P.getPlugin(); private final MiaoChat plugin = P.getPlugin();
private final ChatConfig cc = plugin.getChatConfig();
public ChatListener() { public ChatListener() {
Bukkit.getPluginManager().registerEvents(this, P.instance); Bukkit.getPluginManager().registerEvents(this, P.instance);
@ -43,7 +47,7 @@ public class ChatListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChat(final AsyncPlayerChatEvent e) { public void onChat(final AsyncPlayerChatEvent e) {
final Player p = e.getPlayer(); final Player p = e.getPlayer();
final ChatRule cr = plugin.getConfigExt().getChatRule(e.getPlayer()); final ChatRule cr = cc.getChatRule(e.getPlayer());
if (cr == null) { if (cr == null) {
return; return;
} }
@ -61,7 +65,12 @@ public class ChatListener implements Listener {
private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) { private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) {
final LinkedList<String> formats = cr.getFormats(); final LinkedList<String> formats = cr.getFormats();
for (final String format : formats) { for (final String format : formats) {
final ChatMessagePart cmp = cc.getFormat(format);
if (cmp != null) {
cmp.then(tr, p);
} else {
tr.then(format);
}
} }
} }
@ -103,6 +112,7 @@ public class ChatListener implements Listener {
Collection<? extends Entity> plist = Collections.emptyList(); Collection<? extends Entity> plist = Collections.emptyList();
if (range != 0) { if (range != 0) {
plist = p.getNearbyEntities(range, range, range); plist = p.getNearbyEntities(range, range, range);
tr.send(p);
} else { } else {
plist = C.Player.getOnlinePlayers(); plist = C.Player.getOnlinePlayers();
} }
@ -130,15 +140,10 @@ public class ChatListener implements Listener {
while (!ml.isEmpty()) { while (!ml.isEmpty()) {
final String mm = ml.removeFirst(); final String mm = ml.removeFirst();
if (il.contains(mm)) { if (il.contains(mm)) {
ItemStack is = null;
final char k = mm.charAt(1); final char k = mm.charAt(1);
if (k == 'i') { final ItemStack is = k == 'i' ? player.getItemInHand() : player.getInventory().getItem(k - '0' - 1);
is = player.getItemInHand();
} else {
is = player.getInventory().getItem(k - '0' - 1);
}
if (is != null && is.getType() != Material.AIR) { if (is != null && is.getType() != Material.AIR) {
tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), is.hasItemMeta() && is.getItemMeta().hasDisplayName() ? is.getItemMeta().getDisplayName() : is.getType().name())); tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getItemName(is)));
tr.item(is); tr.item(is);
} }
} else { } else {

View File

@ -1,5 +1,5 @@
#配置文件版本号 请勿修改 #配置文件版本号 请勿修改
Version: 1.0 Version: 1.1
#格式列表 #格式列表
Formats: Formats:
@ -11,8 +11,8 @@ Formats:
permission: 'MiaoChat.default' permission: 'MiaoChat.default'
#范围(0为无限制) #范围(0为无限制)
range: 0 range: 0
#聊天格式(#xxx会调用format.yml下的对应格式) #聊天格式([xxx]会调用format.yml下的对应格式)
format: '#world#player: ' format: '[world][player]: '
#解析物品(%i=>手上物品,%0-9快捷栏对应物品) #解析物品(%i=>手上物品,%0-9快捷栏对应物品)
item: true item: true
#物品解析规则 #物品解析规则
@ -23,8 +23,8 @@ Formats:
index: 49 index: 49
#权限 #权限
permission: 'MiaoChat.admin' permission: 'MiaoChat.admin'
#聊天格式(#xxx会调用format.yml下的对应格式) #聊天格式([xxx]会调用format.yml下的对应格式)
format: '#admin#world#player#help: ' format: '[admin][world][player][help]: '
#范围(0为无限制) #范围(0为无限制)
range: 0 range: 0
#解析物品(%i=>手上物品,%0-9快捷栏对应物品) #解析物品(%i=>手上物品,%0-9快捷栏对应物品)

View File

@ -19,7 +19,7 @@ world:
#命令或网址 支持PAPI #命令或网址 支持PAPI
command: '/tpa %player_name%' command: '/tpa %player_name%'
player: player:
text: '&b%player_name%: ' text: '&b%player_name%'
tip: tip:
- '&6玩家名称: &b%player_name%' - '&6玩家名称: &b%player_name%'
- '&6玩家等级: &a%player_level%' - '&6玩家等级: &a%player_level%'
@ -34,7 +34,7 @@ player:
admin: admin:
text: '&6[&c管理员&6]' text: '&6[&c管理员&6]'
help: help:
text: '&4[求助]&r ' text: '&4[求助]'
tip: tip:
- '点击求助OP' - '点击求助OP'
click: click: