Skip to content

Commit 77bfc92

Browse files
InventorySlotElementSupplier with offset + non-cycling suppliers
1 parent 9f15929 commit 77bfc92

10 files changed

Lines changed: 235 additions & 203 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/gui/AbstractGui.java

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -755,21 +755,6 @@ public void setItem(char key, Supplier<? extends @Nullable Item> itemSupplier) {
755755
}
756756
}
757757

758-
@Override
759-
public void setInventory(char key, Inventory inventory) {
760-
setSlotElement(key, new InventorySlotElementSupplier(inventory));
761-
}
762-
763-
@Override
764-
public void setInventory(char key, Inventory inventory, ItemProvider background) {
765-
setSlotElement(key, new InventorySlotElementSupplier(inventory, background));
766-
}
767-
768-
@Override
769-
public void setGui(char key, Gui gui) {
770-
setSlotElement(key, new GuiSlotElementSupplier(gui));
771-
}
772-
773758
@Override
774759
public SequencedCollection<? extends Slot> getSlots(char key) {
775760
var matrix = ingredientMatrix;
@@ -1014,70 +999,6 @@ public S applyPreset(IngredientPreset preset) {
1014999
return (S) this;
10151000
}
10161001

1017-
@Override
1018-
public S addIngredient(char key, ItemStack itemStack) {
1019-
if (structure == null)
1020-
throw new IllegalStateException("Structure is not set");
1021-
structure.addIngredient(key, itemStack);
1022-
return (S) this;
1023-
}
1024-
1025-
@Override
1026-
public S addIngredient(char key, ItemProvider itemProvider) {
1027-
if (structure == null)
1028-
throw new IllegalStateException("Structure is not set");
1029-
structure.addIngredient(key, itemProvider);
1030-
return (S) this;
1031-
}
1032-
1033-
@Override
1034-
public S addIngredient(char key, Item item) {
1035-
if (structure == null)
1036-
throw new IllegalStateException("Structure is not set");
1037-
structure.addIngredient(key, item);
1038-
return (S) this;
1039-
}
1040-
1041-
@Override
1042-
public S addIngredient(char key, Item.Builder<?> builder) {
1043-
if (structure == null)
1044-
throw new IllegalStateException("Structure is not set");
1045-
structure.addIngredient(key, builder);
1046-
return (S) this;
1047-
}
1048-
1049-
@Override
1050-
public S addIngredient(char key, Supplier<? extends Item> itemSupplier) {
1051-
if (structure == null)
1052-
throw new IllegalStateException("Structure is not set");
1053-
structure.addIngredient(key, itemSupplier);
1054-
return (S) this;
1055-
}
1056-
1057-
@Override
1058-
public S addIngredient(char key, Inventory inventory) {
1059-
if (structure == null)
1060-
throw new IllegalStateException("Structure is not set");
1061-
structure.addIngredient(key, inventory);
1062-
return (S) this;
1063-
}
1064-
1065-
@Override
1066-
public S addIngredient(char key, Inventory inventory, @Nullable ItemProvider background) {
1067-
if (structure == null)
1068-
throw new IllegalStateException("Structure is not set");
1069-
structure.addIngredient(key, inventory, background);
1070-
return (S) this;
1071-
}
1072-
1073-
@Override
1074-
public S addIngredient(char key, Gui gui) {
1075-
if (structure == null)
1076-
throw new IllegalStateException("Structure is not set");
1077-
structure.addIngredient(key, gui);
1078-
return (S) this;
1079-
}
1080-
10811002
@Override
10821003
public S addIngredient(char key, SlotElement element) {
10831004
if (structure == null)

invui/src/main/java/xyz/xenondevs/invui/gui/AbstractIngredientMapper.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,6 @@ public S applyPreset(IngredientPreset preset) {
2424
return (S) this;
2525
}
2626

27-
@Override
28-
public S addIngredient(char key, ItemStack itemStack) {
29-
return addIngredient(key, new ItemWrapper(itemStack));
30-
}
31-
32-
@Override
33-
public S addIngredient(char key, ItemProvider itemProvider) {
34-
return addIngredient(key, Item.simple(itemProvider));
35-
}
36-
37-
@Override
38-
public S addIngredient(char key, Item item) {
39-
return addIngredient(key, new SlotElement.Item(item));
40-
}
41-
42-
@Override
43-
public S addIngredient(char key, Item.Builder<?> builder) {
44-
return addIngredient(key, builder::build);
45-
}
46-
47-
@Override
48-
public S addIngredient(char key, Supplier<? extends Item> itemSupplier) {
49-
return addIngredientElementSupplier(key, () -> new SlotElement.Item(itemSupplier.get()));
50-
}
51-
52-
@Override
53-
public S addIngredient(char key, Inventory inventory) {
54-
return addIngredientElementSupplier(key, new InventorySlotElementSupplier(inventory));
55-
}
56-
57-
@Override
58-
public S addIngredient(char key, Inventory inventory, @Nullable ItemProvider background) {
59-
return addIngredientElementSupplier(key, new InventorySlotElementSupplier(inventory, background));
60-
}
61-
62-
@Override
63-
public S addIngredient(char key, Gui gui) {
64-
return addIngredientElementSupplier(key, new GuiSlotElementSupplier(gui));
65-
}
66-
6727
@Override
6828
public S addIngredient(char key, SlotElement element) {
6929
handleUpdate();

invui/src/main/java/xyz/xenondevs/invui/gui/Gui.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,21 @@ static Gui single(Inventory inventory, int slot, ItemProvider background) {
341341
* @param inventory The {@link Inventory} that should be placed on these slots
342342
* @see #applyStructure(Structure)
343343
*/
344-
void setInventory(char key, Inventory inventory);
344+
default void setInventory(char key, Inventory inventory) {
345+
setSlotElement(key, new InventorySlotElementSupplier(inventory, null, 0));
346+
}
347+
348+
/**
349+
* Fills the slots associated with the given key through a {@link Structure} with the given {@link Inventory},
350+
* starting at the given offset slot.
351+
*
352+
* @param key The key
353+
* @param inventory The {@link Inventory} that should be placed on these slots
354+
* @param offset The slot offset inside the {@link Inventory} to start from
355+
*/
356+
default void setInventory(char key, Inventory inventory, int offset) {
357+
setSlotElement(key, new InventorySlotElementSupplier(inventory, null, offset));
358+
}
345359

346360
/**
347361
* Fills the slots associated with the given key through a {@link Structure} with the given {@link Inventory},
@@ -352,7 +366,22 @@ static Gui single(Inventory inventory, int slot, ItemProvider background) {
352366
* @param background The {@link ItemProvider} for empty slots of the {@link Inventory}
353367
* @see #applyStructure(Structure)
354368
*/
355-
void setInventory(char key, Inventory inventory, ItemProvider background);
369+
default void setInventory(char key, Inventory inventory, @Nullable ItemProvider background) {
370+
setSlotElement(key, new InventorySlotElementSupplier(inventory, background, 0));
371+
}
372+
373+
/**
374+
* Fills the slots associated with the given key through a {@link Structure} with the given {@link Inventory},
375+
* using the given {@link ItemProvider} as background for empty slots, starting at the given offset slot.
376+
*
377+
* @param key The key
378+
* @param inventory The {@link Inventory} that should be placed on these slots
379+
* @param background The {@link ItemProvider} for empty slots of the {@link Inventory}
380+
* @param offset The slot offset inside the {@link Inventory} to start from
381+
*/
382+
default void setInventory(char key, Inventory inventory, @Nullable ItemProvider background, int offset) {
383+
setSlotElement(key, new InventorySlotElementSupplier(inventory, background, offset));
384+
}
356385

357386
/**
358387
* Fills the slots associated with the given key through a {@link Structure} with the given {@link Gui},
@@ -361,7 +390,9 @@ static Gui single(Inventory inventory, int slot, ItemProvider background) {
361390
* @param key The key
362391
* @param gui The {@link Gui} that should be placed on these slots
363392
*/
364-
void setGui(char key, Gui gui);
393+
default void setGui(char key, Gui gui) {
394+
setSlotElement(key, new GuiSlotElementSupplier(gui));
395+
}
365396

366397
/**
367398
* Gets the {@link ItemProvider} that will be used if nothing else
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package xyz.xenondevs.invui.gui;
22

3-
import java.util.function.Supplier;
4-
5-
class GuiSlotElementSupplier implements Supplier<SlotElement.GuiLink> {
3+
class GuiSlotElementSupplier implements ResettableSlotElementSupplier<SlotElement.GuiLink> {
64

75
private final Gui gui;
86
private int slot;
@@ -16,9 +14,15 @@ public GuiSlotElementSupplier(Gui gui) {
1614

1715
@Override
1816
public SlotElement.GuiLink get() {
19-
var element = new SlotElement.GuiLink(gui, slot);
20-
slot = (slot + 1) % gui.getSize();
21-
return element;
17+
if (slot >= gui.getSize())
18+
throw new IllegalStateException("No more slots available");
19+
20+
return new SlotElement.GuiLink(gui, slot++);
21+
}
22+
23+
@Override
24+
public void reset() {
25+
slot = 0;
2226
}
2327

2428
}

invui/src/main/java/xyz/xenondevs/invui/gui/Ingredient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,14 @@ boolean isMarker() {
8282
return marker != null;
8383
}
8484

85+
/**
86+
* Calls {@link ResettableSlotElementSupplier#reset()} if this {@link #isSlotElement()} and
87+
* a {@link ResettableSlotElementSupplier} is used as the supplier.
88+
*/
89+
void reset() {
90+
if (elementSupplier instanceof ResettableSlotElementSupplier<?> e) {
91+
e.reset();
92+
}
93+
}
94+
8595
}

0 commit comments

Comments
 (0)