Skip to content

Commit

Permalink
Paper-only event skeleton horse trap and EntityTrapped properties…
Browse files Browse the repository at this point in the history
… for `SkeletonHorse`s (#2446)

* `EntityTrapped` property for SkeletonHorses

* Paper only event `skeleton horse trap`

Also use updated `instanceof` syntax for `EntityTrapped`

* Update NMS Version

Property does not work in 1.17.1

* Move `trap_time` to its own property: `EntityTrapTime`

* Update `getPropertyString()` and remove redundant checks

Also cast the trap time to a long, because `getTrapTime()` returns the time in ticks, not seconds, so the times would be WAAAYYY off haha.
  • Loading branch information
BreadcrumbIsTaken committed Mar 22, 2023
1 parent 8e73e7d commit 5ea2989
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
Expand Up @@ -72,6 +72,7 @@ public static void init() {
ScriptEvent.registerScriptEvent(ProjectileCollideScriptEvent.class);
ScriptEvent.registerScriptEvent(ServerListPingScriptEventPaperImpl.class);
ScriptEvent.registerScriptEvent(ServerResourcesReloadedScriptEvent.class);
ScriptEvent.registerScriptEvent(SkeletonHorseTrapScriptEvent.class);
ScriptEvent.registerScriptEvent(TNTPrimesScriptEvent.class);
ScriptEvent.registerScriptEvent(UnknownCommandScriptEvent.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19)) {
Expand Down
@@ -0,0 +1,71 @@
package com.denizenscript.denizen.paper.events;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.destroystokyo.paper.event.entity.SkeletonHorseTrapEvent;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class SkeletonHorseTrapScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// skeleton horse trap
//
// @Location true
//
// @Group Paper
//
// @Plugin Paper
//
// @Triggers when a player gets too close to a trapped skeleton horse and triggers the trap.
//
// @Context
// <context.entity> returns an EntityTag of the skeleton horse.
// <context.players> returns a ListTag(PlayerTag) of the players involved in the trap.
// -->

public SkeletonHorseTrapScriptEvent() {
registerCouldMatcher("skeleton horse trap");
}

public EntityTag entity;
public SkeletonHorseTrapEvent event;

@Override
public boolean matches(ScriptPath path) {
if (!runInCheck(path, entity.getLocation())) {
return false;
}
return super.matches(path);
}

@Override
public ObjectTag getContext(String name) {
return switch (name) {
case "entity" -> entity;
case "players" -> {
ListTag players = new ListTag();
for (HumanEntity human : event.getEligibleHumans()) {
if (!EntityTag.isNPC(human) && human instanceof Player player) {
players.addObject(new PlayerTag(player));
}
}
yield players;
}
default -> super.getContext(name);
};
}

@EventHandler
public void onSkeletonHorseTrap(SkeletonHorseTrapEvent event) {
this.event = event;
entity = new EntityTag(event.getEntity());
fire(event);
}
}
Expand Up @@ -118,6 +118,10 @@ public static void registerMainProperties() {
PropertyParser.registerProperty(EntityTame.class, EntityTag.class);
PropertyParser.registerProperty(EntityEyeTargetLocation.class, EntityTag.class);
PropertyParser.registerProperty(EntityTrades.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
PropertyParser.registerProperty(EntityTrapped.class, EntityTag.class);
PropertyParser.registerProperty(EntityTrapTime.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityVillagerExperience.class, EntityTag.class);
PropertyParser.registerProperty(EntityVillagerLevel.class, EntityTag.class);
PropertyParser.registerProperty(EntityVisible.class, EntityTag.class);
Expand Down
@@ -0,0 +1,77 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.SkeletonHorse;

public class EntityTrapTime implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag ent &&
(ent.getBukkitEntity() instanceof SkeletonHorse);
}

public static EntityTrapTime getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityTrapTime((EntityTag) entity);
}
}

private EntityTrapTime(EntityTag ent) {
entity = ent;
}

EntityTag entity;

@Override
public String getPropertyString() {
return new DurationTag((long) getSkeletonHorse().getTrapTime()).identify();
}

@Override
public String getPropertyId() {
return "trap_time";
}

public static void register() {

// <--[tag]
// @attribute <EntityTag.trap_time>
// @returns DurationTag
// @mechanism EntityTag.trap_time
// @group properties
// @description
// Returns the skeleton horse's trap time in ticks.
// Trap time will go up every tick for as long as the horse is trapped (see <@link tag EntityTag.trapped>).
// A trapped horse will despawn after it reaches 18000 ticks (15 minutes).
// -->
PropertyParser.registerTag(EntityTrapTime.class, DurationTag.class, "trap_time", (attribute, object) -> {
return new DurationTag((long) object.getSkeletonHorse().getTrapTime());
});

// <--[mechanism]
// @object EntityTag
// @name trap_time
// @input DurationTag
// @description
// Sets the skeleton horse's trap time.
// Trap time will go up every tick for as long as the horse is trapped (see <@link tag EntityTag.trapped>).
// A trap time greater than 18000 ticks (15 minutes) will despawn the horse on the next tick.
// @tags
// <EntityTag.trapped>
// -->
PropertyParser.registerMechanism(EntityTrapTime.class, DurationTag.class, "trap_time", (object, mechanism, duration) -> {
object.getSkeletonHorse().setTrapTime(duration.getTicksAsInt());
});
}

public SkeletonHorse getSkeletonHorse() {
return (SkeletonHorse) entity.getBukkitEntity();
}
}
@@ -0,0 +1,78 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.entity.SkeletonHorse;

public class EntityTrapped implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag ent &&
(ent.getBukkitEntity() instanceof SkeletonHorse);
}

public static EntityTrapped getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityTrapped((EntityTag) entity);
}
}

private EntityTrapped(EntityTag ent) {
entity = ent;
}

EntityTag entity;

@Override
public String getPropertyString() {
return String.valueOf(((SkeletonHorse) entity.getBukkitEntity()).isTrapped());
}

@Override
public String getPropertyId() {
return "trapped";
}

public static void register() {

// <--[tag]
// @attribute <EntityTag.trapped>
// @returns ElementTag(Boolean)
// @mechanism EntityTag.trapped
// @group properties
// @description
// Returns whether the skeleton horse is trapped.
// A trapped skeleton horse will trigger the skeleton horse trap when the player is within 10 blocks of it.
// -->
PropertyParser.registerTag(EntityTrapped.class, ElementTag.class, "trapped", (attribute, object) -> {
return new ElementTag(object.getSkeletonHorse().isTrapped());
});

// <--[mechanism]
// @object EntityTag
// @name trapped
// @input ElementTag(Boolean)
// @description
// Sets whether the skeleton horse is trapped.
// A trapped skeleton horse will trigger the skeleton horse trap when the player is within 10 blocks of it.
// @tags
// <EntityTag.trapped>
// -->
PropertyParser.registerMechanism(EntityTrapped.class, ElementTag.class, "trapped", (object, mechanism, input) -> {
if (!mechanism.requireBoolean()) {
return;
}
object.getSkeletonHorse().setTrapped(input.asBoolean());
});
}

public SkeletonHorse getSkeletonHorse() {
return (SkeletonHorse) entity.getBukkitEntity();
}
}

0 comments on commit 5ea2989

Please sign in to comment.