Skip to content

Commit 51f4ae2

Browse files
committed
lots of minor fixes with lists and abstract builders
1 parent 3d1f7eb commit 51f4ae2

20 files changed

Lines changed: 782 additions & 560 deletions

changelogs/2.1.1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- Remove padding between list items
2+
- No longer hide add and reset buttons when lists are collapsed, but automatically expand them when clicked
3+
- Fix removing items from lists didn't update up and down buttons
4+
- Fix lists not updating properly when removing the final item from a list
5+
- Refactor some gui list code to abstract all list functionality from main gui classes
6+
- Fix option entries sometimes overlapping due to the reset button
7+
- Fix string elements cropping 1px off the top of the text
8+
- Abstracted builders to restrict API usage

src/client/java/dev/isxander/yacl/api/ButtonOption.java

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,16 @@ public interface ButtonOption extends Option<BiConsumer<YACLScreen, ButtonOption
2020
BiConsumer<YACLScreen, ButtonOption> action();
2121

2222
static Builder createBuilder() {
23-
return new Builder();
23+
return new ButtonOptionImpl.BuilderImpl();
2424
}
2525

26-
class Builder {
27-
private Text name;
28-
private final List<Text> tooltipLines = new ArrayList<>();
29-
private boolean available = true;
30-
private Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> controlGetter;
31-
private BiConsumer<YACLScreen, ButtonOption> action;
32-
33-
private Builder() {
34-
35-
}
36-
26+
interface Builder {
3727
/**
3828
* Sets the name to be used by the option.
3929
*
4030
* @see Option#name()
4131
*/
42-
public Builder name(@NotNull Text name) {
43-
Validate.notNull(name, "`name` cannot be null");
44-
45-
this.name = name;
46-
return this;
47-
}
32+
Builder name(@NotNull Text name);
4833

4934
/**
5035
* Sets the tooltip to be used by the option.
@@ -53,71 +38,33 @@ public Builder name(@NotNull Text name) {
5338
*
5439
* @param tooltips text lines - merged with a new-line on {@link Option.Builder#build()}.
5540
*/
56-
public Builder tooltip(@NotNull Text... tooltips) {
57-
Validate.notNull(tooltips, "`tooltips` cannot be empty");
58-
59-
tooltipLines.addAll(List.of(tooltips));
60-
return this;
61-
}
62-
63-
public Builder action(@NotNull BiConsumer<YACLScreen, ButtonOption> action) {
64-
Validate.notNull(action, "`action` cannot be null");
41+
Builder tooltip(@NotNull Text... tooltips);
6542

66-
this.action = action;
67-
return this;
68-
}
43+
Builder action(@NotNull BiConsumer<YACLScreen, ButtonOption> action);
6944

7045
/**
7146
* Action to be executed upon button press
7247
*
7348
* @see ButtonOption#action()
7449
*/
7550
@Deprecated
76-
public Builder action(@NotNull Consumer<YACLScreen> action) {
77-
Validate.notNull(action, "`action` cannot be null");
78-
79-
this.action = (screen, button) -> action.accept(screen);
80-
return this;
81-
}
51+
Builder action(@NotNull Consumer<YACLScreen> action);
8252

8353
/**
8454
* Sets if the option can be configured
8555
*
8656
* @see Option#available()
8757
*/
88-
public Builder available(boolean available) {
89-
this.available = available;
90-
return this;
91-
}
58+
Builder available(boolean available);
9259

9360
/**
9461
* Sets the controller for the option.
9562
* This is how you interact and change the options.
9663
*
9764
* @see dev.isxander.yacl.gui.controllers
9865
*/
99-
public Builder controller(@NotNull Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> control) {
100-
Validate.notNull(control, "`control` cannot be null");
101-
102-
this.controlGetter = control;
103-
return this;
104-
}
105-
106-
public ButtonOption build() {
107-
Validate.notNull(name, "`name` must not be null when building `Option`");
108-
Validate.notNull(controlGetter, "`control` must not be null when building `Option`");
109-
Validate.notNull(action, "`action` must not be null when building `Option`");
110-
111-
MutableText concatenatedTooltip = Text.empty();
112-
boolean first = true;
113-
for (Text line : tooltipLines) {
114-
if (!first) concatenatedTooltip.append("\n");
115-
first = false;
116-
117-
concatenatedTooltip.append(line);
118-
}
66+
Builder controller(@NotNull Function<ButtonOption, Controller<BiConsumer<YACLScreen, ButtonOption>>> control);
11967

120-
return new ButtonOptionImpl(name, concatenatedTooltip, action, available, controlGetter);
121-
}
68+
ButtonOption build();
12269
}
12370
}

src/client/java/dev/isxander/yacl/api/ConfigCategory.java

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,16 @@ public interface ConfigCategory {
3939
* Creates a builder to construct a {@link ConfigCategory}
4040
*/
4141
static Builder createBuilder() {
42-
return new Builder();
42+
return new ConfigCategoryImpl.BuilderImpl();
4343
}
4444

45-
class Builder {
46-
private Text name;
47-
48-
private final List<Option<?>> rootOptions = new ArrayList<>();
49-
private final List<OptionGroup> groups = new ArrayList<>();
50-
51-
private final List<Text> tooltipLines = new ArrayList<>();
52-
53-
private Builder() {
54-
55-
}
56-
45+
interface Builder {
5746
/**
5847
* Sets name of the category
5948
*
6049
* @see ConfigCategory#name()
6150
*/
62-
public Builder name(@NotNull Text name) {
63-
Validate.notNull(name, "`name` cannot be null");
64-
65-
this.name = name;
66-
return this;
67-
}
51+
Builder name(@NotNull Text name);
6852

6953
/**
7054
* Adds an option to the root group of the category.
@@ -74,17 +58,7 @@ public Builder name(@NotNull Text name) {
7458
* @see ConfigCategory#groups()
7559
* @see OptionGroup#isRoot()
7660
*/
77-
public Builder option(@NotNull Option<?> option) {
78-
Validate.notNull(option, "`option` must not be null");
79-
80-
if (option instanceof ListOption<?> listOption) {
81-
YACLConstants.LOGGER.warn("Adding list option as an option is not supported! Rerouting to group!");
82-
return group(listOption);
83-
}
84-
85-
this.rootOptions.add(option);
86-
return this;
87-
}
61+
Builder option(@NotNull Option<?> option);
8862

8963
/**
9064
* Adds multiple options to the root group of the category.
@@ -94,39 +68,21 @@ public Builder option(@NotNull Option<?> option) {
9468
* @see ConfigCategory#groups()
9569
* @see OptionGroup#isRoot()
9670
*/
97-
public Builder options(@NotNull Collection<Option<?>> options) {
98-
Validate.notNull(options, "`options` must not be null");
99-
100-
if (options.stream().anyMatch(ListOption.class::isInstance))
101-
throw new UnsupportedOperationException("List options must not be added as an option but a group!");
102-
103-
this.rootOptions.addAll(options);
104-
return this;
105-
}
71+
Builder options(@NotNull Collection<Option<?>> options);
10672

10773
/**
10874
* Adds an option group.
10975
* To add an option to the root group, use {@link Builder#option(Option)}
11076
* To construct a group, use {@link OptionGroup#createBuilder()}
11177
*/
112-
public Builder group(@NotNull OptionGroup group) {
113-
Validate.notNull(group, "`group` must not be null");
114-
115-
this.groups.add(group);
116-
return this;
117-
}
78+
Builder group(@NotNull OptionGroup group);
11879

11980
/**
12081
* Adds multiple option groups.
12182
* To add multiple options to the root group, use {@link Builder#options(Collection)}
12283
* To construct a group, use {@link OptionGroup#createBuilder()}
12384
*/
124-
public Builder groups(@NotNull Collection<OptionGroup> groups) {
125-
Validate.notEmpty(groups, "`groups` must not be empty");
126-
127-
this.groups.addAll(groups);
128-
return this;
129-
}
85+
Builder groups(@NotNull Collection<OptionGroup> groups);
13086

13187
/**
13288
* Sets the tooltip to be used by the category.
@@ -135,32 +91,8 @@ public Builder groups(@NotNull Collection<OptionGroup> groups) {
13591
*
13692
* @param tooltips text lines - merged with a new-line on {@link Builder#build()}.
13793
*/
138-
public Builder tooltip(@NotNull Text... tooltips) {
139-
Validate.notEmpty(tooltips, "`tooltips` cannot be empty");
140-
141-
tooltipLines.addAll(List.of(tooltips));
142-
return this;
143-
}
144-
145-
public ConfigCategory build() {
146-
Validate.notNull(name, "`name` must not be null to build `ConfigCategory`");
147-
148-
List<OptionGroup> combinedGroups = new ArrayList<>();
149-
combinedGroups.add(new OptionGroupImpl(Text.empty(), Text.empty(), ImmutableList.copyOf(rootOptions), false, true));
150-
combinedGroups.addAll(groups);
151-
152-
Validate.notEmpty(combinedGroups, "at least one option must be added to build `ConfigCategory`");
153-
154-
MutableText concatenatedTooltip = Text.empty();
155-
boolean first = true;
156-
for (Text line : tooltipLines) {
157-
if (!first) concatenatedTooltip.append("\n");
158-
first = false;
159-
160-
concatenatedTooltip.append(line);
161-
}
94+
Builder tooltip(@NotNull Text... tooltips);
16295

163-
return new ConfigCategoryImpl(name, ImmutableList.copyOf(combinedGroups), concatenatedTooltip);
164-
}
96+
ConfigCategory build();
16597
}
16698
}

0 commit comments

Comments
 (0)