Skip to content

Commit

Permalink
Merge pull request #15 from andrei1058/develop
Browse files Browse the repository at this point in the history
Fix team already exists and performance improvements
  • Loading branch information
andrei1058 authored Feb 5, 2023
2 parents 2960fc0 + 86e8d12 commit 29402b1
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ interface ScoreLine extends Comparable<ScoreLine> {

void sendCreate(Player player);

void setContent(String content);
/**
* @return if content has been changed
*/
boolean setContent(String content);

void sendUpdateToAllReceivers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public interface Sidebar {
@SuppressWarnings("unused")
void removeLine(int line);

void clearLines();

/**
* @return lines amount.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private int getAvailableScore() {
private static void scoreOffsetIncrease(@NotNull Collection<ScoreLine> lineCollections) {
for (ScoreLine line : lineCollections) {
line.setScoreAmount(line.getScoreAmount() + 1);
line.sendCreateToAllReceivers();
line.sendUpdateToAllReceivers();
}
}

Expand All @@ -96,9 +96,9 @@ private void order() {
public void addLine(SidebarLine sidebarLine) {
int score = getAvailableScore();
if (score == -1) return;
if (availableColors.isEmpty()) return;
scoreOffsetIncrease(this.lines);
String color = availableColors.get(0);
availableColors.remove(0);
String color = availableColors.removeFirst();
ScoreLine s = SidebarManager.getInstance().getSidebarProvider().createScoreLine(
this, sidebarLine, score == 0 ? score : score - 1, color
);
Expand Down Expand Up @@ -137,22 +137,21 @@ public void add(Player player) {
@Override
public void refreshPlaceholders() {
for (ScoreLine line : this.lines) {
if (line.getLine().isHasPlaceholders()) {
refreshLinePlaceholders(line);
if (line.getLine().isHasPlaceholders() && refreshLinePlaceholders(line)) {
line.sendUpdateToAllReceivers();
}
}
}

// refresh placeholders for the given line before sending it
private void refreshLinePlaceholders(@NotNull ScoreLine line){
private boolean refreshLinePlaceholders(@NotNull ScoreLine line) {
String content = line.getLine().getLine();
for (PlaceholderProvider pp : this.placeholderProviders) {
if (content.contains(pp.getPlaceholder())) {
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
line.setContent(content);
return line.setContent(content);
}

@Override
Expand All @@ -165,11 +164,14 @@ public void refreshAnimatedLines() {
for (ScoreLine line : lines) {
if (line.getLine() instanceof SidebarLineAnimated) {
if (line.getLine().isHasPlaceholders()) {
refreshLinePlaceholders(line);
if (refreshLinePlaceholders(line)) {
line.sendUpdateToAllReceivers();
}
} else {
line.setContent(line.getLine().getLine());
if (line.setContent(line.getLine().getLine())) {
line.sendUpdateToAllReceivers();
}
}
line.sendUpdateToAllReceivers();
}
}
}
Expand All @@ -191,6 +193,16 @@ public void removeLine(int line) {
}
}

@Override
public void clearLines() {
this.lines.forEach(line -> {
line.sendRemoveToAllReceivers();
this.restoreColor(line.getColor());
});
scoreOffsetDecrease(Collections.emptyList());
this.lines.clear();
}

@Override
public int lineCount() {
return lines.size();
Expand All @@ -208,13 +220,13 @@ public List<PlaceholderProvider> getPlaceholders() {

@Override
public void remove(Player player) {
this.receivers.remove(player);
tabView.forEach(tab -> tab.remove(player));
lines.forEach(line -> line.sendRemove(player));
this.sidebarObjective.sendRemove(player);
if (this.healthObjective != null) {
this.healthObjective.sendRemove(player);
}
this.receivers.remove(player);

// clear player from any cached context
this.tabView.forEach(tab -> {
Expand Down Expand Up @@ -269,7 +281,7 @@ public PlayerTab playerTabCreate(String identifier, @Nullable Player player, Sid
);
// send tab create to sidebar receivers
getReceivers().forEach(tab::sendCreateToPlayer);
if (null != player){
if (null != player) {
// add entity to tab team
tab.sendUserCreateToReceivers(player);
}
Expand All @@ -280,7 +292,7 @@ public PlayerTab playerTabCreate(String identifier, @Nullable Player player, Sid
@Override
public void removeTab(String identifier) {
Optional<VersionedTabGroup> playerTab = tabView.stream().filter(tab -> tab.getIdentifier().equals(identifier)).findFirst();
if (playerTab.isPresent()){
if (playerTab.isPresent()) {
VersionedTabGroup tab = playerTab.get();
tabView.remove(tab);
tab.sendRemoveToReceivers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ public BucharestScoreLine(@NotNull SidebarLine text, int score, @NotNull String
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
//noinspection ResultOfMethodCallIgnored
setContent(content);
} else {
//noinspection ResultOfMethodCallIgnored
setContent(text.getLine());
}
}
Expand All @@ -153,7 +155,7 @@ public int getScoreAmount() {

@Override
public void setScoreAmount(int score) {
this.score = score;
this.setScore(score);
}

@Override
Expand Down Expand Up @@ -191,14 +193,18 @@ public void sendRemoveToAllReceivers() {

public void sendUpdate(Player player) {
PacketPlayOutScoreboardTeam packetTeamUpdate = new PacketPlayOutScoreboardTeam(team, 2);
PacketPlayOutScoreboardScore packetPlayOutScoreboardScore = new PacketPlayOutScoreboardScore(getPlayerName(), (ScoreboardObjective) getSidebarObjective());
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetTeamUpdate);
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutScoreboardScore);
}

@Contract(pure = true)
public void setContent(@NotNull String content) {
public boolean setContent(@NotNull String content) {
if (!getReceivers().isEmpty()) {
content = SidebarManager.getInstance().getPapiSupport().replacePlaceholders(getReceivers().get(0), content);
}
String oldPrefix = this.prefix;
String oldSuffix = this.suffix;
if (content.length() > 16) {
this.prefix = content.substring(0, 16);
if (this.prefix.charAt(15) == ChatColor.COLOR_CHAR) {
Expand All @@ -211,6 +217,7 @@ public void setContent(@NotNull String content) {
this.prefix = content;
this.suffix = "";
}
return !oldPrefix.equals(this.prefix) || !oldSuffix.equals(this.suffix);
}

public void setSuffix(@NotNull String secondPart) {
Expand All @@ -224,7 +231,11 @@ public void setSuffix(@NotNull String secondPart) {

public void sendUpdateToAllReceivers() {
PacketPlayOutScoreboardTeam packetTeamUpdate = new PacketPlayOutScoreboardTeam(team, 2);
getReceivers().forEach(r -> ((CraftPlayer) r).getHandle().playerConnection.sendPacket(packetTeamUpdate));
PacketPlayOutScoreboardScore packetPlayOutScoreboardScore = new PacketPlayOutScoreboardScore(this);
getReceivers().forEach(r -> {
((CraftPlayer) r).getHandle().playerConnection.sendPacket(packetTeamUpdate);
((CraftPlayer) r).getHandle().playerConnection.sendPacket(packetPlayOutScoreboardScore);
});
}

public int compareTo(@NotNull ScoreLine o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ public NarniaScoreLine(@NotNull SidebarLine text, int score, @NotNull String col
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
//noinspection ResultOfMethodCallIgnored
setContent(content);
} else {
//noinspection ResultOfMethodCallIgnored
setContent(text.getLine());
}
}
Expand All @@ -178,7 +180,7 @@ public int getScoreAmount() {

@Override
public void setScoreAmount(int score) {
this.score = score;
this.b(score);
}

@Override
Expand Down Expand Up @@ -228,10 +230,12 @@ public void sendUpdate(Player player) {
}

@Contract(pure = true)
public void setContent(@NotNull String content) {
public boolean setContent(@NotNull String content) {
if (!getReceivers().isEmpty()) {
content = SidebarManager.getInstance().getPapiSupport().replacePlaceholders(getReceivers().get(0), content);
}
var oldPrefix = this.prefix;
var oldSuffix = this.suffix;
if (content.length() > 64) {
this.prefix = content.substring(0, 64);
if (this.prefix.charAt(63) == ChatColor.COLOR_CHAR) {
Expand All @@ -244,6 +248,7 @@ public void setContent(@NotNull String content) {
this.prefix = content;
this.suffix = "";
}
return !oldPrefix.equals(this.prefix) || !oldSuffix.equals(this.suffix);
}

public void setSuffix(@NotNull String secondPart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ public NarniaScoreLine(@NotNull SidebarLine text, int score, @NotNull String col
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
//noinspection ResultOfMethodCallIgnored
setContent(content);
} else {
//noinspection ResultOfMethodCallIgnored
setContent(text.getLine());
}
}
Expand Down Expand Up @@ -228,10 +230,13 @@ public void sendUpdate(Player player) {
}

@Contract(pure = true)
public void setContent(@NotNull String content) {
public boolean setContent(@NotNull String content) {
if (!getReceivers().isEmpty()) {
content = SidebarManager.getInstance().getPapiSupport().replacePlaceholders(getReceivers().get(0), content);
}
var oldPrefix = this.prefix;
var oldSuffix = this.suffix;

if (content.length() > 16) {
this.prefix = content.substring(0, 16);
if (this.prefix.charAt(15) == ChatColor.COLOR_CHAR) {
Expand All @@ -244,6 +249,7 @@ public void setContent(@NotNull String content) {
this.prefix = content;
this.suffix = "";
}
return !oldPrefix.equals(this.prefix) || !oldSuffix.equals(this.suffix);
}

public void setSuffix(@NotNull String secondPart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ public NarniaScoreLine(@NotNull SidebarLine text, int score, @NotNull String col
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
//noinspection ResultOfMethodCallIgnored
setContent(content);
} else {
//noinspection ResultOfMethodCallIgnored
setContent(text.getLine());
}
}
Expand All @@ -169,7 +171,7 @@ public int getScoreAmount() {

@Override
public void setScoreAmount(int score) {
this.score = score;
this.setScore(score);
}

@Override
Expand Down Expand Up @@ -219,10 +221,13 @@ public void sendUpdate(Player player) {
}

@Contract(pure = true)
public void setContent(@NotNull String content) {
public boolean setContent(@NotNull String content) {
if (!getReceivers().isEmpty()) {
content = SidebarManager.getInstance().getPapiSupport().replacePlaceholders(getReceivers().get(0), content);
}
var oldPrefix = this.prefix;
var oldSuffix = this.suffix;

if (content.length() > 32) {
this.prefix = content.substring(0, 32);
if (this.prefix.charAt(31) == ChatColor.COLOR_CHAR) {
Expand All @@ -235,6 +240,7 @@ public void setContent(@NotNull String content) {
this.prefix = content;
this.suffix = "";
}
return !oldPrefix.equals(this.prefix) || !oldSuffix.equals(this.suffix);
}

public void setSuffix(@NotNull String secondPart) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ public BucharestScoreLine(@NotNull SidebarLine text, int score, @NotNull String
content = content.replace(pp.getPlaceholder(), pp.getReplacement());
}
}
//noinspection ResultOfMethodCallIgnored
setContent(content);
} else {
//noinspection ResultOfMethodCallIgnored
setContent(text.getLine());
}
}
Expand All @@ -154,7 +156,7 @@ public int getScoreAmount() {

@Override
public void setScoreAmount(int score) {
this.score = score;
this.setScore(score);
}

@Override
Expand Down Expand Up @@ -206,10 +208,13 @@ public void sendUpdate(Player player) {
}

@Contract(pure = true)
public void setContent(@NotNull String content) {
public boolean setContent(@NotNull String content) {
if (!getReceivers().isEmpty()) {
content = SidebarManager.getInstance().getPapiSupport().replacePlaceholders(getReceivers().get(0), content);
}
var oldPrefix = this.prefix;
var oldSuffix = this.suffix;

if (content.length() > 32) {
this.prefix = content.substring(0, 32);
if (this.prefix.charAt(31) == ChatColor.COLOR_CHAR) {
Expand All @@ -222,6 +227,7 @@ public void setContent(@NotNull String content) {
this.prefix = content;
this.suffix = "";
}
return !oldPrefix.equals(this.prefix) || !oldSuffix.equals(this.suffix);
}

public void setSuffix(@NotNull String secondPart) {
Expand Down
Loading

0 comments on commit 29402b1

Please sign in to comment.