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

Firework effect section #5542

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
be31180
Added Firework Launch Effect Section and removed Firework Launch effect.
TFSMads Jun 9, 2022
e90f26e
Merge branch 'master' into feature/laucnhEffSec
TFSMads Jun 9, 2022
08c9164
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jun 9, 2022
2c89cba
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jun 9, 2022
ef7885b
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jun 10, 2022
50e0a26
Update src/test/skript/tests/syntaxes/sections/EffSecFireworkLaunch.sk
TFSMads Jun 10, 2022
d3bf799
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jun 10, 2022
33d8f6a
Update EffSecFireworkLaunch.java
TFSMads Jun 10, 2022
99be168
Update EffSecFireworkLaunch.java
TFSMads Jun 10, 2022
ae10ac5
Merge branch 'master' into feature/laucnhEffSec
TFSMads Jun 10, 2022
b13ad8c
Merge branch 'master' into feature/laucnhEffSec
TheLimeGlass Jun 30, 2022
9b189d6
Changed from %number% to %-number%
TFSMads Jul 14, 2022
6dc151c
Update EffSecFireworkLaunch.java
TFSMads Jul 14, 2022
6235d08
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jul 14, 2022
6f0157d
Update src/test/skript/tests/syntaxes/sections/EffSecFireworkLaunch.sk
TFSMads Jul 14, 2022
c46239a
Merge branch 'master' into feature/laucnhEffSec
TFSMads Jul 14, 2022
6157d51
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jul 15, 2022
456282a
Update src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
TFSMads Jul 15, 2022
8fc13c0
Update EffSecFireworkLaunch.java
TFSMads Jul 15, 2022
242717b
Check if varibles from section is carried over to the initial section
TFSMads Jul 15, 2022
ff2a955
Added requested changes:
TFSMads Jul 18, 2022
a4d5d81
Merge branch 'master' into feature/laucnhEffSec
TFSMads Jul 18, 2022
251c06e
Apply suggestions from code review
TFSMads Jul 28, 2022
31b8dc6
Take over stale pull request
TheLimeGlass Mar 21, 2023
fc358a2
Apply changes to the new class
TheLimeGlass Mar 21, 2023
28222cc
Final touches
TheLimeGlass Mar 21, 2023
28eef10
Fix incorrect examples
TheLimeGlass Mar 21, 2023
384be9f
Merge branch 'dev/feature' into feature/firework-effect-section
TheLimeGlass Apr 29, 2024
9559c50
Change collect order of local variables
TheLimeGlass Apr 29, 2024
2fa34d4
Fix old bukkit consumer issues with spawning firework
TheLimeGlass Apr 29, 2024
3ff79b4
Change collect order of local variables
TheLimeGlass Apr 29, 2024
3277e4b
Merge branch 'dev/feature' into feature/firework-effect-section
sovdeeth Jun 30, 2024
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
94 changes: 0 additions & 94 deletions src/main/java/ch/njol/skript/effects/EffFireworkLaunch.java

This file was deleted.

60 changes: 31 additions & 29 deletions src/main/java/ch/njol/skript/expressions/ExprLastSpawnedEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import java.lang.reflect.Array;

import ch.njol.skript.effects.EffFireworkLaunch;
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 @@ -44,12 +41,18 @@
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.sections.EffSecFireworkLaunch;
import ch.njol.skript.sections.EffSecSpawn;
import ch.njol.util.Kleenean;

@Name("Last Spawned Entity")
@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.")
@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>, launched firework <a href='../sections/#EffSecFireworkLaunch'>firework launch effect section</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",
Expand All @@ -62,16 +65,15 @@
})
@Since("1.3 (spawned entity), 2.0 (shot entity), 2.2-dev26 (dropped item), 2.7 (struck lightning, firework)")
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)",
"[the] [last[ly]] (created|struck) (3:lightning)",
"[the] [last[ly]] (launched|deployed) (4:firework)");
"[the] [last[ly]] (0:spawned|1:shot) %*entitydata%",
"[the] [last[ly]] dropped (2:item)",
"[the] [last[ly]] (created|struck) (3:lightning)",
"[the] [last[ly]] (launched|deployed) (4:firework)");
}

@SuppressWarnings("NotNullFieldNotInitialized")

private EntityData<?> type;
private int from;

Expand All @@ -90,51 +92,51 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
}
return true;
}

@Override
@Nullable
protected Entity[] get(Event event) {
Entity en;
Entity entity;
switch (from) {
case 0:
en = EffSecSpawn.lastSpawned;
entity = EffSecSpawn.lastSpawned;
break;
case 1:
en = EffShoot.lastSpawned;
entity = EffShoot.lastSpawned;
break;
case 2:
en = EffDrop.lastSpawned;
entity = EffDrop.lastSpawned;
break;
case 3:
en = EffLightning.lastSpawned;
entity = EffLightning.lastSpawned;
break;
case 4:
en = EffFireworkLaunch.lastSpawned;
entity = EffSecFireworkLaunch.lastSpawned;
break;
default:
en = null;
entity = null;
}

if (en == null)
if (entity == null)
return null;
if (!type.isInstance(en))
if (!type.isInstance(entity))
return null;

Entity[] one = (Entity[]) Array.newInstance(type.getType(), 1);
one[0] = en;
one[0] = entity;
return one;
}

@Override
public boolean isSingle() {
return true;
}

@Override
public Class<? extends Entity> getReturnType() {
return type.getType();
}

@Override
public String toString(@Nullable Event event, boolean debug) {
String word = "";
Expand All @@ -157,7 +159,7 @@ public String toString(@Nullable Event event, boolean debug) {
default:
assert false;
}
return "the last " + word + " " + type;
return "last " + word + " " + type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return "last " + word + " " + type;
return "the last " + word + " " + type;

it's better to be verbose for toString IMO

}

}
173 changes: 173 additions & 0 deletions src/main/java/ch/njol/skript/sections/EffSecFireworkLaunch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/

package ch.njol.skript.sections;

import java.util.List;

import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Firework;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.util.Consumer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.EffectSection;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;
import ch.njol.skript.variables.Variables;
import ch.njol.util.Kleenean;

@Name("Launch Firework")
@Description({
"Launch firework effects at the given location(s).",
"This can be used as an effect and as a section.",
"If it is used as a section, the section is run before the entity is added to the world.",
"You can modify the firework in this section, using for example <code>event-entity</code>.",
"Do note that other event values, such as <code>player</code>, won't work in this section."
})
@Examples({
"launch ball large coloured red, purple and white fading to light green and black at player's location with duration 1",
"",
"command /firework:",
"\ttrigger:",
"\t\tlaunch a firework with effects ball large coloured red at player:",
"\t\t\tset metadata value \"cancel damage\" of event-entity to true",
Comment on lines +65 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"\ttrigger:",
"\t\tlaunch a firework with effects ball large coloured red at player:",
"\t\t\tset metadata value \"cancel damage\" of event-entity to true",
"\ttrigger:",
"\t\tlaunch a firework with effects ball large coloured red at player:",
"\t\t\tset metadata value \"cancel damage\" of event-entity to true",

"",
"# Firework launch section example",
"on damage:",
"\tdamage cause is entity explosion",
"\tmetadata value \"cancel damage\" of event-projectile is true",
"\tcancel event",
})
@Since("2.4, INSERT VERSION (section)")
public class EffSecFireworkLaunch extends EffectSection {

public static class FireworkSectionLaunchEvent extends Event {

private final Firework firework;

public FireworkSectionLaunchEvent(Firework firework) {
this.firework = firework;
}

public Firework getFirework() {
return firework;
}

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

}

static {
Skript.registerSection(EffSecFireworkLaunch.class, "(launch|deploy) [[a] firework [with effect[s]]] %fireworkeffects% at %locations% [([with] (duration|power)|timed) %-number%]");
EventValues.registerEventValue(FireworkSectionLaunchEvent.class, Firework.class, new Getter<Firework, FireworkSectionLaunchEvent>() {
@Override
public Firework get(FireworkSectionLaunchEvent fireworkLaunchEvent) {
return fireworkLaunchEvent.getFirework();
}
}, EventValues.TIME_NOW);
}

private Expression<FireworkEffect> effects;
private Expression<Location> locations;

@Nullable
public static Entity lastSpawned;

@Nullable
private Expression<Number> power;

@Nullable
private Trigger trigger;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult,
@Nullable SectionNode sectionNode, @Nullable List<TriggerItem> triggerItems) {

effects = (Expression<FireworkEffect>) exprs[0];
locations = (Expression<Location>) exprs[1];
power = (Expression<Number>) exprs[2];

if (sectionNode != null)
trigger = loadCode(sectionNode, "firework launch", FireworkSectionLaunchEvent.class);
Comment on lines +129 to +130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to include the logic from EffSecSpawn that forbids delays.

if (sectionNode != null) {
AtomicBoolean delayed = new AtomicBoolean(false);
Runnable afterLoading = () -> delayed.set(!getParser().getHasDelayBefore().isFalse());
trigger = loadCode(sectionNode, "spawn", afterLoading, SpawnEvent.class);
if (delayed.get()) {
Skript.error("Delays can't be used within a Spawn Effect Section");
return false;
}
}


return true;
}

@Override
@Nullable
protected TriggerItem walk(Event event) {
FireworkEffect[] effects = this.effects.getArray(event);
Object localVars = Variables.copyLocalVariables(event);
Consumer<Firework> consumer = null;
if (trigger != null) {
consumer = firework -> {
FireworkSectionLaunchEvent fireworkLaunchEvent = new FireworkSectionLaunchEvent(firework);
Variables.setLocalVariables(fireworkLaunchEvent, localVars);
trigger.execute(fireworkLaunchEvent);
Variables.setLocalVariables(event, localVars);
};
}

int power = this.power != null ? this.power.getOptionalSingle(event).orElse(1).intValue() : 1;

for (Location location : locations.getArray(event)) {
World world = location.getWorld();
if (world == null)
continue;
Firework firework = world.spawn(location, Firework.class, consumer);
FireworkMeta meta = firework.getFireworkMeta();
meta.addEffects(effects);
meta.setPower(power);
firework.setFireworkMeta(meta);
lastSpawned = firework;
}

return super.walk(event, false);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "launch fireworks " + effects.toString(event, debug) +
" at " + locations.toString(event, debug) +
" timed " + (power != null ? power.toString(event, debug) : "1");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
" timed " + (power != null ? power.toString(event, debug) : "1");
" with duration " + (power != null ? power.toString(event, debug) : "1");

I think duration is a more familiar term for fireworks

}

}
Loading
Loading