Skip to content

Commit 037d08b

Browse files
Use (Mutable)Property for Window state
1 parent 71b4fba commit 037d08b

15 files changed

Lines changed: 354 additions & 293 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import xyz.xenondevs.commons.provider.MutableProvider
4+
import xyz.xenondevs.invui.ExperimentalReactiveApi
5+
6+
@Suppress("UNCHECKED_CAST")
7+
@ExperimentalReactiveApi
8+
fun AnvilWindow.Builder.addRenameHandler(handler: MutableProvider<String>): AnvilWindow.Builder =
9+
addRenameHandler(handler::set)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import xyz.xenondevs.commons.provider.MutableProvider
4+
import xyz.xenondevs.invui.ExperimentalReactiveApi
5+
import xyz.xenondevs.invui.MutablePropertyAdapter
6+
import xyz.xenondevs.invui.gui.Slot
7+
8+
@ExperimentalReactiveApi
9+
fun CrafterWindow.Builder.setSlot(slot: Int, provider: MutableProvider<Boolean>): CrafterWindow.Builder =
10+
setSlot(slot, MutablePropertyAdapter(provider))
11+
12+
@ExperimentalReactiveApi
13+
fun CrafterWindow.Builder.setSlot(x: Int, y: Int, provider: MutableProvider<Boolean>): CrafterWindow.Builder =
14+
setSlot(x, y, MutablePropertyAdapter(provider))
15+
16+
@ExperimentalReactiveApi
17+
fun CrafterWindow.Builder.setSlot(slot: Slot, provider: MutableProvider<Boolean>): CrafterWindow.Builder =
18+
setSlot(slot, MutablePropertyAdapter(provider))
19+
20+
@ExperimentalReactiveApi
21+
fun CrafterWindow.Builder.setSlots(slots: List<MutableProvider<Boolean>>): CrafterWindow.Builder =
22+
setSlots(slots.map { MutablePropertyAdapter(it) })
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import xyz.xenondevs.commons.provider.Provider
4+
import xyz.xenondevs.invui.ExperimentalReactiveApi
5+
import xyz.xenondevs.invui.PropertyAdapter
6+
7+
@ExperimentalReactiveApi
8+
fun MerchantWindow.Builder.setTrades(trades: Provider<List<MerchantWindow.Trade>>): MerchantWindow.Builder =
9+
setTrades(PropertyAdapter(trades))
10+
11+
@ExperimentalReactiveApi
12+
fun MerchantWindow.Builder.setLevel(level: Provider<Int>): MerchantWindow.Builder =
13+
setLevel(PropertyAdapter(level))
14+
15+
@ExperimentalReactiveApi
16+
fun MerchantWindow.Builder.setProgress(progress: Provider<Double>): MerchantWindow.Builder =
17+
setProgress(PropertyAdapter(progress))
18+
19+
@ExperimentalReactiveApi
20+
fun MerchantWindow.Builder.setRestockMessageEnabled(restockMessageEnabled: Provider<Boolean>): MerchantWindow.Builder =
21+
setRestockMessageEnabled(PropertyAdapter(restockMessageEnabled))
22+
23+
@ExperimentalReactiveApi
24+
fun MerchantWindow.Trade.Builder.setDiscount(discount: Provider<Int>): MerchantWindow.Trade.Builder =
25+
setDiscount(PropertyAdapter(discount))
26+
27+
@ExperimentalReactiveApi
28+
fun MerchantWindow.Trade.Builder.setAvailable(available: Provider<Boolean>): MerchantWindow.Trade.Builder =
29+
setAvailable(PropertyAdapter(available))

invui-kotlin/src/main/kotlin/xyz/xenondevs/invui/window/ReactiveWindows.kt

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import xyz.xenondevs.commons.provider.MutableProvider
4+
import xyz.xenondevs.invui.ExperimentalReactiveApi
5+
import xyz.xenondevs.invui.MutablePropertyAdapter
6+
7+
@ExperimentalReactiveApi
8+
fun StonecutterWindow.Builder.setSelectedSlot(provider: MutableProvider<Int>): StonecutterWindow.Builder =
9+
setSelectedSlot(MutablePropertyAdapter(provider))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import net.kyori.adventure.text.Component
4+
import xyz.xenondevs.commons.provider.MutableProvider
5+
import xyz.xenondevs.commons.provider.Provider
6+
import xyz.xenondevs.invui.ExperimentalReactiveApi
7+
8+
@Suppress("UNCHECKED_CAST")
9+
@ExperimentalReactiveApi
10+
fun <S : Window.Builder<*, *>> S.setTitle(provider: Provider<Component>): S {
11+
addModifier { window -> provider.observeWeak(window) { weakWindow -> weakWindow.updateTitle() } }
12+
return setTitleSupplier(provider) as S
13+
}

invui/src/main/java/xyz/xenondevs/invui/internal/menu/CustomCrafterMenu.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void handleSlotStateChange(ServerboundContainerSlotStateChangedPacket pa
8080
dataSlots[slot] = value;
8181

8282
if (slotStateChangeHandler != null)
83-
slotStateChangeHandler.accept(slot, packet.newState());
83+
slotStateChangeHandler.accept(slot, !packet.newState());
8484
}
8585

8686
}

invui/src/main/java/xyz/xenondevs/invui/internal/util/CollectionUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import xyz.xenondevs.invui.InvUI;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
57
import java.util.function.Consumer;
8+
import java.util.function.Function;
69
import java.util.logging.Level;
710

811
public class CollectionUtils {
@@ -28,4 +31,21 @@ public static <T> void forEachCatching(Iterable<? extends T> iterable, Consumer<
2831
}
2932
}
3033

34+
/**
35+
* Creates a new {@link List} of the specified size, filled with the results of the
36+
* specified {@link Function initializer}.
37+
*
38+
* @param size The size of the list to create
39+
* @param initializer The {@link Function} to use to initialize the elements of the list
40+
* @param <T> The type of the elements in the list
41+
* @return A new {@link List} of the specified size, filled with the results of the
42+
*/
43+
public static <T> List<T> create(int size, Function<? super Integer, ? extends T> initializer) {
44+
var list = new ArrayList<T>();
45+
for (int i = 0; i < size; i++) {
46+
list.add(initializer.apply(i));
47+
}
48+
return list;
49+
}
50+
3151
}

invui/src/main/java/xyz/xenondevs/invui/internal/util/ItemUtils2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@SuppressWarnings("UnstableApiUsage")
2121
public class ItemUtils2 {
2222

23-
private static final ItemStack NON_EMPTY_PLACEHOLDER = new ItemBuilder(Material.STONE)
23+
private static final ItemStack NON_EMPTY_PLACEHOLDER = new ItemBuilder(Material.BARRIER)
2424
.hideTooltip(true)
2525
.set(DataComponentTypes.ITEM_MODEL, Key.key("air"))
2626
.build();

invui/src/main/java/xyz/xenondevs/invui/window/CrafterWindow.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.jspecify.annotations.Nullable;
55
import xyz.xenondevs.invui.gui.Gui;
66
import xyz.xenondevs.invui.gui.Slot;
7+
import xyz.xenondevs.invui.state.MutableProperty;
78

89
import java.util.List;
910
import java.util.function.BiConsumer;
@@ -189,6 +190,51 @@ default Builder setResultGui(Gui.Builder<?, ?> builder) {
189190
*/
190191
Builder addSlotToggleHandler(BiConsumer<? super Integer, ? super Boolean> handler);
191192

193+
/**
194+
* Sets the property that contains the disabled state of the slot at the given index.
195+
*
196+
* @param slot The slot index
197+
* @param state The property that contains the disabled state of the slot
198+
* @return This {@link Builder}
199+
* @throws IllegalArgumentException If the slot index is out of bounds (0-8)
200+
*/
201+
Builder setSlot(int slot, MutableProperty<Boolean> state);
202+
203+
/**
204+
* Sets the property that contains the disabled state of the slot at the given coordinates.
205+
*
206+
* @param x The x coordinate of the slot
207+
* @param y The y coordinate of the slot
208+
* @param state The property that contains the disabled state of the slot
209+
* @return This {@link Builder}
210+
* @throws IllegalArgumentException If the slot coordinates are out of bounds (0-2, 0-2)
211+
*/
212+
default Builder setSlot(int x, int y, MutableProperty<Boolean> state) {
213+
return setSlot(x + y * 3, state);
214+
}
215+
216+
/**
217+
* Sets the property that contains the disabled state of the slot at the given coordinates.
218+
*
219+
* @param slot The slot
220+
* @param state The property that contains the disabled state of the slot
221+
* @return This {@link Builder}
222+
* @throws IllegalArgumentException If the slot coordinates are out of bounds (0-2, 0-2)
223+
*/
224+
default Builder setSlot(Slot slot, MutableProperty<Boolean> state) {
225+
return setSlot(slot.x(), slot.y(), state);
226+
}
227+
228+
/**
229+
* Sets the slot properties that contain the disabled state of the slots, ordered by their slot index.
230+
* The given list must contain exactly 9 properties.
231+
*
232+
* @param slots The slot properties that contain the disabled state of the slots
233+
* @return This {@link Builder}
234+
* @throws IllegalArgumentException If the list does not contain exactly 9 properties
235+
*/
236+
Builder setSlots(List<? extends MutableProperty<Boolean>> slots);
237+
192238
}
193239

194240
}

0 commit comments

Comments
 (0)