Added support for accepting and rejecting an invite.

This commit is contained in:
tastybento 2024-01-03 18:42:12 +09:00
parent 1d6556613e
commit 7639c59db9
4 changed files with 123 additions and 39 deletions

View File

@ -17,9 +17,11 @@ import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent; import world.bentobox.bentobox.api.events.team.TeamEvent;
@ -32,6 +34,7 @@ import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.panels.builders.TemplatedPanelBuilder; import world.bentobox.bentobox.api.panels.builders.TemplatedPanelBuilder;
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord; import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord;
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord.ActionRecords; import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord.ActionRecords;
import world.bentobox.bentobox.api.panels.reader.PanelTemplateRecord.TemplateItem;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
@ -67,6 +70,14 @@ public class IslandTeamCommand extends CompositeCommand {
private IslandTeamUntrustCommand unTrustCommand; private IslandTeamUntrustCommand unTrustCommand;
private @Nullable TemplateItem border;
private @Nullable TemplateItem background;
private IslandTeamInviteAcceptCommand acceptCommand;
private IslandTeamInviteRejectCommand rejectCommand;
public IslandTeamCommand(CompositeCommand parent) { public IslandTeamCommand(CompositeCommand parent) {
super(parent, "team"); super(parent, "team");
inviteMap = new HashMap<>(); inviteMap = new HashMap<>();
@ -82,8 +93,8 @@ public class IslandTeamCommand extends CompositeCommand {
leaveCommand = new IslandTeamLeaveCommand(this); leaveCommand = new IslandTeamLeaveCommand(this);
setOwnerCommand = new IslandTeamSetownerCommand(this); setOwnerCommand = new IslandTeamSetownerCommand(this);
kickCommand = new IslandTeamKickCommand(this); kickCommand = new IslandTeamKickCommand(this);
new IslandTeamInviteAcceptCommand(this); acceptCommand = new IslandTeamInviteAcceptCommand(this);
new IslandTeamInviteRejectCommand(this); rejectCommand = new IslandTeamInviteRejectCommand(this);
if (RanksManager.getInstance().rankExists(RanksManager.COOP_RANK_REF)) { if (RanksManager.getInstance().rankExists(RanksManager.COOP_RANK_REF)) {
new IslandTeamCoopCommand(this); new IslandTeamCoopCommand(this);
uncoopCommand = new IslandTeamUncoopCommand(this); uncoopCommand = new IslandTeamUncoopCommand(this);
@ -105,6 +116,11 @@ public class IslandTeamCommand extends CompositeCommand {
// Player issuing the command must have an island // Player issuing the command must have an island
island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
if (island == null) { if (island == null) {
if (isInvited(user.getUniqueId())) {
// Player has an invite, so show the invite
build();
return true;
}
user.sendMessage("general.errors.no-island"); user.sendMessage("general.errors.no-island");
return false; return false;
} }
@ -146,10 +162,11 @@ public class IslandTeamCommand extends CompositeCommand {
panelBuilder.registerTypeBuilder("STATUS", this::createStatusButton); panelBuilder.registerTypeBuilder("STATUS", this::createStatusButton);
panelBuilder.registerTypeBuilder("MEMBER", this::createMemberButton); panelBuilder.registerTypeBuilder("MEMBER", this::createMemberButton);
panelBuilder.registerTypeBuilder("INVITE", this::createInviteButton); panelBuilder.registerTypeBuilder("INVITED", this::createInvitedButton);
panelBuilder.registerTypeBuilder("RANK", this::createRankButton); panelBuilder.registerTypeBuilder("RANK", this::createRankButton);
//panelBuilder.registerTypeBuilder("KICK", this::createKickButton); //panelBuilder.registerTypeBuilder("KICK", this::createKickButton);
border = panelBuilder.getPanelTemplate().border();
background = panelBuilder.getPanelTemplate().background();
// Register unknown type builder. // Register unknown type builder.
panelBuilder.build(); panelBuilder.build();
} }
@ -203,24 +220,61 @@ public class IslandTeamCommand extends CompositeCommand {
return builder.build(); return builder.build();
} }
/** /**
* Create invite button panel item. * Create invited button panel item.
* *
* @param template the template * @param template the template
* @param slot the slot * @param slot the slot
* @return the panel item * @return the panel item
*/ */
private PanelItem createInviteButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) { private PanelItem createInvitedButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
// Player issuing the command must have an island if (isInvited(user.getUniqueId())) {
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); Invite invite = getInvite(user.getUniqueId());
if (island == null) { User inviter = User.getInstance(invite.getInviter());
return builder.icon(Material.BARRIER).name(user.getTranslation("general.errors.no-island")).build(); String name = inviter.getName();
builder.icon(inviter.getName());
builder.name("Invitation");
builder.description(switch (invite.getType()) {
case COOP ->
List.of(user.getTranslation("commands.island.team.invite.name-has-invited-you.coop", TextVariables.NAME,
name));
case TRUST ->
List.of(user.getTranslation("commands.island.team.invite.name-has-invited-you.trust",
TextVariables.NAME, name));
default ->
List.of(user.getTranslation("commands.island.team.invite.name-has-invited-you", TextVariables.NAME,
name), user.getTranslation("commands.island.team.invite.accept.confirmation"));
});
// Add all the tool tips
builder.description(template.actions().stream()
.map(ar -> user.getTranslation("commands.island.team.gui.tips." + ar.clickType().name() + ".name")
+ " "
+ user.getTranslation(ar.tooltip()))
.toList());
builder.clickHandler((panel, user, clickType, clickSlot) -> {
if (clickType.equals(ClickType.SHIFT_LEFT)) {
// Accept
switch (invite.getType()) {
case COOP -> this.acceptCommand.acceptCoopInvite(user, invite);
case TRUST -> this.acceptCommand.acceptTrustInvite(user, invite);
default -> this.acceptCommand.acceptTeamInvite(user, invite);
}
user.closeInventory();
}
if (clickType.equals(ClickType.SHIFT_RIGHT)) {
// Reject
BentoBox.getInstance().logDebug("Reject");
this.rejectCommand.execute(user, "", List.of());
user.closeInventory();
}
return true;
});
} else {
return this.getBlankBorder();
} }
// The player must be able to invite a player return builder.build();
return builder.icon(user.getName()).name(user.getTranslation("commands.island.team.gui.buttons.status.name"))
.description(showMembers()).build();
} }
/** /**
@ -235,13 +289,24 @@ public class IslandTeamCommand extends CompositeCommand {
// Player issuing the command must have an island // Player issuing the command must have an island
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
if (island == null) { if (island == null) {
return builder.icon(Material.BARRIER).name(user.getTranslation("general.errors.no-island")).build(); return getBlankBorder();
} }
return builder.icon(user.getName()).name(user.getTranslation("commands.island.team.gui.buttons.status.name")) return builder.icon(user.getName()).name(user.getTranslation("commands.island.team.gui.buttons.status.name"))
.description(showMembers()).build(); .description(showMembers()).build();
} }
private PanelItem getBlankBorder() {
return new PanelItemBuilder().icon(Objects.requireNonNullElse(border.icon(), new ItemStack(Material.BARRIER)))
.name((Objects.requireNonNullElse(border.title(), ""))).build();
}
private PanelItem getBlankBackground() {
return new PanelItemBuilder()
.icon(Objects.requireNonNullElse(background.icon(), new ItemStack(Material.BARRIER)))
.name((Objects.requireNonNullElse(background.title(), ""))).build();
}
/** /**
* Create member button panel item. * Create member button panel item.
* *
@ -253,8 +318,7 @@ public class IslandTeamCommand extends CompositeCommand {
// Player issuing the command must have an island // Player issuing the command must have an island
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
if (island == null) { if (island == null) {
return new PanelItemBuilder().icon(Material.BARRIER).name(user.getTranslation("general.errors.no-island")) return this.getBlankBackground();
.build();
} }
return switch (rank) { return switch (rank) {
case RanksManager.OWNER_RANK -> ownerView(template, slot); case RanksManager.OWNER_RANK -> ownerView(template, slot);
@ -337,12 +401,16 @@ public class IslandTeamCommand extends CompositeCommand {
int userRank = Objects.requireNonNull(island).getRank(user); int userRank = Objects.requireNonNull(island).getRank(user);
if (userRank >= island.getRankCommand(this.getLabel() + " kick") && !user.equals(member)) { if (userRank >= island.getRankCommand(this.getLabel() + " kick") && !user.equals(member)) {
// Add the tooltip for kicking // Add the tooltip for kicking
actions.stream().filter(ar -> ar.actionType().equalsIgnoreCase("kick")).map(ActionRecords::tooltip) actions.stream().filter(ar -> ar.actionType().equalsIgnoreCase("kick"))
.map(ar -> user.getTranslation("commands.island.team.gui.tips." + ar.clickType().name()) + " "
+ user.getTranslation(ar.tooltip()))
.findFirst().map(user::getTranslation).ifPresent(desc::add); .findFirst().map(user::getTranslation).ifPresent(desc::add);
} }
if (!user.equals(member) && userRank >= RanksManager.OWNER_RANK && targetRank >= RanksManager.MEMBER_RANK) { if (!user.equals(member) && userRank >= RanksManager.OWNER_RANK && targetRank >= RanksManager.MEMBER_RANK) {
// Add the tooltip for setowner // Add the tooltip for setowner
actions.stream().filter(ar -> ar.actionType().equalsIgnoreCase("setowner")).map(ActionRecords::tooltip) actions.stream().filter(ar -> ar.actionType().equalsIgnoreCase("setowner"))
.map(ar -> user.getTranslation("commands.island.team.gui.tips." + ar.clickType().name()) + " "
+ user.getTranslation(ar.tooltip()))
.findFirst().map(user::getTranslation).ifPresent(desc::add); .findFirst().map(user::getTranslation).ifPresent(desc::add);
} }
if (member != null) { if (member != null) {

View File

@ -23,7 +23,6 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
private static final String INVALID_INVITE = "commands.island.team.invite.errors.invalid-invite"; private static final String INVALID_INVITE = "commands.island.team.invite.errors.invalid-invite";
private final IslandTeamCommand itc; private final IslandTeamCommand itc;
private UUID playerUUID;
public IslandTeamInviteAcceptCommand(IslandTeamCommand islandTeamCommand) { public IslandTeamInviteAcceptCommand(IslandTeamCommand islandTeamCommand) {
super(islandTeamCommand, "accept"); super(islandTeamCommand, "accept");
@ -39,7 +38,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
@Override @Override
public boolean canExecute(User user, String label, List<String> args) { public boolean canExecute(User user, String label, List<String> args) {
playerUUID = user.getUniqueId(); UUID playerUUID = user.getUniqueId();
// Check if player has been invited // Check if player has been invited
if (!itc.isInvited(playerUUID)) { if (!itc.isInvited(playerUUID)) {
user.sendMessage("commands.island.team.invite.errors.none-invited-you"); user.sendMessage("commands.island.team.invite.errors.none-invited-you");
@ -79,7 +78,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
// Get the invite // Get the invite
Invite invite = itc.getInvite(playerUUID); Invite invite = itc.getInvite(user.getUniqueId());
switch (invite.getType()) { switch (invite.getType()) {
case COOP -> askConfirmation(user, () -> acceptCoopInvite(user, invite)); case COOP -> askConfirmation(user, () -> acceptCoopInvite(user, invite));
case TRUST -> askConfirmation(user, () -> acceptTrustInvite(user, invite)); case TRUST -> askConfirmation(user, () -> acceptTrustInvite(user, invite));
@ -89,9 +88,9 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
return true; return true;
} }
private void acceptTrustInvite(User user, Invite invite) { void acceptTrustInvite(User user, Invite invite) {
// Remove the invite // Remove the invite
itc.removeInvite(playerUUID); itc.removeInvite(user.getUniqueId());
User inviter = User.getInstance(invite.getInviter()); User inviter = User.getInstance(invite.getInviter());
Island island = invite.getIsland(); Island island = invite.getIsland();
if (island != null) { if (island != null) {
@ -115,9 +114,9 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
} }
} }
private void acceptCoopInvite(User user, Invite invite) { void acceptCoopInvite(User user, Invite invite) {
// Remove the invite // Remove the invite
itc.removeInvite(playerUUID); itc.removeInvite(user.getUniqueId());
User inviter = User.getInstance(invite.getInviter()); User inviter = User.getInstance(invite.getInviter());
Island island = invite.getIsland(); Island island = invite.getIsland();
if (island != null) { if (island != null) {
@ -141,11 +140,11 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
} }
} }
private void acceptTeamInvite(User user, Invite invite) { void acceptTeamInvite(User user, Invite invite) {
// Remove the invite // Remove the invite
itc.removeInvite(playerUUID); itc.removeInvite(user.getUniqueId());
// Get the player's island - may be null if the player has no island // Get the player's island - may be null if the player has no island
Set<Island> islands = getIslands().getIslands(getWorld(), playerUUID); Set<Island> islands = getIslands().getIslands(getWorld(), user.getUniqueId());
// Get the team's island // Get the team's island
Island teamIsland = invite.getIsland(); Island teamIsland = invite.getIsland();
if (teamIsland == null) { if (teamIsland == null) {
@ -158,11 +157,11 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
return; return;
} }
// Remove player as owner of the old island // Remove player as owner of the old island
getIslands().removePlayer(getWorld(), playerUUID); getIslands().removePlayer(getWorld(), user.getUniqueId());
// Remove money inventory etc. for leaving // Remove money inventory etc. for leaving
cleanPlayer(user); cleanPlayer(user);
// Add the player as a team member of the new island // Add the player as a team member of the new island
getIslands().setJoinTeam(teamIsland, playerUUID); getIslands().setJoinTeam(teamIsland, user.getUniqueId());
// Move player to team's island // Move player to team's island
getIslands().homeTeleportAsync(getWorld(), user.getPlayer()).thenRun(() -> { getIslands().homeTeleportAsync(getWorld(), user.getPlayer()).thenRun(() -> {
// Delete the old islands // Delete the old islands
@ -178,7 +177,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
}); });
// Reset deaths // Reset deaths
if (getIWM().isTeamJoinDeathReset(getWorld())) { if (getIWM().isTeamJoinDeathReset(getWorld())) {
getPlayers().setDeaths(getWorld(), playerUUID, 0); getPlayers().setDeaths(getWorld(), user.getUniqueId(), 0);
} }
user.sendMessage("commands.island.team.invite.accept.you-joined-island", TextVariables.LABEL, getTopLabel()); user.sendMessage("commands.island.team.invite.accept.you-joined-island", TextVariables.LABEL, getTopLabel());
User inviter = User.getInstance(invite.getInviter()); User inviter = User.getInstance(invite.getInviter());
@ -188,7 +187,8 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
} }
getIslands().save(teamIsland); getIslands().save(teamIsland);
// Fire event // Fire event
TeamEvent.builder().island(teamIsland).reason(TeamEvent.Reason.JOINED).involvedPlayer(playerUUID).build(); TeamEvent.builder().island(teamIsland).reason(TeamEvent.Reason.JOINED).involvedPlayer(user.getUniqueId())
.build();
IslandEvent.builder().island(teamIsland).involvedPlayer(user.getUniqueId()).admin(false) IslandEvent.builder().island(teamIsland).involvedPlayer(user.getUniqueId()).admin(false)
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(teamIsland.getRank(user), RanksManager.MEMBER_RANK) .reason(IslandEvent.Reason.RANK_CHANGE).rankChange(teamIsland.getRank(user), RanksManager.MEMBER_RANK)
.build(); .build();

View File

@ -623,16 +623,20 @@ commands:
name: Rank Filter name: Rank Filter
description: &a Click to cycle ranks description: &a Click to cycle ranks
tips: tips:
shift-right: SHIFT_RIGHT:
name: &b Right Shift
reject: &a click to reject
kick: | kick: |
&a Shift-right click &a click
&a to kick player &a to kick player
leave: | leave: |
&a Shift-right click &a click
&a to leave team &a to leave team
shift-left: SHIFT_LEFT:
name: &b Left Shift
accept: &a click to accept
setowner: | setowner: |
&a Shift-left to &a click to
&a set owner to &a set owner to
&a this player &a this player
info: info:

View File

@ -17,7 +17,7 @@ team_panel:
title: "&b&r" # Empty text title: "&b&r" # Empty text
# This tag indicates which rows in the panel must be shown. The panel will be sized vertically accordingly. This does not include the borders. # This tag indicates which rows in the panel must be shown. The panel will be sized vertically accordingly. This does not include the borders.
# This can be a list and rows must be between 1 and 6, if used. # This can be a list and rows must be between 1 and 6, if used.
force-shown: [1, 2] force-shown: []
# The content section contains details of each item/button in the panel. The numbers indicate the rows and then then columns of each item. # The content section contains details of each item/button in the panel. The numbers indicate the rows and then then columns of each item.
content: content:
# Row number # Row number
@ -49,6 +49,18 @@ team_panel:
cycle-down: cycle-down:
click-type: RIGHT click-type: RIGHT
tooltip: commands.island.team.gui.tips.right-click.rank tooltip: commands.island.team.gui.tips.right-click.rank
5:
# Invited button - this appears if you have been invited to join a team
data:
type: INVITED
name: commands.island.team.gui.buttons.invited
actions:
accept:
click-type: SHIFT_LEFT
tooltip: commands.island.team.gui.tips.SHIFT_LEFT.accept
reject:
click-type: SHIFT_RIGHT
tooltip: commands.island.team.gui.tips.SHIFT_RIGHT.reject
2: 2:
2: member_button 2: member_button
3: member_button 3: member_button