From f9a405a5ca0a263f2859803309cdfc6fdda1a428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sat, 17 Mar 2018 22:34:49 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E8=87=B3?= =?UTF-8?q?=EF=BC=9A3.81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增:TagManager 工具,用于更改玩家头顶称号,并兼容其他计分板插件。 修复:Language2 工具的 [json] 类型 ”url“ 参数失效。 --- resources/plugin.yml | 2 +- .../listener/ListenerPluginDisable.java | 54 +++-- .../expression/ExpressionTabooCodeItem.java | 12 +- .../string/language2/value/Language2Json.java | 2 +- .../me/skymc/taboolib/team/TagManager.java | 187 ++++++++++++++++++ .../java/me/skymc/taboolib/team/TagUtils.java | 137 ++++++++----- 6 files changed, 319 insertions(+), 75 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/team/TagManager.java diff --git a/resources/plugin.yml b/resources/plugin.yml index 51c4d3f..01ff73e 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -6,7 +6,7 @@ website: http://www.15imc.com/index.html main: me.skymc.taboolib.Main -version: 3.8 +version: 3.81 commands: taboolib: diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java b/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java index cdbb08a..51c3ccc 100644 --- a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java +++ b/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java @@ -1,9 +1,15 @@ package me.skymc.taboolib.listener; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.scheduler.BukkitRunnable; +import me.skymc.taboolib.Main; import me.skymc.taboolib.message.MsgUtils; import me.skymc.taboolib.mysql.MysqlUtils; import me.skymc.taboolib.mysql.protect.MySQLConnection; @@ -13,20 +19,42 @@ public class ListenerPluginDisable implements Listener { @EventHandler public void disable(PluginDisableEvent e) { - int i = 0; - for (MySQLConnection conn : MysqlUtils.CONNECTIONS) { - if (conn.getPlugin().equals(e.getPlugin())) { - MysqlUtils.CONNECTIONS.remove(conn); - conn.closeConnection(); - i++; - } - } - if (i > 0) { - MsgUtils.send("已停止插件 &f" + e.getPlugin().getName() + "&7 的 &f" + i + "&7 条数据库连接"); - } - // 注销时间周期 TimeCycleManager.cancel(e.getPlugin()); + + // 获取连接 + List conns = new ArrayList<>(); + for (MySQLConnection conn : MysqlUtils.CONNECTIONS) { + if (conn.getPlugin().equals(e.getPlugin())) { + conns.add(conn); + MysqlUtils.CONNECTIONS.remove(conn); + } + } + + // 异步注销 + BukkitRunnable runnable = new BukkitRunnable() { + + @Override + public void run() { + int i = 0; + for (MySQLConnection conn : conns) { + conn.setFallReconnection(false); + conn.closeConnection(); + i++; + } + if (i > 0) { + MsgUtils.send("已停止插件 &f" + e.getPlugin().getName() + "&7 的 &f" + i + "&7 条数据库连接"); + } + } + }; + + // 如果插件关闭 + try { + runnable.runTaskLater(Main.getInst(), 40); + } + catch (Exception err) { + MsgUtils.warn("异步任务失败, 执行方式改为同步执行"); + runnable.run(); + } } - } diff --git a/src/main/java/me/skymc/taboolib/skript/expression/ExpressionTabooCodeItem.java b/src/main/java/me/skymc/taboolib/skript/expression/ExpressionTabooCodeItem.java index 41555f2..38e407c 100644 --- a/src/main/java/me/skymc/taboolib/skript/expression/ExpressionTabooCodeItem.java +++ b/src/main/java/me/skymc/taboolib/skript/expression/ExpressionTabooCodeItem.java @@ -9,6 +9,7 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Kleenean; +import me.skymc.taboocode.TabooCodeItem; /** * @author sky @@ -17,8 +18,6 @@ import ch.njol.util.Kleenean; public class ExpressionTabooCodeItem extends SimpleExpression { private Expression name; - private Class tabooCodeItem; - private Method getItem; @Override public Class getReturnType() { @@ -34,13 +33,6 @@ public class ExpressionTabooCodeItem extends SimpleExpression { @Override public boolean init(Expression[] args, int arg1, Kleenean arg2, ParseResult arg3) { name = (Expression) args[0]; - try { - tabooCodeItem = Class.forName("me.skymc.taboocode.TabooCodeItem"); - getItem = tabooCodeItem.getMethod("getItem", String.class, Boolean.class); - } - catch (Exception e) { - // - } return true; } @@ -52,7 +44,7 @@ public class ExpressionTabooCodeItem extends SimpleExpression { @Override protected ItemStack[] get(Event e) { try { - ItemStack item = (ItemStack) getItem.invoke(null, name.getSingle(e), false); + ItemStack item = TabooCodeItem.getItem(name.getSingle(e), false); return new ItemStack[] { item == null ? null : item.clone() }; } catch (Exception err) { diff --git a/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json.java b/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json.java index 731b949..5b1303f 100644 --- a/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json.java +++ b/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json.java @@ -88,7 +88,7 @@ public class Language2Json implements Language2Line { } // 打开连接 else if (message.startsWith(KEY_URL)) { - clickEvent = new OpenUrlEvent(message.substring(KEY_SUGGEST.length())); + clickEvent = new OpenUrlEvent(message.substring(KEY_URL.length())); } // 换行 else if (message.equals("[break]")) { diff --git a/src/main/java/me/skymc/taboolib/team/TagManager.java b/src/main/java/me/skymc/taboolib/team/TagManager.java new file mode 100644 index 0000000..79b5958 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/team/TagManager.java @@ -0,0 +1,187 @@ +package me.skymc.taboolib.team; + +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.Team; + +import lombok.Getter; +import lombok.Setter; +import me.skymc.taboolib.Main; + +/** + * @author sky + * @since 2018-03-17 21:43:49 + */ +public class TagManager implements Listener { + + private static TagManager inst; + + @Getter + private HashMap playerdata = new HashMap<>(); + + private TagManager() { + Bukkit.getPluginManager().registerEvents(this, Main.getInst()); + } + + public static TagManager getInst() { + synchronized (TagManager.class) { + if (inst == null) { + inst = new TagManager(); + } + } + return inst; + } + + /** + * 设置玩家前缀 + * + * @param player 名称 + * @param prefix 前缀 + */ + public void setPrefix(Player player, String prefix) { + getPlayerData(player).setPrefix(prefix); + uploadData(player); + } + + /** + * 设置玩家后缀 + * + * @param player 玩家 + * @param suffix 后缀 + */ + public void setSuffix(Player player, String suffix) { + getPlayerData(player).setSuffix(suffix); + uploadData(player); + } + + /** + * 获取玩家前缀 + * + * @param player 玩家 + * @return String + */ + public String getPrefix(Player player) { + return getPlayerData(player).getPrefix(); + } + + /** + * 获取玩家后缀 + * + * @param player 玩家 + * @return String + */ + public String getSuffix(Player player) { + return getPlayerData(player).getSuffix(); + } + + /** + * 获取玩家数据 + * + * @param player 玩家 + * @return {@link PlayerData} + */ + private PlayerData getPlayerData(Player player) { + PlayerData data = playerdata.get(player.getName()); + if (data == null) { + data = new PlayerData(player.getName()); + playerdata.put(player.getName(), data); + } + return data; + } + + /** + * 将该玩家的数据向服务器所有玩家更新 + * + * @param player 玩家 + */ + public void uploadData(Player player) { + PlayerData data = getPlayerData(player); + String prefix = data.getPrefix().length() > 16 ? data.getPrefix().substring(0, 16) : data.getPrefix(); + String suffix = data.getSuffix().length() > 16 ? data.getSuffix().substring(0, 16) : data.getSuffix(); + // 如果没有称号数据 + if (prefix.isEmpty() && suffix.isEmpty()) { + return; + } + + for (Player _player : Bukkit.getOnlinePlayers()) { + Scoreboard scoreboard = _player.getScoreboard(); + if (scoreboard == null) { + _player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); + } + Team team = scoreboard.getTeam(player.getName()); + if (team == null) { + team = scoreboard.registerNewTeam(player.getName()); + team.addEntry(player.getName()); + } + team.setPrefix(prefix); + team.setSuffix(suffix); + } + } + + /** + * 下载服务器内的称号数据到该玩家 + * + * @param player 玩家 + */ + public void downloadData(Player player) { + Scoreboard scoreboard = player.getScoreboard(); + if (scoreboard == null) { + player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); + } + + for (Player _player : Bukkit.getOnlinePlayers()) { + PlayerData data = getPlayerData(_player); + String prefix = data.getPrefix().length() > 16 ? data.getPrefix().substring(0, 16) : data.getPrefix(); + String suffix = data.getSuffix().length() > 16 ? data.getSuffix().substring(0, 16) : data.getSuffix(); + // 如果没有称号数据 + if (prefix.isEmpty() && suffix.isEmpty()) { + continue; + } + + Team team = scoreboard.getTeam(_player.getName()); + if (team == null) { + team = scoreboard.registerNewTeam(_player.getName()); + team.addEntry(_player.getName()); + } + team.setPrefix(prefix); + team.setSuffix(suffix); + } + } + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + downloadData(e.getPlayer()); + } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + playerdata.remove(e.getPlayer().getName()); + } + + static class PlayerData { + + @Getter + private String name; + + @Getter + @Setter + private String prefix; + + @Getter + @Setter + private String suffix; + + public PlayerData(String name) { + this.name = name; + this.prefix = ""; + this.suffix = ""; + } + } +} diff --git a/src/main/java/me/skymc/taboolib/team/TagUtils.java b/src/main/java/me/skymc/taboolib/team/TagUtils.java index 81e2ec7..e9d96a9 100644 --- a/src/main/java/me/skymc/taboolib/team/TagUtils.java +++ b/src/main/java/me/skymc/taboolib/team/TagUtils.java @@ -9,90 +9,127 @@ import org.bukkit.scoreboard.Team; import me.skymc.taboolib.message.MsgUtils; import me.skymc.taboolib.methods.MethodsUtils; -@Deprecated +/** + * @author sky + * @since 2018-03-17 21:36:52 + */ public class TagUtils { - public static void setTag(Player p, String prefix, String suffix) throws Exception { + /** + * 设置玩家前后缀 + * + * @param p 玩家 + * @param prefix 前缀 + * @param suffix 后缀 + */ + public static void setTag(Player p, String prefix, String suffix) { + // 判断长度 + if (prefix.length() > 16) { + prefix = prefix.substring(0, 16); + } if (suffix.length() > 16) { suffix = suffix.substring(0, 16); } + + // 获取计分板 Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard(); Team t = board.getTeam(p.getName()); - if (t == null) - { + if (t == null) { t = board.registerNewTeam(p.getName()); - t.setPrefix(prefix); - t.setSuffix(suffix); - t.addPlayer(p); - - } - else - { - t = board.getTeam(p.getName()); - t.setPrefix(prefix); - t.setSuffix(suffix); - t.addPlayer(p); } + + // 更新称号 + t.setPrefix(prefix); + t.setSuffix(suffix); + t.addEntry(p.getName()); + + // 更新玩家 for (Player o : Bukkit.getOnlinePlayers()) { - o.setScoreboard(board); - } - } - - public static void unregisterTag(Player p) throws Exception - { - Bukkit.getScoreboardManager().getMainScoreboard().getPlayerTeam(p).unregister(); - } - - public static void delete() { - try { - for (Team t : Bukkit.getScoreboardManager().getMainScoreboard().getTeams()) { - t.unregister(); + if (!board.equals(o.getScoreboard())) { + o.setScoreboard(board); } } - catch (Exception e) { - // TODO: handle exception - } } - public static void unregisterAll() throws Exception - { + /** + * 注销玩家前后缀 + * + * @param p 玩家 + */ + public static void unregisterTag(Player p) { + Team team = Bukkit.getScoreboardManager().getMainScoreboard().getTeam(p.getName()); + if (team != null) { + team.unregister(); + } + } + + /** + * 注销所有在线玩家前后缀 + */ + public static void unregisterAll() { for (Player o : Bukkit.getOnlinePlayers()) { unregisterTag(o); } } - - public static void registerAll(String prefix, String suffix) throws Exception - { - for (Player o : Bukkit.getOnlinePlayers()) { - setTag(o, prefix, " " + suffix); + + /** + * 删除所有前后缀 + */ + public static void delete() { + for (Team t : Bukkit.getScoreboardManager().getMainScoreboard().getTeams()) { + t.unregister(); } } - public static void refresh() - { + /** + * 设置全服玩家前后缀 + * + * @param prefix 前缀 + * @param suffix 后缀 + */ + public static void registerAll(String prefix, String suffix) { + for (Player o : Bukkit.getOnlinePlayers()) { + setTag(o, prefix, suffix); + } + } + + /** + * 刷新计分板数据 + */ + public static void refresh() { Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard(); for (Player o : Bukkit.getOnlinePlayers()) { o.setScoreboard(board); } } - public static String getPrefix(Player p) throws Exception - { + /** + * 获取玩家前缀 + * + * @param p 玩家 + * @return String + */ + public static String getPrefix(Player p) { Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard(); Team t = board.getTeam(p.getName()); - if ((t != null) && (board.getPlayerTeam(p).getPrefix() != null) && (!board.getPlayerTeam(p).getPrefix().isEmpty())) { - return board.getPlayerTeam(p).getPrefix(); + if (t != null) { + return t.getPrefix(); } - return ""; + return null; } - public static String getSuffix(Player p) throws Exception - { + /** + * 获取玩玩家后缀 + * + * @param p 玩家 + * @return String + */ + public static String getSuffix(Player p) { Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard(); Team t = board.getTeam(p.getName()); - if ((t != null) && (board.getPlayerTeam(p).getSuffix() != null) && (!board.getPlayerTeam(p).getSuffix().isEmpty())) { - return board.getPlayerTeam(p).getSuffix(); + if (t != null) { + return t.getSuffix(); } - return ""; + return null; } }