1
0
mirror of https://e.coding.net/circlecloud/MiaoBoard.git synced 2024-12-22 06:49:02 +00:00

feat: 添加自身变量支持

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-08-16 14:48:16 +08:00
parent 840dd57056
commit 03a92697d0
7 changed files with 125 additions and 33 deletions

View File

@ -1,17 +1,15 @@
package pw.yumc.MiaoBoard;
import java.net.MalformedURLException;
import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.PluginHelper.commands.HandlerCommand;
import cn.citycraft.PluginHelper.commands.HandlerCommands;
import cn.citycraft.PluginHelper.commands.InvokeCommandEvent;
import cn.citycraft.PluginHelper.commands.InvokeSubCommand;
import cn.citycraft.PluginHelper.kit.PluginKit;
import cn.citycraft.PluginHelper.utils.VersionChecker;
import pw.yumc.MiaoBoard.listener.PlayerListener;
import pw.yumc.MiaoBoard.scoreboard.ScoreBoardManager;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.CommandExecutor;
import pw.yumc.YumCore.commands.CommandManager;
import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.update.SubscribeTask;
/**
@ -20,7 +18,7 @@ import pw.yumc.YumCore.update.SubscribeTask;
* @since 2016年6月4日 上午9:08:13
* @author
*/
public class MiaoBoard extends JavaPlugin implements HandlerCommands {
public class MiaoBoard extends JavaPlugin implements CommandExecutor {
@Override
public void onDisable() {
@ -29,17 +27,11 @@ public class MiaoBoard extends JavaPlugin implements HandlerCommands {
@Override
public void onEnable() {
new InvokeSubCommand(this, "mb").registerCommands(this);
ScoreBoardManager.start();
new CommandManager("mb").register(this);
new PlayerListener();
try {
new VersionChecker(this, "aHR0cDovL2NpLnl1bWMucHcvam9iL01pYW9Cb2FyZC9sYXN0U3VjY2Vzc2Z1bEJ1aWxkL2FydGlmYWN0L3BvbS54bWw=".getBytes());
ScoreBoardManager.start();
} catch (final MalformedURLException e) {
PluginKit.disable("DON'T TRY TO CHANGE THE URL...");
throw new IllegalAccessError("Access Check Failed...");
}
SubscribeTask.navite = true;
new SubscribeTask(true);
new Statistics();
new SubscribeTask(true, true);
}
@Override
@ -47,8 +39,9 @@ public class MiaoBoard extends JavaPlugin implements HandlerCommands {
ScoreBoardManager.load();
}
@HandlerCommand(name = "reload", permission = "mb.reload", description = "重新载入配置文件")
public void reload(final InvokeCommandEvent e) {
@Cmd(permission = "mb.reload")
@Help("重新载入配置文件")
public void reload(final CommandArgument e) {
ScoreBoardManager.reload();
e.getSender().sendMessage("§a配置重载完毕!");
}

View File

@ -7,9 +7,9 @@ import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import cn.citycraft.PluginHelper.kit.P;
import pw.yumc.MiaoBoard.config.MiaoBoardConfig;
import pw.yumc.MiaoBoard.scoreboard.ScoreBoardManager;
import pw.yumc.YumCore.bukkit.P;
/**
* 玩家监听

View File

@ -0,0 +1,11 @@
package pw.yumc.MiaoBoard.misc;
import org.bukkit.entity.Player;
import pw.yumc.MiaoBoard.model.BoardModel;
public class Checker {
public static boolean $(final Player player, final BoardModel model) {
return player.hasPermission(model.permission);
}
}

View File

@ -0,0 +1,88 @@
package pw.yumc.MiaoBoard.misc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.entity.Player;
import cn.citycraft.PluginHelper.kit.StrKit;
import cn.citycraft.PluginHelper.pluginapi.PluginAPI;
import pw.yumc.YumCore.bukkit.compatible.C;
public class Replace {
public static String $(final Player p, final String text) {
return s(p(p, text));
}
private static String p(final Player p, final String text) {
return SimpleRelpace.$(p, PluginAPI.PlaceholderAPI(p, text));
}
private static String s(final String text) {
return StrKit.substring(text, 0, 40);
}
static class SimpleRelpace {
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
public static String $(final Player player, String text) {
final Matcher m = PLACEHOLDER_PATTERN.matcher(text);
while (m.find()) {
final String k = m.group(1);
if (k.contains("_")) {
final String[] ka = k.split("_", 2);
switch (ka[0]) {
case "player":
text = player(player, ka[1]);
break;
case "server":
text = server(player, ka[1]);
break;
}
}
}
return text;
}
public static String player(final Player player, final String key) {
switch (key) {
case "x":
return String.valueOf(player.getLocation().getBlockX());
case "y":
return String.valueOf(player.getLocation().getBlockY());
case "z":
return String.valueOf(player.getLocation().getBlockZ());
case "world":
return player.getWorld().getName();
case "name":
return player.getName();
case "displayname":
return player.getDisplayName();
case "health":
return String.valueOf(player.getHealth());
case "max_health":
return String.valueOf(player.getMaxHealth());
default:
return "";
}
}
private static String server(final Player player, final String key) {
final Runtime runtime = Runtime.getRuntime();
switch (key) {
case "online":
return String.valueOf(C.Player.getOnlinePlayers().size());
case "ram_used":
return String.valueOf((runtime.totalMemory() - runtime.freeMemory()) / 1048576L);
case "ram_free":
return String.valueOf(runtime.freeMemory() / 1048576L);
case "ram_total":
return String.valueOf(runtime.totalMemory() / 1048576L);
case "ram_max":
return String.valueOf(runtime.maxMemory() / 1048576L);
default:
return "";
}
}
}
}

View File

@ -6,15 +6,15 @@ import java.util.List;
import org.bukkit.entity.Player;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.kit.P;
import cn.citycraft.PluginHelper.scoreboard.BoardUpdateFunction;
import cn.citycraft.PluginHelper.scoreboard.Condition;
import cn.citycraft.PluginHelper.scoreboard.SidebarBoard;
import cn.citycraft.PluginHelper.utils.CompatibleUtil;
import pw.yumc.MiaoBoard.config.MiaoBoardConfig;
import pw.yumc.MiaoBoard.model.BoardModel;
import pw.yumc.MiaoBoard.scoreboard.updater.BodyUpdater;
import pw.yumc.MiaoBoard.scoreboard.updater.TitleUpdater;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
/**
* 记分板管理类
@ -52,7 +52,7 @@ public class ScoreBoardManager {
public static void start() {
sbd.update(cot, MiaoBoardConfig.UpdateTime);
for (final Player player : CompatibleUtil.getOnlinePlayers()) {
for (final Player player : C.Player.getOnlinePlayers()) {
sbd.addTarget(player);
}
}

View File

@ -8,8 +8,8 @@ import java.util.List;
import org.bukkit.entity.Player;
import cn.citycraft.PluginHelper.callback.CallBackReturn;
import cn.citycraft.PluginHelper.kit.StrKit;
import cn.citycraft.PluginHelper.pluginapi.PluginAPI;
import pw.yumc.MiaoBoard.misc.Checker;
import pw.yumc.MiaoBoard.misc.Replace;
import pw.yumc.MiaoBoard.model.BoardModel;
import pw.yumc.MiaoBoard.scoreboard.ScoreBoardManager;
@ -26,10 +26,10 @@ public class BodyUpdater extends CallBackReturn.One<Player, List<String>> {
final Iterator<BoardModel> iterator = ScoreBoardManager.getModels().iterator();
while (iterator.hasNext()) {
final BoardModel bmodel = iterator.next();
if (param.hasPermission(bmodel.permission)) {
if (Checker.$(param, bmodel)) {
final List<String> temp = new LinkedList<>();
for (final String line : bmodel.lines) {
temp.add(StrKit.substring(PluginAPI.PlaceholderAPI(param, line), 0, 40));
temp.add(Replace.$(param, line));
}
return temp;
}

View File

@ -5,8 +5,8 @@ import java.util.Iterator;
import org.bukkit.entity.Player;
import cn.citycraft.PluginHelper.callback.CallBackReturn;
import cn.citycraft.PluginHelper.kit.StrKit;
import cn.citycraft.PluginHelper.pluginapi.PluginAPI;
import pw.yumc.MiaoBoard.misc.Checker;
import pw.yumc.MiaoBoard.misc.Replace;
import pw.yumc.MiaoBoard.model.BoardModel;
import pw.yumc.MiaoBoard.scoreboard.ScoreBoardManager;
@ -23,8 +23,8 @@ public class TitleUpdater extends CallBackReturn.One<Player, String> {
final Iterator<BoardModel> iterator = ScoreBoardManager.getModels().iterator();
while (iterator.hasNext()) {
final BoardModel bmodel = iterator.next();
if (param.hasPermission(bmodel.permission)) {
return StrKit.substring(PluginAPI.PlaceholderAPI(param, bmodel.title), 0, 40);
if (Checker.$(param, bmodel)) {
return Replace.$(param, bmodel.title);
}
}
return "";