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

fix: 修复颜色符号处理错误

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-10-16 00:22:52 +08:00
parent a6585f11db
commit 3104428641
5 changed files with 247 additions and 147 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoBoard</artifactId>
<version>2.3.7</version>
<version>2.4.0</version>
<description>喵式记分板</description>
<build>
<finalName>${project.artifactId}</finalName>
@ -71,8 +71,9 @@
<url>http://ci.yumc.pw/job/${project.artifactId}/</url>
</ciManagement>
<properties>
<update.description>§a正式版本 2.3.7</update.description>
<update.description>§a正式版本 2.4.0</update.description>
<update.changes>
§617-10-15 §cfix: 修复颜色字符分割错误;
§617-08-15 §cfix: 修复事件未标记为异步的问题;
§617-06-28 §cfix: 修复T端不兼容的问题;
§617-03-09 §cfix: 修复行更新错误;

View File

@ -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<String, Object> serialize() {
return null;
}
@Override
public boolean isOp() {
return false;
}
@Override
public void setOp(boolean value) {
}
}

View File

@ -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);

View File

@ -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<ChatColor> colors = Arrays.asList(ChatColor.values()); //所有颜色
private final Objective objective;
private final List<BoardLine> 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<String> 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<ChatColor> colors = Arrays.asList(ChatColor.values()); //所有颜色
private final Objective objective;
private final List<BoardLine> 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<String> 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;
}
}
}

View File

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