Added support for accepting and rejecting an invite.
This commit is contained in:
parent
1d6556613e
commit
7639c59db9
|
@ -17,9 +17,11 @@ import org.bukkit.Material;
|
|||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.events.IslandBaseEvent;
|
||||
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.reader.ItemTemplateRecord;
|
||||
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.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
|
@ -67,6 +70,14 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
|
||||
private IslandTeamUntrustCommand unTrustCommand;
|
||||
|
||||
private @Nullable TemplateItem border;
|
||||
|
||||
private @Nullable TemplateItem background;
|
||||
|
||||
private IslandTeamInviteAcceptCommand acceptCommand;
|
||||
|
||||
private IslandTeamInviteRejectCommand rejectCommand;
|
||||
|
||||
public IslandTeamCommand(CompositeCommand parent) {
|
||||
super(parent, "team");
|
||||
inviteMap = new HashMap<>();
|
||||
|
@ -82,8 +93,8 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
leaveCommand = new IslandTeamLeaveCommand(this);
|
||||
setOwnerCommand = new IslandTeamSetownerCommand(this);
|
||||
kickCommand = new IslandTeamKickCommand(this);
|
||||
new IslandTeamInviteAcceptCommand(this);
|
||||
new IslandTeamInviteRejectCommand(this);
|
||||
acceptCommand = new IslandTeamInviteAcceptCommand(this);
|
||||
rejectCommand = new IslandTeamInviteRejectCommand(this);
|
||||
if (RanksManager.getInstance().rankExists(RanksManager.COOP_RANK_REF)) {
|
||||
new IslandTeamCoopCommand(this);
|
||||
uncoopCommand = new IslandTeamUncoopCommand(this);
|
||||
|
@ -105,6 +116,11 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
// Player issuing the command must have an island
|
||||
island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
|
||||
if (island == null) {
|
||||
if (isInvited(user.getUniqueId())) {
|
||||
// Player has an invite, so show the invite
|
||||
build();
|
||||
return true;
|
||||
}
|
||||
user.sendMessage("general.errors.no-island");
|
||||
return false;
|
||||
}
|
||||
|
@ -146,10 +162,11 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
|
||||
panelBuilder.registerTypeBuilder("STATUS", this::createStatusButton);
|
||||
panelBuilder.registerTypeBuilder("MEMBER", this::createMemberButton);
|
||||
panelBuilder.registerTypeBuilder("INVITE", this::createInviteButton);
|
||||
panelBuilder.registerTypeBuilder("INVITED", this::createInvitedButton);
|
||||
panelBuilder.registerTypeBuilder("RANK", this::createRankButton);
|
||||
//panelBuilder.registerTypeBuilder("KICK", this::createKickButton);
|
||||
|
||||
border = panelBuilder.getPanelTemplate().border();
|
||||
background = panelBuilder.getPanelTemplate().background();
|
||||
// Register unknown type builder.
|
||||
panelBuilder.build();
|
||||
}
|
||||
|
@ -203,24 +220,61 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create invite button panel item.
|
||||
* Create invited button panel item.
|
||||
*
|
||||
* @param template the template
|
||||
* @param slot the slot
|
||||
* @return the panel item
|
||||
*/
|
||||
private PanelItem createInviteButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
|
||||
private PanelItem createInvitedButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
|
||||
PanelItemBuilder builder = new PanelItemBuilder();
|
||||
// Player issuing the command must have an island
|
||||
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
|
||||
if (island == null) {
|
||||
return builder.icon(Material.BARRIER).name(user.getTranslation("general.errors.no-island")).build();
|
||||
if (isInvited(user.getUniqueId())) {
|
||||
Invite invite = getInvite(user.getUniqueId());
|
||||
User inviter = User.getInstance(invite.getInviter());
|
||||
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.icon(user.getName()).name(user.getTranslation("commands.island.team.gui.buttons.status.name"))
|
||||
.description(showMembers()).build();
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -235,13 +289,24 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
// Player issuing the command must have an island
|
||||
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
|
||||
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"))
|
||||
.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.
|
||||
*
|
||||
|
@ -253,8 +318,7 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
// Player issuing the command must have an island
|
||||
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
|
||||
if (island == null) {
|
||||
return new PanelItemBuilder().icon(Material.BARRIER).name(user.getTranslation("general.errors.no-island"))
|
||||
.build();
|
||||
return this.getBlankBackground();
|
||||
}
|
||||
return switch (rank) {
|
||||
case RanksManager.OWNER_RANK -> ownerView(template, slot);
|
||||
|
@ -337,12 +401,16 @@ public class IslandTeamCommand extends CompositeCommand {
|
|||
int userRank = Objects.requireNonNull(island).getRank(user);
|
||||
if (userRank >= island.getRankCommand(this.getLabel() + " kick") && !user.equals(member)) {
|
||||
// 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);
|
||||
}
|
||||
if (!user.equals(member) && userRank >= RanksManager.OWNER_RANK && targetRank >= RanksManager.MEMBER_RANK) {
|
||||
// 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);
|
||||
}
|
||||
if (member != null) {
|
||||
|
|
|
@ -23,7 +23,6 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
|
||||
private static final String INVALID_INVITE = "commands.island.team.invite.errors.invalid-invite";
|
||||
private final IslandTeamCommand itc;
|
||||
private UUID playerUUID;
|
||||
|
||||
public IslandTeamInviteAcceptCommand(IslandTeamCommand islandTeamCommand) {
|
||||
super(islandTeamCommand, "accept");
|
||||
|
@ -39,7 +38,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
|
||||
@Override
|
||||
public boolean canExecute(User user, String label, List<String> args) {
|
||||
playerUUID = user.getUniqueId();
|
||||
UUID playerUUID = user.getUniqueId();
|
||||
// Check if player has been invited
|
||||
if (!itc.isInvited(playerUUID)) {
|
||||
user.sendMessage("commands.island.team.invite.errors.none-invited-you");
|
||||
|
@ -79,7 +78,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
@Override
|
||||
public boolean execute(User user, String label, List<String> args) {
|
||||
// Get the invite
|
||||
Invite invite = itc.getInvite(playerUUID);
|
||||
Invite invite = itc.getInvite(user.getUniqueId());
|
||||
switch (invite.getType()) {
|
||||
case COOP -> askConfirmation(user, () -> acceptCoopInvite(user, invite));
|
||||
case TRUST -> askConfirmation(user, () -> acceptTrustInvite(user, invite));
|
||||
|
@ -89,9 +88,9 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
return true;
|
||||
}
|
||||
|
||||
private void acceptTrustInvite(User user, Invite invite) {
|
||||
void acceptTrustInvite(User user, Invite invite) {
|
||||
// Remove the invite
|
||||
itc.removeInvite(playerUUID);
|
||||
itc.removeInvite(user.getUniqueId());
|
||||
User inviter = User.getInstance(invite.getInviter());
|
||||
Island island = invite.getIsland();
|
||||
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
|
||||
itc.removeInvite(playerUUID);
|
||||
itc.removeInvite(user.getUniqueId());
|
||||
User inviter = User.getInstance(invite.getInviter());
|
||||
Island island = invite.getIsland();
|
||||
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
|
||||
itc.removeInvite(playerUUID);
|
||||
itc.removeInvite(user.getUniqueId());
|
||||
// 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
|
||||
Island teamIsland = invite.getIsland();
|
||||
if (teamIsland == null) {
|
||||
|
@ -158,11 +157,11 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
return;
|
||||
}
|
||||
// Remove player as owner of the old island
|
||||
getIslands().removePlayer(getWorld(), playerUUID);
|
||||
getIslands().removePlayer(getWorld(), user.getUniqueId());
|
||||
// Remove money inventory etc. for leaving
|
||||
cleanPlayer(user);
|
||||
// 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
|
||||
getIslands().homeTeleportAsync(getWorld(), user.getPlayer()).thenRun(() -> {
|
||||
// Delete the old islands
|
||||
|
@ -178,7 +177,7 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
});
|
||||
// Reset deaths
|
||||
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 inviter = User.getInstance(invite.getInviter());
|
||||
|
@ -188,7 +187,8 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand {
|
|||
}
|
||||
getIslands().save(teamIsland);
|
||||
// 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)
|
||||
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(teamIsland.getRank(user), RanksManager.MEMBER_RANK)
|
||||
.build();
|
||||
|
|
|
@ -623,16 +623,20 @@ commands:
|
|||
name: Rank Filter
|
||||
description: &a Click to cycle ranks
|
||||
tips:
|
||||
shift-right:
|
||||
SHIFT_RIGHT:
|
||||
name: &b Right Shift
|
||||
reject: &a click to reject
|
||||
kick: |
|
||||
&a Shift-right click
|
||||
&a click
|
||||
&a to kick player
|
||||
leave: |
|
||||
&a Shift-right click
|
||||
&a click
|
||||
&a to leave team
|
||||
shift-left:
|
||||
SHIFT_LEFT:
|
||||
name: &b Left Shift
|
||||
accept: &a click to accept
|
||||
setowner: |
|
||||
&a Shift-left to
|
||||
&a click to
|
||||
&a set owner to
|
||||
&a this player
|
||||
info:
|
||||
|
|
|
@ -17,7 +17,7 @@ team_panel:
|
|||
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 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.
|
||||
content:
|
||||
# Row number
|
||||
|
@ -49,6 +49,18 @@ team_panel:
|
|||
cycle-down:
|
||||
click-type: RIGHT
|
||||
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: member_button
|
||||
3: member_button
|
||||
|
|
Loading…
Reference in New Issue