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 16 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
19 changes: 19 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,8 @@
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.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
Expand Down Expand Up @@ -1631,6 +1633,23 @@ public FireworkEffect get(FireworkExplodeEvent e) {
return effects.get(0);
}
}, 0);
EventValues.registerEventValue(FireworkExplodeEvent.class, Color[].class, new Getter<Color[], FireworkExplodeEvent>() {
@Override
public @Nullable Color[] get(FireworkExplodeEvent event) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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()) {
colors.add(SkriptColor.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
46 changes: 15 additions & 31 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,7 +7,6 @@
import ch.njol.skript.util.Color;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.FireworkEffect;
import org.bukkit.event.Event;
Expand All @@ -41,15 +22,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",
.examples("on firework explode:",
"\tif event-colors contains red:",
"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
.since("2.4");
"\tbroadcast \"A firework colored %colors% was exploded at %location%!\"")
.since("2.4, INSERT VERSION (fix colored)");
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}

@Nullable
private Literal<Color> colors;

private @Nullable Literal<Color> colors;

@SuppressWarnings("unchecked")
@Override
Expand All @@ -61,13 +42,16 @@ public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResu

@SuppressWarnings("null")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@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 = Arrays.stream(colors.getArray(event))
.map(color -> color.asDyeColor().getFireworkColor())
.toList();
FireworkMeta meta = fireworkExplodeEvent.getEntity().getFireworkMeta();
for (FireworkEffect effect : meta.getEffects()) {
if (colours.containsAll(effect.getColors()))
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ protected FireworkEffect[] get(Event e) {
FireworkEffect.Builder builder = FireworkEffect.builder().with(type);

for (Color colour : color.getArray(e))
builder.withColor(colour.asBukkitColor());
builder.withColor(colour.asDyeColor().getFireworkColor());
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
if (hasFade)
for (Color colour : fade.getArray(e))
builder.withFade(colour.asBukkitColor());
builder.withFade(colour.asDyeColor().getFireworkColor());

builder.flicker(flicker);
builder.trail(trail);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/util/SkriptColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public static SkriptColor fromBukkitColor(org.bukkit.Color color) {
for (SkriptColor c : colors) {
if (c.asBukkitColor().equals(color))
return c;
else if (c.asDyeColor().getFireworkColor().equals(color))
return c;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
assert false;
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.entity.Entity;
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.Before;
import org.junit.Test;

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


TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public class EvtFireworkTest extends SkriptJUnitTest {

private EntityType entityType;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@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);
events.add(new FireworkExplodeEvent(firework));
}

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

}
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}