Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paper-only event
skeleton horse trap
and EntityTrapped
properties…
… 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
1 parent
8e73e7d
commit 5ea2989
Showing
5 changed files
with
231 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
71 changes: 71 additions & 0 deletions
71
paper/src/main/java/com/denizenscript/denizen/paper/events/SkeletonHorseTrapScriptEvent.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,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); | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityTrapTime.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,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(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
plugin/src/main/java/com/denizenscript/denizen/objects/properties/entity/EntityTrapped.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,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(); | ||
} | ||
} |