Skip to content

Commit

Permalink
Add island object to Panel for context. (#2263)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tastybento authored Jan 8, 2024
1 parent 29a6a51 commit c62d4f6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public boolean execute(User user, String label, List<String> args) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public boolean canExecute(User user, String label, List<String> args) {
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()
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/world/bentobox/bentobox/api/panels/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand All @@ -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.
Expand Down Expand Up @@ -234,4 +236,18 @@ public void setWorld(World world) {
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;
}

}
13 changes: 13 additions & 0 deletions src/main/java/world/bentobox/bentobox/api/panels/Tab.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
*/
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -208,4 +210,5 @@ public void setActivePage(int activePage) {
public void setActiveTab(int activeTab) {
this.activeTab = activeTab;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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
Expand All @@ -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.
Expand Down Expand Up @@ -97,7 +109,10 @@ public TabbedPanel build() {
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;
}

/**
Expand Down Expand Up @@ -142,6 +157,12 @@ public boolean isHideIfEmpty() {
return hideIfEmpty;
}

/**
* @return the island
*/
public Island getIsland() {
return island;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public enum EntityLimitTabType {
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
Expand All @@ -61,7 +63,6 @@ public GeoMobLimitTab(@NonNull User user, @NonNull EntityLimitTabType type, Worl
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
Expand Down Expand Up @@ -140,4 +141,14 @@ private PanelItem getPanelItem(EntityType c, User user) {
return pib.build();
}

@Override
public TabbedPanel getParentPanel() {
return parent;
}

@Override
public void setParentPanel(TabbedPanel parent) {
this.parent = parent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ public class SettingsTab implements Tab, ClickHandler {
protected User user;
protected World world;
protected Island island;
protected TabbedPanel parent;

/**
* Show a tab of settings
* @param user - user who is viewing the tab
* @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();
}
Expand Down Expand Up @@ -124,7 +125,9 @@ public String getName() {
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
Expand Down Expand Up @@ -223,4 +226,14 @@ public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
return true;
}

@Override
public TabbedPanel getParentPanel() {
return parent;
}

@Override
public void setParentPanel(TabbedPanel parent) {
this.parent = parent;
}

}

1 comment on commit c62d4f6

@IAISI
Copy link

@IAISI IAISI commented on c62d4f6 Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit broken ATM...

[13:34:34 INFO]: Player issued server command: /is settings
[13:34:34 WARN]: com.destroystokyo.paper.exception.ServerCommandException: java.lang.NullPointerException: Cannot invoke "world.bentobox.bentobox.api.panels.TabbedPanel.getIsland()" because "this.parent" is null
[13:34:34 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:164)
[13:34:34 WARN]:        at org.bukkit.craftbukkit.v1_20_R3.CraftServer.dispatchCommand(CraftServer.java:987)
[13:34:34 WARN]:        at org.bukkit.craftbukkit.v1_20_R3.command.BukkitCommandWrapper.run(BukkitCommandWrapper.java:64)
[13:34:34 WARN]:        at com.mojang.brigadier.context.ContextChain.runExecutable(ContextChain.java:73)
[13:34:34 WARN]:        at net.minecraft.commands.execution.tasks.ExecuteCommand.a(ExecuteCommand.java:32)
[13:34:34 WARN]:        at net.minecraft.commands.execution.tasks.ExecuteCommand.execute(ExecuteCommand.java:19)
[13:34:34 WARN]:        at net.minecraft.commands.execution.UnboundEntryAction.a(UnboundEntryAction.java:8)
[13:34:34 WARN]:        at net.minecraft.commands.execution.CommandQueueEntry.a(CommandQueueEntry.java:5)
[13:34:34 WARN]:        at net.minecraft.commands.execution.ExecutionContext.a(ExecutionContext.java:101)
[13:34:34 WARN]:        at net.minecraft.commands.CommandDispatcher.a(CommandDispatcher.java:435)
[13:34:34 WARN]:        at net.minecraft.commands.CommandDispatcher.performCommand(CommandDispatcher.java:336)
[13:34:34 WARN]:        at net.minecraft.commands.CommandDispatcher.a(CommandDispatcher.java:323)
[13:34:34 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:2234)
[13:34:34 WARN]:        at net.minecraft.server.network.PlayerConnection.lambda$handleChatCommand$16(PlayerConnection.java:2194)
[13:34:34 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.b(IAsyncTaskHandler.java:59)
[13:34:34 WARN]:        at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
[13:34:34 WARN]:        at net.minecraft.server.TickTask.run(TickTask.java:18)
[13:34:34 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.d(IAsyncTaskHandler.java:153)
[13:34:34 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.d(IAsyncTaskHandlerReentrant.java:24)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1458)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:194)
[13:34:34 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.x(IAsyncTaskHandler.java:126)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.bl(MinecraftServer.java:1435)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1358)
[13:34:34 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.c(IAsyncTaskHandler.java:136)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.w_(MinecraftServer.java:1336)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1220)
[13:34:34 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:321)
[13:34:34 WARN]:        at java.base/java.lang.Thread.run(Thread.java:840)
[13:34:34 WARN]: Caused by: java.lang.NullPointerException: Cannot invoke "world.bentobox.bentobox.api.panels.TabbedPanel.getIsland()" because "this.parent" is null
[13:34:34 WARN]:        at BentoBox.jar//world.bentobox.bentobox.panels.settings.SettingsTab.<init>(SettingsTab.java:57)
[13:34:34 WARN]:        at BentoBox.jar//world.bentobox.bentobox.api.commands.island.IslandSettingsCommand.execute(IslandSettingsCommand.java:51)
[13:34:34 WARN]:        at BentoBox.jar//world.bentobox.bentobox.api.commands.CompositeCommand.call(CompositeCommand.java:292)
[13:34:34 WARN]:        at BentoBox.jar//world.bentobox.bentobox.api.commands.CompositeCommand.execute(CompositeCommand.java:260)
[13:34:34 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155)
[13:34:34 WARN]:        ... 28 more


> bentobox versions
[13:36:52 INFO]: Running PAPER 1.20.4.
[13:36:52 INFO]: BentoBox version: 2.0.0-SNAPSHOT-b2526
[13:36:52 INFO]: Database: JSON
[13:36:52 INFO]: Loaded Game Worlds:
[13:36:52 INFO]: bskyblock_world (BSkyBlock): Overworld, Nether, The End
[13:36:52 INFO]: Loaded Addons:
[13:36:52 INFO]: Bank 1.7.1-SNAPSHOT-b87 (ENABLED)
[13:36:52 INFO]: Biomes 2.2.0-SNAPSHOT-b256 (ENABLED)
[13:36:52 INFO]: Border 4.2.2-SNAPSHOT-b194 (ENABLED)
[13:36:52 INFO]: BSkyBlock 1.17.0-SNAPSHOT-b751 (ENABLED)
[13:36:52 INFO]: CauldronWitchery 2.0.2-SNAPSHOT-#59 (ENABLED)
[13:36:52 INFO]: Challenges 1.3.0-SNAPSHOT-b539 (ENABLED)
[13:36:52 INFO]: CheckMeOut 1.2.0-SNAPSHOT-b93 (ENABLED)
[13:36:52 INFO]: ControlPanel 1.13.1-SNAPSHOT-b61 (ENABLED)
[13:36:52 INFO]: DimensionalTrees 1.6.1-SNAPSHOT-b89 (ENABLED)
[13:36:52 INFO]: ExtraMobs 1.12 (ENABLED)
[13:36:52 INFO]: IslandFly 1.11.1 (ENABLED)
[13:36:52 INFO]: Level 2.12.0-SNAPSHOT-b555 (ENABLED)
[13:36:52 INFO]: Likes 2.4.0-SNAPSHOT-b100 (ENABLED)
[13:36:52 INFO]: MagicCobblestoneGenerator 2.5.2-SNAPSHOT-b271 (ENABLED)
[13:36:52 INFO]: TwerkingForTrees 1.4.4-SNAPSHOT-b68 (ENABLED)
[13:36:52 INFO]: Visit 1.7.0-SNAPSHOT (ENABLED)
[13:36:52 INFO]: Warps 1.14.0-SNAPSHOT-b359 (ENABLED)
> ver
[13:36:53 INFO]: Checking version, please wait...
[13:36:53 INFO]: This server is running Paper version git-Paper-381 (MC: 1.20.4) (Implementing API version 1.20.4-R0.1-SNAPSHOT) (Git: 07b956e)
You are running the latest version
Previous version: git-Paper-378 (MC: 1.20.4)

Please sign in to comment.