-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Leash Event and Its Methods (#6427)
* Add leash event and its methods * Fix a woopsy in EvtLeash.java * Attempt to fix branch * Make ExprUnleashReason extend EVE * Bulk requested changes * Requested changes * Remove license header (#6684) * Requested changes * Merge branch 'dev/feature' into feature/event-leash * Pickle changes * Replace Getter, optimize imports * Remove duplicate value registration. Co-authored-by: Moderocky <admin@moderocky.com> --------- Co-authored-by: Moderocky <admin@moderocky.com>
- Loading branch information
1 parent
c8fee91
commit 7795331
Showing
6 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/ch/njol/skript/conditions/CondLeashWillDrop.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ch.njol.skript.conditions; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.entity.EntityUnleashEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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; | ||
|
||
@Name("Leash Will Drop") | ||
@Description("Checks whether the leash item will drop during the leash detaching in an unleash event.") | ||
@Examples({ | ||
"on unleash:", | ||
"\tif the leash will drop:", | ||
"\t\tprevent the leash from dropping", | ||
"\telse:", | ||
"\t\tallow the leash to drop" | ||
}) | ||
@Keywords("lead") | ||
@Events("Unleash") | ||
@RequiredPlugins("Paper 1.16+") | ||
@Since("INSERT VERSION") | ||
public class CondLeashWillDrop extends Condition { | ||
|
||
static { | ||
if (Skript.methodExists(EntityUnleashEvent.class, "isDropLeash")) | ||
Skript.registerCondition(CondLeashWillDrop.class, "[the] (lead|leash) [item] (will|not:(won't|will not)) (drop|be dropped)"); | ||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
if (!getParser().isCurrentEvent(EntityUnleashEvent.class)) { | ||
Skript.error("The 'leash will drop' condition can only be used in an 'unleash' event"); | ||
return false; | ||
} | ||
setNegated(parseResult.hasTag("not")); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
if (!(event instanceof EntityUnleashEvent unleashEvent)) | ||
return false; | ||
return unleashEvent.isDropLeash() ^ isNegated(); | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "the leash will" + (isNegated() ? " not" : "") + " be dropped"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.entity.EntityUnleashEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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; | ||
import ch.njol.util.Kleenean; | ||
|
||
@Name("Allow / Prevent Leash Drop") | ||
@Description("Allows or prevents the leash from being dropped in an unleash event.") | ||
@Examples({ | ||
"on unleash:", | ||
"\tif player is not set:", | ||
"\t\tprevent the leash from dropping", | ||
"\telse if player is op:", | ||
"\t\tallow the leash to drop" | ||
}) | ||
@Keywords("lead") | ||
@Events("Unleash") | ||
@Since("INSERT VERSION") | ||
public class EffDropLeash extends Effect { | ||
|
||
static { | ||
Skript.registerEffect(EffDropLeash.class, | ||
"(force|allow) [the] (lead|leash) [item] to drop", | ||
"(block|disallow|prevent) [the] (lead|leash) [item] from dropping" | ||
); | ||
} | ||
|
||
private boolean allowLeashDrop; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { | ||
if (!getParser().isCurrentEvent(EntityUnleashEvent.class)) { | ||
Skript.error("The 'drop leash' effect can only be used in an 'unleash' event"); | ||
return false; | ||
} | ||
allowLeashDrop = matchedPattern == 0; | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void execute(Event event) { | ||
if (!(event instanceof EntityUnleashEvent unleashEvent)) | ||
return; | ||
unleashEvent.setDropLeash(allowLeashDrop); | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return allowLeashDrop ? "allow the leash to drop" : "prevent the leash from dropping"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package ch.njol.skript.events; | ||
|
||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.event.entity.EntityUnleashEvent; | ||
import org.bukkit.event.entity.PlayerLeashEntityEvent; | ||
import org.bukkit.event.player.PlayerUnleashEntityEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.entity.EntityData; | ||
import ch.njol.skript.lang.Literal; | ||
import ch.njol.skript.lang.SkriptEvent; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.registrations.Classes; | ||
import ch.njol.skript.registrations.EventValues; | ||
import ch.njol.util.coll.CollectionUtils; | ||
|
||
public class EvtLeash extends SkriptEvent { | ||
|
||
static { | ||
Skript.registerEvent("Leash / Unleash", EvtLeash.class, CollectionUtils.array(PlayerLeashEntityEvent.class, EntityUnleashEvent.class), "[:player] [:un]leash[ing] [of %-entitydatas%]") | ||
.description("Called when an entity is leashed or unleashed. Cancelling these events will prevent the leashing or unleashing from occurring.") | ||
.examples( | ||
"on player leash of a sheep:", | ||
"\tsend \"Baaaaa--\" to player", | ||
"", | ||
"on player leash:", | ||
"\tsend \"<%event-entity%> Let me go!\" to player", | ||
"", | ||
"on unleash:", | ||
"\tbroadcast \"<%event-entity%> I'm free\"", | ||
"", | ||
"on player unleash:", | ||
"\tsend \"<%event-entity%> Thanks for freeing me!\" to player" | ||
) | ||
.since("INSERT VERSION"); | ||
|
||
// PlayerLeashEntityEvent | ||
// event-player is explicitly registered due to event does not extend PlayerEvent | ||
EventValues.registerEventValue(PlayerLeashEntityEvent.class, Player.class, PlayerLeashEntityEvent::getPlayer); | ||
EventValues.registerEventValue(PlayerLeashEntityEvent.class, Entity.class, PlayerLeashEntityEvent::getEntity); | ||
|
||
// EntityUnleashEvent | ||
EventValues.registerEventValue(EntityUnleashEvent.class, EntityUnleashEvent.UnleashReason.class, EntityUnleashEvent::getReason); | ||
|
||
// PlayerUnleashEntityEvent | ||
EventValues.registerEventValue(PlayerUnleashEntityEvent.class, Player.class, PlayerUnleashEntityEvent::getPlayer); | ||
} | ||
|
||
private enum EventType { | ||
|
||
LEASH("leash"), | ||
UNLEASH("unleash"), | ||
UNLEASH_BY_PLAYER("player unleash"); | ||
|
||
private final String name; | ||
|
||
EventType(String name) { | ||
this.name = name; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
|
||
} | ||
|
||
private @Nullable EntityData<?>[] types; | ||
private EventType eventType; | ||
|
||
@Override | ||
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResult) { | ||
//noinspection unchecked | ||
types = args[0] == null ? null : ((Literal<EntityData<?>>) args[0]).getAll(); | ||
eventType = EventType.LEASH; | ||
if (parseResult.hasTag("un")) { | ||
eventType = EventType.UNLEASH; | ||
if (parseResult.hasTag("player")) { | ||
eventType = EventType.UNLEASH_BY_PLAYER; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
Entity leashedEntity; | ||
switch (eventType) { | ||
case LEASH -> { | ||
if (!(event instanceof PlayerLeashEntityEvent playerLeash)) | ||
return false; | ||
leashedEntity = playerLeash.getEntity(); | ||
} | ||
case UNLEASH -> { | ||
if (!(event instanceof EntityUnleashEvent entityUnleash)) | ||
return false; | ||
leashedEntity = entityUnleash.getEntity(); | ||
} | ||
case UNLEASH_BY_PLAYER -> { | ||
if (!(event instanceof PlayerUnleashEntityEvent playerUnleash)) | ||
return false; | ||
leashedEntity = playerUnleash.getEntity(); | ||
} | ||
default -> { | ||
return false; | ||
} | ||
} | ||
if (types == null) | ||
return true; | ||
for (EntityData<?> entityData : types) { | ||
if (entityData.isInstance(leashedEntity)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return eventType + (types != null ? " of " + Classes.toString(types, false) : ""); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/ch/njol/skript/expressions/ExprUnleashReason.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.entity.EntityUnleashEvent; | ||
import org.bukkit.event.entity.EntityUnleashEvent.UnleashReason; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.*; | ||
import ch.njol.skript.expressions.base.EventValueExpression; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
|
||
@Name("Unleash Reason") | ||
@Description("The unleash reason in an unleash event.") | ||
@Examples({ | ||
"if the unleash reason is distance:", | ||
"\tbroadcast \"The leash was snapped in half.\"" | ||
}) | ||
@Events("Unleash") | ||
@Since("INSERT VERSION") | ||
public class ExprUnleashReason extends EventValueExpression<UnleashReason> { | ||
|
||
public ExprUnleashReason() { | ||
super(UnleashReason.class); | ||
} | ||
|
||
static { | ||
Skript.registerExpression(ExprUnleashReason.class, EntityUnleashEvent.UnleashReason.class, ExpressionType.SIMPLE, "[the] unleash[ing] reason"); | ||
} | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
if (!getParser().isCurrentEvent(EntityUnleashEvent.class)) { | ||
Skript.error("The 'unleash reason' expression can only be used in an 'unleash' event"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
protected UnleashReason[] get(Event event) { | ||
if (!(event instanceof EntityUnleashEvent unleashEvent)) | ||
return new UnleashReason[0]; | ||
return new UnleashReason[] {unleashEvent.getReason()}; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Class<? extends UnleashReason> getReturnType() { | ||
return UnleashReason.class; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "the unleash reason"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters