Last active
March 20, 2016 18:38
-
-
Save serega6531/bb0262d81467be8491d8 to your computer and use it in GitHub Desktop.
A simple no-flashing scoreboard manager with rows up to 32 chars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.entity.Player; | |
import org.bukkit.plugin.Plugin; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import org.bukkit.scoreboard.DisplaySlot; | |
import org.bukkit.scoreboard.Objective; | |
import org.bukkit.scoreboard.Scoreboard; | |
import org.bukkit.scoreboard.Team; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class NoFlash extends BukkitRunnable { | |
private final Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); | |
private final Objective obj = board.registerNewObjective("noflash", "dummy"); | |
private Team[] teams; | |
private ChatColor next = ChatColor.RED; | |
private int max; | |
private String player; | |
private ScoreboardTextProvider textProvider; | |
private List<String> last, list; | |
private List<ScoreboardRow> rows; | |
public static String DONOTUPDATE = "§6§a§r"; | |
private NoFlash(Player p, ScoreboardTextProvider textProvider, int max) { | |
this.player = p.getName(); | |
this.textProvider = textProvider; | |
setTitle(textProvider.getScoreboardTitle()); | |
obj.setDisplaySlot(DisplaySlot.SIDEBAR); | |
if(max > 16) | |
max = 16; | |
this.max = max; | |
teams = new Team[max]; | |
for(int i = 0; i < max; i++){ | |
teams[i] = board.registerNewTeam("team" + (i + 1)); | |
teams[i].addPlayer(Bukkit.getOfflinePlayer(ChatColor.values()[i].toString() + ChatColor.RESET)); | |
} | |
setList(textProvider.getScoreboardText(p)); | |
p.setScoreboard(board); | |
} | |
private NoFlash setList(List<String> list){ | |
if(list.size() > max) | |
return this; | |
if(this.list != null) | |
this.last = new ArrayList<>(this.list); | |
this.list = list; | |
rows = list.stream().map(ScoreboardRow::new).collect(Collectors.toList()); | |
return this; | |
} | |
private NoFlash setTitle(String title){ | |
if(title.length() > 32) | |
title = title.substring(0, 32); | |
obj.setDisplayName(title); | |
return this; | |
} | |
@Override | |
public void run() { | |
Player p = Bukkit.getPlayerExact(player); | |
if(p == null){ | |
cancel(); | |
return; | |
} | |
if(!textProvider.getScoreboardText(p).equals(last)){ | |
setList(textProvider.getScoreboardText(p)); | |
} | |
if(!textProvider.getScoreboardTitle().equals(obj.getDisplayName())){ | |
setTitle(textProvider.getScoreboardTitle()); | |
} | |
if(next == ChatColor.RED) { | |
next = ChatColor.BLUE; | |
} else { | |
next = ChatColor.RED; | |
} | |
int size = list.size() <= max ? list.size() : max; | |
if(list != null){ | |
if(last != null && last.size() > list.size()){ | |
for(int k = max - 1; k > last.size() - list.size(); k--){ | |
board.resetScores(teams[max - k - 1].getPlayers().iterator().next()); | |
} | |
} | |
for(int i = 0; i < list.size(); i++) { | |
if(list.get(i).equals(DONOTUPDATE)) | |
continue; | |
ScoreboardRow row = rows.get(i); | |
teams[size - i - 1].setPrefix(row.getPrefix()); | |
teams[size - i - 1].setSuffix(row.getSuffix()); | |
obj.getScore(teams[size - i - 1].getPlayers().iterator().next()).setScore(size - i); | |
} | |
} | |
} | |
public static NoFlash start(Plugin pl, Player p, ScoreboardTextProvider textProvider, int max){ | |
NoFlash nf = new NoFlash(p, textProvider, max); | |
nf.runTaskTimer(pl, 1L, 5L); | |
return nf; | |
} | |
public static NoFlash start(Plugin pl, Player p, ScoreboardTextProvider textProvider){ | |
return start(pl, p, textProvider, 16); | |
} | |
public void stop(){ | |
cancel(); | |
} | |
public interface ScoreboardTextProvider { | |
List<String> getScoreboardText(Player p); | |
String getScoreboardTitle(); | |
} | |
private class ScoreboardRow { | |
private String prefix, suffix; | |
public ScoreboardRow(String row){ | |
if(row.length() <= 16) { | |
prefix = row; | |
suffix = ""; | |
} else { //up to 16+16, color pair is in single part | |
int cut = findCutPoint(row, 16); | |
prefix = row.substring(0, cut); | |
suffix = row.substring(cut, cut + (cut + 16 <= row.length() ? cut + 16 : row.length() - cut)); | |
} | |
} | |
private int findCutPoint(String s, int max){ | |
for(int i = max; i > 0; i--){ | |
if(s.charAt(i-1) == ChatColor.COLOR_CHAR && ChatColor.getByChar(s.charAt(i)) != null) | |
continue; | |
return i; | |
} | |
return 16; | |
} | |
public String getPrefix() { | |
return prefix; | |
} | |
public String getSuffix() { | |
return suffix; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May contain bugs now, look at supported version of NoFlashPacket instead
https://gist.github.com/serega6531/4acd23ac188c8c568287