Skip to content

Commit 343ae02

Browse files
Add AnvilWindow#setResultAlwaysValid
1 parent 94ec94f commit 343ae02

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ fun <T> AnvilWindow.Builder.setTextFieldAlwaysEnabled(provider: Provider<T>, tra
1616

1717
@ExperimentalReactiveApi
1818
fun AnvilWindow.Builder.setTextFieldAlwaysEnabled(enabled: Provider<Boolean>): AnvilWindow.Builder =
19-
setTextFieldAlwaysEnabled(PropertyAdapter(enabled))
19+
setTextFieldAlwaysEnabled(PropertyAdapter(enabled))
20+
21+
@ExperimentalReactiveApi
22+
fun <T> AnvilWindow.Builder.setResultAlwaysValid(provider: Provider<T>, transform: (T) -> Boolean): AnvilWindow.Builder =
23+
setResultAlwaysValid(provider.map(transform))
24+
25+
@ExperimentalReactiveApi
26+
fun AnvilWindow.Builder.setResultAlwaysValid(enabled: Provider<Boolean>): AnvilWindow.Builder =
27+
setResultAlwaysValid(PropertyAdapter(enabled))

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ static Builder builder() {
5757
*/
5858
void setTextFieldAlwaysEnabled(boolean textFieldAlwaysEnabled);
5959

60+
/**
61+
* Gets whether the result of the anvil is always valid, i.e. the arrow is never crossed out.
62+
*
63+
* @return True if the result is always valid, false otherwise.
64+
*/
65+
boolean getResultAlwaysValid();
66+
67+
/**
68+
* Sets whether the result of the anvil is always valid, i.e. the arrow is never crossed out.
69+
*
70+
* @param resultAlwaysValid Whether the result should always be valid.
71+
*/
72+
void setResultAlwaysValid(boolean resultAlwaysValid);
73+
6074
/**
6175
* Registers a rename handler that is called when the input text changes.
6276
* @param handler The rename handler to add.
@@ -152,6 +166,26 @@ default Builder setTextFieldAlwaysEnabled(boolean textFieldAlwaysEnabled) {
152166
*/
153167
Builder setTextFieldAlwaysEnabled(Property<? extends Boolean> textFieldAlwaysEnabled);
154168

169+
/**
170+
* Sets whether the result of the anvil is always valid, i.e. the arrow is never crossed out.
171+
* Defaults to false.
172+
*
173+
* @param resultAlwaysValid Whether the result should always be valid.
174+
* @return This {@link Builder}
175+
*/
176+
default Builder setResultAlwaysValid(boolean resultAlwaysValid) {
177+
return setResultAlwaysValid(Property.of(resultAlwaysValid));
178+
}
179+
180+
/**
181+
* Sets the property containing whether the result of the anvil is always valid, i.e. the arrow is never crossed out.
182+
* Defaults to false.
183+
*
184+
* @param resultAlwaysValid The property containing whether the result should always be valid.
185+
* @return This {@link Builder}
186+
*/
187+
Builder setResultAlwaysValid(Property<? extends Boolean> resultAlwaysValid);
188+
155189
}
156190

157191
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ final class AnvilWindowImpl extends AbstractSplitWindow<CustomAnvilMenu> impleme
2424
private final AbstractGui upperGui;
2525
private final AbstractGui lowerGui;
2626
private Property<? extends Boolean> textFieldAlwaysEnabled;
27+
private Property<? extends Boolean> resultAlwaysValid;
2728

2829
public AnvilWindowImpl(
2930
Player player,
3031
Supplier<? extends Component> title,
3132
AbstractGui upperGui,
3233
AbstractGui lowerGui,
3334
Property<? extends Boolean> textFieldAlwaysEnabled,
35+
Property<? extends Boolean> resultAlwaysValid,
3436
Property<? extends Boolean> closeable
3537
) {
3638
super(player, title, lowerGui, upperGui.getSize() + lowerGui.getSize(), new CustomAnvilMenu(player), closeable);
@@ -40,6 +42,7 @@ public AnvilWindowImpl(
4042
this.upperGui = upperGui;
4143
this.lowerGui = lowerGui;
4244
this.textFieldAlwaysEnabled = textFieldAlwaysEnabled;
45+
this.resultAlwaysValid = resultAlwaysValid;
4346

4447
textFieldAlwaysEnabled.observeWeak(this, thisRef -> thisRef.notifyUpdate(0));
4548
menu.setRenameHandler(this::handleRename);
@@ -53,8 +56,8 @@ private void handleRename(String text) {
5356

5457
@Override
5558
protected void setMenuItem(int slot, @Nullable ItemStack itemStack) {
56-
if (slot == 0 && textFieldAlwaysEnabled.get()) {
57-
menu.setItem(0, ItemUtils.takeOrPlaceholder(itemStack));
59+
if ((slot == 0 && textFieldAlwaysEnabled.get()) || (slot == 2 && resultAlwaysValid.get())) {
60+
menu.setItem(slot, ItemUtils.takeOrPlaceholder(itemStack));
5861
} else {
5962
super.setMenuItem(slot, itemStack);
6063
}
@@ -87,6 +90,18 @@ public void setTextFieldAlwaysEnabled(boolean textFieldAlwaysEnabled) {
8790
notifyUpdate(0);
8891
}
8992

93+
@Override
94+
public boolean getResultAlwaysValid() {
95+
return resultAlwaysValid.get();
96+
}
97+
98+
@Override
99+
public void setResultAlwaysValid(boolean resultAlwaysValid) {
100+
this.resultAlwaysValid.unobserveWeak(this);
101+
this.resultAlwaysValid = Property.of(resultAlwaysValid);
102+
notifyUpdate(2);
103+
}
104+
90105
@Override
91106
public void addRenameHandler(Consumer<? super String> handler) {
92107
renameHandlers.add(handler);
@@ -121,6 +136,7 @@ public static final class BuilderImpl
121136
private final List<Consumer<? super String>> renameHandlers = new ArrayList<>();
122137
private Supplier<? extends Gui> upperGuiSupplier = () -> Gui.empty(3, 1);
123138
private Property<? extends Boolean> textFieldAlwaysEnabled = Property.of(true);
139+
private Property<? extends Boolean> resultAlwaysValid = Property.of(false);
124140

125141
@Override
126142
public BuilderImpl setUpperGui(Supplier<? extends Gui> guiSupplier) {
@@ -147,6 +163,12 @@ public AnvilWindow.Builder setTextFieldAlwaysEnabled(Property<? extends Boolean>
147163
return this;
148164
}
149165

166+
@Override
167+
public AnvilWindow.Builder setResultAlwaysValid(Property<? extends Boolean> resultAlwaysValid) {
168+
this.resultAlwaysValid = resultAlwaysValid;
169+
return this;
170+
}
171+
150172
@SuppressWarnings({"unchecked", "rawtypes"})
151173
@Override
152174
public AnvilWindow build(Player viewer) {
@@ -156,6 +178,7 @@ public AnvilWindow build(Player viewer) {
156178
(AbstractGui) upperGuiSupplier.get(),
157179
supplyLowerGui(viewer),
158180
textFieldAlwaysEnabled,
181+
resultAlwaysValid,
159182
closeable
160183
);
161184

0 commit comments

Comments
 (0)