From 28b07fc6a4ffbefc127a97f1eed1dddb5bdf85ca Mon Sep 17 00:00:00 2001 From: Driftay Date: Fri, 19 Jun 2020 11:15:22 -0400 Subject: [PATCH] Updates To Scoreboard --- .../scoreboards/BufferedObjective.java | 104 ++++++++++++++---- .../factions/scoreboards/FScoreboard.java | 9 +- .../scoreboards/FSidebarProvider.java | 9 +- .../factions/scoreboards/FTeamWrapper.java | 72 ++++-------- 4 files changed, 115 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/scoreboards/BufferedObjective.java b/src/main/java/com/massivecraft/factions/scoreboards/BufferedObjective.java index 112a2295..8bb77e78 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/BufferedObjective.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/BufferedObjective.java @@ -9,15 +9,27 @@ import org.bukkit.scoreboard.Team; import java.lang.reflect.Method; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class BufferedObjective { - - /** - * @author FactionsUUID Team - */ - private static final Method addEntryMethod; private static final int MAX_LINE_LENGTH; + private static final Pattern PATTERN = Pattern.compile("(\u00A7[0-9a-fk-r])|(.)"); + + private final Scoreboard scoreboard; + private final String baseName; + + private Objective current; + private List currentTeams = new ArrayList<>(); + private String title; + private DisplaySlot displaySlot; + + private int objPtr; + private int teamPtr; + private boolean requiresUpdate = false; + + private final Map contents = new HashMap<>(); static { // Check for long line support. @@ -39,17 +51,6 @@ public class BufferedObjective { } } - private final Scoreboard scoreboard; - private final String baseName; - private final Map contents = new HashMap<>(); - private Objective current; - private List currentTeams = new ArrayList<>(); - private String title; - private DisplaySlot displaySlot; - private int objPtr; - private int teamPtr; - private boolean requiresUpdate = false; - public BufferedObjective(Scoreboard scoreboard) { this.scoreboard = scoreboard; this.baseName = createBaseName(); @@ -122,14 +123,75 @@ public class BufferedObjective { Team team = scoreboard.registerNewTeam(getNextTeamName()); bufferTeams.add(team); - Iterator split = Splitter.fixedLength(16).split(entry.getValue()).iterator(); + String name, prefix = null, suffix = null; - team.setPrefix(split.next()); - String name = split.next(); - if (split.hasNext()) { // We only guarantee two splits - team.setSuffix(split.next()); + String value = entry.getValue(); + if (value.length() > 16) { + String[] arrImAPirate = new String[3]; + Matcher matcherrr = PATTERN.matcher(value); + StringBuilder builderrr = new StringBuilder(); + int sCURvy = 0; + char currrentColorrr = 'r'; + char currrentFormat = 'r'; + while (sCURvy < 3 && matcherrr.find()) { + String tharSheBlows = matcherrr.group(); + boolean hoist = false; + if (tharSheBlows.length() == 1) { + builderrr.append(tharSheBlows); + if (builderrr.length() == 16) { + hoist = true; + } + } else { + char c = tharSheBlows.charAt(1); + if (c >= 'k' && c <= 'r') { // format! + currrentFormat = c; + if (c == 'r') { + currrentColorrr = 'r'; + } + } else { + currrentColorrr = c; + currrentFormat = 'r'; + } + if (builderrr.length() < 14) { + builderrr.append(tharSheBlows); + } else { + hoist = true; + } + } + if (hoist) { + arrImAPirate[sCURvy++] = builderrr.toString(); + builderrr = new StringBuilder(); + if (currrentColorrr != 'r') { + builderrr.append('\u00A7').append(currrentColorrr); + } + if (currrentFormat != 'r') { + builderrr.append('\u00A7').append(currrentFormat); + } + } + } + if (sCURvy < 3 && builderrr.length() > 0) { + arrImAPirate[sCURvy] = builderrr.toString(); + } + if (arrImAPirate[2] == null) { + name = arrImAPirate[0]; + suffix = arrImAPirate[1]; + } else { + prefix = arrImAPirate[0]; + name = arrImAPirate[1]; + suffix = arrImAPirate[2]; + } + } else { + name = value; } + if (prefix != null) { + team.setPrefix(prefix); + } + if (suffix != null) { + team.setSuffix(suffix); + } + + try { addEntryMethod.invoke(team, name); } catch (ReflectiveOperationException ignored) { diff --git a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java index 4df3269e..5cd08ab8 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/FScoreboard.java @@ -58,10 +58,13 @@ public class FScoreboard { FTeamWrapper.track(fboard); } - public static void remove(FPlayer fplayer) { + public static void remove(FPlayer fplayer, Player player) { FScoreboard fboard = fscoreboards.remove(fplayer); if (fboard != null) { + if (fboard.scoreboard == player.getScoreboard()) { // No equals method implemented, so may as well skip a nullcheck + player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard()); + } fboard.removed = true; FTeamWrapper.untrack(fboard); } @@ -91,7 +94,7 @@ public class FScoreboard { bufferedObjective.setDisplaySlot(visible ? DisplaySlot.SIDEBAR : null); } - public void setDefaultSidebar(final FSidebarProvider provider, int updateInterval) { + public void setDefaultSidebar(final FSidebarProvider provider) { if (!isSupportedByServer()) { return; } @@ -114,7 +117,7 @@ public class FScoreboard { updateObjective(); } } - }.runTaskTimer(FactionsPlugin.getInstance(), updateInterval, updateInterval); + }.runTaskTimer(FactionsPlugin.getInstance(), 20, 20); } public void setTemporarySidebar(final FSidebarProvider provider) { diff --git a/src/main/java/com/massivecraft/factions/scoreboards/FSidebarProvider.java b/src/main/java/com/massivecraft/factions/scoreboards/FSidebarProvider.java index a2c96054..b53ad75b 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/FSidebarProvider.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/FSidebarProvider.java @@ -3,6 +3,7 @@ package com.massivecraft.factions.scoreboards; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.FactionsPlugin; +import com.massivecraft.factions.tag.Tag; import com.massivecraft.factions.zcore.util.TL; import com.massivecraft.factions.zcore.util.TagUtil; @@ -20,16 +21,16 @@ public abstract class FSidebarProvider { public abstract List getLines(FPlayer fplayer); public String replaceTags(FPlayer fPlayer, String s) { - s = TagUtil.parsePlaceholders(fPlayer.getPlayer(), s); + s = Tag.parsePlaceholders(fPlayer.getPlayer(), s); - return qualityAssure(TagUtil.parsePlain(fPlayer, s)); + return qualityAssure(Tag.parsePlain(fPlayer, s)); } public String replaceTags(Faction faction, FPlayer fPlayer, String s) { // Run through Placeholder API first - s = TagUtil.parsePlaceholders(fPlayer.getPlayer(), s); + s = Tag.parsePlaceholders(fPlayer.getPlayer(), s); - return qualityAssure(Objects.requireNonNull(TagUtil.parsePlain(faction, fPlayer, s))); + return qualityAssure(Tag.parsePlain(faction, fPlayer, s)); } private String qualityAssure(String line) { diff --git a/src/main/java/com/massivecraft/factions/scoreboards/FTeamWrapper.java b/src/main/java/com/massivecraft/factions/scoreboards/FTeamWrapper.java index 98996e85..aa7596c6 100644 --- a/src/main/java/com/massivecraft/factions/scoreboards/FTeamWrapper.java +++ b/src/main/java/com/massivecraft/factions/scoreboards/FTeamWrapper.java @@ -19,8 +19,9 @@ public class FTeamWrapper { private static final Map wrappers = new HashMap<>(); private static final List tracking = new ArrayList<>(); - private static final Set updating = new HashSet<>(); private static int factionTeamPtr; + private static final Set updating = new HashSet<>(); + private final Map teams = new HashMap<>(); private final String teamName; private final Faction faction; @@ -36,18 +37,11 @@ public class FTeamWrapper { } public static void applyUpdatesLater(final Faction faction) { - if (!FScoreboard.isSupportedByServer()) { + if (!FScoreboard.isSupportedByServer()) return; + if (faction.isWilderness()) return; + if (!FactionsPlugin.getInstance().getConfig().getBoolean("scoreboard.default-prefixes", false) + || FactionsPlugin.getInstance().getConfig().getBoolean("See-Invisible-Faction-Members")) return; - } - - if (faction.isWilderness()) { - return; - } - - if (!FactionsPlugin.getInstance().getConfig().getBoolean("scoreboard.default-prefixes", false) || FactionsPlugin.getInstance().getConfig().getBoolean("See-Invisible-Faction-Members")) { - return; - } - if (updating.add(faction)) { Bukkit.getScheduler().runTask(FactionsPlugin.getInstance(), () -> { @@ -58,22 +52,17 @@ public class FTeamWrapper { } public static void applyUpdates(Faction faction) { - if (!FScoreboard.isSupportedByServer()) { - return; - } + if (!FScoreboard.isSupportedByServer()) return; - if (faction.isWilderness()) { - return; - } + if (faction.isWilderness()) return; - if (!FactionsPlugin.getInstance().getConfig().getBoolean("scoreboard.default-prefixes", false) || FactionsPlugin.getInstance().getConfig().getBoolean("See-Invisible-Faction-Members")) { - return; - } - if (updating.contains(faction)) { - // Faction will be updated soon. + if (!FactionsPlugin.getInstance().getConfig().getBoolean("scoreboard.default-prefixes", false) + || FactionsPlugin.getInstance().getConfig().getBoolean("See-Invisible-Faction-Members")) return; - } + + + if (updating.contains(faction)) return; FTeamWrapper wrapper = wrappers.get(faction); Set factionMembers = faction.getFPlayers(); @@ -98,21 +87,16 @@ public class FTeamWrapper { } for (FPlayer fmember : factionMembers) { - if (!fmember.isOnline()) { - continue; - } + if (!fmember.isOnline()) continue; // Scoreboard might not have player; add him/her wrapper.addPlayer(fmember.getPlayer()); } - wrapper.updatePrefixes(); } public static void updatePrefixes(Faction faction) { - if (!FScoreboard.isSupportedByServer()) { - return; - } + if (!FScoreboard.isSupportedByServer()) return; if (!wrappers.containsKey(faction)) { applyUpdates(faction); @@ -122,34 +106,22 @@ public class FTeamWrapper { } protected static void track(FScoreboard fboard) { - if (!FScoreboard.isSupportedByServer()) { - return; - } + if (!FScoreboard.isSupportedByServer()) return; tracking.add(fboard); - for (FTeamWrapper wrapper : wrappers.values()) { - wrapper.add(fboard); - } + for (FTeamWrapper wrapper : wrappers.values()) wrapper.add(fboard); } protected static void untrack(FScoreboard fboard) { - if (!FScoreboard.isSupportedByServer()) { - return; - } + if (!FScoreboard.isSupportedByServer()) return; tracking.remove(fboard); - for (FTeamWrapper wrapper : wrappers.values()) { - wrapper.remove(fboard); - } + for (FTeamWrapper wrapper : wrappers.values()) wrapper.remove(fboard); } private void add(FScoreboard fboard) { Scoreboard board = fboard.getScoreboard(); Team team = board.registerNewTeam(teamName); teams.put(fboard, team); - - for (OfflinePlayer player : getPlayers()) { - team.addPlayer(player); - } - + for (OfflinePlayer player : getPlayers()) team.addPlayer(player); updatePrefix(fboard); } @@ -159,9 +131,7 @@ public class FTeamWrapper { private void updatePrefixes() { if (FactionsPlugin.getInstance().getConfig().getBoolean("scoreboard.default-prefixes", false)) { - for (FScoreboard fboard : teams.keySet()) { - updatePrefix(fboard); - } + for (FScoreboard fboard : teams.keySet()) updatePrefix(fboard); } }