Skip to content

Commit 52a54a8

Browse files
committed
Predicate on callback-action to avoid NPE issues on some conditions.
1 parent a865a50 commit 52a54a8

3 files changed

Lines changed: 66 additions & 40 deletions

File tree

‎api/src/main/java/fr/jamailun/ultimatespellsystem/api/entities/CallbackAction.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.jetbrains.annotations.NotNull;
99

1010
import java.util.function.Function;
11+
import java.util.function.Predicate;
1112

1213
/**
1314
* An action for a callback.
@@ -20,6 +21,21 @@ public final class CallbackAction<E extends Event, A> {
2021
private final CallbackEvent dslDefinition;
2122
private final Class<E> listenedEvent;
2223
private final Function<E, A> argumentExtractor;
24+
private final Predicate<E> conditionalPredicate;
25+
26+
/**
27+
* Create a new callback definition.
28+
* @param dslDefinition DSL definition to register.
29+
* @param listenedEvent type of event to react to.
30+
* @param argumentExtractor argument extractor. Must extract an argument from the event instance (matching the type).
31+
* @param conditionalPredicate optional predicate to determine if the callback should be executed.
32+
*/
33+
public CallbackAction(@NotNull CallbackEvent dslDefinition, @NotNull Class<E> listenedEvent, @NotNull Function<E, A> argumentExtractor, @NotNull Predicate<E> conditionalPredicate) {
34+
this.dslDefinition = dslDefinition;
35+
this.listenedEvent = listenedEvent;
36+
this.argumentExtractor = argumentExtractor;
37+
this.conditionalPredicate = conditionalPredicate;
38+
}
2339

2440
/**
2541
* Create a new callback definition.
@@ -31,6 +47,7 @@ public CallbackAction(@NotNull CallbackEvent dslDefinition, @NotNull Class<E> li
3147
this.dslDefinition = dslDefinition;
3248
this.listenedEvent = listenedEvent;
3349
this.argumentExtractor = argumentExtractor;
50+
this.conditionalPredicate = x -> true;
3451
}
3552

3653
/**
@@ -42,11 +59,16 @@ public CallbackAction(@NotNull CallbackEvent dslDefinition, @NotNull Class<E> li
4259
*/
4360
public void registerToSummon(@NotNull SummonAttributes summon, String argVarName, @NotNull SpellRuntime runtime, @NotNull RuntimeStatement child) {
4461
summon.registerCallback(listenedEvent, (event) -> {
45-
// 1. Register variable
62+
// 1. Check predicate
63+
if( ! conditionalPredicate.test(event))
64+
return;
65+
66+
// 2. Register variable, if variable exists
4667
if(argVarName != null) {
4768
runtime.variables().set(argVarName, argumentExtractor.apply(event));
4869
}
49-
// 2. Execute child
70+
71+
// 3. Execute child
5072
child.run(runtime);
5173
});
5274
}

‎plugin/src/main/java/fr/jamailun/ultimatespellsystem/extension/callbacks/EntityDeathCallbacks.java‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
import java.util.List;
1313

1414
/**
15-
* Used by {@code LAND} callbacks.
15+
* Used by {@code DIE} callbacks.
1616
*/
1717
public class EntityDeathCallbacks extends CallbackProvider<EntityDeathEvent> {
1818

19-
@EventHandler
20-
void onEvent(@NotNull EntityDeathEvent event) {
21-
SummonAttributes summon = UltimateSpellSystem.getSummonsManager().find(event.getEntity().getUniqueId());
22-
if(summon == null) return;
23-
summon.applyCallback(event);
24-
}
19+
@EventHandler
20+
void onEvent(@NotNull EntityDeathEvent event) {
21+
SummonAttributes summon = UltimateSpellSystem.getSummonsManager().find(event.getEntity().getUniqueId());
22+
if (summon != null)
23+
summon.applyCallback(event);
24+
}
2525

26-
@Override
27-
public @NotNull Collection<CallbackAction<EntityDeathEvent, ?>> getCallbacks() {
28-
return List.of(
29-
new CallbackAction<>(
30-
CallbackEvent.of("die"),
31-
EntityDeathEvent.class,
32-
x -> null
33-
)
34-
);
35-
}
26+
@Override
27+
public @NotNull Collection<CallbackAction<EntityDeathEvent, ?>> getCallbacks() {
28+
return List.of(
29+
new CallbackAction<>(
30+
CallbackEvent.of("die"),
31+
EntityDeathEvent.class,
32+
x -> null
33+
)
34+
);
35+
}
3636

3737
}

‎plugin/src/main/java/fr/jamailun/ultimatespellsystem/extension/callbacks/ProjectileLandCallbacks.java‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,31 @@
1919
*/
2020
public class ProjectileLandCallbacks extends CallbackProvider<ProjectileHitEvent> {
2121

22-
@EventHandler
23-
void onEvent(@NotNull ProjectileHitEvent event) {
24-
SummonAttributes summon = UltimateSpellSystem.getSummonsManager().find(event.getEntity().getUniqueId());
25-
if(summon == null) return;
26-
summon.applyCallback(event);
27-
}
22+
@EventHandler
23+
void onEvent(@NotNull ProjectileHitEvent event) {
24+
SummonAttributes summon = UltimateSpellSystem.getSummonsManager().find(event.getEntity().getUniqueId());
25+
if (summon != null)
26+
summon.applyCallback(event);
27+
}
2828

29-
@Override
30-
public @NotNull Collection<CallbackAction<ProjectileHitEvent, ?>> getCallbacks() {
31-
return List.of(
32-
new CallbackAction<>(
33-
CallbackEvent.of("landed", TokenType.AT, TypePrimitive.LOCATION),
34-
ProjectileHitEvent.class,
35-
e -> e.getEntity().getLocation()
36-
),
37-
new CallbackAction<>(
38-
CallbackEvent.of("hit", TokenType.TO, TypePrimitive.ENTITY),
39-
ProjectileHitEvent.class,
40-
e -> new BukkitSpellEntity(e.getHitEntity())
41-
)
42-
);
43-
}
29+
@Override
30+
public @NotNull Collection<CallbackAction<ProjectileHitEvent, ?>> getCallbacks() {
31+
return List.of(
32+
// Always valid.
33+
new CallbackAction<>(
34+
CallbackEvent.of("landed", TokenType.AT, TypePrimitive.LOCATION),
35+
ProjectileHitEvent.class,
36+
e -> e.getEntity().getLocation().clone()
37+
),
38+
39+
// Hit (requires an entity)
40+
new CallbackAction<>(
41+
CallbackEvent.of("hit", TokenType.TO, TypePrimitive.ENTITY),
42+
ProjectileHitEvent.class,
43+
e -> e.getHitEntity() == null ? null : new BukkitSpellEntity(e.getHitEntity()),
44+
e -> e.getHitEntity() != null // Must have an entity to be triggered
45+
)
46+
);
47+
}
4448

4549
}

0 commit comments

Comments
 (0)