Skip to content

Commit

Permalink
EntityTag.is_sneaking Paper-only property (#2508)
Browse files Browse the repository at this point in the history
* Add `EntityTag.sneaking` property

* Meta improvements

* Deprecate `PlayerTag.is_sneaking`

* `ItemTag.trim` minor meta fix

* `PlayerTag.is_sneaking` deprecation fix

* Cleanup `MaterialAttachmentFace`

* Revert `PlayerTag.is_sneaking` deprecation

* Register existing tags when the prop isn't present

* Rename to `is_sneaking`

* Update another place

* Bump Paper API, add Citizens dependency

* Properly make NPCs sneak

* Remove trait when standing up
  • Loading branch information
tal5 committed Aug 6, 2023
1 parent 890a0b6 commit 19433ee
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 32 deletions.
15 changes: 14 additions & 1 deletion paper/pom.xml
Expand Up @@ -25,9 +25,22 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizens-main</artifactId>
<version>2.0.32-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -102,6 +102,9 @@ public static void init() {
PropertyParser.registerProperty(EntityLeftHanded.class, EntityTag.class);
PropertyParser.registerProperty(EntityReputation.class, EntityTag.class);
PropertyParser.registerProperty(EntityShouldBurn.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19)) {
PropertyParser.registerProperty(EntitySneaking.class, EntityTag.class);
}
PropertyParser.registerProperty(EntityWitherInvulnerable.class, EntityTag.class);
PropertyParser.registerProperty(ItemArmorStand.class, ItemTag.class);

Expand Down
Expand Up @@ -14,7 +14,7 @@ public class EntityLeftHanded extends EntityProperty<ElementTag> {
// @input ElementTag(Boolean)
// @plugin Paper
// @description
// Whether the mob is left-handed. Mobs have a rare chance to spawn left-handed.
// Whether a mob is left-handed. Mobs have a rare chance of spawning left-handed.
// -->

public static boolean describes(EntityTag entity) {
Expand Down
@@ -0,0 +1,63 @@
package com.denizenscript.denizen.paper.properties;

import com.denizenscript.denizen.npc.traits.SneakingTrait;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.properties.entity.EntityProperty;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import net.citizensnpcs.api.npc.NPC;

public class EntitySneaking extends EntityProperty<ElementTag> {

// <--[property]
// @object EntityTag
// @name is_sneaking
// @input ElementTag(Boolean)
// @plugin Paper
// @description
// Whether an entity is sneaking.
// For most entities this just makes the name tag less visible, and doesn't actually update the pose.
// Note that <@link command sneak> is also available.
// -->

public static boolean describes(EntityTag entity) {
return true;
}

@Override
public ElementTag getPropertyValue() {
return new ElementTag(getEntity().isSneaking());
}

@Override
public boolean isDefaultValue(ElementTag value) {
return !value.asBoolean();
}

@Override
public void setPropertyValue(ElementTag value, Mechanism mechanism) {
if (mechanism.requireBoolean()) {
boolean sneaking = value.asBoolean();
getEntity().setSneaking(sneaking);
if (object.isCitizensNPC()) {
NPC npc = object.getDenizenNPC().getCitizen();
if (sneaking) {
npc.getOrAddTrait(SneakingTrait.class).sneak();
}
else if (npc.hasTrait(SneakingTrait.class)) {
npc.getTraitNullable(SneakingTrait.class).stand();
npc.removeTrait(SneakingTrait.class);
}
}
}
}

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

public static void register() {
autoRegister("is_sneaking", EntitySneaking.class, ElementTag.class, false);
}
}
Expand Up @@ -78,7 +78,7 @@ public ObjectTag getContext(String name) {
case "item" -> result;
case "inventory" -> InventoryTag.mirrorBukkitInventory(event.getInventory());
case "recipe" -> new ListTag(Arrays.asList(event.getInventory().getMatrix()), ItemTag::new);
case "recipe_id" -> event.getRecipe() instanceof Keyed keyed ? new ElementTag(keyed.getKey().toString()): null;
case "recipe_id" -> event.getRecipe() instanceof Keyed keyed ? new ElementTag(keyed.getKey().toString(), true): null;
case "is_repair" -> new ElementTag(event.isRepair());
default -> super.getContext(name);
};
Expand Down
16 changes: 10 additions & 6 deletions plugin/src/main/java/com/denizenscript/denizen/objects/NPCTag.java
@@ -1,6 +1,8 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.npc.DenizenNPCHelper;
import com.denizenscript.denizen.npc.traits.*;
import com.denizenscript.denizen.scripts.commands.npc.EngageCommand;
Expand Down Expand Up @@ -772,12 +774,14 @@ else if (attribute.startsWith("list", 2)) {
// @description
// Returns whether the NPC is currently sneaking. Only works for player-type NPCs.
// -->
tagProcessor.registerTag(ElementTag.class, "is_sneaking", (attribute, object) -> {
if (!object.isSpawned() && object.getEntity() instanceof Player) {
return null;
}
return new ElementTag(((Player) object.getEntity()).isSneaking());
});
if (!Denizen.supportsPaper || NMSHandler.getVersion().isAtMost(NMSVersion.v1_18)) {
tagProcessor.registerTag(ElementTag.class, "is_sneaking", (attribute, object) -> {
if (!object.isSpawned() && object.getEntity() instanceof Player) {
return null;
}
return new ElementTag(((Player) object.getEntity()).isSneaking());
});
}

// <--[tag]
// @attribute <NPCTag.engaged[(<player>)]>
Expand Down
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.objects;

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.abstracts.ImprovedOfflinePlayer;
Expand Down Expand Up @@ -2084,9 +2085,11 @@ else if (foodLevel / maxHunger < 1) {
// @description
// Returns whether the player is currently sneaking.
// -->
registerOnlineOnlyTag(ElementTag.class, "is_sneaking", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().isSneaking());
});
if (!Denizen.supportsPaper || NMSHandler.getVersion().isAtMost(NMSVersion.v1_18)) {
registerOnlineOnlyTag(ElementTag.class, "is_sneaking", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().isSneaking());
});
}

// <--[tag]
// @attribute <PlayerTag.is_sprinting>
Expand Down
Expand Up @@ -18,10 +18,10 @@ public class ItemTrim extends ItemProperty<MapTag> {
// @name trim
// @input MapTag
// @description
// Controls an armor item's trim.
// An armor item's trim.
// Allowed keys: material, pattern.
// Valid material inputs can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimMaterial.html>
// Valid pattern inputs can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimPattern.html>
// Valid material values can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimMaterial.html>
// Valid pattern values can be found here: <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/trim/TrimPattern.html>
// Valid values also include ones added by datapacks, plugins, etc. as a namespaced key.
// For the mechanism, if an item already has a trim, you can omit either material or pattern to keep the original data while also changing the other option.
// For example, if you only want to change the pattern and not the material, you can omit the material, and it will use the already existing material.
Expand Down
@@ -1,12 +1,9 @@
package com.denizenscript.denizen.objects.properties.material;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.FaceAttachable;
import org.bukkit.block.data.type.Bell;
Expand All @@ -24,8 +21,7 @@ public class MaterialAttachmentFace extends MaterialProperty<ElementTag> {
// -->

public static boolean describes(MaterialTag material) {
BlockData data = material.getModernData();
return data instanceof FaceAttachable || data instanceof Bell;
return material.getModernData() instanceof FaceAttachable || material.getModernData() instanceof Bell;
}

public MaterialAttachmentFace(MaterialTag material) {
Expand All @@ -49,28 +45,23 @@ public String getPropertyId() {
}

@Override
public void setPropertyValue(ElementTag attachment, Mechanism mechanism) {
public void setPropertyValue(ElementTag value, Mechanism mechanism) {
if (getBlockData() instanceof FaceAttachable attachable) {
if (mechanism.requireEnum(FaceAttachable.AttachedFace.class)) {
attachable.setAttachedFace(FaceAttachable.AttachedFace.valueOf(attachment.asString().toUpperCase()));
attachable.setAttachedFace(value.asEnum(FaceAttachable.AttachedFace.class));
}
}
else if (getBlockData() instanceof Bell bell) {
if (mechanism.requireEnum(Bell.Attachment.class)) {
bell.setAttachment(Bell.Attachment.valueOf(attachment.asString().toUpperCase()));
bell.setAttachment(value.asEnum(Bell.Attachment.class));
}
}
}

public BlockFace getAttachedTo() {
if (getBlockData() instanceof FaceAttachable attachable) {
return switch (attachable.getAttachedFace()) {
case WALL -> {
if (getBlockData() instanceof Directional) {
yield ((Directional) getBlockData()).getFacing().getOppositeFace();
}
yield BlockFace.SELF;
}
case WALL -> getBlockData() instanceof Directional directional ? directional.getFacing().getOppositeFace() : BlockFace.SELF;
case FLOOR -> BlockFace.DOWN;
case CEILING -> BlockFace.UP;
};
Expand Down
Expand Up @@ -45,10 +45,10 @@ public SneakCommand() {
// A fake sneak only affects the name plate, not the entity's pose.
//
// Note: using this command on a player will only show to other players. You cannot alter a player in their own view.
// Note that <@link property EntityTag.is_sneaking> is also available.
//
// @Tags
// <NPCTag.is_sneaking>
// <PlayerTag.is_sneaking>
// <EntityTag.is_sneaking>
//
// @Usage
// Make the linked NPC start sneaking.
Expand Down

0 comments on commit 19433ee

Please sign in to comment.