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 4 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
13 changes: 11 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,16 +47,24 @@ public class ExprShooter extends PropertyExpression<Projectile, LivingEntity> {
static {
Skript.registerExpression(ExprShooter.class, LivingEntity.class, ExpressionType.SIMPLE, "[the] shooter [of %projectile%]");
}


private boolean withinShootEvent;

@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) {
if (getParser().isCurrentEvent(EffSecShoot.ShootEvent.class) && exprs[0] == null)
withinShootEvent = true;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
setExpr((Expression<? extends Projectile>) exprs[0]);
return true;
}

@Override
protected LivingEntity[] get(final Event e, final Projectile[] source) {
protected LivingEntity @Nullable [] get(final Event event, final Projectile[] source) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
if (withinShootEvent && event instanceof EffSecShoot.ShootEvent shootEvent) {
return new LivingEntity[]{shootEvent.getShooter()};
}

return get(source, projectile -> {
Object shooter = projectile != null ? projectile.getShooter() : null;
if (shooter instanceof LivingEntity)
Expand Down
188 changes: 188 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,188 @@
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.*;
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
@NotNull
public HandlerList getHandlers() {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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);
}

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, SkriptParser.ParseResult parseResult, @Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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 can't be used within a Shoot Effect Section");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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) getConsumer(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) getConsumer(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) getConsumer(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) getConsumer(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> getConsumer(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)
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) : "");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading