Skip to content

Commit 0350b07

Browse files
Allow placing BoundItem in any gui after binding
1 parent 673afe3 commit 0350b07

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public void setSlotElement(int index, @Nullable SlotElement slotElement) {
550550

551551
// set the gui if it is a bound item
552552
if (slotElement instanceof SlotElement.Item(Item item)) {
553-
if (item instanceof BoundItem boundItem) {
553+
if (item instanceof BoundItem boundItem && !boundItem.isBound()) {
554554
boundItem.bind(this);
555555
}
556556
}

invui/src/main/java/xyz/xenondevs/invui/item/AbstractBoundItem.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ public Gui getGui() {
2121
@Override
2222
public void bind(Gui gui) {
2323
if (this.gui != null)
24-
return;
24+
throw new IllegalStateException("Item is already bound to a gui");
2525

2626
this.gui = gui;
2727
}
2828

29+
@Override
30+
public boolean isBound() {
31+
return gui != null;
32+
}
33+
2934
}

invui/src/main/java/xyz/xenondevs/invui/item/BoundItem.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ public sealed interface BoundItem extends Item permits AbstractBoundItem {
2828
* Called when the item is added to a {@link Gui}.
2929
*
3030
* @param gui The {@link Gui} to bind this item to.
31+
* @throws IllegalStateException If this item is already bound to a {@link Gui}.
3132
*/
3233
void bind(Gui gui);
3334

35+
/**
36+
* Checks if this item is already bound to a {@link Gui}.
37+
*
38+
* @return Whether this item is already bound to a {@link Gui}.
39+
*/
40+
boolean isBound();
41+
3442
static Builder<Gui> gui() {
3543
return new CustomBoundItem.Builder<>();
3644
}
@@ -48,7 +56,7 @@ static Builder<TabGui> tabGui() {
4856
}
4957

5058
sealed interface Builder<G extends Gui> extends Item.Builder<Builder<G>> permits CustomBoundItem.Builder {
51-
59+
5260
Builder<G> addBindHandler(BiConsumer<Item, G> handler);
5361

5462
Builder<G> addClickHandler(TriConsumer<Item, G, Click> handler);

0 commit comments

Comments
 (0)