Skip to content

Commit

Permalink
Remove player from old team on add instead of throwing exception
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Jul 13, 2024
1 parent 6a48c16 commit 8d41045
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ public VelocityTeam registerTeam(@NotNull ProxyTeam.Builder builder) {
VelocityTeam team = ((VelocityTeam.Builder)builder).build(this);
if (teams.containsKey(team.getName())) throw new IllegalStateException("A team with this name (" + team.getName() + ") already exists");
for (String entry : team.getEntriesRaw()) {
VelocityTeam entryTeam = getTeamFromEntry(entry);
if (entryTeam != null) {
throw new IllegalStateException("An entry with named (" + entry + ") already exists in team " + entryTeam.getName());
VelocityTeam oldTeam = teamEntries.put(entry, team);
if (oldTeam != null) {
oldTeam.getEntriesRaw().remove(entry);
}
teamEntries.put(entry, team);
}

teams.put(team.getName(), team);
Expand All @@ -154,13 +153,8 @@ public Set<ProxyTeam> getTeams() {

@ApiStatus.Internal
@Nullable
public VelocityTeam getTeamFromEntry(String entry) {
return teamEntries.get(entry);
}

@ApiStatus.Internal
public void addEntryToTeam(@NotNull String entry, @NotNull VelocityTeam team) {
teamEntries.put(entry, team);
public VelocityTeam addEntryToTeam(@NotNull String entry, @NotNull VelocityTeam team) {
return teamEntries.put(entry, team);
}

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,9 @@ public void updateProperties(@NotNull Consumer<ProxyTeam.PropertyBuilder> builde
public void addEntry(@NotNull String entry) {
checkState();
if (entries.contains(entry)) return;
for (String iteratedEntry : getEntriesRaw()) {
VelocityTeam entryTeam = scoreboard.getTeamFromEntry(entry);
if (entryTeam != null) {
throw new IllegalStateException("An entry with named (" + entry + ") already exists in team " + entryTeam.getName());
}
scoreboard.addEntryToTeam(iteratedEntry, this);
VelocityTeam oldTeam = scoreboard.addEntryToTeam(entry, this);
if (oldTeam != null) {
oldTeam.entries.remove(entry);
}
entries.add(entry);
scoreboard.sendPacket(TeamPacket.addOrRemovePlayer(name, entry, true), this);
Expand All @@ -220,11 +217,6 @@ public void removeEntry(@NotNull String entry) throws IllegalArgumentException {
}
}

public void removeEntriesRaw(@NotNull Collection<String> entries) {
this.entries.removeAll(entries);
entries.forEach(entry -> scoreboard.removeEntryFromTeam(entry, this));
}

public void sendRegister() {
scoreboard.sendPacket(new TeamPacket(TeamPacket.TeamAction.REGISTER, name, properties, entries), this);
}
Expand Down

0 comments on commit 8d41045

Please sign in to comment.