Skip to content

Commit

Permalink
Add administration subcommands (#23)
Browse files Browse the repository at this point in the history
* Add the add subcommand to the party command

* Add the remove subcommand to the party command
  • Loading branch information
haykam821 authored Dec 8, 2024
1 parent c84d27e commit e789acf
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/main/java/xyz/nucleoid/parties/Party.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public final class Party {

void setOwner(PlayerRef owner) {
this.owner = owner;
if (this.memberPlayers.add(owner)) {
this.members.add(owner);
}
this.add(owner);
}

boolean invite(PlayerRef player) {
Expand All @@ -41,6 +39,12 @@ boolean invite(PlayerRef player) {
return this.pendingMembers.add(player);
}

void add(PlayerRef player) {
if (this.memberPlayers.add(player)) {
this.members.add(player);
}
}

boolean remove(PlayerRef player) {
if (this.memberPlayers.remove(player)) {
this.members.remove(player);
Expand All @@ -51,9 +55,7 @@ boolean remove(PlayerRef player) {

boolean acceptInvite(PlayerRef player) {
if (this.pendingMembers.remove(player)) {
if (this.memberPlayers.add(player)) {
this.members.add(player);
}
this.add(player);
return true;
}
return false;
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/xyz/nucleoid/parties/PartyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
)
.then(literal("leave").executes(PartyCommand::leave))
.then(literal("disband").executes(PartyCommand::disband))
.then(literal("add")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("player", EntityArgumentType.player())
.then(argument("owner", EntityArgumentType.player())
.executes(PartyCommand::addPlayerByOwner)
)
.then(argument("party", UuidArgumentType.uuid())
.executes(PartyCommand::addPlayerByUuid)
)
)
)
.then(literal("remove")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("player", EntityArgumentType.player())
.executes(PartyCommand::removePlayer)
))
);
}
// @formatter:on
Expand Down Expand Up @@ -247,4 +263,52 @@ private static int disband(CommandContext<ServerCommandSource> ctx) throws Comma

return Command.SINGLE_SUCCESS;
}

private static int addPlayerByOwner(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var owner = EntityArgumentType.getPlayer(ctx, "owner");
var partyManager = PartyManager.get(ctx.getSource().getServer());

return addPlayer(ctx, partyManager.getOrCreateOwnParty(PlayerRef.of(owner)));
}

private static int addPlayerByUuid(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var uuid = UuidArgumentType.getUuid(ctx, "party");
var partyManager = PartyManager.get(ctx.getSource().getServer());

return addPlayer(ctx, partyManager.getParty(uuid));
}

private static int addPlayer(CommandContext<ServerCommandSource> ctx, Party party) throws CommandSyntaxException {
var source = ctx.getSource();
var player = EntityArgumentType.getPlayer(ctx, "player");

var partyManager = PartyManager.get(source.getServer());
var result = partyManager.addPlayer(PlayerRef.of(player), party);
if (result.isOk()) {
var message = PartyTexts.addSuccess(player);
party.getMemberPlayers().sendMessage(message.formatted(Formatting.GOLD));
} else {
var error = result.error();
source.sendError(PartyTexts.displayError(error, player));
}

return Command.SINGLE_SUCCESS;
}

private static int removePlayer(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
var source = ctx.getSource();
var player = EntityArgumentType.getPlayer(ctx, "player");

var partyManager = PartyManager.get(source.getServer());
var result = partyManager.removePlayer(PlayerRef.of(player));
if (result.isOk()) {
var message = PartyTexts.removeSuccess(player);
result.party().getMemberPlayers().sendMessage(message.formatted(Formatting.GOLD));
} else {
var error = result.error();
source.sendError(PartyTexts.displayError(error, player));
}

return Command.SINGLE_SUCCESS;
}
}
42 changes: 41 additions & 1 deletion src/main/java/xyz/nucleoid/parties/PartyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,45 @@ public void disbandParty(Party party) {
}
}

public PartyResult addPlayer(PlayerRef player, @Nullable Party party) {
if (party == null) {
return PartyResult.err(PartyError.DOES_NOT_EXIST);
}

var oldParty = this.getParty(player);
if (party == oldParty) {
return PartyResult.err(PartyError.ALREADY_IN_PARTY);
} else if (oldParty != null) {
if (party.isOwner(player)) {
this.disbandParty(player);
} else if (party.remove(player)) {
this.playerToParty.remove(player, party);
}
}

this.playerToParty.put(player, party);
if (!party.acceptInvite(player)) {
party.add(player);
}

return PartyResult.ok(party);
}

public PartyResult removePlayer(PlayerRef player) {
var party = this.getParty(player);
if (party == null) {
return PartyResult.err(PartyError.NOT_IN_PARTY);
}

if (party.isOwner(player)) {
this.disbandParty(player);
} else if (party.remove(player)) {
this.playerToParty.remove(player, party);
}

return PartyResult.ok(party);
}

@Nullable
public Party getParty(PlayerRef player) {
return this.playerToParty.get(player);
Expand All @@ -219,7 +258,8 @@ public Party getOwnParty(PlayerRef owner) {
return null;
}

private Party getOrCreateOwnParty(PlayerRef owner) {
@Nullable
Party getOrCreateOwnParty(PlayerRef owner) {
var party = this.playerToParty.computeIfAbsent(owner, this::createParty);
if (party.isOwner(owner)) {
return party;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/xyz/nucleoid/parties/PartyTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public static MutableText disbandSuccess() {
return Text.translatable("text.game_parties.party.disband.success");
}

public static MutableText addSuccess(ServerPlayerEntity player) {
return Text.translatable("text.game_parties.party.add.success", player.getDisplayName());
}

public static MutableText removeSuccess(ServerPlayerEntity player) {
return Text.translatable("text.game_parties.party.remove.success", player.getDisplayName());
}

public static MutableText transferredSender(ServerPlayerEntity transferredTo) {
return Text.translatable("text.game_parties.party.transferred.sender", transferredTo.getDisplayName());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/data/game_parties/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"text.game_parties.party.add.success": "%s has been added to the party!",
"text.game_parties.party.disband.success": "Your party has been disbanded!",
"text.game_parties.party.error.already_in_party": "You are already in this party!",
"text.game_parties.party.error.already_invited": "%s is already invited to this party!",
Expand All @@ -20,6 +21,7 @@
"text.game_parties.party.list.member.type.owner": "owner",
"text.game_parties.party.list.member.type.pending": "pending",
"text.game_parties.party.list.none": "There are no parties!",
"text.game_parties.party.remove.success": "%s has been removed from the party",
"text.game_parties.party.transferred.receiver": "%s's party has been transferred to you",
"text.game_parties.party.transferred.sender": "Your party has been transferred to %s",
"text.game_parties.party.left_game": "%s left the game and has been removed from the party!"
Expand Down

0 comments on commit e789acf

Please sign in to comment.