Skip to content

Commit

Permalink
Merge branch 'v0.6.1' into v0.6.1-#29
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-i-martin committed Aug 21, 2023
2 parents 237b967 + ce2d423 commit a9fc4f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ buildNumber.properties

# Common working directory
run/

node_modules/
bin/
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
public class Party {
private final Vector<Player> players;
private final int maxSize;
private WeakReference<Player> leader;
private Player leader;
private WeakReference<PlayerServer> server;

public Party(int maxSize, Player host, PlayerServer server) {
this.players = new Vector<>(maxSize);
this.maxSize = maxSize;
this.setLeader(host);
this.leader = host;
this.players.add(host);
this.server = new WeakReference<>(server);
}

Expand All @@ -35,29 +36,24 @@ public PlayerServer server() {

public synchronized Player leader() {
if(this.isEmpty()) throw new IllegalStateException("This party is empty and is no-longer useable!");
if(this.leader.get() == null) {
Player newLeader = players.lastElement();
this.setLeader(newLeader);
this.broadcast(Component.text("The old party leader is no-longer available! "+newLeader.getUsername()+" is the new leader!", NamedTextColor.YELLOW));
if(!this.players.contains(this.leader)) {
this.newRandomLeader();
this.broadcast(Component.text("The old party leader is no-longer available! "+this.leader.getUsername()+" is the new leader!", NamedTextColor.YELLOW));
}

return this.leader.get();
return this.leader;
}

/**
* Assign a new leader to the party.
* If `null` is passed, this will find a random player and make them leader.
* @param player The player to make leader. Or `null` to assign a random member.
*/
public void setLeader(Player player) {
if(player == null) {
this.leader.clear();
leader();
}

if(!this.players.contains(player))
this.players.add(player);
this.leader = new WeakReference<>(player);
throw new IllegalStateException(player.getUsername() + " isn't in this party, they can't be made leader!");
this.leader = player;
}

public void newRandomLeader() {
if(this.isEmpty()) throw new IllegalStateException("This party is empty and is no-longer useable!");

this.leader = this.players.firstElement();
}

public boolean isEmpty() {
Expand All @@ -82,12 +78,18 @@ public synchronized void leave(Player player) {
if(this.isEmpty()) throw new IllegalStateException("This party is empty and is no-longer useable!");
this.players.remove(player);

if(this.isEmpty()) { // This was the last member of the party
VelocityAPI.get().services().partyService().orElseThrow().disband(this);
return;
}

this.players.forEach(partyMember -> partyMember.sendMessage(Component.text(player.getUsername() + " left the party.", NamedTextColor.YELLOW)));

if(player.equals(leader())) setLeader(null);
if(player.equals(this.leader)) {
newRandomLeader();
this.broadcast(Component.text(this.leader.getUsername()+" is the new party leader!", NamedTextColor.YELLOW));
}

if(this.isEmpty())
VelocityAPI.get().services().partyService().orElseThrow().disband(this);
}

public void broadcast(Component message) {
Expand All @@ -100,7 +102,6 @@ public boolean contains(Player player) {

public void decompose() {
this.players.clear();
this.leader.clear();
this.leader = null;
}

Expand Down Expand Up @@ -143,7 +144,7 @@ public synchronized void connect(PlayerServer server) {
@Override
public String toString() {
try {
return "<Party players=" + this.players.size() + " leader=" + Objects.requireNonNull(this.leader.get()).getUsername() + ">";
return "<Party players=" + this.players.size() + " leader=" + this.leader.getUsername() + ">";
} catch (Exception ignore) {
return "<Party players=" + this.players.size() + " leader=null>";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ public static BrigadierCommand create() {
});

context.getSource().sendMessage(VelocityLang.PARTY_BOARD.build(party, player));
} catch (IllegalStateException e) {
return closeMessage(player, Component.text(targetPlayer.getUsername() + " isn't in this party, they can't be made leader!", NamedTextColor.RED));
} catch (Exception e) {
return closeMessage(player, Component.text(username + "There was an issue doing that!", NamedTextColor.RED));
}
Expand Down

0 comments on commit a9fc4f1

Please sign in to comment.