Add spigot constraint for 48-char sidebar text

Spigot has a method `addEntry(String)` for Teams that doesn't block,
unlike the OfflinePlayer option. If the server doesn't have this method,
lines are limited to 16 characters as usual.
This commit is contained in:
eueln 2014-12-05 15:58:00 -06:00
parent ff2e32feff
commit 0487ca77e5

View File

@ -1,16 +1,19 @@
package com.massivecraft.factions.scoreboards; package com.massivecraft.factions.scoreboards;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import java.lang.reflect.Method;
import java.util.*; import java.util.*;
public class BufferedObjective { public class BufferedObjective {
private static final Method addEntryMethod;
private static final int MAX_LINE_LENGTH;
private final Scoreboard scoreboard; private final Scoreboard scoreboard;
private final String baseName; private final String baseName;
@ -25,6 +28,25 @@ public class BufferedObjective {
private final Map<Integer, String> contents = new HashMap<Integer, String>(); private final Map<Integer, String> contents = new HashMap<Integer, String>();
static {
// Check for long line support.
// We require use of Spigot's `addEntry(String)` method on
// Teams, as adding OfflinePlayers to a team is far too slow.
Method addEntryMethodLookup = null;
try {
addEntryMethodLookup = Team.class.getMethod("addEntry", String.class);
} catch (NoSuchMethodException ignored) {}
addEntryMethod = addEntryMethodLookup;
if (addEntryMethod != null) {
MAX_LINE_LENGTH = 48;
} else {
MAX_LINE_LENGTH = 16;
}
}
public BufferedObjective(Scoreboard scoreboard) { public BufferedObjective(Scoreboard scoreboard) {
this.scoreboard = scoreboard; this.scoreboard = scoreboard;
this.baseName = createBaseName(); this.baseName = createBaseName();
@ -63,8 +85,8 @@ public class BufferedObjective {
} }
public void setLine(int lineNumber, String content) { public void setLine(int lineNumber, String content) {
if (content.length() > 48) { if (content.length() > MAX_LINE_LENGTH) {
content = content.substring(0, 48); content = content.substring(0, MAX_LINE_LENGTH);
} }
content = ChatColor.translateAlternateColorCodes('&', content); content = ChatColor.translateAlternateColorCodes('&', content);
@ -106,7 +128,9 @@ public class BufferedObjective {
team.setSuffix(split.next()); team.setSuffix(split.next());
} }
team.addPlayer(Bukkit.getOfflinePlayer(name)); try {
addEntryMethod.invoke(team, name);
} catch (ReflectiveOperationException ignored) {}
buffer.getScore(name).setScore(entry.getKey()); buffer.getScore(name).setScore(entry.getKey());
} else { } else {
buffer.getScore(entry.getValue()).setScore(entry.getKey()); buffer.getScore(entry.getValue()).setScore(entry.getKey());