From 613fe793c14e6b74f1286ccd08811cd039bab90d Mon Sep 17 00:00:00 2001 From: Nathan Martin <63524975+nathan-i-martin@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:17:49 -0400 Subject: [PATCH 1/4] Implement fix for #31 --- .../plugin/velocity/lib/parties/Party.java | 41 +++++++++---------- .../lib/parties/commands/CommandParty.java | 2 + 2 files changed, 21 insertions(+), 22 deletions(-) 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..f8c45fd02 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,13 @@ 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.server = new WeakReference<>(server); } @@ -35,29 +35,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() { @@ -84,7 +79,10 @@ public synchronized void leave(Player player) { 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); @@ -100,7 +98,6 @@ public boolean contains(Player player) { public void decompose() { this.players.clear(); - this.leader.clear(); this.leader = null; } @@ -143,7 +140,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)); } From 6d4f3943479444dd91ab4c96650288bd2f878d42 Mon Sep 17 00:00:00 2001 From: Nathan Martin <63524975+nathan-i-martin@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:19:50 -0400 Subject: [PATCH 2/4] Implement fix for #30 --- .../rustyconnector/plugin/velocity/lib/parties/Party.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 f8c45fd02..c4c3bfad4 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 @@ -77,6 +77,11 @@ 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(this.leader)) { @@ -84,8 +89,6 @@ public synchronized void leave(Player player) { 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) { From 123fe298f41451dce7ed2c5f13b37acad002787a Mon Sep 17 00:00:00 2001 From: Nathan Martin <63524975+nathan-i-martin@users.noreply.github.com> Date: Mon, 21 Aug 2023 18:07:31 -0400 Subject: [PATCH 3/4] Adjust fix of #31 and #30 --- .gitignore | 2 ++ .../rustyconnector/plugin/velocity/lib/parties/Party.java | 1 + 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index a896ef891..1446d8ed4 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,5 @@ buildNumber.properties # Common working directory run/ + +node_modules/ \ 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 c4c3bfad4..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 @@ -21,6 +21,7 @@ public Party(int maxSize, Player host, PlayerServer server) { this.players = new Vector<>(maxSize); this.maxSize = maxSize; this.leader = host; + this.players.add(host); this.server = new WeakReference<>(server); } From ce2d4236486df960f2e2c0af80a54501be205109 Mon Sep 17 00:00:00 2001 From: Nathan Martin <63524975+nathan-i-martin@users.noreply.github.com> Date: Mon, 21 Aug 2023 18:14:56 -0400 Subject: [PATCH 4/4] Update .gitignore again --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1446d8ed4..3c73868db 100644 --- a/.gitignore +++ b/.gitignore @@ -117,4 +117,5 @@ buildNumber.properties # Common working directory run/ -node_modules/ \ No newline at end of file +node_modules/ +bin/ \ No newline at end of file