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 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>
<groupId>pw.yumc</groupId>
<artifactId>MiaoBoard</artifactId>
<version>2.3.3</version>
<version>2.3.5</version>
<description>喵式记分板</description>
<build>
<finalName>${project.artifactId}</finalName>
@ -70,6 +70,12 @@
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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.TitleUpdateEvent;
import pw.yumc.MiaoBoard.misc.Checker;
import pw.yumc.MiaoBoard.misc.Replace;
import pw.yumc.MiaoBoard.model.BoardModel;
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.model.BoardModel;
import pw.yumc.MiaoBoard.scoreboard.core.BoardUpdateFunction;
import pw.yumc.MiaoBoard.scoreboard.core.Condition;
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.compatible.C;
import pw.yumc.YumCore.config.FileConfig;
@ -25,7 +22,7 @@ import pw.yumc.YumCore.config.FileConfig;
*/
public class ScoreBoardManager {
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 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 Set<Player> removeQueue = new HashSet<>();
private final BoardUpdateFunction updateFunction;
public Board(final Plugin plugin, final BoardUpdateFunction updateFunction) {
this.updateFunction = updateFunction;
public Board(final Plugin plugin) {
this.plugin = plugin;
}
@ -64,10 +61,6 @@ public abstract class Board implements Iterable<Player> {
return targets.keySet();
}
public BoardUpdateFunction getUpdateFunction() {
return updateFunction;
}
public boolean isRunning() {
return this.taskId != 0;
}
@ -92,22 +85,19 @@ public abstract class Board implements Iterable<Player> {
}
public void update(final Condition condition, final int interval) {
taskId = plugin.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
@Override
public void run() {
if (condition.get()) {
final Iterator<Player> iterator = iterator();
while (iterator.hasNext()) {
final Player next = iterator.next();
if (shouldRemove(next)) {
iterator.remove();
} else {
update(next);
}
taskId = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> {
if (condition.get()) {
final Iterator<Player> iterator = iterator();
while (iterator.hasNext()) {
final Player next = iterator.next();
if (shouldRemove(next)) {
iterator.remove();
} else {
update(next);
}
} else {
cancel();
}
} else {
cancel();
}
}, 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.plugin.Plugin;
import pw.yumc.MiaoBoard.event.BodyUpdateEvent;
import pw.yumc.MiaoBoard.event.TitleUpdateEvent;
/**
*
* @since 2016年7月4日 下午4:40:21
* @author 尘曲
*/
public class SidebarBoard extends Board {
public SidebarBoard(final Plugin plugin, final BoardUpdateFunction updateFunction) {
super(plugin, updateFunction);
public SidebarBoard(final Plugin plugin) {
super(plugin);
}
@Override
@ -28,16 +31,17 @@ public class SidebarBoard extends Board {
public void update(final Player player) {
final SiderbarBoardPage boardPage = this.getBoardPage(player);
if (boardPage == null) { return; }
String title = null;
if (this.getUpdateFunction().getTitleFunction() != null) {
title = this.getUpdateFunction().getTitleFunction().run(player);
}
TitleUpdateEvent te = new TitleUpdateEvent(player);
Bukkit.getPluginManager().callEvent(te);
String title = te.getTitle();
if (title == null) {
player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
return;
}
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());
}
}

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();
}
}