1010import xyz .xenondevs .invui .i18n .Languages ;
1111import xyz .xenondevs .invui .inventory .Inventory ;
1212import xyz .xenondevs .invui .item .ItemProvider ;
13+ import xyz .xenondevs .invui .state .Property ;
1314
1415import java .util .ArrayList ;
1516import java .util .Collections ;
@@ -105,9 +106,11 @@ public int getUpdatePeriod() {
105106 *
106107 * @param inventory The {@link Inventory} to link to
107108 * @param slot The slot in the {@link Inventory} to link to
108- * @param background The {@link ItemProvider} to use as background if the slot is empty
109+ * @param backgroundProperty The property containing the {@link ItemProvider} to use as background if the linked slot is empty.
109110 */
110- record InventoryLink (Inventory inventory , int slot , @ Nullable ItemProvider background ) implements SlotElement {
111+ record InventoryLink (Inventory inventory , int slot ,
112+ Property <@ Nullable ItemProvider > backgroundProperty ) implements SlotElement
113+ {
111114
112115 /**
113116 * Creates a new {@link InventoryLink} using the given {@link Inventory} and slot.
@@ -116,12 +119,33 @@ record InventoryLink(Inventory inventory, int slot, @Nullable ItemProvider backg
116119 * @param slot The slot in the {@link Inventory} to link to
117120 */
118121 public InventoryLink (Inventory inventory , int slot ) {
119- this (inventory , slot , null );
122+ this (inventory , slot , (ItemProvider ) null );
123+ }
124+
125+ /**
126+ * Creates a new {@link InventoryLink} using the given {@link Inventory}, slot and background {@link ItemProvider}.
127+ *
128+ * @param inventory The {@link Inventory} to link to
129+ * @param slot The slot in the {@link Inventory} to link to
130+ * @param background The {@link ItemProvider} to use as background if the linked slot is empty.
131+ */
132+ public InventoryLink (Inventory inventory , int slot , @ Nullable ItemProvider background ) {
133+ this (inventory , slot , Property .of (background ));
134+ }
135+
136+ /**
137+ * Gets the background {@link ItemProvider}.
138+ *
139+ * @return The background {@link ItemProvider}
140+ */
141+ public @ Nullable ItemProvider background () {
142+ return backgroundProperty ().get ();
120143 }
121144
122145 @ Override
123146 public @ Nullable ItemStack getItemStack (Player player ) {
124147 ItemStack itemStack = inventory .getUnsafeItem (slot );
148+ ItemProvider background = backgroundProperty ().get ();
125149 if (itemStack == null && background != null )
126150 itemStack = background .get (Languages .getInstance ().getLocale (player ));
127151 return itemStack ;
@@ -135,11 +159,13 @@ public SlotElement getHoldingElement() {
135159 @ Override
136160 public void addObserver (Observer who , int how ) {
137161 inventory .addObserver (who , slot , how );
162+ backgroundProperty .observeWeak (this , thisRef -> thisRef .inventory .notifyWindows (slot ));
138163 }
139164
140165 @ Override
141166 public void removeObserver (Observer who , int how ) {
142167 inventory .removeObserver (who , slot , how );
168+ backgroundProperty .unobserveWeak (this );
143169 }
144170
145171 @ Override
0 commit comments