From 31044286414e1154a5873e3682cad0168057a165 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 16 Oct 2017 00:22:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E5=A4=84=E7=90=86=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- pom.xml | 5 +- .../pw/yumc/MiaoBoard/misc/FakePlayer.java | 99 +++++++ .../scoreboard/core/SidebarBoard.java | 10 +- ...arBoardPage.java => SidebarBoardPage.java} | 261 +++++++++--------- ...ageTest.java => SidebarBoardPageTest.java} | 19 +- 5 files changed, 247 insertions(+), 147 deletions(-) create mode 100644 src/main/java/pw/yumc/MiaoBoard/misc/FakePlayer.java rename src/main/java/pw/yumc/MiaoBoard/scoreboard/core/{SiderbarBoardPage.java => SidebarBoardPage.java} (67%) rename src/test/java/pw/yumc/MiaoBoard/scoreboard/core/{SiderbarBoardPageTest.java => SidebarBoardPageTest.java} (63%) diff --git a/pom.xml b/pom.xml index 7d07bae..97314bc 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 pw.yumc MiaoBoard - 2.3.7 + 2.4.0 喵式记分板 ${project.artifactId} @@ -71,8 +71,9 @@ http://ci.yumc.pw/job/${project.artifactId}/ - §a正式版本 2.3.7 + §a正式版本 2.4.0 + §617-10-15 §cfix: 修复颜色字符分割错误; §617-08-15 §cfix: 修复事件未标记为异步的问题; §617-06-28 §cfix: 修复T端不兼容的问题; §617-03-09 §cfix: 修复行更新错误; diff --git a/src/main/java/pw/yumc/MiaoBoard/misc/FakePlayer.java b/src/main/java/pw/yumc/MiaoBoard/misc/FakePlayer.java new file mode 100644 index 0000000..fe98d54 --- /dev/null +++ b/src/main/java/pw/yumc/MiaoBoard/misc/FakePlayer.java @@ -0,0 +1,99 @@ +package pw.yumc.MiaoBoard.misc; + +import java.util.Map; +import java.util.UUID; + +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import com.google.common.base.Charsets; + +/** + * Created with IntelliJ IDEA + * + * @author 喵♂呜 + * Created on 2017/10/15 23:30. + */ +public class FakePlayer implements OfflinePlayer { + private String name; + + public FakePlayer(String name) { + this.name = name; + } + + @Override + public boolean isOnline() { + return false; + } + + @Override + public String getName() { + return name; + } + + @Override + public UUID getUniqueId() { + return UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)); + } + + @Override + public boolean isBanned() { + return false; + } + + @Override + public void setBanned(boolean banned) { + + } + + @Override + public boolean isWhitelisted() { + return false; + } + + @Override + public void setWhitelisted(boolean value) { + + } + + @Override + public Player getPlayer() { + return null; + } + + @Override + public long getFirstPlayed() { + return 0; + } + + @Override + public long getLastPlayed() { + return 0; + } + + @Override + public boolean hasPlayedBefore() { + return false; + } + + @Override + public Location getBedSpawnLocation() { + return null; + } + + @Override + public Map serialize() { + return null; + } + + @Override + public boolean isOp() { + return false; + } + + @Override + public void setOp(boolean value) { + + } +} diff --git a/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoard.java b/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoard.java index b81e75f..5c03738 100644 --- a/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoard.java +++ b/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoard.java @@ -18,19 +18,19 @@ public class SidebarBoard extends Board { } @Override - public SiderbarBoardPage getBoardPage(final Player player) { - return (SiderbarBoardPage) super.getBoardPage(player); + public SidebarBoardPage getBoardPage(final Player player) { + return (SidebarBoardPage) super.getBoardPage(player); } @Override - public SiderbarBoardPage newPage() { - return new SiderbarBoardPage(); + public SidebarBoardPage newPage() { + return new SidebarBoardPage(); } @Override public void update(final Player player) { Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> { - final SiderbarBoardPage boardPage = this.getBoardPage(player); + final SidebarBoardPage boardPage = this.getBoardPage(player); if (boardPage == null) { return; } TitleUpdateEvent te = new TitleUpdateEvent(player); Bukkit.getPluginManager().callEvent(te); diff --git a/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPage.java b/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPage.java similarity index 67% rename from src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPage.java rename to src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPage.java index e5fda91..75ee4e6 100644 --- a/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPage.java +++ b/src/main/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPage.java @@ -1,129 +1,132 @@ -package pw.yumc.MiaoBoard.scoreboard.core; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.apache.commons.lang.Validate; -import org.bukkit.ChatColor; -import org.bukkit.scoreboard.DisplaySlot; -import org.bukkit.scoreboard.Objective; -import org.bukkit.scoreboard.Team; - -import pw.yumc.YumCore.bukkit.compatible.C; -import pw.yumc.YumCore.kit.StrKit; - -/** - * - * @since 2016年7月4日 下午4:40:21 - * @author 尘曲 - */ -public class SiderbarBoardPage extends BoardPage { - private static boolean newVer = true; - static { - try { - Team.class.getDeclaredMethod("addEntry", String.class); - } catch (NoSuchMethodException e) { - newVer = false; - } - } - - private static final List colors = Arrays.asList(ChatColor.values()); //所有颜色 - private final Objective objective; - private final List boardLines = new ArrayList<>();// "行" - private int maxLine;//用于标注最大行数 - - public SiderbarBoardPage() { - super(); - objective = getBoard().registerNewObjective("default", "dummy"); - objective.setDisplaySlot(DisplaySlot.SIDEBAR); - for (int i = 0; i < colors.size(); i++) { //循环所有的颜色 - final ChatColor color = colors.get(i); - final Team team = getBoard().registerNewTeam("MiaoboardLine" + i); //为每个颜色注册一个队伍 - //为队伍设置一个"行" - if (newVer) { - team.addEntry(color.toString()); - } else { - team.addPlayer(C.Player.getOfflinePlayer(color.toString())); - } - boardLines.add(new BoardLine(color, team)); //将"行"添加至列表 - } - } - - public Objective getObjective() { - return objective; - } - - public void setTitle(String title) { - objective.setDisplayName(title); - } - - public void setValue(int line, String value) { - final BoardLine boardLine = getBoardLine(line); //得到我们的"行" - Validate.notNull(boardLine, "Unable to find BoardLine with index of " + line + "."); //确认是否存在 - objective.getScore(boardLine.getColor().toString()).setScore(line); //设置"行" - //分割字符串为前16个和后16个 - String prefix = StrKit.substring(value, 0, 16); - String suffix = ""; - if (value.length() > 16) { - suffix = value.substring(16, value.length()); - //处理前后的颜色 - String sufpre = ChatColor.getLastColors(prefix); - if (value.charAt(15) == '§') { - sufpre = "§"; - } else if (!suffix.isEmpty() && suffix.charAt(0) == '§') { - sufpre = ""; - } - suffix = StrKit.substring(sufpre + suffix, 0, 16); - } - boardLine.getTeam().setPrefix(prefix); //设置前16个字符 - boardLine.getTeam().setSuffix(suffix); //设置后16个字符 - maxLine = line + 1; - } - - //all 5 [0 1 2 3 4] maxLine = 5 all 3 [0 1 2] maxLine=4 - public void clear(int size) { - if (maxLine > size) { - for (int i = size; i < maxLine; i++) { - removeLine(i); - } - maxLine = size; - } - } - - public void removeLine(int line) { - final BoardLine boardLine = getBoardLine(line); - Validate.notNull(boardLine, "Unable to find BoardLine with index of " + line + "."); //确认是否存在 - getBoard().resetScores(boardLine.getColor().toString()); //删除这个"行" - } - - private BoardLine getBoardLine(int line) { - return boardLines.get(line); - } - - public void setBody(List newContents) { - for (int i = 0; i < newContents.size(); i++) { - setValue(newContents.size() - i, newContents.get(i)); - } - clear(newContents.size()); - } - - class BoardLine { - private final ChatColor color; - private final Team team; - - public BoardLine(ChatColor color, Team team) { - this.color = color; - this.team = team; - } - - public ChatColor getColor() { - return color; - } - - public Team getTeam() { - return team; - } - - } -} +package pw.yumc.MiaoBoard.scoreboard.core; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.lang.Validate; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Team; + +import pw.yumc.MiaoBoard.misc.FakePlayer; +import pw.yumc.YumCore.kit.StrKit; + +/** + * + * @since 2016年7月4日 下午4:40:21 + * @author 尘曲 + */ +public class SidebarBoardPage extends BoardPage { + private static boolean newVer = true; + static { + try { + Team.class.getDeclaredMethod("addEntry", String.class); + } catch (NoSuchMethodException e) { + newVer = false; + } + } + + private static final List colors = Arrays.asList(ChatColor.values()); //所有颜色 + private final Objective objective; + private final List boardLines = new ArrayList<>();// "行" + private int maxLine;//用于标注最大行数 + + public SidebarBoardPage() { + super(); + objective = getBoard().registerNewObjective("default", "dummy"); + objective.setDisplaySlot(DisplaySlot.SIDEBAR); + for (int i = 0; i < colors.size(); i++) { //循环所有的颜色 + final String name = colors.get(i) + "" + ChatColor.RESET; + final Team team = getBoard().registerNewTeam("MiaoboardLine" + i); //为每个颜色注册一个队伍 + boardLines.add(new BoardLine(name, team)); //将"行"添加至列表 + } + } + + public Objective getObjective() { + return objective; + } + + public void setTitle(String title) { + objective.setDisplayName(title); + } + + public void setValue(int line, String value) { + final BoardLine boardLine = getBoardLine(line); //得到我们的"行" + Validate.notNull(boardLine, "Unable to find BoardLine with index of " + line + "."); //确认是否存在 + objective.getScore(boardLine.getName()).setScore(line); //设置"行" + //分割字符串为前16个和后16个 + String prefix = value; + String suffix = ""; + if (value.length() > 16) { + int splitIndex = value.charAt(15) == '§' ? 15 : 16; + prefix = StrKit.substring(value, 0, splitIndex); + suffix = value.substring(splitIndex, value.length()); + // 如果过suffix开头不是颜色符号就把prefix颜色转移到suffix + if (suffix.charAt(0) != '§') suffix = ChatColor.getLastColors(prefix) + suffix; + if (suffix.length() > 16) suffix = suffix.substring(16, suffix.length()); + } + boardLine.getTeam().setPrefix(prefix); //设置前16个字符 + boardLine.getTeam().setSuffix(suffix); //设置后16个字符 + maxLine = line + 1; + } + + //all 5 [0 1 2 3 4] maxLine = 5 all 3 [0 1 2] maxLine=4 + public void clear(int size) { + if (maxLine > size) { + for (int i = size; i < maxLine; i++) { + removeLine(i); + } + maxLine = size; + } + } + + public void removeLine(int line) { + final BoardLine boardLine = getBoardLine(line); + Validate.notNull(boardLine, "Unable to find BoardLine with index of " + line + "."); //确认是否存在 + getBoard().resetScores(boardLine.getName()); //删除这个"行" + } + + private BoardLine getBoardLine(int line) { + return boardLines.get(line); + } + + public void setBody(List newContents) { + for (int i = 0; i < newContents.size(); i++) { + setValue(newContents.size() - i, newContents.get(i)); + } + clear(newContents.size()); + } + + class BoardLine { + private String name; + private Team team; + private OfflinePlayer player; + + public BoardLine(String name, Team team) { + this.name = name; + this.team = team; + this.player = new FakePlayer(name); + addEntry(); + } + + public void addEntry() { + if (newVer) { + team.addEntry(name); + } else { + team.addPlayer(player); + } + } + + public String getName() { + return name; + } + + public Team getTeam() { + return team; + } + + } +} diff --git a/src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPageTest.java b/src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPageTest.java similarity index 63% rename from src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPageTest.java rename to src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPageTest.java index 0ce8e68..b8c79c1 100644 --- a/src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SiderbarBoardPageTest.java +++ b/src/test/java/pw/yumc/MiaoBoard/scoreboard/core/SidebarBoardPageTest.java @@ -10,7 +10,7 @@ import pw.yumc.YumCore.kit.StrKit; * @since 2017/6/6 */ -public class SiderbarBoardPageTest { +public class SidebarBoardPageTest { @Test public void testSubStr() { @@ -24,19 +24,16 @@ public class SiderbarBoardPageTest { public void substr(String value) { System.out.println("变量: " + value); String prefix = StrKit.substring(value, 0, 16); - System.out.println("前缀: " + prefix); //设置前16个字符 String suffix = ""; if (value.length() > 16) { - suffix = value.substring(16, value.length()); - //处理前后的颜色 - String sufpre = ChatColor.getLastColors(prefix); - if (value.charAt(15) == '§') { - sufpre = "§"; - } else if (!suffix.isEmpty() && suffix.charAt(0) == '§') { - sufpre = ""; - } - suffix = StrKit.substring(sufpre + suffix, 0, 16); + int splitIndex = value.charAt(15) == '§' ? 15 : 16; + prefix = StrKit.substring(value, 0, splitIndex); + suffix = value.substring(splitIndex, value.length()); + // 如果过suffix开头不是颜色符号就把prefix颜色转移到suffix + if (suffix.charAt(0) != '§') suffix = ChatColor.getLastColors(prefix) + suffix; + if (suffix.length() > 16) suffix = suffix.substring(16, suffix.length()); } + System.out.println("前缀: " + prefix); //设置前16个字符 System.out.println("后缀: " + suffix); System.out.println("===================="); }