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

EffSecShoot #7136

Open
wants to merge 21 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 0 additions & 138 deletions src/main/java/ch/njol/skript/effects/EffShoot.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.lang.reflect.Array;

import ch.njol.skript.effects.EffFireworkLaunch;
import ch.njol.skript.sections.EffSecShoot;
import ch.njol.skript.sections.EffSecSpawn;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Item;
Expand All @@ -37,7 +37,6 @@
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;
import ch.njol.skript.lang.ExpressionType;
Expand Down Expand Up @@ -100,7 +99,7 @@ protected Entity[] get(Event event) {
en = EffSecSpawn.lastSpawned;
break;
case 1:
en = EffShoot.lastSpawned;
en = EffSecShoot.lastSpawned;
break;
case 2:
en = EffDrop.lastSpawned;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprShooter.java
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.sections.EffSecShoot;
import ch.njol.util.Kleenean;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
Expand All @@ -46,7 +47,7 @@ public class ExprShooter extends PropertyExpression<Projectile, LivingEntity> {
static {
Skript.registerExpression(ExprShooter.class, LivingEntity.class, ExpressionType.SIMPLE, "[the] shooter [of %projectile%]");
}

@SuppressWarnings({"unchecked", "null"})
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
Expand All @@ -55,7 +56,11 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
}

@Override
protected LivingEntity[] get(final Event e, final Projectile[] source) {
protected LivingEntity @Nullable [] get(Event event, Projectile[] source) {
if (event instanceof EffSecShoot.ShootEvent shootEvent && getExpr().isDefault() && !(shootEvent.getProjectile() instanceof Projectile)) {
return new LivingEntity[]{shootEvent.getShooter()};
}

return get(source, projectile -> {
Object shooter = projectile != null ? projectile.getShooter() : null;
if (shooter instanceof LivingEntity)
Expand Down
201 changes: 201 additions & 0 deletions src/main/java/ch/njol/skript/sections/EffSecShoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package ch.njol.skript.sections;

import ch.njol.skript.Skript;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.entity.EntityData;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.*;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Direction;
import ch.njol.skript.util.Getter;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class EffSecShoot extends EffectSection {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

public static class ShootEvent extends Event {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

private Entity projectile;
private @Nullable LivingEntity shooter;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

public ShootEvent(Entity projectile, @Nullable LivingEntity shooter) {
this.projectile = projectile;
this.shooter = shooter;
}

public Entity getProjectile() {
return projectile;
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved

public @Nullable LivingEntity getShooter() {
return shooter;
}

@Override
public @NotNull HandlerList getHandlers() {
throw new IllegalStateException();
}
}

static {
Skript.registerSection(EffSecShoot.class,
"shoot %entitydatas% [from %livingentities/locations%] [(at|with) (speed|velocity) %-number%] [%-direction%]",
"(make|let) %livingentities/locations% shoot %entitydatas% [(at|with) (speed|velocity) %-number%] [%-direction%]"
);
EventValues.registerEventValue(ShootEvent.class, Entity.class, new Getter<Entity, ShootEvent>() {
@Override
public @Nullable Entity get(ShootEvent shootEvent) {
return shootEvent.getProjectile();
}
}, EventValues.TIME_NOW);
EventValues.registerEventValue(ShootEvent.class, Projectile.class, new Getter<Projectile, ShootEvent>() {
@Override
public @Nullable Projectile get(ShootEvent shootEvent) {
return shootEvent.getProjectile() instanceof Projectile projectile ? projectile : null;
}
}, EventValues.TIME_NOW);
}

private final static Double DEFAULT_SPEED = 5.;
private Expression<EntityData<?>> types;
private Expression<?> shooters;
private @Nullable Expression<Number> velocity;
private @Nullable Expression<Direction> direction;
public static Entity lastSpawned = null;
private @Nullable Trigger trigger;

@Override
@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
types = (Expression<EntityData<?>>) exprs[matchedPattern];
shooters = exprs[1 - matchedPattern];
velocity = (Expression<Number>) exprs[2];
direction = (Expression<Direction>) exprs[3];

if (sectionNode != null) {
AtomicBoolean delayed = new AtomicBoolean(false);
Runnable afterLoading = () -> delayed.set(!getParser().getHasDelayBefore().isFalse());
trigger = loadCode(sectionNode, "shoot", afterLoading, ShootEvent.class);
if (delayed.get()) {
Skript.error("Delays cannot be used within a 'shoot' effect section");
return false;
}
}
return true;
}

@Override
@SuppressWarnings({"unchecked","rawtypes"})
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
protected @Nullable TriggerItem walk(Event event) {
lastSpawned = null;
Number finalVelocity = velocity != null ? velocity.getSingle(event) : DEFAULT_SPEED;
Direction finalDirection = direction != null ? direction.getSingle(event) : Direction.IDENTITY;
if (finalVelocity == null || finalDirection == null)
return null;

for (Object shooter : shooters.getArray(event)) {
for (EntityData<?> entityData : types.getArray(event)) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
Entity finalProjectile = null;
Vector vector;
if (shooter instanceof LivingEntity livingShooter) {
vector = finalDirection.getDirection(livingShooter.getLocation()).multiply(finalVelocity.doubleValue());
Class<? extends Entity> type = entityData.getType();
if (Fireball.class.isAssignableFrom(type)) {
if (trigger != null) {
livingShooter.getWorld().spawn(
livingShooter.getEyeLocation().add(vector.clone().normalize().multiply(0.5)),
type,
(Consumer) getSpawnConsumer(event, vector, entityData, livingShooter)
);
} else {
Fireball fireball = (Fireball) livingShooter.getWorld().spawn(
livingShooter.getEyeLocation().add(vector.clone().normalize().multiply(0.5)),
type
);
fireball.setShooter(livingShooter);
finalProjectile = fireball;
}
} else if (Projectile.class.isAssignableFrom(type)) {
if (trigger != null) {
livingShooter.launchProjectile(
(Class<? extends Projectile>) type,
vector,
(Consumer) getSpawnConsumer(event, vector, entityData, livingShooter)
);
} else {
finalProjectile = livingShooter.launchProjectile((Class<? extends Projectile>) type);
set(finalProjectile, entityData);
}
} else {
Location location = livingShooter.getLocation();
location.setY(location.getY() + livingShooter.getEyeHeight() / 2);
if (trigger != null) {
entityData.spawn(location, (Consumer) getSpawnConsumer(event, vector, entityData, livingShooter));
} else {
finalProjectile = entityData.spawn(location);
}
}
} else {
vector = finalDirection.getDirection((Location) shooter).multiply(finalVelocity.doubleValue());
if (trigger != null) {
entityData.spawn((Location) shooter, (Consumer) getSpawnConsumer(event, vector, entityData, null));
} else {
finalProjectile = entityData.spawn((Location) shooter);
}
}
if (finalProjectile != null) {
finalProjectile.setVelocity(vector);
lastSpawned = finalProjectile;
}
}
}

return super.walk(event, false);
}

@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
private static <E extends Entity> void set(Entity entity, EntityData<E> entityData) {
entityData.set((E) entity);
}

private Consumer<? extends Entity> getSpawnConsumer(Event event, Vector vector, EntityData<?> entityData, @Nullable LivingEntity shooter) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return entity -> {
if (entity instanceof Fireball fireball)
fireball.setShooter(shooter);
else if (entity instanceof Projectile projectile && shooter != null) {
projectile.setShooter(shooter);
set(projectile, entityData);
}
entity.setVelocity(vector);
ShootEvent shootEvent = new ShootEvent(entity, shooter);
lastSpawned = entity;
Variables.setLocalVariables(shootEvent, Variables.copyLocalVariables(event));
TriggerItem.walk(trigger, shootEvent);
Variables.setLocalVariables(event, Variables.copyLocalVariables(shootEvent));
Variables.removeLocals(shootEvent);
};
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "shoot " + types.toString(event, debug) + " from " + shooters.toString(event, debug) +
(velocity != null ? " at speed " + velocity.toString(event, debug) : "") +
(direction != null ? " " + direction.toString(event, debug) : "");
}

}
Loading