1414import org .jetbrains .annotations .NotNull ;
1515import org .jetbrains .annotations .Nullable ;
1616
17+ import org .mvplugins .multiverse .core .config .node .functions .NodeChangeCallback ;
18+ import org .mvplugins .multiverse .core .config .node .functions .NodeValueCallback ;
19+ import org .mvplugins .multiverse .core .config .node .functions .SenderNodeChangeCallback ;
1720import org .mvplugins .multiverse .core .config .node .functions .SenderNodeStringParser ;
1821import org .mvplugins .multiverse .core .config .node .functions .SenderNodeSuggester ;
1922import org .mvplugins .multiverse .core .config .node .serializer .DefaultSerializerProvider ;
@@ -53,7 +56,9 @@ public class ConfigNode<T> extends ConfigHeaderNode implements ValueNode<T> {
5356 protected @ Nullable NodeStringParser <T > stringParser ;
5457 protected @ Nullable NodeSerializer <T > serializer ;
5558 protected @ Nullable Function <T , Try <Void >> validator ;
56- protected @ Nullable BiConsumer <T , T > onSetValue ;
59+ protected @ Nullable NodeValueCallback <T > onLoad ;
60+ protected @ Nullable NodeChangeCallback <T > onLoadAndChange ;
61+ protected @ Nullable NodeChangeCallback <T > onChange ;
5762
5863 protected ConfigNode (
5964 @ NotNull String path ,
@@ -66,7 +71,9 @@ protected ConfigNode(
6671 @ Nullable NodeStringParser <T > stringParser ,
6772 @ Nullable NodeSerializer <T > serializer ,
6873 @ Nullable Function <T , Try <Void >> validator ,
69- @ Nullable BiConsumer <T , T > onSetValue ) {
74+ @ Nullable NodeValueCallback <T > onLoad ,
75+ @ Nullable NodeChangeCallback <T > onLoadAndChange ,
76+ @ Nullable NodeChangeCallback <T > onChange ) {
7077 super (path , comments );
7178 this .name = name ;
7279 this .type = type ;
@@ -82,7 +89,9 @@ protected ConfigNode(
8289 ? serializer
8390 : DefaultSerializerProvider .getDefaultSerializer (type );
8491 this .validator = validator ;
85- this .onSetValue = onSetValue ;
92+ this .onLoad = onLoad ;
93+ this .onLoadAndChange = onLoadAndChange ;
94+ this .onChange = onChange ;
8695 }
8796
8897 /**
@@ -186,9 +195,29 @@ public Try<Void> validate(@Nullable T value) {
186195 * {@inheritDoc}
187196 */
188197 @ Override
189- public void onSetValue (@ Nullable T oldValue , @ Nullable T newValue ) {
190- if (onSetValue != null ) {
191- onSetValue .accept (oldValue , newValue );
198+ public void onLoad (@ Nullable T value ) {
199+ if (onLoad != null ) {
200+ onLoad .run (value );
201+ }
202+ }
203+
204+ /**
205+ * {@inheritDoc}
206+ */
207+ @ Override
208+ public void onLoadAndChange (@ NotNull CommandSender sender , @ Nullable T oldValue , @ Nullable T newValue ) {
209+ if (onLoadAndChange != null ) {
210+ onLoadAndChange .run (sender , oldValue , newValue );
211+ }
212+ }
213+
214+ /**
215+ * {@inheritDoc}
216+ */
217+ @ Override
218+ public void onChange (@ NotNull CommandSender sender , @ Nullable T oldValue , @ Nullable T newValue ) {
219+ if (onChange != null ) {
220+ onChange .run (sender , oldValue , newValue );
192221 }
193222 }
194223
@@ -208,7 +237,9 @@ public static class Builder<T, B extends ConfigNode.Builder<T, B>> extends Confi
208237 protected @ Nullable NodeStringParser <T > stringParser ;
209238 protected @ Nullable NodeSerializer <T > serializer ;
210239 protected @ Nullable Function <T , Try <Void >> validator ;
211- protected @ Nullable BiConsumer <T , T > onSetValue ;
240+ protected @ Nullable NodeValueCallback <T > onLoad ;
241+ protected @ Nullable NodeChangeCallback <T > onLoadAndChange ;
242+ protected @ Nullable NodeChangeCallback <T > onChange ;
212243
213244 /**
214245 * Creates a new builder.
@@ -368,15 +399,83 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
368399 return self ();
369400 }
370401
402+ /**
403+ * Sets the action to be performed when the value is loaded.
404+ *
405+ * @param onLoad The action to be performed.
406+ * @return This builder.
407+ */
408+ @ ApiStatus .AvailableSince ("5.4" )
409+ public @ NotNull B onLoad (@ NotNull NodeValueCallback <T > onLoad ) {
410+ this .onLoad = this .onLoad == null ? onLoad : this .onLoad .then (onLoad );
411+ return self ();
412+ }
413+
414+ /**
415+ * Sets the action to be performed when the value is loaded and changed.
416+ *
417+ * @param onLoadAndChange The action to be performed.
418+ * @return This builder.
419+ *
420+ * @since 5.4
421+ */
422+ @ ApiStatus .AvailableSince ("5.4" )
423+ public @ NotNull B onLoadAndChange (@ NotNull NodeChangeCallback <T > onLoadAndChange ) {
424+ this .onLoadAndChange = this .onLoadAndChange == null ? onLoadAndChange : this .onLoadAndChange .then (onLoadAndChange );
425+ return self ();
426+ }
427+
428+ /**
429+ * Sets the action to be performed when the value is loaded and changed.
430+ *
431+ * @param onLoadAndChange The action to be performed.
432+ * @return This builder.
433+ *
434+ * @since 5.4
435+ */
436+ @ ApiStatus .AvailableSince ("5.4" )
437+ public @ NotNull B onLoadAndChange (@ NotNull SenderNodeChangeCallback <T > onLoadAndChange ) {
438+ return onLoadAndChange ((NodeChangeCallback <T >) onLoadAndChange );
439+ }
440+
441+ /**
442+ * Sets the action to be performed when the value is changed.
443+ *
444+ * @param onChange The action to be performed.
445+ * @return This builder.
446+ *
447+ * @since 5.4
448+ */
449+ @ ApiStatus .AvailableSince ("5.4" )
450+ public @ NotNull B onChange (@ NotNull NodeChangeCallback <T > onChange ) {
451+ this .onChange = this .onChange == null ? onChange : this .onChange .then (onChange );
452+ return self ();
453+ }
454+
455+ /**
456+ * Sets the action to be performed when the value is changed.
457+ *
458+ * @param onChange The action to be performed.
459+ * @return This builder.
460+ *
461+ * @since 5.4
462+ */
463+ @ ApiStatus .AvailableSince ("5.4" )
464+ public @ NotNull B onChange (@ NotNull SenderNodeChangeCallback <T > onChange ) {
465+ return onChange ((NodeChangeCallback <T >) onChange );
466+ }
467+
371468 /**
372469 * Sets the action to be performed when the value is set.
373470 *
374471 * @param onSetValue The action to be performed.
375472 * @return This builder.
473+ *
474+ * @deprecated Use {@link #onLoadAndChange(NodeChangeCallback)} instead.
376475 */
476+ @ Deprecated (since = "5.4" , forRemoval = true )
377477 public @ NotNull B onSetValue (@ NotNull BiConsumer <T , T > onSetValue ) {
378- this .onSetValue = this .onSetValue == null ? onSetValue : this .onSetValue .andThen (onSetValue );
379- return self ();
478+ return onLoadAndChange (onSetValue ::accept );
380479 }
381480
382481 /**
@@ -385,7 +484,8 @@ protected Builder(@NotNull String path, @NotNull Class<T> type) {
385484 @ Override
386485 public @ NotNull ConfigNode <T > build () {
387486 return new ConfigNode <>(path , comments .toArray (new String [0 ]),
388- name , type , aliases , defaultValue , suggester , stringParser , serializer , validator , onSetValue );
487+ name , type , aliases , defaultValue , suggester , stringParser , serializer , validator ,
488+ onLoad , onLoadAndChange , onChange );
389489 }
390490 }
391491}
0 commit comments