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

Elytra Boost #7283

Merged
merged 9 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import io.papermc.paper.event.entity.EntityMoveEvent;
import io.papermc.paper.event.player.*;
import org.bukkit.*;
Expand Down Expand Up @@ -1872,6 +1873,13 @@ public Block get(PlayerChangeBeaconEffectEvent event) {
}
}, EventValues.TIME_NOW);
}

// PlayerElytraBoostEvent
if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
EventValues.registerEventValue(PlayerElytraBoostEvent.class, ItemStack.class, PlayerElytraBoostEvent::getItemStack);
EventValues.registerEventValue(PlayerElytraBoostEvent.class, Entity.class, PlayerElytraBoostEvent::getFirework);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ch.njol.skript.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Will Consume Boosting Firework")
@Description("Checks to see if the firework used in an 'elytra boost' event will be consumed.")
@Examples({
"on elytra boost:",
"\tif the used firework will be consumed:",
"\t\tprevent the used firework from being consume"
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class CondElytraBoostConsume extends Condition {

static {
if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
Skript.registerCondition(CondElytraBoostConsume.class,
"[the] (boosting|used) firework will be consumed",
"[the] (boosting|used) firework (will not|won't) be consumed");
}
}

private boolean checkConsume;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlayerElytraBoostEvent.class)) {
Skript.error("This condition can only be used in an 'elytra boost' event.");
return false;
}
checkConsume = matchedPattern == 0;
return true;
}

@Override
public boolean check(Event event) {
if (!(event instanceof PlayerElytraBoostEvent boostEvent))
return false;
return boostEvent.shouldConsume() == checkConsume;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "the boosting firework will " + (checkConsume ? "" : "not") + " be consumed";
}

}
58 changes: 58 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffElytraBoostConsume.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Consume Boosting Firework")
@Description("Prevent the firework used in an 'elytra boost' event to be consumed.")
@Examples({
"on elytra boost:",
"\tif the used firework will be consumed:",
"\t\tprevent the used firework from being consume"
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class EffElytraBoostConsume extends Effect {

static {
if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
Skript.registerEffect(EffElytraBoostConsume.class,
"(prevent|disallow) [the] (boosting|used) firework from being consumed",
"allow [the] (boosting|used) firework to be consumed");
}
}

private boolean consume;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(PlayerElytraBoostEvent.class)) {
Skript.error("This effect can only be used in an 'elytra boost' event.");
return false;
}
consume = matchedPattern == 1;
return true;
}

@Override
protected void execute(Event event) {
if (!(event instanceof PlayerElytraBoostEvent boostEvent))
return;
boostEvent.setShouldConsume(consume);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (consume)
return "allow the boosting firework to be consumed";
return "prevent the boosting firework from being consumed";
}

}
13 changes: 13 additions & 0 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.destroystokyo.paper.event.entity.EntityJumpEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import com.destroystokyo.paper.event.player.PlayerJumpEvent;
import com.destroystokyo.paper.event.player.PlayerReadyArrowEvent;
import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
Expand Down Expand Up @@ -750,6 +751,18 @@ public class SimpleEvents {
)
.since("INSERT VERSION");

if (Skript.classExists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent")) {
Skript.registerEvent("Elytra Boost", SimpleEvent.class, PlayerElytraBoostEvent.class, "elytra boost")
.description("Called when a player uses a firework to boost their fly speed when flying with an elytra.")
.examples(
"on elytra boost:",
"\tif the used firework will be consumed:",
"\t\tprevent the used firework from being consume"
)
.requiredPlugins("Paper")
.since("INSERT VERSION");
}

}

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

import ch.njol.skript.test.runner.SkriptJUnitTest;
import com.destroystokyo.paper.event.player.PlayerElytraBoostEvent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Constructor;

public class PlayerElytraBoostEventTest extends SkriptJUnitTest {

private Player player;
private Firework firework;

@Before
public void setUp() {
player = EasyMock.niceMock(Player.class);
EntityType entityType = EntityType.valueOf("FIREWORK");
if (entityType == null) {
entityType = EntityType.valueOf("FIREWORK_ROCKET");
}
assert entityType != null;
firework = (Firework) getTestWorld().spawnEntity(getTestLocation(), entityType);
firework.setTicksToDetonate(9999999);
}

@Test
public void test() {
Constructor<?> constructor = null;
boolean newerConstructor = false;
try {
constructor = PlayerElytraBoostEvent.class.getConstructor(Player.class, ItemStack.class, Firework.class, EquipmentSlot.class);
newerConstructor = true;
} catch (Exception ignored) {
try {
constructor = PlayerElytraBoostEvent.class.getConstructor(Player.class, ItemStack.class, Firework.class);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No valid constructor for 'PlayerElytraBoostEvent'");
}
}

try {
Event event;
if (newerConstructor) {
event = (Event) constructor.newInstance(player, new ItemStack(Material.FIREWORK_ROCKET), firework, EquipmentSlot.HAND);
} else {
event = (Event) constructor.newInstance(player, new ItemStack(Material.FIREWORK_ROCKET), firework);
}

Bukkit.getPluginManager().callEvent(event);
} catch (Exception e) {
throw new RuntimeException("Unable to construct event.");
}
}

@After
public void cleanUp() {
if (firework != null)
firework.remove();
}

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

test "PlayerElytraBoosEventTest" when running JUnit:
set {_tests::*} to "boost event called", "boost event - firework item", "boost event - player" and "boost event - firework entity"
ensure junit test {@test} completes {_tests::*}

on elytra boost:
junit test is {@test}
complete objective "boost event called" for {@test}
if event-item is a firework rocket:
complete objective "boost event - firework item" for {@test}
if event-entity is a firework:
complete objective "boost event - firework entity" for {@test}
if event-player is set:
complete objective "boost event - player" for {@test}
Loading