1
0
mirror of https://e.coding.net/circlecloud/MiaoBoard.git synced 2024-11-22 01:49:05 +00:00

feat: 调整记分板更新逻辑

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-03-09 11:11:35 +08:00
parent 76e567618d
commit d7a5276831
9 changed files with 32 additions and 292 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>MiaoBoard</artifactId> <artifactId>MiaoBoard</artifactId>
<version>2.3.3</version> <version>2.3.5</version>
<description>喵式记分板</description> <description>喵式记分板</description>
<build> <build>
<finalName>${project.artifactId}</finalName> <finalName>${project.artifactId}</finalName>
@ -70,6 +70,12 @@
<url>http://ci.yumc.pw/job/${project.artifactId}/</url> <url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement> </ciManagement>
<properties> <properties>
<update.description>§a正式版本 2.3.5</update.description>
<update.changes>
§617-03-09 §cfix: 修复行更新错误;
§617-03-09 §afeat: 添加记分板更新事件
</update.changes>
<update.changelog></update.changelog>
<env.GIT_COMMIT>DEV</env.GIT_COMMIT> <env.GIT_COMMIT>DEV</env.GIT_COMMIT>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>

View File

@ -9,9 +9,9 @@ import pw.yumc.MiaoBoard.MiaoBoard;
import pw.yumc.MiaoBoard.event.BodyUpdateEvent; import pw.yumc.MiaoBoard.event.BodyUpdateEvent;
import pw.yumc.MiaoBoard.event.TitleUpdateEvent; import pw.yumc.MiaoBoard.event.TitleUpdateEvent;
import pw.yumc.MiaoBoard.misc.Checker; import pw.yumc.MiaoBoard.misc.Checker;
import pw.yumc.MiaoBoard.misc.Replace;
import pw.yumc.MiaoBoard.model.BoardModel; import pw.yumc.MiaoBoard.model.BoardModel;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.text.Replace;
/** /**
* 自身记分板监听类 * 自身记分板监听类

View File

@ -1,162 +0,0 @@
package pw.yumc.MiaoBoard.misc;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C;
public class Replace {
public static List<String> $(final Player p, final List<String> text) {
final List<String> temp = new LinkedList<>();
for (final String i : text) {
temp.add($(p, i));
}
return temp;
}
public static String $(final Player p, final String text) {
return p(p, text);
}
private static String p(final Player p, final String text) {
return PlaceholderAPI.setPlaceholders(p, SimpleRelpace.$(p, text));
}
static class SimpleRelpace {
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("[%]([^%]+)[%]");
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static String $(final Player player, String text) {
final Matcher m = PLACEHOLDER_PATTERN.matcher(text);
while (m.find()) {
final String format = m.group(1);
if (format.contains("_")) {
final String[] ka = format.split("_", 2);
String value = null;
switch (ka[0]) {
case "player":
value = player(player, ka[1]);
break;
case "server":
value = server(player, ka[1]);
break;
case "plugin":
value = plugin(player, ka[1]);
break;
case "time":
value = time(player, ka[1]);
break;
}
if (value != null) {
text = text.replace("%" + format + "%", Matcher.quoteReplacement(value));
}
}
}
return text;
}
private 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 "yaw":
return String.valueOf(Math.round(player.getLocation().getYaw() * 100) / 100);
case "pitch":
return String.valueOf(Math.round(player.getLocation().getPitch() * 100) / 100);
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 String.format("%%player_%s%%", key);
}
}
private static String plugin(final Player player, final String key) {
switch (key) {
case "version":
return P.getDescription().getVersion().split("-")[0];
case "name":
return P.getName();
case "author":
return Arrays.toString(P.getDescription().getAuthors().toArray());
default:
return String.format("%%plugin_%s%%", key);
}
}
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 "max":
return String.valueOf(Bukkit.getMaxPlayers());
case "unique_joins":
return String.valueOf(Bukkit.getOfflinePlayers().length);
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 String.format("%%server_%s%%", key);
}
}
private static String time(final Player player, final String key) {
final Date date = new Date();
if (key.startsWith("left") && key.contains("_")) {
final String time = key.split("_")[1];
String value = "解析错误";
try {
final long left = df.parse(time).getTime() - System.currentTimeMillis();
value = String.valueOf(left / 1000);
} catch (final ParseException ignored) {
}
return value;
}
switch (key) {
case "now":
return df.format(date);
case "year":
return String.valueOf(date.getYear() + 1900);
case "month":
return String.valueOf(date.getMonth() + 1);
case "day":
return String.valueOf(date.getDate());
case "hour":
return String.valueOf(date.getHours() + 1);
case "minute":
return String.valueOf(date.getMinutes());
case "second":
return String.valueOf(date.getSeconds());
}
return String.format("%%time_%s%%", key);
}
}
}

View File

@ -8,11 +8,8 @@ import org.bukkit.entity.Player;
import pw.yumc.MiaoBoard.config.MiaoBoardConfig; import pw.yumc.MiaoBoard.config.MiaoBoardConfig;
import pw.yumc.MiaoBoard.model.BoardModel; import pw.yumc.MiaoBoard.model.BoardModel;
import pw.yumc.MiaoBoard.scoreboard.core.BoardUpdateFunction;
import pw.yumc.MiaoBoard.scoreboard.core.Condition; import pw.yumc.MiaoBoard.scoreboard.core.Condition;
import pw.yumc.MiaoBoard.scoreboard.core.SidebarBoard; import pw.yumc.MiaoBoard.scoreboard.core.SidebarBoard;
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.P;
import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.bukkit.compatible.C;
import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.FileConfig;
@ -25,7 +22,7 @@ import pw.yumc.YumCore.config.FileConfig;
*/ */
public class ScoreBoardManager { public class ScoreBoardManager {
public Status cot = new Status(); public Status cot = new Status();
public SidebarBoard sbd = new SidebarBoard(P.instance, new BoardUpdateFunction(new TitleUpdater(), new BodyUpdater())); public SidebarBoard sbd = new SidebarBoard(P.instance);
public FileConfig config = MiaoBoardConfig.i().getConfig(); public FileConfig config = MiaoBoardConfig.i().getConfig();
public List<BoardModel> bms = new LinkedList<>(); public List<BoardModel> bms = new LinkedList<>();

View File

@ -22,10 +22,7 @@ public abstract class Board implements Iterable<Player> {
private final HashMap<Player, BoardPage> targets = new HashMap<>(); private final HashMap<Player, BoardPage> targets = new HashMap<>();
private final Set<Player> removeQueue = new HashSet<>(); private final Set<Player> removeQueue = new HashSet<>();
private final BoardUpdateFunction updateFunction; public Board(final Plugin plugin) {
public Board(final Plugin plugin, final BoardUpdateFunction updateFunction) {
this.updateFunction = updateFunction;
this.plugin = plugin; this.plugin = plugin;
} }
@ -64,10 +61,6 @@ public abstract class Board implements Iterable<Player> {
return targets.keySet(); return targets.keySet();
} }
public BoardUpdateFunction getUpdateFunction() {
return updateFunction;
}
public boolean isRunning() { public boolean isRunning() {
return this.taskId != 0; return this.taskId != 0;
} }
@ -92,9 +85,7 @@ public abstract class Board implements Iterable<Player> {
} }
public void update(final Condition condition, final int interval) { public void update(final Condition condition, final int interval) {
taskId = plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() { taskId = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> {
@Override
public void run() {
if (condition.get()) { if (condition.get()) {
final Iterator<Player> iterator = iterator(); final Iterator<Player> iterator = iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
@ -108,7 +99,6 @@ public abstract class Board implements Iterable<Player> {
} else { } else {
cancel(); cancel();
} }
}
}, 0, interval).getTaskId(); }, 0, interval).getTaskId();
} }

View File

@ -1,39 +0,0 @@
package pw.yumc.MiaoBoard.scoreboard.core;
import java.util.List;
import org.bukkit.entity.Player;
import pw.yumc.YumCore.callback.CallBackReturn;
/**
*
* @since 2016年7月4日 下午4:40:21
* @author 尘曲
*/
public class BoardUpdateFunction {
private CallBackReturn.One<Player, String> titleFunction;
private CallBackReturn.One<Player, List<String>> bodyFunction;
public BoardUpdateFunction(final CallBackReturn.One<Player, String> titleFunction, final CallBackReturn.One<Player, List<String>> bodyFunction) {
this.titleFunction = titleFunction;
this.bodyFunction = bodyFunction;
}
public CallBackReturn.One<Player, List<String>> getBodyFunction() {
return bodyFunction;
}
public CallBackReturn.One<Player, String> getTitleFunction() {
return titleFunction;
}
public void setBodyFunction(final CallBackReturn.One<Player, List<String>> bodyFunction) {
this.bodyFunction = bodyFunction;
}
public void setTitleFunction(final CallBackReturn.One<Player, String> titleFunction) {
this.titleFunction = titleFunction;
}
}

View File

@ -4,14 +4,17 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import pw.yumc.MiaoBoard.event.BodyUpdateEvent;
import pw.yumc.MiaoBoard.event.TitleUpdateEvent;
/** /**
* *
* @since 2016年7月4日 下午4:40:21 * @since 2016年7月4日 下午4:40:21
* @author 尘曲 * @author 尘曲
*/ */
public class SidebarBoard extends Board { public class SidebarBoard extends Board {
public SidebarBoard(final Plugin plugin, final BoardUpdateFunction updateFunction) { public SidebarBoard(final Plugin plugin) {
super(plugin, updateFunction); super(plugin);
} }
@Override @Override
@ -28,16 +31,17 @@ public class SidebarBoard extends Board {
public void update(final Player player) { public void update(final Player player) {
final SiderbarBoardPage boardPage = this.getBoardPage(player); final SiderbarBoardPage boardPage = this.getBoardPage(player);
if (boardPage == null) { return; } if (boardPage == null) { return; }
String title = null; TitleUpdateEvent te = new TitleUpdateEvent(player);
if (this.getUpdateFunction().getTitleFunction() != null) { Bukkit.getPluginManager().callEvent(te);
title = this.getUpdateFunction().getTitleFunction().run(player); String title = te.getTitle();
}
if (title == null) { if (title == null) {
player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard()); player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
return; return;
} }
boardPage.setTitle(title); boardPage.setTitle(title);
boardPage.setBody(getUpdateFunction().getBodyFunction().run(player)); BodyUpdateEvent be = new BodyUpdateEvent(player);
Bukkit.getPluginManager().callEvent(be);
boardPage.setBody(be.getBody());
player.setScoreboard(boardPage.getBoard()); player.setScoreboard(boardPage.getBoard());
} }
} }

View File

@ -1,29 +0,0 @@
package pw.yumc.MiaoBoard.scoreboard.updater;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import pw.yumc.MiaoBoard.MiaoBoard;
import pw.yumc.MiaoBoard.event.BodyUpdateEvent;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.callback.CallBackReturn;
/**
* 记分板行更新类
*
* @since 2016年7月4日 下午4:47:17
* @author
*/
public class BodyUpdater extends CallBackReturn.One<Player, List<String>> {
MiaoBoard plugin = P.getPlugin();
@Override
public List<String> run(final Player param) {
BodyUpdateEvent event = new BodyUpdateEvent(param);
Bukkit.getPluginManager().callEvent(event);
return event.getBody();
}
}

View File

@ -1,27 +0,0 @@
package pw.yumc.MiaoBoard.scoreboard.updater;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import pw.yumc.MiaoBoard.MiaoBoard;
import pw.yumc.MiaoBoard.event.TitleUpdateEvent;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.callback.CallBackReturn;
/**
* 记分板标题更新类
*
* @since 2016年7月4日 下午4:47:17
* @author
*/
public class TitleUpdater extends CallBackReturn.One<Player, String> {
MiaoBoard plugin = P.getPlugin();
@Override
public String run(final Player param) {
TitleUpdateEvent event = new TitleUpdateEvent(param);
Bukkit.getPluginManager().callEvent(event);
return event.getTitle();
}
}