Added support for kick, setowner, and leave.

This commit is contained in:
tastybento 2024-01-03 15:55:31 +09:00
parent 1958a71a03
commit 1d6556613e
9 changed files with 160 additions and 60 deletions

View File

@ -8,17 +8,18 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
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;
@ -56,6 +57,16 @@ public class IslandTeamCommand extends CompositeCommand {
private int rank = RanksManager.OWNER_RANK; private int rank = RanksManager.OWNER_RANK;
private IslandTeamKickCommand kickCommand;
private IslandTeamLeaveCommand leaveCommand;
private IslandTeamSetownerCommand setOwnerCommand;
private IslandTeamUncoopCommand uncoopCommand;
private IslandTeamUntrustCommand unTrustCommand;
public IslandTeamCommand(CompositeCommand parent) { public IslandTeamCommand(CompositeCommand parent) {
super(parent, "team"); super(parent, "team");
inviteMap = new HashMap<>(); inviteMap = new HashMap<>();
@ -68,24 +79,24 @@ public class IslandTeamCommand extends CompositeCommand {
setDescription("commands.island.team.description"); setDescription("commands.island.team.description");
// Register commands // Register commands
new IslandTeamInviteCommand(this); new IslandTeamInviteCommand(this);
new IslandTeamLeaveCommand(this); leaveCommand = new IslandTeamLeaveCommand(this);
new IslandTeamSetownerCommand(this); setOwnerCommand = new IslandTeamSetownerCommand(this);
new IslandTeamKickCommand(this); kickCommand = new IslandTeamKickCommand(this);
new IslandTeamInviteAcceptCommand(this); new IslandTeamInviteAcceptCommand(this);
new IslandTeamInviteRejectCommand(this); 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);
new IslandTeamUncoopCommand(this); uncoopCommand = new IslandTeamUncoopCommand(this);
} }
if (RanksManager.getInstance().rankExists(RanksManager.TRUSTED_RANK_REF)) { if (RanksManager.getInstance().rankExists(RanksManager.TRUSTED_RANK_REF)) {
new IslandTeamTrustCommand(this); new IslandTeamTrustCommand(this);
new IslandTeamUntrustCommand(this); unTrustCommand = new IslandTeamUntrustCommand(this);
} }
new IslandTeamPromoteCommand(this, "promote"); new IslandTeamPromoteCommand(this, "promote");
new IslandTeamPromoteCommand(this, "demote"); new IslandTeamPromoteCommand(this, "demote");
// Panels // Panels
getPlugin().saveResource("panels/team_panel.yml", false); getPlugin().saveResource("panels/team_panel.yml", true);
} }
@Override @Override
@ -145,18 +156,46 @@ public class IslandTeamCommand extends CompositeCommand {
private PanelItem createRankButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) { private PanelItem createRankButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
PanelItemBuilder builder = new PanelItemBuilder(); PanelItemBuilder builder = new PanelItemBuilder();
builder.name("Rank"); builder.name(user.getTranslation("commands.island.team.gui.buttons.rank-filter.name"));
builder.icon(Material.AMETHYST_SHARD); builder.icon(Material.AMETHYST_SHARD);
builder.description("Rank shown = " + user.getTranslation(RanksManager.getInstance().getRank(rank))); // Create description
builder.clickHandler((panel, user, clickType, clickSlot) -> { RanksManager.getInstance().getRanks().forEach((reference, score) -> {
BentoBox.getInstance().logDebug("Rank = " + rank); if (rank == RanksManager.OWNER_RANK && score > RanksManager.VISITOR_RANK
if (clickType.equals(ClickType.RIGHT)) { && score <= RanksManager.OWNER_RANK) {
rank = RanksManager.getInstance().getRankDownValue(rank); builder.description(user.getTranslation("protection.panel.flag-item.allowed-rank")
+ user.getTranslation(reference));
} else { } else if (score > RanksManager.VISITOR_RANK && score < rank) {
rank = RanksManager.getInstance().getRankUpValue(rank); builder.description(user.getTranslation("protection.panel.flag-item.blocked-rank")
+ user.getTranslation(reference));
} else if (score <= RanksManager.OWNER_RANK && score > rank) {
builder.description(user.getTranslation("protection.panel.flag-item.blocked-rank")
+ user.getTranslation(reference));
} else if (score == rank) {
builder.description(user.getTranslation("protection.panel.flag-item.allowed-rank")
+ user.getTranslation(reference));
} }
BentoBox.getInstance().logDebug("New Rank = " + rank); });
builder.description(user.getTranslation("commands.island.team.gui.buttons.rank-filter.description"));
builder.clickHandler((panel, user, clickType, clickSlot) -> {
if (clickType.equals(ClickType.LEFT)) {
rank = RanksManager.getInstance().getRankDownValue(rank);
if (rank <= RanksManager.VISITOR_RANK) {
rank = RanksManager.OWNER_RANK;
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
} else {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
}
}
if (clickType.equals(ClickType.RIGHT)) {
rank = RanksManager.getInstance().getRankUpValue(rank);
if (rank >= RanksManager.OWNER_RANK) {
rank = RanksManager.getInstance().getRankUpValue(RanksManager.VISITOR_RANK);
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
} else {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
}
}
// Update panel after click // Update panel after click
build(); build();
return true; return true;
@ -279,35 +318,48 @@ public class IslandTeamCommand extends CompositeCommand {
} }
/** /**
* Shows a member's head * Shows a member's head. The clicks available will depend on who is viewing.
* @param rank - the rank to show * @param targetRank - the rank to show
* @param slot - the slot number * @param slot - the slot number
* @param actions - actions that need to apply to this member button as provided by the template * @param actions - actions that need to apply to this member button as provided by the template
* @return panel item * @return panel item
*/ */
private PanelItem getMemberButton(int rank, int slot, List<ActionRecords> actions) { private PanelItem getMemberButton(int targetRank, int slot, List<ActionRecords> actions) {
if (slot == 0 && island.getOwner() != null) { if (slot == 0 && island.getOwner() != null) {
// Owner // Owner
return getMemberButton(RanksManager.OWNER_RANK, 1, actions); return getMemberButton(RanksManager.OWNER_RANK, 1, actions);
} }
long count = island.getMemberSet(rank, false).size(); String ref = RanksManager.getInstance().getRank(targetRank);
String ref = RanksManager.getInstance().getRank(rank); User member = island.getMemberSet(targetRank, false).stream().sorted().skip(slot - 1L).limit(1L)
User player = island.getMemberSet(rank, false).stream().sorted().skip(slot - 1L).limit(1L)
.map(User::getInstance).findFirst().orElse(null); .map(User::getInstance).findFirst().orElse(null);
if (player != null) { // Make button description depending on viewer
if (player.isOnline()) { List<String> desc = new ArrayList<>();
return new PanelItemBuilder().icon(player.getName()).name(player.getDisplayName()) int userRank = Objects.requireNonNull(island).getRank(user);
.description( if (userRank >= island.getRankCommand(this.getLabel() + " kick") && !user.equals(member)) {
user.getTranslation("commands.island.team.info.rank-layout.generic", TextVariables.RANK, // Add the tooltip for kicking
user.getTranslation(ref), TextVariables.NUMBER, String.valueOf(count))) actions.stream().filter(ar -> ar.actionType().equalsIgnoreCase("kick")).map(ActionRecords::tooltip)
.clickHandler((panel, user, clickType, i) -> clickListener(panel, user, clickType, i, player, .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)
.findFirst().map(user::getTranslation).ifPresent(desc::add);
}
if (member != null) {
if (member.isOnline()) {
desc.add(0, user.getTranslation(ref));
return new PanelItemBuilder().icon(member.getName()).name(member.getDisplayName())
.description(desc)
.clickHandler((panel, user, clickType, i) -> clickListener(panel, user, clickType, i, member,
actions)) actions))
.build(); .build();
} else { } else {
// Offline player // Offline player
return new PanelItemBuilder().icon(player.getName()).name(player.getDisplayName()) desc.add(0, user.getTranslation(ref));
.description(offlinePlayerStatus(user, Bukkit.getOfflinePlayer(player.getUniqueId()))) desc.add(1, offlinePlayerStatus(user, Bukkit.getOfflinePlayer(member.getUniqueId())));
.clickHandler((panel, user, clickType, i) -> clickListener(panel, user, clickType, i, player, return new PanelItemBuilder().icon(member.getName()).name(member.getDisplayName())
.description(desc)
.clickHandler((panel, user, clickType, i) -> clickListener(panel, user, clickType, i, member,
actions)) actions))
.build(); .build();
} }
@ -315,20 +367,32 @@ public class IslandTeamCommand extends CompositeCommand {
return null; return null;
} }
private boolean clickListener(Panel panel, User user, ClickType clickType, int i, User player, private boolean clickListener(Panel panel, User clicker, ClickType clickType, int i, User member,
List<ActionRecords> actions) { List<ActionRecords> actions) {
int rank = Objects.requireNonNull(island).getRank(clicker);
for (ItemTemplateRecord.ActionRecords action : actions) { for (ItemTemplateRecord.ActionRecords action : actions) {
if (clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN) { if (clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN) {
switch (action.actionType().toUpperCase(Locale.ENGLISH)) { switch (action.actionType().toUpperCase(Locale.ENGLISH)) {
case "KICK" -> { case "KICK" -> {
// Kick the player // Kick the player, or uncoop, or untrust
if (!player.equals(user)) { if (!member.equals(clicker) && rank >= island.getRankCommand(this.getLabel() + " kick")) {
this.user.closeInventory(); clicker.closeInventory();
BentoBox.getInstance() removePlayer(clicker, member);
.logDebug(this.getTopLabel() + " " + this.getLabel() + " kick " + player.getName()); clicker.getPlayer().playSound(clicker.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F);
user.performCommand(this.getTopLabel() + " " + this.getLabel() + " kick " + player.getName()); }
}
case "SETOWNER" -> {
// Make the player the leader of the island
if (!member.equals(clicker) && clicker.getUniqueId().equals(island.getOwner())) {
clicker.closeInventory();
this.setOwnerCommand.setOwner(clicker, member.getUniqueId());
}
}
case "LEAVE" -> {
if (member.equals(clicker) && !clicker.getUniqueId().equals(island.getOwner())) {
clicker.closeInventory();
leaveCommand.leave(clicker);
} }
} }
} }
} }
@ -336,6 +400,16 @@ public class IslandTeamCommand extends CompositeCommand {
return true; return true;
} }
private void removePlayer(User clicker, User member) {
// If member then kick, if coop, uncoop, if trusted, then untrust
switch (island.getRank(member)) {
case RanksManager.COOP_RANK -> this.uncoopCommand.unCoopCmd(user, member.getUniqueId());
case RanksManager.TRUSTED_RANK -> this.unTrustCommand.unTrustCmd(user, member.getUniqueId());
default -> kickCommand.kick(clicker, member.getUniqueId());
}
}
private List<String> showMembers() { private List<String> showMembers() {
List<String> message = new ArrayList<>(); List<String> message = new ArrayList<>();
// Gather online members // Gather online members

View File

@ -84,7 +84,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
} }
} }
private void kick(User user, UUID targetUUID) { protected void kick(User user, UUID targetUUID) {
User target = User.getInstance(targetUUID); User target = User.getInstance(targetUUID);
Island oldIsland = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); // Should never be Island oldIsland = Objects.requireNonNull(getIslands().getIsland(getWorld(), targetUUID)); // Should never be
// null because of // null because of

View File

@ -65,7 +65,7 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand {
} }
private void leave(User user) { protected void leave(User user) {
Island island = getIslands().getIsland(getWorld(), user); Island island = getIslands().getIsland(getWorld(), user);
if (island == null) { if (island == null) {
user.sendMessage("general.errors.no-island"); user.sendMessage("general.errors.no-island");

View File

@ -69,19 +69,24 @@ public class IslandTeamSetownerCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
return setOwner(user, targetUUID);
}
protected boolean setOwner(User user, @Nullable UUID targetUUID2) {
// Fire event so add-ons can run commands, etc. // Fire event so add-ons can run commands, etc.
Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); Island island = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId());
// Fire event so add-ons can run commands, etc. // Fire event so add-ons can run commands, etc.
IslandBaseEvent e = TeamEvent.builder().island(island).reason(TeamEvent.Reason.SETOWNER) IslandBaseEvent e = TeamEvent.builder().island(island).reason(TeamEvent.Reason.SETOWNER)
.involvedPlayer(targetUUID).build(); .involvedPlayer(targetUUID2).build();
if (e.isCancelled()) { if (e.isCancelled()) {
return false; return false;
} }
getIslands().setOwner(getWorld(), user, targetUUID); getIslands().setOwner(getWorld(), user, targetUUID2);
// Call the event for the new owner // Call the event for the new owner
IslandEvent.builder().island(island).involvedPlayer(targetUUID).admin(false) IslandEvent.builder().island(island).involvedPlayer(targetUUID2).admin(false)
.reason(IslandEvent.Reason.RANK_CHANGE) .reason(IslandEvent.Reason.RANK_CHANGE)
.rankChange(island.getRank(User.getInstance(targetUUID)), RanksManager.OWNER_RANK).build(); .rankChange(island.getRank(User.getInstance(targetUUID2)), RanksManager.OWNER_RANK).build();
// Call the event for the previous owner // Call the event for the previous owner
IslandEvent.builder().island(island).involvedPlayer(user.getUniqueId()).admin(false) IslandEvent.builder().island(island).involvedPlayer(user.getUniqueId()).admin(false)
.reason(IslandEvent.Reason.RANK_CHANGE).rankChange(RanksManager.OWNER_RANK, RanksManager.SUB_OWNER_RANK) .reason(IslandEvent.Reason.RANK_CHANGE).rankChange(RanksManager.OWNER_RANK, RanksManager.SUB_OWNER_RANK)

View File

@ -68,7 +68,7 @@ public class IslandTeamUncoopCommand extends CompositeCommand {
return unCoopCmd(user, targetUUID); return unCoopCmd(user, targetUUID);
} }
private boolean unCoopCmd(User user, UUID targetUUID) { protected boolean unCoopCmd(User user, UUID targetUUID) {
// Player cannot uncoop themselves // Player cannot uncoop themselves
if (user.getUniqueId().equals(targetUUID)) { if (user.getUniqueId().equals(targetUUID)) {
user.sendMessage("commands.island.team.uncoop.cannot-uncoop-yourself"); user.sendMessage("commands.island.team.uncoop.cannot-uncoop-yourself");

View File

@ -68,7 +68,7 @@ public class IslandTeamUntrustCommand extends CompositeCommand {
return unTrustCmd(user, targetUUID); return unTrustCmd(user, targetUUID);
} }
private boolean unTrustCmd(User user, UUID targetUUID) { protected boolean unTrustCmd(User user, UUID targetUUID) {
// Player cannot untrust themselves // Player cannot untrust themselves
if (user.getUniqueId().equals(targetUUID)) { if (user.getUniqueId().equals(targetUUID)) {
user.sendMessage("commands.island.team.untrust.cannot-untrust-yourself"); user.sendMessage("commands.island.team.untrust.cannot-untrust-yourself");

View File

@ -430,7 +430,6 @@ public class Flag implements Comparable<Flag> {
// Protection flag // Protection flag
pib.description(user.getTranslation("protection.panel.flag-item.description-layout", pib.description(user.getTranslation("protection.panel.flag-item.description-layout",
TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference()))); TextVariables.DESCRIPTION, user.getTranslation(getDescriptionReference())));
plugin.getRanksManager();
RanksManager.getInstance().getRanks().forEach((reference, score) -> { RanksManager.getInstance().getRanks().forEach((reference, score) -> {
if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) { if (score > RanksManager.BANNED_RANK && score < island.getFlag(this)) {
pib.description(user.getTranslation("protection.panel.flag-item.blocked-rank") + user.getTranslation(reference)); pib.description(user.getTranslation("protection.panel.flag-item.blocked-rank") + user.getTranslation(reference));

View File

@ -619,10 +619,22 @@ commands:
status: status:
name: Status name: Status
description: The status of the team description: The status of the team
rank-filter:
name: Rank Filter
description: &a Click to cycle ranks
tips: tips:
click-to-view: Click to view shift-right:
right-click-to-kick: Right click to kick player - requires confirmation kick: |
click-to-invite: Click to invite a team member &a Shift-right click
&a to kick player
leave: |
&a Shift-right click
&a to leave team
shift-left:
setowner: |
&a Shift-left to
&a set owner to
&a this player
info: info:
description: display detailed info about your team description: display detailed info about your team
member-layout: member-layout:

View File

@ -21,7 +21,7 @@ team_panel:
# 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
0: 1:
# Column number # Column number
1: 1:
# The data section is a key-value list of data relavent for this button. It is interpreted by the code implemented the panel. # The data section is a key-value list of data relavent for this button. It is interpreted by the code implemented the panel.
@ -38,14 +38,17 @@ team_panel:
# tooltip is a locale reference that will be translated for the user and shown when they hover over the button. # tooltip is a locale reference that will be translated for the user and shown when they hover over the button.
tooltip: commands.island.team.gui.tips.click-to-view tooltip: commands.island.team.gui.tips.click-to-view
3: 3:
# Change rank # Rank filter
data: data:
type: RANK type: RANK
name: commands.island.team.gui.buttons.rank-filter
actions: actions:
add: cycle-up:
click-type: LEFT click-type: LEFT
tooltip: commands.island.team.gui.tips.click-to-change-rank tooltip: commands.island.team.gui.tips.right-click.rank
cycle-down:
click-type: RIGHT
tooltip: commands.island.team.gui.tips.right-click.rank
2: 2:
2: member_button 2: member_button
3: member_button 3: member_button
@ -103,6 +106,13 @@ team_panel:
# Each action has an arbitrary descriptive name to define it. # Each action has an arbitrary descriptive name to define it.
kick: kick:
# The click-type is the same as the bukkit {@link org.bukkit.event.inventory.ClickType}. UNKNOWN is the default. # The click-type is the same as the bukkit {@link org.bukkit.event.inventory.ClickType}. UNKNOWN is the default.
click-type: RIGHT click-type: SHIFT_RIGHT
# tooltip is a locale reference that will be translated for the user and shown when they hover over the button. # tooltip is a locale reference that will be translated for the user and shown when they hover over the button.
tooltip: commands.island.team.gui.tips.right-click-to-kick tooltip: commands.island.team.gui.tips.shift-right.kick
leave:
click-type: SHIFT_RIGHT
tooltip: commands.island.team.gui.tips.shift-right.leave
setowner:
click-type: SHIFT_LEFT
tooltip: commands.island.team.gui.tips.shift-left.setowner