4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-12-21 19:08: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>
<groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId>
<version>1.0</version>
<version>1.1</version>
<build>
<finalName>${project.name}</finalName>
<resources>

View File

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

View File

@ -1,28 +1,78 @@
package pw.yumc.MiaoChat.config;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
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;
/**
*
* @since 2016年9月9日 下午4:40:50
* @author
*/
public class ChatConfig {
private final FileConfig config;
private static final String F = "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) {
config = cfg;
public ChatConfig() {
config = P.getConfig();
format = new FileConfig("format.yml");
rulecomp = new RuleComparator();
formats = new HashMap<>();
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 ChatMessagePart getFormat(final String name) {
return formats.get(name);
}
public void reload() {
formats.clear();
for (final String name : config.getKeys(false)) {
formats.put(name, new ChatMessagePart(config.getConfigurationSection(name)));
for (final String name : format.getKeys(false)) {
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
*/
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;
@Default("50")
private Integer index;
@Default("MiaoChat.default")
private String permission;
@Default("#world#player: ")
@Default("#world##player#: ")
private String format;
@Default("0")
private Integer range;
@ -40,10 +40,7 @@ public class ChatRule extends InjectConfigurationSection {
permission = String.format("MiaoChat.%s", name);
}
formats = new LinkedList<>();
final Matcher m = FORMAT_PATTERN.matcher(format);
while (m.find()) {
formats.add(m.group(1));
}
load();
}
public boolean check(final Player player) {
@ -85,4 +82,24 @@ public class ChatRule extends InjectConfigurationSection {
public boolean isItem() {
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 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.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
import pw.yumc.YumCore.misc.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<>();
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() {
Bukkit.getPluginManager().registerEvents(this, P.instance);
@ -43,7 +47,7 @@ public class ChatListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChat(final AsyncPlayerChatEvent e) {
final Player p = e.getPlayer();
final ChatRule cr = plugin.getConfigExt().getChatRule(e.getPlayer());
final ChatRule cr = cc.getChatRule(e.getPlayer());
if (cr == null) {
return;
}
@ -61,7 +65,12 @@ public class ChatListener implements Listener {
private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) {
final LinkedList<String> formats = cr.getFormats();
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();
if (range != 0) {
plist = p.getNearbyEntities(range, range, range);
tr.send(p);
} else {
plist = C.Player.getOnlinePlayers();
}
@ -130,15 +140,10 @@ public class ChatListener implements Listener {
while (!ml.isEmpty()) {
final String mm = ml.removeFirst();
if (il.contains(mm)) {
ItemStack is = null;
final char k = mm.charAt(1);
if (k == 'i') {
is = player.getItemInHand();
} else {
is = player.getInventory().getItem(k - '0' - 1);
}
final ItemStack is = k == 'i' ? player.getItemInHand() : player.getInventory().getItem(k - '0' - 1);
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);
}
} else {

View File

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

View File

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