aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/TestController.java2
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java22
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java40
3 files changed, 61 insertions, 3 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java b/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java
index 43a0a6f..1d3c955 100644
--- a/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java
+++ b/GuiTest/src/main/java/io/github/cottonmc/test/TestController.java
@@ -22,7 +22,7 @@ public class TestController extends CottonInventoryController {
root.add(button, 0, 3, 5, 1);
- root.add(new WPlayerInvPanel(playerInventory), 0, 5);
+ root.add(createPlayerInventoryPanel(), 0, 5);
this.getRootPanel().validate(this);
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java
index 272d251..cbefb7a 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonInventoryController.java
@@ -336,6 +336,28 @@ public class CottonInventoryController extends ScreenHandler implements GuiDescr
}
/**
+ * Creates a player inventory widget from this panel's {@linkplain #playerInventory player inventory}.
+ *
+ * @param hasTitle whether the "Inventory" title should be displayed
+ * @return the created inventory widget
+ * @since 2.0.0
+ */
+ public WPlayerInvPanel createPlayerInventoryPanel(boolean hasTitle) {
+ return new WPlayerInvPanel(this.playerInventory, hasTitle);
+ }
+
+ /**
+ * Creates a player inventory widget from this panel's {@linkplain #playerInventory player inventory}.
+ *
+ * @param title the inventory title widget
+ * @return the created inventory widget
+ * @since 2.0.0
+ */
+ public WPlayerInvPanel createPlayerInventoryPanel(WWidget title) {
+ return new WPlayerInvPanel(this.playerInventory, title);
+ }
+
+ /**
* Gets the block inventory at the context.
*
* <p>If no inventory is found, returns {@link EmptyInventory#INSTANCE}.
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
index 47df88f..198c708 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java
@@ -5,6 +5,8 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.player.PlayerInventory;
+import javax.annotation.Nullable;
+
/**
* A player inventory widget that has a visually separate hotbar.
*/
@@ -12,11 +14,45 @@ public class WPlayerInvPanel extends WPlainPanel {
private final WItemSlot inv;
private final WItemSlot hotbar;
+ /**
+ * Constructs a player inventory panel with a title.
+ *
+ * @param playerInventory the player inventory
+ */
public WPlayerInvPanel(PlayerInventory playerInventory) {
+ this(playerInventory, true);
+ }
+
+ /**
+ * Constructs a player inventory panel.
+ *
+ * @param playerInventory the player inventory
+ * @param hasTitle whether there should be an "Inventory" title
+ * @since 2.0.0
+ */
+ public WPlayerInvPanel(PlayerInventory playerInventory, boolean hasTitle) {
+ this(playerInventory, hasTitle ? new WLabel(playerInventory.getDisplayName()) : null);
+ }
+
+ /**
+ * Constructs a player inventory panel.
+ *
+ * @param playerInventory the player inventory
+ * @param title the title widget, can be null
+ * @since 2.0.0
+ */
+ public WPlayerInvPanel(PlayerInventory playerInventory, @Nullable WWidget title) {
+ int y = 0;
+
+ if (title != null) {
+ this.add(title, 0, 0, 9*18, 11);
+ y += title.getHeight();
+ }
+
inv = WItemSlot.ofPlayerStorage(playerInventory);
hotbar = WItemSlot.of(playerInventory, 0, 9, 1);
- this.add(inv, 0, 0);
- this.add(hotbar, 0, 58);
+ this.add(inv, 0, y);
+ this.add(hotbar, 0, y + 58);
}
/**