Skip to content

Commit 82959bd

Browse files
committed
Reworked the API : cast with a SpellEntity instead of a LivingEntity.
1 parent 264b63e commit 82959bd

15 files changed

Lines changed: 92 additions & 48 deletions

File tree

api/src/main/java/fr/jamailun/ultimatespellsystem/api/entities/SpellEntity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ default boolean isBukkit() {
5454
*/
5555
void teleport(@NotNull Location location);
5656

57-
/**
58-
* Send a message to the entity.
59-
* @param component component to send.
60-
*/
61-
void sendMessage(Component component);
62-
6357
/**
6458
* Remove the entity from the game.
6559
* Will not do anything if already removed.
@@ -72,6 +66,12 @@ default boolean isBukkit() {
7266
*/
7367
boolean isValid();
7468

69+
/**
70+
* Send a message to the entity. You probably don't want to implement this fully often.
71+
* @param component component to send.
72+
*/
73+
void sendMessage(Component component);
74+
7575
/**
7676
* Add a potion effect.
7777
* @param effect the potion effect to add. May do nothing.

api/src/main/java/fr/jamailun/ultimatespellsystem/api/entities/SummonAttributes.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import fr.jamailun.ultimatespellsystem.api.utils.AttributesHolder;
55
import fr.jamailun.ultimatespellsystem.dsl.nodes.type.Duration;
66
import org.bukkit.Location;
7-
import org.bukkit.entity.LivingEntity;
87
import org.bukkit.event.Event;
98
import org.bukkit.scheduler.BukkitRunnable;
109
import org.jetbrains.annotations.ApiStatus;
@@ -14,8 +13,6 @@
1413

1514
import java.time.Instant;
1615
import java.util.*;
17-
import java.util.function.BiConsumer;
18-
import java.util.function.BiFunction;
1916
import java.util.function.Consumer;
2017

2118
/**
@@ -38,7 +35,7 @@ public interface SummonAttributes extends AttributesHolder {
3835
* @return a non-null LivingEntity.
3936
*/
4037
@Contract(pure = true)
41-
@NotNull LivingEntity getSummoner();
38+
@NotNull SpellEntity getSummoner();
4239

4340
/**
4441
* Get the summoned entity.

api/src/main/java/fr/jamailun/ultimatespellsystem/api/events/EntityCastSpellEvent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package fr.jamailun.ultimatespellsystem.api.events;
22

3+
import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity;
34
import fr.jamailun.ultimatespellsystem.api.spells.Spell;
4-
import org.bukkit.entity.LivingEntity;
55
import org.bukkit.event.Event;
66
import org.bukkit.event.HandlerList;
77
import org.jetbrains.annotations.NotNull;
@@ -11,7 +11,7 @@
1111
*/
1212
public class EntityCastSpellEvent extends Event implements MaybeCancellable {
1313

14-
private final LivingEntity caster;
14+
private final SpellEntity caster;
1515
private final Spell spell;
1616
private boolean cancelled = false;
1717
private final boolean cancellable;
@@ -22,7 +22,7 @@ public class EntityCastSpellEvent extends Event implements MaybeCancellable {
2222
* @param spell spell to be cast.
2323
* @param cancellable true if the spell can be cancelled.
2424
*/
25-
public EntityCastSpellEvent(@NotNull LivingEntity caster, @NotNull Spell spell, boolean cancellable) {
25+
public EntityCastSpellEvent(@NotNull SpellEntity caster, @NotNull Spell spell, boolean cancellable) {
2626
this.caster = caster;
2727
this.spell = spell;
2828
this.cancellable = cancellable;
@@ -40,7 +40,7 @@ public EntityCastSpellEvent(@NotNull LivingEntity caster, @NotNull Spell spell,
4040
* Get the caster
4141
* @return the reference to the bukkit caster.
4242
*/
43-
public @NotNull LivingEntity getCaster() {
43+
public @NotNull SpellEntity getCaster() {
4444
return caster;
4545
}
4646

api/src/main/java/fr/jamailun/ultimatespellsystem/api/events/EntitySummonedEvent.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity;
44
import fr.jamailun.ultimatespellsystem.api.entities.SummonAttributes;
5-
import org.bukkit.entity.LivingEntity;
65
import org.bukkit.event.Event;
76
import org.bukkit.event.HandlerList;
87
import org.jetbrains.annotations.NotNull;
@@ -34,7 +33,7 @@ public EntitySummonedEvent(@NotNull SummonAttributes attributes) {
3433
* Get the entity that has been summoned.
3534
* @return a non-null bukkit Entity.
3635
*/
37-
public @NotNull LivingEntity getSummoner() {
36+
public @NotNull SpellEntity getSummoner() {
3837
return attributes.getSummoner();
3938
}
4039

api/src/main/java/fr/jamailun/ultimatespellsystem/api/runner/SpellRuntime.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package fr.jamailun.ultimatespellsystem.api.runner;
22

3-
import org.bukkit.entity.LivingEntity;
3+
import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity;
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
@@ -16,7 +16,7 @@ public interface SpellRuntime {
1616
* Get the caster of the current spell.
1717
* @return a non-null reference to the caster.
1818
*/
19-
@NotNull LivingEntity getCaster();
19+
@NotNull SpellEntity getCaster();
2020

2121
/**
2222
* A reference to the variables set.
@@ -70,7 +70,7 @@ public interface SpellRuntime {
7070
* @return a new instance with a copy of other variables.
7171
*/
7272
@Contract("_ -> new")
73-
@NotNull SpellRuntime makeChildNewCaster(@NotNull LivingEntity newCaster);
73+
@NotNull SpellRuntime makeChildNewCaster(@NotNull SpellEntity newCaster);
7474

7575
/**
7676
* Get the final exit code.

api/src/main/java/fr/jamailun/ultimatespellsystem/api/spells/Spell.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.jamailun.ultimatespellsystem.api.spells;
22

3+
import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity;
34
import fr.jamailun.ultimatespellsystem.api.events.EntityCastSpellEvent;
45
import fr.jamailun.ultimatespellsystem.api.runner.SpellRuntime;
56
import fr.jamailun.ultimatespellsystem.api.utils.MultivaluedMap;
@@ -15,22 +16,30 @@ public interface Spell {
1516

1617
/**
1718
* Cast a spell, in a forceful way. An event will be emitted.
18-
* @param caster the living-entity to cast the spell.
19+
* @param caster the bukkit living entity, caster of the spell.
1920
* @return {@code false} if the spell finished with an error.
2021
* @see EntityCastSpellEvent
2122
*/
22-
default boolean castNotCancellable(@NotNull LivingEntity caster) {
23+
boolean castNotCancellable(@NotNull LivingEntity caster);
24+
25+
/**
26+
* Cast a spell, in a forceful way. An event will be emitted.
27+
* @param caster the caster of the spell.
28+
* @return {@code false} if the spell finished with an error.
29+
* @see EntityCastSpellEvent
30+
*/
31+
default boolean castNotCancellable(@NotNull SpellEntity caster) {
2332
return castNotCancellable(caster, null);
2433
}
2534

2635
/**
2736
* Cast a spell, in a forceful way, with a specific runtime. An event will be emitted.
28-
* @param caster the living-entity to cast the spell.
37+
* @param caster the non-null caster for the spell.
2938
* @param runtime the runtime to use.
3039
* @return {@code false} if the spell finished with an error.
3140
* @see EntityCastSpellEvent
3241
*/
33-
boolean castNotCancellable(@NotNull LivingEntity caster, @Nullable SpellRuntime runtime);
42+
boolean castNotCancellable(@NotNull SpellEntity caster, @Nullable SpellRuntime runtime);
3443

3544
/**
3645
* Cast a spell, in a cancellable way. An event will be emitted.

plugin/src/main/java/fr/jamailun/ultimatespellsystem/extension/functions/AbstractFunction.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fr.jamailun.ultimatespellsystem.api.runner.functions.RunnableJavaFunction;
88
import fr.jamailun.ultimatespellsystem.dsl.nodes.expressions.functions.FunctionArgument;
99
import fr.jamailun.ultimatespellsystem.dsl.nodes.type.Type;
10+
import fr.jamailun.ultimatespellsystem.plugin.entities.BukkitSpellEntity;
1011
import org.bukkit.Location;
1112
import org.bukkit.entity.Entity;
1213
import org.bukkit.entity.LivingEntity;
@@ -33,17 +34,27 @@ protected AbstractFunction(@NotNull String id, @NotNull Type type, @NotNull List
3334
}
3435

3536
protected @Nullable LivingEntity toLivingEntity(@NotNull String context, @NotNull RuntimeExpression expression, @NotNull SpellRuntime runtime) {
36-
Object locationRaw = expression.evaluate(runtime);
37-
if(locationRaw instanceof LivingEntity livingEntity) {
37+
Object entityRaw = expression.evaluate(runtime);
38+
if(entityRaw instanceof LivingEntity livingEntity) {
3839
return livingEntity;
39-
} else if(locationRaw instanceof SpellEntity entity) {
40+
} else if(entityRaw instanceof SpellEntity entity) {
4041
Entity be = entity.getBukkitEntity().orElse(null);
4142
if(be instanceof LivingEntity le)
4243
return le;
4344
return null;
4445
} else {
45-
throw new InvalidTypeException(context, "location or entity", locationRaw);
46+
throw new InvalidTypeException(context, "location or entity", entityRaw);
4647
}
4748
}
4849

50+
protected @Nullable SpellEntity toSpellEntity(@NotNull String context, @NotNull RuntimeExpression expression, @NotNull SpellRuntime runtime) {
51+
Object entityRaw = expression.evaluate(runtime);
52+
return switch (entityRaw) {
53+
case null -> null;
54+
case LivingEntity le -> new BukkitSpellEntity(le);
55+
case SpellEntity se -> se;
56+
default -> throw new InvalidTypeException(context, "location or entity", entityRaw);
57+
};
58+
}
59+
4960
}

plugin/src/main/java/fr/jamailun/ultimatespellsystem/extension/functions/CastSpellFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.jamailun.ultimatespellsystem.extension.functions;
22

33
import fr.jamailun.ultimatespellsystem.api.UltimateSpellSystem;
4+
import fr.jamailun.ultimatespellsystem.api.entities.SpellEntity;
45
import fr.jamailun.ultimatespellsystem.api.spells.Spell;
56
import fr.jamailun.ultimatespellsystem.api.runner.RuntimeExpression;
67
import fr.jamailun.ultimatespellsystem.api.runner.SpellRuntime;
@@ -41,7 +42,7 @@ public CastSpellFunction() {
4142

4243
@Override
4344
public Object compute(@NotNull List<RuntimeExpression> arguments, @NotNull SpellRuntime runtime) {
44-
LivingEntity entity = toLivingEntity("cast(entity)", arguments.getFirst(), runtime);
45+
SpellEntity entity = toSpellEntity("cast(entity)", arguments.getFirst(), runtime);
4546
if(entity == null) return false;
4647
String spellId = runtime.safeEvaluate(arguments.get(1), String.class);
4748

plugin/src/main/java/fr/jamailun/ultimatespellsystem/plugin/entities/SummonAttributesImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.bukkit.Location;
1313
import org.bukkit.entity.*;
1414
import org.bukkit.event.Event;
15+
import org.bukkit.projectiles.ProjectileSource;
1516
import org.bukkit.scheduler.BukkitRunnable;
1617
import org.jetbrains.annotations.NotNull;
1718
import org.jetbrains.annotations.Nullable;
@@ -26,7 +27,7 @@
2627
*/
2728
public class SummonAttributesImpl implements SummonAttributes {
2829

29-
protected final LivingEntity summoner;
30+
protected final SpellEntity summoner;
3031
protected final Map<String, Object> attributes;
3132
protected final Location summonLocation;
3233
protected final UssEntityType summonEntityType;
@@ -44,7 +45,7 @@ public class SummonAttributesImpl implements SummonAttributes {
4445
* @param attributes properties to use. Implementation-dependent.
4546
* @param duration the duration of the summoning, after which the creature will be removed.
4647
*/
47-
public SummonAttributesImpl(LivingEntity summoner, Location location, UssEntityType type, Map<String, Object> attributes, Duration duration) {
48+
public SummonAttributesImpl(SpellEntity summoner, Location location, UssEntityType type, Map<String, Object> attributes, Duration duration) {
4849
this.summoner = summoner;
4950
this.summonLocation = location;
5051
this.summonEntityType = type;
@@ -71,11 +72,16 @@ public final void summon(Consumer<UUID> callback, SpellRuntime runtime) {
7172
if(summonEntityType.isBukkit()) {
7273
Entity raw = summonLocation.getWorld().spawnEntity(summonLocation, summonEntityType.getBukkit(), false);
7374
if(raw instanceof Projectile projectile) {
74-
projectile.setShooter(runtime.getCaster());
75+
// Set the projectile source... if possible
76+
if(runtime.getCaster().getBukkitEntity().orElse(null) instanceof ProjectileSource ps) {
77+
projectile.setShooter(ps);
78+
}
79+
// Set the pickup status when required.
7580
if(projectile instanceof Arrow arrow) {
7681
arrow.setPickupStatus(AbstractArrow.PickupStatus.DISALLOWED);
7782
}
7883
}
84+
// Always wrap the entity.
7985
entity = new BukkitSpellEntity(raw);
8086
} else {
8187
entity = summonEntityType.generateCustom(this);
@@ -101,7 +107,7 @@ public final void summon(Consumer<UUID> callback, SpellRuntime runtime) {
101107
}
102108

103109
@Override
104-
public @NotNull LivingEntity getSummoner() {
110+
public @NotNull SpellEntity getSummoner() {
105111
return summoner;
106112
}
107113

plugin/src/main/java/fr/jamailun/ultimatespellsystem/plugin/entities/implem/Orb.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ private void applyEffects(@NotNull Collection<LivingEntity> nearby) {
123123
// Fire + Damages
124124
if(fireTicks > 0)
125125
nearby.forEach(e -> e.setFireTicks(e.getFireTicks() + fireTicks));
126-
if(damages > 0)
127-
nearby.forEach(e -> e.damage(damages, getAttributes().getSummoner()));
126+
if(damages > 0) {
127+
getAttributes().getSummoner().getBukkitEntity().ifPresentOrElse(
128+
summoner -> nearby.forEach(e -> e.damage(damages, summoner)),
129+
() -> nearby.forEach(e -> e.damage(damages))
130+
);
131+
}
128132

129133
// Hit effect
130134
hitSound.apply(other.getLocation());

0 commit comments

Comments
 (0)