feat: 完善物品展示 添加聊天开关

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-10 03:17:07 +08:00
parent f41da56c62
commit 335b04d01a
9 changed files with 264 additions and 31 deletions

View File

@ -5,6 +5,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import pw.yumc.MiaoChat.config.Config;
import pw.yumc.MiaoChat.listeners.ChatListener;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.CommandExecutor;
import pw.yumc.YumCore.commands.CommandManager;
@ -25,6 +26,20 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
return config;
}
@Cmd(permission = "MiaoChat.toggle")
@Help("关闭聊天功能")
public void off(final CommandArgument e) {
ChatListener.offList.remove(e.getSender().getName());
Log.toSender(e.getSender(), "§c聊天功能已关闭!");
}
@Cmd(permission = "MiaoChat.toggle")
@Help("开启聊天功能")
public void on(final CommandArgument e) {
ChatListener.offList.add(e.getSender().getName());
Log.toSender(e.getSender(), "§a聊天功能已开启!");
}
@Override
public void onEnable() {
new ChatListener();
@ -34,6 +49,7 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
@Override
public void onLoad() {
cfg = new FileConfig();
config = new Config();
}
@Cmd(permission = "MiaoChat.reload")
@ -41,6 +57,6 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
public void reload(final CommandArgument e) {
cfg.reload();
config.reload();
e.getSender().sendMessage("§a配置文件已重载!");
Log.toSender(e.getSender(), "§a配置文件已重载!");
}
}

View File

@ -7,6 +7,7 @@ import pw.yumc.YumCore.config.FileConfig;
public class ChatConfig {
private static String PrefixKey = "Format.Prefix";
private static String PlayerKey = "Format.Player";
private static String SuffixKey = "Format.Suffix";
private final FileConfig config;
private final List<ChatMessagePart> prefixs;
@ -39,7 +40,9 @@ public class ChatConfig {
prefixs.add(new ChatMessagePart(config.getConfigurationSection(PrefixKey + "." + part)));
}
}
player = new ChatMessagePart(config.getConfigurationSection("Format.Player"));
if (config.isSet(PlayerKey)) {
player = new ChatMessagePart(config.getConfigurationSection(PlayerKey));
}
suffixs.clear();
if (config.isSet(SuffixKey)) {
for (final String part : config.getConfigurationSection(SuffixKey).getKeys(false)) {

View File

@ -8,14 +8,18 @@ import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.YumCore.config.ConfigNode;
import pw.yumc.YumCore.config.InjectConfigurationSection;
import pw.yumc.YumCore.config.Nullable;
import pw.yumc.YumCore.tellraw.Tellraw;
public class ChatMessagePart extends InjectConfigurationSection {
private String text;
@Nullable
private List<String> tip;
@Nullable
@ConfigNode("click.type")
private String typestring;
private transient CLICKTYPE type = CLICKTYPE.SUGGEST;
@Nullable
@ConfigNode("click.command")
private String command;

View File

@ -1,7 +1,10 @@
package pw.yumc.MiaoChat.config;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.config.Default;
import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.config.InjectConfigurationSection;
/**
@ -11,41 +14,60 @@ import pw.yumc.YumCore.config.InjectConfigurationSection;
* @author
*/
public class ChatRule extends InjectConfigurationSection {
private transient String name;
@Default("50")
private Integer index;
private String permission;
@Default("0")
private Integer range;
private boolean item;
@Default("false")
private Boolean item;
@Default("&6[%s&6]&r")
private String itemformat;
private transient ChatConfig formats;
public ChatRule(final ConfigurationSection config) {
public ChatRule(final String name, final ConfigurationSection config) {
super(config);
this.name = name;
if (permission == null) {
permission = String.format("MiaoChat.%s", name);
}
formats = new ChatConfig(new FileConfig(name + ".yml"));
}
public boolean check(final Player player) {
return player.hasPermission(permission);
}
public ChatConfig getFormats() {
return formats;
}
/**
* @return the index
*/
public Integer getIndex() {
return index;
}
/**
* @return the permission
*/
public Boolean getItem() {
return item;
}
public String getItemformat() {
return itemformat;
}
public String getName() {
return name;
}
public String getPermission() {
return permission;
}
/**
* @return the range
*/
public Integer getRange() {
return range;
}
/**
* @return the item
*/
public boolean isItem() {
return item;
}
}

View File

@ -1,12 +1,15 @@
package pw.yumc.MiaoChat.config;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.entity.Player;
import cn.citycraft.PluginHelper.config.FileConfig;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.config.FileConfig;
/**
*
@ -14,10 +17,15 @@ import pw.yumc.YumCore.bukkit.P;
* @author
*/
public class Config {
FileConfig config = P.getConfig();
Map<String, ChatConfig> formats = new HashMap<>();
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();
}
@ -27,11 +35,30 @@ public class Config {
* @param player
* @return {@link ChatConfig}
*/
public ChatConfig getChatConfig(final Player player) {
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

@ -1,21 +1,38 @@
package pw.yumc.MiaoChat.listeners;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
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.Listener;
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.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]?)");
MiaoChat plugin = P.getPlugin();
public ChatListener() {
@ -27,10 +44,11 @@ public class ChatListener implements Listener {
@EventHandler
public void onChat(final AsyncPlayerChatEvent e) {
final Player p = e.getPlayer();
final ChatConfig cc = plugin.getConfigExt().getChatConfig(e.getPlayer());
if (cc == null) {
final ChatRule cr = plugin.getConfigExt().getChatRule(e.getPlayer());
if (cr == null) {
return;
}
final ChatConfig cc = cr.getFormats();
e.setCancelled(true);
final String msg = e.getMessage();
final Tellraw tr = Tellraw.create();
@ -41,7 +59,85 @@ public class ChatListener implements Listener {
for (final ChatMessagePart cmp : cc.getSuffixs()) {
cmp.then(tr, p);
}
tr.then(ChatColor.translateAlternateColorCodes('&', msg));
tr.broadcast();
final String message = ChatColor.translateAlternateColorCodes('&', msg);
if (!message.isEmpty() && cr.isItem()) {
if (!handlerTellraw(p, tr, message, cr.getItemformat())) {
Log.toSender(p, "§c不允许展示相同的物品!");
return;
}
}
final int range = cr.getRange();
Collection<? extends Entity> plist = Collections.emptyList();
if (range != 0) {
plist = p.getNearbyEntities(range, range, range);
} else {
plist = C.Player.getOnlinePlayers();
}
for (final Entity ne : plist) {
if (ne instanceof Player && !offList.contains(ne.getName())) {
tr.send(ne);
}
}
}
private LinkedList<String> handlerMessage(final LinkedList<String> il, String message) {
final LinkedList<String> mlist = new LinkedList<>();
if (!il.isEmpty()) {
for (final String k : il) {
final String[] args = message.split(k, 2);
mlist.add(args[0]);
mlist.add(k);
message = args[1];
}
}
if (!message.isEmpty()) {
mlist.add(message);
}
return mlist;
}
private LinkedList<String> handlerPattern(final String message) {
final Matcher m = ITEM_PATTERN.matcher(message);
final Set<String> temp = new HashSet<>();
final LinkedList<String> ilist = new LinkedList<>();
while (m.find()) {
final String key = m.group(0);
if (key.length() == 2) {
if (temp.add(key)) {
ilist.add(key);
} else {
return null;
}
}
}
return ilist;
}
private boolean handlerTellraw(final Player player, final Tellraw tr, final String message, final String itemformat) {
final LinkedList<String> il = handlerPattern(message);
if (il == null) {
return false;
}
final LinkedList<String> ml = handlerMessage(il, message);
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);
}
if (is != null && is.getType() != Material.AIR) {
tr.then(String.format(ChatColor.translateAlternateColorCodes('&', itemformat),
is.hasItemMeta() && is.getItemMeta().hasDisplayName() ? is.getItemMeta().getDisplayName() : is.getType().name()));
tr.item(is);
}
} else {
tr.then(mm);
}
}
return true;
}
}

View File

@ -0,0 +1,55 @@
#格式化配置
Format:
#前缀配置 于玩家名称前
Prefix:
0:
text: '&6[&c管理员&6]'
#顺序
1:
#文本 支持PAPI变量
text: '&6[&a%player_world%&6]'
#悬浮提示 支持PAPI
tip:
- '&6当前所在位置:'
- '&6世界: &d%player_world%'
- '&6坐标: &aX:%player_x% Y: %player_y% Z: %player_z%'
- ''
- '&c点击即可TP我!'
#点击操作
click:
#操作类型: [COMMAND,SUGGEST]
#COMMAND代表执行命令
#SUGGEST代表命令补全
type: 'COMMAND'
#命令 支持PAPI
command: '/tpa %player_name%'
# 可以添加多个
# 2:
# #文本 支持PAPI变量
# text: '&6[&a%player_world%&6]'
# #悬浮提示 支持PAPI
# tip: []
#玩家名称配置
Player:
text: '&b%player_name%'
tip:
- '&6玩家名称: &b%player_name%'
- '&6玩家等级: &a%player_level%'
- '&6玩家血量: &c%player_health%'
- '&6玩家饥饿: &d%player_food_level%'
- '&6游戏模式: &4%player_gamemode%'
- ''
- '&c点击与我聊天'
click:
type: 'SUGGEST'
command: '/tell %player_name%'
#后缀配置 于玩家名称后
Suffix:
1:
text: '&4[求助]&r '
tip:
- '点击求助OP'
click:
type: 'COMMAND'
command: '管理员@%player_name% 我需要你的帮助!'
#同样可以添加多个

View File

@ -11,15 +11,19 @@ Formats:
permission: 'MiaoChat.default'
#范围(0为无限制)
range: 0
#解析物品(%i%=>手上物品,%0-9%快捷栏对应物品)
#解析物品(%i=>手上物品,%0-9快捷栏对应物品)
item: true
#物品解析规则
itemformat: '&6[&b%s&6]&r'
#格式名称 对应当前文件夹下的admin.yml
admin:
#优先级(将按照从小到大依次检测 比如 1-50 优先检测 1 符合则显示 不符合 检测 2 ...)
index: 49
#权限
permission: 'MiaoChat.reload'
permission: 'MiaoChat.admin'
#范围(0为无限制)
range: 0
#解析物品(%i%=>手上物品,%0-9%快捷栏对应物品)
#解析物品(%i=>手上物品,%0-9快捷栏对应物品)
item: true
#物品解析规则
itemformat: '&6[&b%s&6]&r'

View File

@ -19,6 +19,12 @@ permissions:
${project.artifactId}.default:
description: 默认格式权限!
default: true
${project.artifactId}.admin:
description: 管理员格式权限!
default: op
${project.artifactId}.toggle:
description: 开启关闭聊天!
default: true
${project.artifactId}.reload:
description: 重新载入插件!
default: op