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

Fireworks Fix #7131

Open
wants to merge 13 commits into
base: dev/feature
Choose a base branch
from
23 changes: 23 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.Color;
import ch.njol.skript.util.SkriptColor;
import ch.njol.skript.util.ColorRGB;
import ch.njol.skript.util.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
Expand Down Expand Up @@ -1632,6 +1635,26 @@ public FireworkEffect get(FireworkExplodeEvent e) {
return effects.get(0);
}
}, 0);
EventValues.registerEventValue(FireworkExplodeEvent.class, Color[].class, new Getter<Color[], FireworkExplodeEvent>() {
@Override
public Color @Nullable [] get(FireworkExplodeEvent event) {
List<FireworkEffect> effects = event.getEntity().getFireworkMeta().getEffects();
if (effects.isEmpty())
return null;
List<Color> colors = new ArrayList<>();
for (FireworkEffect fireworkEffect : effects) {
for (org.bukkit.Color color : fireworkEffect.getColors()) {
if (SkriptColor.fromBukkitColor(color) != null)
colors.add(SkriptColor.fromBukkitColor(color));
else
colors.add(ColorRGB.fromBukkitColor(color));
}
}
if (colors.isEmpty())
return null;
return colors.toArray(Color[]::new);
}
}, EventValues.TIME_NOW);
//PlayerRiptideEvent
EventValues.registerEventValue(PlayerRiptideEvent.class, ItemStack.class, new Getter<ItemStack, PlayerRiptideEvent>() {
@Override
Expand Down
55 changes: 22 additions & 33 deletions src/main/java/ch/njol/skript/events/EvtFirework.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
/**
* 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.events;

import ch.njol.skript.Skript;
Expand All @@ -25,8 +7,8 @@
import ch.njol.skript.util.Color;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import ch.njol.skript.util.ColorRGB;
import org.bukkit.FireworkEffect;
import org.bukkit.event.Event;
import org.bukkit.event.entity.FireworkExplodeEvent;
Expand All @@ -41,15 +23,15 @@ public class EvtFirework extends SkriptEvent {
//Making the event argument type fireworkeffects, led to Skript having troubles parsing for some reason.
Skript.registerEvent("Firework Explode", EvtFirework.class, FireworkExplodeEvent.class, "[a] firework explo(d(e|ing)|sion) [colo[u]red %-colors%]")
.description("Called when a firework explodes.")
.examples("on firework explode",
"on firework exploding colored red, light green and black",
"on firework explosion colored light green:",
" broadcast \"A firework colored %colors% was exploded at %location%!\"")//TODO fix
.examples("on firework explode:",
"\tif event-colors contains red:",
"on firework exploding colored red, light green and black:",
"on firework explosion colored rgb 0, 255, 0:",
"\tbroadcast \"A firework colored %colors% was exploded at %location%!\"")
.since("2.4");
}

@Nullable
private Literal<Color> colors;

private @Nullable Literal<Color> colors;

@SuppressWarnings("unchecked")
@Override
Expand All @@ -58,16 +40,23 @@ public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResu
colors = (Literal<Color>) args[0];
return true;
}

@SuppressWarnings("null")

@Override
public boolean check(Event e) {
public boolean check(Event event) {
if (!(event instanceof FireworkExplodeEvent fireworkExplodeEvent))
return false;

if (colors == null)
return true;
List<org.bukkit.Color> colours = Arrays.stream(colors.getArray(e))
.map(color -> color.asBukkitColor())
.collect(Collectors.toList());
FireworkMeta meta = ((FireworkExplodeEvent)e).getEntity().getFireworkMeta();

List<org.bukkit.Color> colours = colors.stream(event)
.map(color -> {
if (color instanceof ColorRGB)
return color.asBukkitColor();
return color.asDyeColor().getFireworkColor();
})
.toList();
FireworkMeta meta = fireworkExplodeEvent.getEntity().getFireworkMeta();
for (FireworkEffect effect : meta.getEffects()) {
if (colours.containsAll(effect.getColors()))
return true;
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/ch/njol/skript/expressions/ExprFireworkEffect.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
/**
* 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.expressions;

import ch.njol.skript.util.ColorRGB;
import org.bukkit.FireworkEffect;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -86,11 +69,19 @@ protected FireworkEffect[] get(Event e) {
return null;
FireworkEffect.Builder builder = FireworkEffect.builder().with(type);

for (Color colour : color.getArray(e))
builder.withColor(colour.asBukkitColor());
for (Color colour : color.getArray(e)) {
if (colour instanceof ColorRGB)
builder.withColor(colour.asBukkitColor());
else
builder.withColor(colour.asDyeColor().getFireworkColor());
}
if (hasFade)
for (Color colour : fade.getArray(e))
builder.withFade(colour.asBukkitColor());
for (Color colour : fade.getArray(e)) {
if (colour instanceof ColorRGB)
builder.withFade(colour.asBukkitColor());
else
builder.withFade(colour.asDyeColor().getFireworkColor());
}

builder.flicker(flicker);
builder.trail(trail);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ch/njol/skript/util/SkriptColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ public static SkriptColor fromDyeColor(DyeColor dye) {

public static SkriptColor fromBukkitColor(org.bukkit.Color color) {
for (SkriptColor c : colors) {
if (c.asBukkitColor().equals(color))
if (c.asBukkitColor().equals(color) || c.asDyeColor().getFireworkColor().equals(color))
return c;
}
assert false;
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.skriptlang.skript.test.tests.syntaxes.events;

import ch.njol.skript.Skript;
import ch.njol.skript.test.runner.SkriptJUnitTest;
import ch.njol.skript.util.SkriptColor;
import org.bukkit.Bukkit;
import org.bukkit.FireworkEffect;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.event.Event;
import org.bukkit.event.entity.FireworkExplodeEvent;
import org.bukkit.inventory.meta.FireworkMeta;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class EvtFireworkTest extends SkriptJUnitTest {

private EntityType entityType;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
private List<Firework> fireworkList = new ArrayList<>();

@Before
public void getEntity() {
if (Skript.isRunningMinecraft(1, 20, 5)) {
entityType = EntityType.FIREWORK_ROCKET;
} else {
entityType = EntityType.valueOf("FIREWORK");
}
}

@Test
public void callEvents() {
List<Event> events = new ArrayList<>();
for (SkriptColor color : SkriptColor.values()) {
Firework firework = (Firework) getTestWorld().spawnEntity(getTestLocation(), entityType);
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
FireworkEffect fireworkEffect = FireworkEffect.builder().withColor(color.asDyeColor().getFireworkColor()).build();
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.addEffects(fireworkEffect);
firework.setFireworkMeta(fireworkMeta);
fireworkList.add(firework);
events.add(new FireworkExplodeEvent(firework));
}

for (Event event : events) {
Bukkit.getPluginManager().callEvent(event);
}
}

@After
public void cleanUp() {
for (Firework firework : fireworkList) {
firework.remove();
}
}

}
16 changes: 16 additions & 0 deletions src/test/skript/junit/EvtFireworkTest.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
options:
EvtFireworkTest: "org.skriptlang.skript.test.tests.syntaxes.events.EvtFireworkTest"

test "EvtFireworkJUnit" when running JUnit:
set {_tests::1} to "any firework"
loop all colors:
set {_tests::%loop-iteration + 1%} to "%loop-color% firework"

ensure junit test {@EvtFireworkTest} completes {_tests::*}

on firework explode:
junit test is {@EvtFireworkTest}
complete objective "any firework" for {@EvtFireworkTest}
if event-colors is set:
set {_color} to first element of event-colors
complete objective "%{_color}% firework" for {@EvtFireworkTest}