Skip to content

Commit 798aa1d

Browse files
committed
added callback on value changed
1 parent 55fa281 commit 798aa1d

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<groupId>net.azalealibrary</groupId>
1515
<artifactId>configuration</artifactId>
16-
<version>1.0.5</version>
16+
<version>1.0.6</version>
1717

1818
<repositories>
1919
<repository>

src/main/java/net/azalealibrary/configuration/AzaleaConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ public void onLoad() {
2828
}
2929
}
3030

31-
// TODO - callback when property is updated?
3231
// TODO - regex to set/update many props with shared name shop.item.*

src/main/java/net/azalealibrary/configuration/property/ConfigurableProperty.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99
import javax.annotation.Nullable;
1010
import java.util.List;
1111
import java.util.Optional;
12+
import java.util.function.Consumer;
1213
import java.util.function.Supplier;
1314

1415
public abstract class ConfigurableProperty<T, P> {
1516

1617
protected final PropertyType<T> type;
1718
protected final List<AssignmentPolicy<T>> policies;
1819
protected final String name;
20+
protected final Consumer<P> callback;
1921
private final String description;
2022
private final Supplier<P> defaultValue;
2123
private @Nullable P value;
2224

2325
@SafeVarargs
24-
protected ConfigurableProperty(PropertyType<T> type, Supplier<P> defaultValue, String name, String description, AssignmentPolicy<T>... policies) {
26+
protected ConfigurableProperty(PropertyType<T> type, Supplier<P> defaultValue, String name, String description, Consumer<P> callback, AssignmentPolicy<T>... policies) {
2527
this.type = type;
2628
this.name = name;
2729
this.description = description;
2830
this.defaultValue = defaultValue;
31+
this.callback = callback;
2932
this.policies = List.of(policies);
3033
}
3134

src/main/java/net/azalealibrary/configuration/property/ListProperty.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111
import java.util.Optional;
12+
import java.util.function.Consumer;
1213
import java.util.function.Supplier;
1314
import java.util.stream.Collectors;
1415

@@ -19,13 +20,13 @@ public final class ListProperty<T> extends ConfigurableProperty<T, List<T>> {
1920
private static final String REPLACE = "replace";
2021

2122
@SafeVarargs
22-
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, AssignmentPolicy<T>... policies) {
23-
this(type, defaultValue, name, name, policies);
23+
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, Consumer<List<T>> callback, AssignmentPolicy<T>... policies) {
24+
this(type, defaultValue, name, name, callback, policies);
2425
}
2526

2627
@SafeVarargs
27-
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, String description, AssignmentPolicy<T>... policies) {
28-
super(type, defaultValue, name, description, policies);
28+
public ListProperty(PropertyType<T> type, Supplier<List<T>> defaultValue, String name, String description, Consumer<List<T>> callback, AssignmentPolicy<T>... policies) {
29+
super(type, defaultValue, name, description, callback, policies);
2930
set(defaultValue.get());
3031
}
3132

@@ -35,6 +36,7 @@ protected void set(CommandSender sender, Arguments arguments) {
3536

3637
if (action.equals(ADD)) {
3738
get().add(verify(getType().parse(sender, arguments.subArguments(1), null)));
39+
callback.accept(get());
3840
} else {
3941
int index = arguments.find(1, "position", input -> Integer.parseInt(input.replace("@", "")));
4042

@@ -44,8 +46,10 @@ protected void set(CommandSender sender, Arguments arguments) {
4446

4547
if (action.equals(REPLACE)) {
4648
get().set(index, verify(getType().parse(sender, arguments.subArguments(2), null)));
49+
callback.accept(get());
4750
} else {
4851
get().remove(index);
52+
callback.accept(get());
4953
}
5054
}
5155
}

src/main/java/net/azalealibrary/configuration/property/Property.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66

77
import javax.annotation.Nonnull;
88
import java.util.Optional;
9+
import java.util.function.Consumer;
910
import java.util.function.Supplier;
1011

1112
public final class Property<T> extends ConfigurableProperty<T, T> {
1213

1314
@SafeVarargs
14-
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, AssignmentPolicy<T>... policies) {
15-
this(type, defaultValue, name, name, policies);
15+
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, Consumer<T> callback, AssignmentPolicy<T>... policies) {
16+
this(type, defaultValue, name, name, callback, policies);
1617
}
1718

1819
@SafeVarargs
19-
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, String description, AssignmentPolicy<T>... policies) {
20-
super(type, defaultValue, name, description, policies);
20+
public Property(PropertyType<T> type, Supplier<T> defaultValue, String name, String description, Consumer<T> callback, AssignmentPolicy<T>... policies) {
21+
super(type, defaultValue, name, description, callback, policies);
2122
}
2223

2324
@Override
2425
protected void set(CommandSender sender, Arguments arguments) {
2526
set(verify(getType().parse(sender, arguments, get())));
27+
callback.accept(get());
2628
}
2729

2830
@Override

0 commit comments

Comments
 (0)