1
0
mirror of https://e.coding.net/circlecloud/MiaoBoard.git synced 2024-11-22 01:49:05 +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

@ -6,11 +6,12 @@ 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.YumCore.bukkit.compatible.C;
import pw.yumc.MiaoBoard.misc.FakePlayer;
import pw.yumc.YumCore.kit.StrKit;
/**
@ -18,7 +19,7 @@ import pw.yumc.YumCore.kit.StrKit;
* @since 2016年7月4日 下午4:40:21
* @author 尘曲
*/
public class SiderbarBoardPage extends BoardPage {
public class SidebarBoardPage extends BoardPage {
private static boolean newVer = true;
static {
try {
@ -33,20 +34,14 @@ public class SiderbarBoardPage extends BoardPage {
private final List<BoardLine> boardLines = new ArrayList<>();// ""
private int maxLine;//用于标注最大行数
public SiderbarBoardPage() {
public SidebarBoardPage() {
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 String name = colors.get(i) + "" + ChatColor.RESET;
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)); //""添加至列表
boardLines.add(new BoardLine(name, team)); //""添加至列表
}
}
@ -61,20 +56,17 @@ public class SiderbarBoardPage extends BoardPage {
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); //设置""
objective.getScore(boardLine.getName()).setScore(line); //设置""
//分割字符串为前16个和后16个
String prefix = StrKit.substring(value, 0, 16);
String prefix = value;
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());
}
boardLine.getTeam().setPrefix(prefix); //设置前16个字符
boardLine.getTeam().setSuffix(suffix); //设置后16个字符
@ -94,7 +86,7 @@ public class SiderbarBoardPage extends BoardPage {
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()); //删除这个""
getBoard().resetScores(boardLine.getName()); //删除这个""
}
private BoardLine getBoardLine(int line) {
@ -109,16 +101,27 @@ public class SiderbarBoardPage extends BoardPage {
}
class BoardLine {
private final ChatColor color;
private final Team team;
private String name;
private Team team;
private OfflinePlayer player;
public BoardLine(ChatColor color, Team team) {
this.color = color;
public BoardLine(String name, Team team) {
this.name = name;
this.team = team;
this.player = new FakePlayer(name);
addEntry();
}
public ChatColor getColor() {
return color;
public void addEntry() {
if (newVer) {
team.addEntry(name);
} else {
team.addPlayer(player);
}
}
public String getName() {
return name;
}
public Team getTeam() {

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