Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last struck lightning #3873

Merged
merged 11 commits into from
Jul 16, 2022
8 changes: 6 additions & 2 deletions src/main/java/ch/njol/skript/effects/EffLightning.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package ch.njol.skript.effects;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -52,6 +53,9 @@ public class EffLightning extends Effect {

private boolean effectOnly;

@Nullable
public static Entity lastSpawned = null;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
Expand All @@ -64,9 +68,9 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
protected void execute(final Event e) {
for (final Location l : locations.getArray(e)) {
if (effectOnly)
l.getWorld().strikeLightningEffect(l);
lastSpawned = l.getWorld().strikeLightningEffect(l);
else
l.getWorld().strikeLightning(l);
lastSpawned = l.getWorld().strikeLightning(l);
}
}

Expand Down
67 changes: 55 additions & 12 deletions src/main/java/ch/njol/skript/expressions/ExprLastSpawnedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ch.njol.skript.sections.EffSecSpawn;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LightningStrike;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -32,6 +33,7 @@
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.effects.EffDrop;
import ch.njol.skript.effects.EffLightning;
import ch.njol.skript.effects.EffShoot;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.Expression;
Expand All @@ -45,19 +47,24 @@
* @author Peter Güttinger
*/
@Name("Last Spawned Entity")
@Description("Holds the entity that was spawned most recently with the spawn effect, drop with the <a href='../effects/#EffDrop'>drop effect</a> or shot with the <a href='../effects/#EffShoot'>shoot effect</a>. " +
@Description("Holds the entity that was spawned most recently with the spawn effect (section), dropped with the <a href='../effects/#EffDrop'>drop effect</a>, shot with the <a href='../effects/#EffShoot'>shoot effect</a> or created with the <a href='../effects/#EffLightning'>lightning effect</a>. " +
"Please note that even though you can spawn multiple mobs simultaneously (e.g. with 'spawn 5 creepers'), only the last spawned mob is saved and can be used. " +
"If you spawn an entity, shoot a projectile and drop an item you can however access all them together.")
@Examples({"spawn a priest",
"set {healer::%spawned priest%} to true",
"shoot an arrow from the last spawned entity",
"ignite the shot projectile",
"drop a diamond sword",
"push last dropped item upwards"})
@Since("1.3 (spawned entity), 2.0 (shot entity), 2.2-dev26 (dropped item)")
"push last dropped item upwards",
"teleport player to last struck lightning"})
@Since("1.3 (spawned entity), 2.0 (shot entity), 2.2-dev26 (dropped item), SINCE VERSION (struck lightning)")
public class ExprLastSpawnedEntity extends SimpleExpression<Entity> {

static {
Skript.registerExpression(ExprLastSpawnedEntity.class, Entity.class, ExpressionType.SIMPLE, "[the] [last[ly]] (0¦spawned|1¦shot) %*entitydata%", "[the] [last[ly]] dropped (2¦item)");
Skript.registerExpression(ExprLastSpawnedEntity.class, Entity.class, ExpressionType.SIMPLE,
"[the] [last[ly]] (0:spawned|1:shot) %*entitydata%",
"[the] [last[ly]] dropped (2:item)",
"[the] [last[ly]] (created|struck) (3:lightning)");
}

int from;
Expand All @@ -66,24 +73,43 @@ public class ExprLastSpawnedEntity extends SimpleExpression<Entity> {

@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
if (parseResult.mark == 2) // It's just to make an extra expression for item only
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (parseResult.mark == 2) {// It's just to make an extra expression for item only
type = EntityData.fromClass(Item.class);
else
} else if (parseResult.mark == 3) {
type = EntityData.fromClass(LightningStrike.class);
} else {
type = ((Literal<EntityData<?>>) exprs[0]).getSingle();
}
from = parseResult.mark;
return true;
}

@Override
@Nullable
protected Entity[] get(final Event e) {
final Entity en = from == 0 ? EffSecSpawn.lastSpawned : from == 1 ? EffShoot.lastSpawned : EffDrop.lastSpawned;
protected Entity[] get(Event e) {
Entity en;
switch (from) {
case 0:
en = EffSecSpawn.lastSpawned;
break;
case 1:
en = EffShoot.lastSpawned;
break;
case 2:
en = EffDrop.lastSpawned;
break;
case 3:
en = EffLightning.lastSpawned;
break;
default:
en = null;
}
if (en == null)
return null;
if (!type.isInstance(en))
return null;
final Entity[] one = (Entity[]) Array.newInstance(type.getType(), 1);
Entity[] one = (Entity[]) Array.newInstance(type.getType(), 1);
one[0] = en;
return one;
}
Expand All @@ -99,8 +125,25 @@ public Class<? extends Entity> getReturnType() {
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "the last " + (from == 1 ? "spawned" : from == 1 ? "shot" : "dropped") + " " + type;
public String toString(@Nullable Event e, boolean debug) {
String word;
switch (from) {
case 0:
word = "spawned";
break;
case 1:
word = "shot";
break;
case 2:
word = "dropped";
break;
case 3:
word = "struck";
break;
default:
throw new IllegalStateException();
}
return "the last " + word + " " + type;
}

}