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

refactor: 代码结构优化

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-29 18:34:20 +08:00
parent f7dd78dee8
commit 5a53213666
6 changed files with 22 additions and 26 deletions

View File

@ -10,6 +10,7 @@ import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.CommandExecutor; import pw.yumc.YumCore.commands.CommandExecutor;
import pw.yumc.YumCore.commands.CommandManager; import pw.yumc.YumCore.commands.CommandManager;
import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Cmd.Executor;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.annotation.Sort; import pw.yumc.YumCore.commands.annotation.Sort;
@ -26,7 +27,7 @@ public class MiaoBoard extends JavaPlugin implements CommandExecutor {
return scoreBoardManager; return scoreBoardManager;
} }
@Cmd(permission = "mb.toggle") @Cmd(permission = "mb.toggle", executor = Executor.PLAYER)
@Help("关闭记分板") @Help("关闭记分板")
@Sort(2) @Sort(2)
public void off(final CommandArgument e) { public void off(final CommandArgument e) {
@ -34,7 +35,7 @@ public class MiaoBoard extends JavaPlugin implements CommandExecutor {
Log.toSender(e.getSender(), "§c记分板已关闭!"); Log.toSender(e.getSender(), "§c记分板已关闭!");
} }
@Cmd(permission = "mb.toggle") @Cmd(permission = "mb.toggle", executor = Executor.PLAYER)
@Help("打开记分板") @Help("打开记分板")
@Sort(1) @Sort(1)
public void on(final CommandArgument e) { public void on(final CommandArgument e) {

View File

@ -13,8 +13,8 @@ import pw.yumc.YumCore.config.InjectConfig;
*/ */
public class MiaoBoardConfig extends InjectConfig { public class MiaoBoardConfig extends InjectConfig {
public transient static MiaoBoardConfig instance = new MiaoBoardConfig(); public transient static MiaoBoardConfig instance = new MiaoBoardConfig();
private Integer UpdateTime; public Integer UpdateTime;
private List<String> DisableWorld; public List<String> DisableWorld = Collections.emptyList();
public static MiaoBoardConfig i() { public static MiaoBoardConfig i() {
return instance; return instance;
@ -23,15 +23,4 @@ public class MiaoBoardConfig extends InjectConfig {
public static void reInject() { public static void reInject() {
instance.reload(); instance.reload();
} }
public List<String> getDisableWorld() {
if (DisableWorld == null) {
DisableWorld = Collections.emptyList();
}
return DisableWorld;
}
public Integer getUpdateTime() {
return UpdateTime;
}
} }

View File

@ -9,6 +9,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import pw.yumc.MiaoBoard.MiaoBoard; import pw.yumc.MiaoBoard.MiaoBoard;
import pw.yumc.MiaoBoard.config.MiaoBoardConfig; import pw.yumc.MiaoBoard.config.MiaoBoardConfig;
import pw.yumc.MiaoBoard.scoreboard.ScoreBoardManager;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.statistic.Statistics; import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.update.SubscribeTask; import pw.yumc.YumCore.update.SubscribeTask;
@ -21,6 +22,7 @@ import pw.yumc.YumCore.update.SubscribeTask;
*/ */
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
MiaoBoard plugin = P.getPlugin(); MiaoBoard plugin = P.getPlugin();
ScoreBoardManager manager = plugin.getScoreBoardManager();
public PlayerListener() { public PlayerListener() {
Bukkit.getPluginManager().registerEvents(this, plugin); Bukkit.getPluginManager().registerEvents(this, plugin);
@ -30,20 +32,20 @@ public class PlayerListener implements Listener {
@EventHandler @EventHandler
public void onPlayerChangeWorld(final PlayerChangedWorldEvent e) { public void onPlayerChangeWorld(final PlayerChangedWorldEvent e) {
if (MiaoBoardConfig.i().getDisableWorld().contains(e.getPlayer().getWorld().getName())) { if (MiaoBoardConfig.i().DisableWorld.contains(e.getPlayer().getWorld().getName())) {
plugin.getScoreBoardManager().getSidebarBoard().removeTarget(e.getPlayer()); manager.removeTarget(e.getPlayer());
} else { } else {
plugin.getScoreBoardManager().getSidebarBoard().addTarget(e.getPlayer()); manager.addTarget(e.getPlayer());
} }
} }
@EventHandler @EventHandler
public void onPlayerJoin(final PlayerJoinEvent e) { public void onPlayerJoin(final PlayerJoinEvent e) {
plugin.getScoreBoardManager().addTarget(e.getPlayer()); manager.addTarget(e.getPlayer());
} }
@EventHandler @EventHandler
public void onPlayerQuit(final PlayerQuitEvent e) { public void onPlayerQuit(final PlayerQuitEvent e) {
plugin.getScoreBoardManager().getSidebarBoard().removeTarget(e.getPlayer()); manager.removeTarget(e.getPlayer());
} }
} }

View File

@ -35,7 +35,7 @@ public class ScoreBoardManager {
} }
public void addTarget(final Player player) { public void addTarget(final Player player) {
if (!MiaoBoardConfig.i().getDisableWorld().contains(player.getWorld().getName())) { if (!MiaoBoardConfig.i().DisableWorld.contains(player.getWorld().getName())) {
getSidebarBoard().addTarget(player); getSidebarBoard().addTarget(player);
} }
} }
@ -63,8 +63,12 @@ public class ScoreBoardManager {
start(); start();
} }
public void removeTarget(final Player player) {
sbd.removeTarget(player);
}
public void start() { public void start() {
sbd.update(cot.set(true), MiaoBoardConfig.i().getUpdateTime()); sbd.update(cot.set(true), MiaoBoardConfig.i().UpdateTime);
for (final Player player : C.Player.getOnlinePlayers()) { for (final Player player : C.Player.getOnlinePlayers()) {
addTarget(player); addTarget(player);
} }

View File

@ -45,7 +45,7 @@ public abstract class Board implements Iterable<Player> {
plugin.getServer().getScheduler().cancelTask(taskId); plugin.getServer().getScheduler().cancelTask(taskId);
taskId = 0; taskId = 0;
for (final Player player : this.targets.keySet()) { for (final Player player : this.targets.keySet()) {
player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
} }
this.targets.clear(); this.targets.clear();
this.removeQueue.clear(); this.removeQueue.clear();
@ -85,7 +85,7 @@ public abstract class Board implements Iterable<Player> {
public boolean removeTarget(final Player player) { public boolean removeTarget(final Player player) {
if (this.isTarget(player)) { if (this.isTarget(player)) {
player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
return this.removeQueue.add(player); return this.removeQueue.add(player);
} }
return false; return false;
@ -93,7 +93,6 @@ 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().runTaskTimer(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
if (condition.get()) { if (condition.get()) {

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -49,7 +50,7 @@ public class SidebarBoard extends Board {
title = this.getUpdateFunction().getTitleFunction().run(player); title = this.getUpdateFunction().getTitleFunction().run(player);
} }
if (title == null) { if (title == null) {
this.removeTarget(player); player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
return; return;
} }
final List<String> newContents = this.getUpdateFunction().getBodyFunction().run(player); final List<String> newContents = this.getUpdateFunction().getBodyFunction().run(player);