Skip to content

Commit

Permalink
Adds inviting to the GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 4, 2024
1 parent f290a2c commit 5fcc203
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -80,6 +79,10 @@ public class IslandTeamCommand extends CompositeCommand {

private IslandTeamInviteCommand inviteCommand;

private IslandTeamCoopCommand coopCommand;

private IslandTeamTrustCommand trustCommand;

public IslandTeamCommand(CompositeCommand parent) {
super(parent, "team");
inviteMap = new HashMap<>();
Expand All @@ -98,18 +101,18 @@ public void setup() {
acceptCommand = new IslandTeamInviteAcceptCommand(this);
rejectCommand = new IslandTeamInviteRejectCommand(this);
if (RanksManager.getInstance().rankExists(RanksManager.COOP_RANK_REF)) {
new IslandTeamCoopCommand(this);
coopCommand = new IslandTeamCoopCommand(this);
uncoopCommand = new IslandTeamUncoopCommand(this);
}
if (RanksManager.getInstance().rankExists(RanksManager.TRUSTED_RANK_REF)) {
new IslandTeamTrustCommand(this);
trustCommand = new IslandTeamTrustCommand(this);
unTrustCommand = new IslandTeamUntrustCommand(this);
}
new IslandTeamPromoteCommand(this, "promote");
new IslandTeamPromoteCommand(this, "demote");

// Panels
getPlugin().saveResource("panels/team_panel.yml", true);
getPlugin().saveResource("panels/team_panel.yml", false);
}

@Override
Expand Down Expand Up @@ -666,4 +669,18 @@ public Invite getInvite(UUID invitee) {
public void removeInvite(@NonNull UUID invitee) {
inviteMap.remove(invitee);
}

/**
* @return the coopCommand
*/
protected IslandTeamCoopCommand getCoopCommand() {
return coopCommand;
}

/**
* @return the trustCommand
*/
protected IslandTeamTrustCommand getTrustCommand() {
return trustCommand;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -17,6 +19,7 @@
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.team.TeamEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
Expand All @@ -37,6 +40,8 @@ public class IslandTeamInviteCommand extends CompositeCommand {
private @Nullable TemplateItem border;
private @Nullable TemplateItem background;
private User user;
private int page = 0; // This number by 35
private static final int PER_PAGE = 35;

public IslandTeamInviteCommand(IslandTeamCommand parent) {
super(parent, "invite");
Expand All @@ -50,7 +55,7 @@ public void setup() {
setDescription("commands.island.team.invite.description");
setConfigurableRankCommand();
// Panels
getPlugin().saveResource("panels/team_invite_panel.yml", true);
getPlugin().saveResource("panels/team_invite_panel.yml", false);
}


Expand Down Expand Up @@ -227,21 +232,55 @@ public void build(User user) {
panelBuilder.user(user);
panelBuilder.world(user.getWorld());

panelBuilder.template("team_panel", new File(getPlugin().getDataFolder(), "panels"));
panelBuilder.template("team_invite_panel", new File(getPlugin().getDataFolder(), "panels"));

panelBuilder.parameters("[name]", user.getName(), "[display_name]", user.getDisplayName());

panelBuilder.registerTypeBuilder("PROSPECT", this::createProspectButton);
//panelBuilder.registerTypeBuilder("INVITED", this::createInvitedButton);
//panelBuilder.registerTypeBuilder("RANK", this::createRankButton);
//panelBuilder.registerTypeBuilder("INVITE", this::createInviteButton);
panelBuilder.registerTypeBuilder("PREVIOUS", this::createPreviousButton);
panelBuilder.registerTypeBuilder("NEXT", this::createNextButton);
border = panelBuilder.getPanelTemplate().border();
background = panelBuilder.getPanelTemplate().background();
// Register unknown type builder.
panelBuilder.build();

}

private PanelItem createNextButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
long count = getWorld().getPlayers().stream().filter(player -> user.getPlayer().canSee(player))
.filter(player -> !player.equals(user.getPlayer())).count();
if (count > page * PER_PAGE) {
// We need to show a next button
return new PanelItemBuilder().name(user.getTranslation("protection.panel.next")).icon(Material.ARROW)
.clickHandler((panel, user, clickType, clickSlot) -> {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
page++;
build(user);
return true;
}).build();
}
return getBlankBorder();
}

private PanelItem createPreviousButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot) {
if (page > 0) {
// We need to show a next button
return new PanelItemBuilder().name(user.getTranslation("protection.panel.previous")).icon(Material.ARROW)
.clickHandler((panel, user, clickType, clickSlot) -> {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
page--;
build(user);
return true;
}).build();
}
return getBlankBorder();
}

private PanelItem getBlankBorder() {
return new PanelItemBuilder().icon(Objects.requireNonNullElse(border.icon(), new ItemStack(Material.BARRIER)))
.name((Objects.requireNonNullElse(border.title(), ""))).build();
}

/**
* Create member button panel item.
*
Expand All @@ -255,20 +294,48 @@ private PanelItem createProspectButton(ItemTemplateRecord template, TemplatedPan
if (island == null) {
return this.getBlankBackground();
}
// TODO: THERE"S A BUG HERE
return user.getWorld().getPlayers().stream()
.filter(player -> !getIslands().inTeam(getWorld(), player.getUniqueId())).skip(slot.slot() - 1)
.limit(1L)
.findFirst().map(this::getProspect).orElse(this.getBlankBackground());
if (page < 0) {
page = 0;
}
return getWorld().getPlayers().stream().filter(player -> user.getPlayer().canSee(player))
.filter(player -> !player.equals(user.getPlayer())).skip(slot.slot() + page * PER_PAGE).findFirst()
.map(player -> getProspect(player, template)).orElse(this.getBlankBackground());
}

private PanelItem getProspect(Player player) {
return new PanelItemBuilder().icon(player.getName()).build();
private PanelItem getProspect(Player player, ItemTemplateRecord template) {
// Check if the prospect has already been invited
if (this.itc.isInvited(player.getUniqueId())
&& user.getUniqueId().equals(this.itc.getInvite(player.getUniqueId()).getInviter())) {
return new PanelItemBuilder().icon(player.getName()).name(player.getDisplayName())
.description(user.getTranslation("commands.island.team.invite.gui.button.already-invited")).build();
}
List<String> desc = template.actions().stream().map(ar -> user
.getTranslation("commands.island.team.invite.gui.tips." + ar.clickType().name() + ".name")
+ " " + user.getTranslation(ar.tooltip())).toList();
return new PanelItemBuilder().icon(player.getName()).name(player.getDisplayName()).description(desc)
.clickHandler(
(panel, user, clickType, clickSlot) -> clickHandler(panel, user, clickType, clickSlot, player))
.build();
}

private PanelItem getBlankBorder() {
return new PanelItemBuilder().icon(Objects.requireNonNullElse(border.icon(), new ItemStack(Material.BARRIER)))
.name((Objects.requireNonNullElse(border.title(), ""))).build();
private boolean clickHandler(Panel panel, User user, ClickType clickType, int clickSlot, Player player) {
if (clickType.equals(ClickType.LEFT)) {
user.closeInventory();
if (this.canExecute(user, this.getLabel(), List.of(player.getName()))) {
this.execute(user, getLabel(), List.of(player.getName()));
}
} else if (clickType.equals(ClickType.RIGHT)) {
user.closeInventory();
if (this.itc.getCoopCommand().canExecute(user, this.getLabel(), List.of(player.getName()))) {
this.itc.getCoopCommand().execute(user, getLabel(), List.of(player.getName()));
}
} else if (clickType.equals(ClickType.SHIFT_LEFT)) {
user.closeInventory();
if (this.itc.getTrustCommand().canExecute(user, this.getLabel(), List.of(player.getName()))) {
this.itc.getTrustCommand().execute(user, getLabel(), List.of(player.getName()));
}
}
return true;
}

private PanelItem getBlankBackground() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ commands:
gui:
titles:
team-invite-panel: "Invite Players"
button:
already-invited: "&c Invited already"
tips:
LEFT:
name: "&b Left Click"
Expand Down
28 changes: 27 additions & 1 deletion src/main/resources/panels/team_invite_panel.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Name of panel used for indentification in the code - must be the same name as the filename.
team_panel:
team_invite_panel:
# Title of the panel shown to the user. This is a reference and the reference will be translatable in the locale file
title: commands.island.team.invite.gui.titles.team-invite-panel
# The type of panel to show. Options are INVENTORY, HOPPER, DROPPER. INVENTORY is that standard chest inventory and
Expand All @@ -21,6 +21,32 @@ 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.
content:
# Row number
1:
2:
data:
type: PREVIOUS
# Actions cover what happens if the button is clicked or the mouse is moved over it. There can be multiple actions possible for different
# click-types.
actions:
# Each action has an arbitrary descriptive name to define it.
view:
# The click-type is the same as the bukkit {@link org.bukkit.event.inventory.ClickType}. UNKNOWN is the default.
click-type: LEFT
# tooltip is a locale reference that will be translated for the user and shown when they hover over the button.
tooltip: commands.island.team.invite.gui.tips.previous
8:
data:
type: NEXT
# Actions cover what happens if the button is clicked or the mouse is moved over it. There can be multiple actions possible for different
# click-types.
actions:
# Each action has an arbitrary descriptive name to define it.
view:
# The click-type is the same as the bukkit {@link org.bukkit.event.inventory.ClickType}. UNKNOWN is the default.
click-type: LEFT
# tooltip is a locale reference that will be translated for the user and shown when they hover over the button.
tooltip: commands.island.team.invite.gui.tips.next

2:
2: prospect_button
3: prospect_button
Expand Down

0 comments on commit 5fcc203

Please sign in to comment.