diff --git a/.gitignore b/.gitignore index a896ef891..3c73868db 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,6 @@ buildNumber.properties # Common working directory run/ + +node_modules/ +bin/ \ No newline at end of file diff --git a/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/Party.java b/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/Party.java index 6fa67af88..6b5e5f987 100644 --- a/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/Party.java +++ b/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/Party.java @@ -14,13 +14,14 @@ public class Party { private final Vector players; private final int maxSize; - private WeakReference leader; + private Player leader; private WeakReference 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); } @@ -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() { @@ -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) { @@ -100,7 +102,6 @@ public boolean contains(Player player) { public void decompose() { this.players.clear(); - this.leader.clear(); this.leader = null; } @@ -143,7 +144,7 @@ public synchronized void connect(PlayerServer server) { @Override public String toString() { try { - return ""; + return ""; } catch (Exception ignore) { return ""; } diff --git a/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/commands/CommandParty.java b/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/commands/CommandParty.java index 219d9a9ce..b9f27ecd8 100644 --- a/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/commands/CommandParty.java +++ b/velocity/src/main/java/group/aelysium/rustyconnector/plugin/velocity/lib/parties/commands/CommandParty.java @@ -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)); }