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

BeaconEvents + Expressions #7079

Open
wants to merge 22 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 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
55 changes: 39 additions & 16 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@
import ch.njol.skript.util.slot.InventorySlot;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.event.block.AnvilDamagedEvent;
import com.destroystokyo.paper.event.block.BeaconEffectEvent;
import com.destroystokyo.paper.event.entity.EndermanAttackPlayerEvent;
import com.destroystokyo.paper.event.entity.ProjectileCollideEvent;
import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
import io.papermc.paper.event.entity.EntityMoveEvent;
import io.papermc.paper.event.player.PlayerStopUsingItemEvent;
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent;
import io.papermc.paper.event.player.PlayerTradeEvent;
import io.papermc.paper.event.player.*;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.FireworkEffect;
Expand Down Expand Up @@ -1889,19 +1887,19 @@ public TransformReason get(EntityTransformEvent event) {
// BellRingEvent - these are BlockEvents and not EntityEvents, so they have declared methods for getEntity()
if (Skript.classExists("org.bukkit.event.block.BellRingEvent")) {
EventValues.registerEventValue(BellRingEvent.class, Entity.class, new Getter<Entity, BellRingEvent>() {
@Override
@Nullable
public Entity get(BellRingEvent event) {
return event.getEntity();
}
}, EventValues.TIME_NOW);
@Override
@Nullable
public Entity get(BellRingEvent event) {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
return event.getEntity();
}
}, EventValues.TIME_NOW);

EventValues.registerEventValue(BellRingEvent.class, Direction.class, new Getter<Direction, BellRingEvent>() {
@Override
public Direction get(BellRingEvent event) {
return new Direction(event.getDirection(), 1);
}
}, EventValues.TIME_NOW);
@Override
public Direction get(BellRingEvent event) {
return new Direction(event.getDirection(), 1);
}
}, EventValues.TIME_NOW);
} else if (Skript.classExists("io.papermc.paper.event.block.BellRingEvent")) {
EventValues.registerEventValue(
io.papermc.paper.event.block.BellRingEvent.class, Entity.class,
Expand All @@ -1923,7 +1921,7 @@ public Entity[] get(BellResonateEvent event) {
}
}, EventValues.TIME_NOW);
}

// InventoryMoveItemEvent
EventValues.registerEventValue(InventoryMoveItemEvent.class, Inventory.class, new Getter<Inventory, InventoryMoveItemEvent>() {
@Override
Expand Down Expand Up @@ -1964,5 +1962,30 @@ public RegainReason get(EntityRegainHealthEvent event) {
return event.getRegainReason();
}
}, EventValues.TIME_NOW);

// BeaconEffectEvent
if (Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent")) {
EventValues.registerEventValue(BeaconEffectEvent.class, PotionEffectType.class, new Getter<PotionEffectType, BeaconEffectEvent>() {
@Override
public PotionEffectType get(BeaconEffectEvent event) {
return event.getEffect().getType();
}
}, EventValues.TIME_NOW, "Use 'applied effect' in beacon effect events.", BeaconEffectEvent.class);
EventValues.registerEventValue(BeaconEffectEvent.class, Player.class, new Getter<Player, BeaconEffectEvent>() {
@Override
public Player get(BeaconEffectEvent event) {
return event.getPlayer();
}
}, EventValues.TIME_NOW);
}
// PlayerChangeBeaconEffectEvent
if (Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent")) {
EventValues.registerEventValue(PlayerChangeBeaconEffectEvent.class, Block.class, new Getter<Block, PlayerChangeBeaconEffectEvent>() {
@Override
public Block get(PlayerChangeBeaconEffectEvent event) {
return event.getBeacon();
}
}, EventValues.TIME_NOW);
}
}
}
73 changes: 73 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtBeaconEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ch.njol.skript.events;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.data.DefaultComparators;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import com.destroystokyo.paper.event.block.BeaconEffectEvent;
import org.bukkit.event.Event;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.comparator.Relation;

@Name("Beacon Effect")
@Description("Called when a player gets an effect from a beacon.")
@Examples({
"on beacon effect:",
"\tbroadcast applied effect",
"\tbroadcast event-player",
"\tbroadcast event-block",
"on primary beacon effect apply of haste:",
"on application of secondary beacon effect:",
"on beacon effect of speed:"
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class EvtBeaconEffect extends SkriptEvent {

static {
if (Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent"))
Skript.registerEvent("Beacon Effect", EvtBeaconEffect.class, BeaconEffectEvent.class,
"[:primary|:secondary] beacon effect [of %-potioneffecttypes%]",
"application of [:primary|:secondary] beacon effect [of %-potioneffecttypes%]",
"[:primary|:secondary] beacon effect apply [of %-potioneffecttypes%]");

}

private @Nullable Literal<PotionEffectType> potionTypes;
private @Nullable Boolean primaryCheck;

@Override
@SuppressWarnings("unchecked")
public boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
potionTypes = (Literal<PotionEffectType>) exprs[0];
if (parseResult.hasTag("primary")) {
primaryCheck = true;
} else if (parseResult.hasTag("secondary")) {
primaryCheck = false;
}
return true;
}

@Override
public boolean check(Event event) {
if (!(event instanceof BeaconEffectEvent effectEvent))
return false;
if (primaryCheck != null && effectEvent.isPrimary() != primaryCheck)
return false;
if (potionTypes != null) {
return potionTypes.check(event, type -> effectEvent.getEffect().getType() == type);
}
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return (primaryCheck == null ? "" : primaryCheck ? "primary " : "secondary ") +
"beacon effect" + (potionTypes == null ? "" : " of " + potionTypes.toString(event, debug));
}

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

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import io.papermc.paper.event.block.BeaconActivatedEvent;
import io.papermc.paper.event.block.BeaconDeactivatedEvent;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Beacon Toggle")
@Description("Called when a beacon is activated or deactivated.")
@Examples({
"on beacon toggle:",
"on beacon activate:",
"on beacon deactivate:"
})
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
public class EvtBeaconToggle extends SkriptEvent {

static {
if (Skript.classExists("io.papermc.paper.event.block.BeaconActivatedEvent"))
Skript.registerEvent("Beacon Toggle", EvtBeaconToggle.class, new Class[] {BeaconActivatedEvent.class, BeaconDeactivatedEvent.class},
"beacon toggle",
"beacon activat(e|ion)",
"beacon deactivat(e|ion)");
}

private boolean isActivate, isToggle;

@Override
public boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
isToggle = matchedPattern == 0;
isActivate = matchedPattern == 1;
return true;
}

@Override
public boolean check(Event event) {
if (!isToggle) {
if (event instanceof BeaconActivatedEvent) {
return isActivate;
} else if (event instanceof BeaconDeactivatedEvent) {
return !isActivate;
}
}
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "beacon " + (isToggle ? "toggle" : isActivate ? "activate" : "deactivate");
}

}
33 changes: 25 additions & 8 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import org.bukkit.event.world.LootGenerateEvent;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.event.world.SpawnChangeEvent;
import io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent;

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -473,7 +474,7 @@ public class SimpleEvents {
.description("Called when a player enters a world. Does not work with other entities!")
.examples("on player world change:",
" world is \"city\"",
" send \"Welcome to the City!\"")
" send \"Welcome to the City!\"")
.since("2.2-dev28");
Skript.registerEvent("Flight Toggle", SimpleEvent.class, PlayerToggleFlightEvent.class, "[player] flight toggl(e|ing)", "[player] toggl(e|ing) flight")
.description("Called when a players stops/starts flying.")
Expand Down Expand Up @@ -502,11 +503,11 @@ public class SimpleEvents {
if (Skript.classExists("org.bukkit.event.player.PlayerSwapHandItemsEvent")) {
Skript.registerEvent("Hand Item Swap", SimpleEvent.class, PlayerSwapHandItemsEvent.class, "swap[ping of] [(hand|held)] item[s]")
.description("Called whenever a player swaps the items in their main- and offhand slots.",
"Works also when one or both of the slots are empty.",
"The event is called before the items are actually swapped,",
"so when you use the player's tool or player's offtool expressions,",
"they will return the values before the swap -",
"this enables you to cancel the event before anything happens.")
"Works also when one or both of the slots are empty.",
"The event is called before the items are actually swapped,",
"so when you use the player's tool or player's offtool expressions,",
"they will return the values before the swap -",
"this enables you to cancel the event before anything happens.")
.examples("on swap hand items:",
" event-player's tool is a diamond sword",
" cancel event")
Expand Down Expand Up @@ -637,7 +638,7 @@ public class SimpleEvents {
if (Skript.classExists("com.destroystokyo.paper.event.block.AnvilDamagedEvent")) {
Skript.registerEvent("Anvil Damage", SimpleEvent.class, AnvilDamagedEvent.class, "anvil damag(e|ing)")
.description("Called when an anvil is damaged/broken from being used to repair/rename items.",
"Note: this does not include anvil damage from falling.")
"Note: this does not include anvil damage from falling.")
.requiredPlugins("Paper")
.examples("on anvil damage:",
"\tcancel the event")
Expand Down Expand Up @@ -683,7 +684,7 @@ public class SimpleEvents {
)
.since("2.7");
}

//noinspection deprecation
Skript.registerEvent("Chat", SimpleEvent.class, AsyncPlayerChatEvent.class, "chat")
.description(
Expand Down Expand Up @@ -820,5 +821,21 @@ public class SimpleEvents {
.since("2.9.0")
.requiredPlugins("Paper");
}
if (Skript.classExists("io.papermc.paper.event.player.PlayerChangeBeaconEffectEvent")) {
Skript.registerEvent("Beacon Change Effect", SimpleEvent.class, PlayerChangeBeaconEffectEvent.class,
"beacon change effect", "beacon effect change", "player chang(e[s]|ing) [of] beacon effect")
.description("Called when a player changes the effects of a beacon.")
.examples(
"on beacon effect change:",
"\tbroadcast event-player",
"\tbroadcast event-block",
"\tbroadcast primary beacon effect",
"\tbroadcast secondary beacon effect",
"on beacon change effect:",
"on player change beacon effect:"
)
.since("INSERT VERSION")
.requiredPlugins("Paper");
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprAppliedEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import com.destroystokyo.paper.event.block.BeaconEffectEvent;
import org.bukkit.event.Event;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.Nullable;

@Name("Applied Beacon Effect")
@Description("The type of effect applied by a beacon.")
@Examples({
"on beacon effect:",
"\tif the applied effect is primary beacon effect:",
"\t\tbroadcast \"Is Primary\"",
"\telse if applied effect = secondary effect:",
"\t\tbroadcast \"Is Secondary\""
})
@Events("Beacon Effect")
@RequiredPlugins("Paper")
@Since("INSERT VERSION")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public class ExprAppliedEffect extends SimpleExpression<PotionEffectType> {

static {
if (Skript.classExists("com.destroystokyo.paper.event.block.BeaconEffectEvent")) {
Skript.registerExpression(ExprAppliedEffect.class, PotionEffectType.class, ExpressionType.SIMPLE, "[the] applied [beacon] effect");
}
}


@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (!getParser().isCurrentEvent(BeaconEffectEvent.class)) {
Skript.error("You can only use 'applied effect' in a beacon effect event.");
return false;
}
return true;
}

@Override
protected PotionEffectType @Nullable [] get(Event event) {
if (!(event instanceof BeaconEffectEvent effectEvent))
return null;
return new PotionEffectType[]{effectEvent.getEffect().getType()};
}

@Override
public Class<PotionEffectType> getReturnType() {
return PotionEffectType.class;
}

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

@Override
public String toString(@Nullable Event event, boolean debug) {
return "applied effect";
}

}
Loading