Add island object to Panel for context.

Enable Tab object to reference parent TabbedPanel in the builder. It is
late assigned after building. This enables tabs to get the parent, and
therefore get the Island object.

default methods were used to support backward compatibility.
This commit is contained in:
tastybento 2024-01-08 11:05:13 +09:00
parent 29a6a51f30
commit c07b480349
8 changed files with 86 additions and 9 deletions

View File

@ -242,8 +242,8 @@ public class AdminSettingsCommand extends CompositeCommand {
new TabbedPanelBuilder()
.user(user)
.world(island.getWorld())
.tab(1, new SettingsTab(user, island, Flag.Type.PROTECTION))
.tab(2, new SettingsTab(user, island, Flag.Type.SETTING))
.island(island).tab(1, new SettingsTab(user, Flag.Type.PROTECTION))
.tab(2, new SettingsTab(user, Flag.Type.SETTING))
.startingSlot(1)
.size(54)
.build().openPanel();

View File

@ -47,9 +47,9 @@ public class IslandSettingsCommand extends CompositeCommand {
public boolean execute(User user, String label, List<String> args) {
new TabbedPanelBuilder()
.user(user)
.island(island)
.world(island.getWorld())
.tab(1, new SettingsTab(user, island, Flag.Type.PROTECTION))
.tab(2, new SettingsTab(user, island, Flag.Type.SETTING))
.tab(1, new SettingsTab(user, Flag.Type.PROTECTION)).tab(2, new SettingsTab(user, Flag.Type.SETTING))
.startingSlot(1)
.size(54)
.hideIfEmpty()

View File

@ -13,6 +13,7 @@ import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.util.heads.HeadGetter;
import world.bentobox.bentobox.util.heads.HeadRequester;
@ -30,6 +31,7 @@ public class Panel implements HeadRequester, InventoryHolder {
private User user;
private String name;
private World world;
private Island island;
/**
* Various types of Panels that can be created that use InventoryTypes.
@ -234,4 +236,18 @@ public class Panel implements HeadRequester, InventoryHolder {
this.world = world;
}
/**
* @return the island
*/
public Island getIsland() {
return island;
}
/**
* @param island the island to set
*/
protected void setIsland(Island island) {
this.island = island;
}
}

View File

@ -15,6 +15,19 @@ import org.eclipse.jdt.annotation.Nullable;
*/
public interface Tab {
/**
* @return the tabbed panel that owns this tab
*/
default TabbedPanel getParentPanel() {
return null;
}
/**
* @param parent set the tabbed panel that owns this tab
*/
default void setParentPanel(TabbedPanel parent) {
}
// The icon that should be shown at the top of the tabbed panel
PanelItem getIcon();

View File

@ -44,6 +44,8 @@ public class TabbedPanel extends Panel implements PanelListener {
public TabbedPanel(TabbedPanelBuilder tpb) {
this.tpb = tpb;
this.setWorld(tpb.getWorld());
// Set island context in Panel
this.setIsland(tpb.getIsland());
}
/* (non-Javadoc)
@ -208,4 +210,5 @@ public class TabbedPanel extends Panel implements PanelListener {
public void setActiveTab(int activeTab) {
this.activeTab = activeTab;
}
}

View File

@ -9,6 +9,7 @@ import org.bukkit.World;
import world.bentobox.bentobox.api.panels.Tab;
import world.bentobox.bentobox.api.panels.TabbedPanel;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
* Builds {@link TabbedPanel}'s
@ -23,6 +24,17 @@ public class TabbedPanelBuilder {
private World world;
private User user;
private boolean hideIfEmpty;
private Island island;
/**
* Set the island related to this panel
* @param island island
* @return PanelBuilder - PanelBuilder
*/
public TabbedPanelBuilder island(Island island) {
this.island = island;
return this;
}
/**
* Forces panel to be a specific number of slots.
@ -97,7 +109,10 @@ public class TabbedPanelBuilder {
if (!tabs.isEmpty() && !tabs.containsKey(startingSlot)) {
startingSlot = ((TreeMap<Integer, Tab>)tabs).firstKey();
}
return new TabbedPanel(this);
TabbedPanel tp = new TabbedPanel(this);
// Set tab parents
tabs.values().forEach(tab -> tab.setParentPanel(tp));
return tp;
}
/**
@ -142,6 +157,12 @@ public class TabbedPanelBuilder {
return hideIfEmpty;
}
/**
* @return the island
*/
public Island getIsland() {
return island;
}
}

View File

@ -48,8 +48,10 @@ public class GeoMobLimitTab implements Tab, ClickHandler {
private final User user;
private final EntityLimitTabType type;
private final World world;
private TabbedPanel parent;
/**
* @param parent - tabbed panel that owns this panel
* @param user - user viewing the tab
* @param type - type of tab to show - Geo limit or Mob limit
* @param world - world where this tab is being used
@ -61,7 +63,6 @@ public class GeoMobLimitTab implements Tab, ClickHandler {
this.world = world;
}
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
// Case panel to Tabbed Panel to get the active page
@ -140,4 +141,14 @@ public class GeoMobLimitTab implements Tab, ClickHandler {
return pib.build();
}
@Override
public TabbedPanel getParentPanel() {
return parent;
}
@Override
public void setParentPanel(TabbedPanel parent) {
this.parent = parent;
}
}

View File

@ -44,6 +44,7 @@ public class SettingsTab implements Tab, ClickHandler {
protected User user;
protected World world;
protected Island island;
protected TabbedPanel parent;
/**
* Show a tab of settings
@ -51,9 +52,9 @@ public class SettingsTab implements Tab, ClickHandler {
* @param island - the island
* @param type - flag type
*/
public SettingsTab(User user, Island island, Type type) {
public SettingsTab(User user, Type type) {
this.user = user;
this.island = island;
this.island = parent.getIsland();
this.type = type;
this.world = island.getWorld();
}
@ -124,7 +125,9 @@ public class SettingsTab implements Tab, ClickHandler {
plugin.getPlayers().setFlagsDisplayMode(user.getUniqueId(), plugin.getPlayers().getFlagsDisplayMode(user.getUniqueId()).getNext());
flags = getFlags();
}
return flags.stream().map((f -> f.toPanelItem(plugin, user, island, plugin.getIWM().getHiddenFlags(world).contains(f.getID())))).toList();
return flags.stream().map(
(f -> f.toPanelItem(plugin, user, island, plugin.getIWM().getHiddenFlags(world).contains(f.getID()))))
.toList();
}
@Override
@ -223,4 +226,14 @@ public class SettingsTab implements Tab, ClickHandler {
return true;
}
@Override
public TabbedPanel getParentPanel() {
return parent;
}
@Override
public void setParentPanel(TabbedPanel parent) {
this.parent = parent;
}
}