Skip to content

Commit

Permalink
Fix "SLEEP" and "STOP_SLEEPING" animations for 1.14 and 1.15 (#2029)
Browse files Browse the repository at this point in the history
These now both fully work as they used to, allowing an NPC (or any Player entity) to go into a sleeping animation without requiring a bed (which is a bug introduced in the one of the recent MC updates). Fully tested and working in 1.15, presumed working for 1.14 as most things work the same. Not ported to 1.13 as I'm less confident on whether things are equivalent that far back.
  • Loading branch information
mcmonkey4eva authored and fullwall committed Jan 20, 2020
1 parent 577b1fc commit 72c7332
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.citizensnpcs.nms.v1_14_R1.util;

import java.lang.invoke.MethodHandle;
import java.util.EnumMap;

import org.bukkit.entity.EntityType;
Expand All @@ -17,8 +18,9 @@
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.PlayerAnimation;
import net.minecraft.server.v1_14_R1.BlockPosition;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EntityPose;
import net.minecraft.server.v1_14_R1.EnumHand;
import net.minecraft.server.v1_14_R1.Packet;
import net.minecraft.server.v1_14_R1.PacketPlayOutAnimation;
Expand Down Expand Up @@ -75,7 +77,11 @@ public void run() {
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
break;
case SLEEP:
player.sleep(new BlockPosition(player));
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.SLEEPING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case SNEAK:
player.getBukkitEntity().setSneaking(true);
Expand All @@ -101,7 +107,11 @@ public void run() {
NMS.mount(player.getBukkitEntity(), null);
break;
case STOP_SLEEPING:
playDefaultAnimation(player, radius, 2);
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.STANDING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case STOP_SNEAKING:
player.getBukkitEntity().setSneaking(false);
Expand All @@ -127,6 +137,8 @@ protected static void sendPacketNearby(Packet<?> packet, EntityPlayer player, in
NMSImpl.sendPacketNearby(player.getBukkitEntity(), player.getBukkitEntity().getLocation(), packet, radius);
}

private static final MethodHandle ENTITY_SETPOSE_METHOD = NMS.getMethodHandle(Entity.class, "setPose", true,
EntityPose.class);
private static EnumMap<PlayerAnimation, Integer> DEFAULTS = Maps.newEnumMap(PlayerAnimation.class);
static {
DEFAULTS.put(PlayerAnimation.ARM_SWING, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.citizensnpcs.nms.v1_15_R1.util;

import java.lang.invoke.MethodHandle;
import java.util.EnumMap;

import org.bukkit.entity.EntityType;
Expand All @@ -17,8 +18,9 @@
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.PlayerAnimation;
import net.minecraft.server.v1_15_R1.BlockPosition;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.EntityPose;
import net.minecraft.server.v1_15_R1.EnumHand;
import net.minecraft.server.v1_15_R1.Packet;
import net.minecraft.server.v1_15_R1.PacketPlayOutAnimation;
Expand Down Expand Up @@ -75,7 +77,11 @@ public void run() {
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
break;
case SLEEP:
player.sleep(new BlockPosition(player));
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.SLEEPING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case SNEAK:
player.getBukkitEntity().setSneaking(true);
Expand All @@ -101,7 +107,11 @@ public void run() {
NMS.mount(player.getBukkitEntity(), null);
break;
case STOP_SLEEPING:
playDefaultAnimation(player, radius, 2);
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.STANDING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case STOP_SNEAKING:
player.getBukkitEntity().setSneaking(false);
Expand All @@ -127,6 +137,8 @@ protected static void sendPacketNearby(Packet<?> packet, EntityPlayer player, in
NMSImpl.sendPacketNearby(player.getBukkitEntity(), player.getBukkitEntity().getLocation(), packet, radius);
}

private static final MethodHandle ENTITY_SETPOSE_METHOD = NMS.getMethodHandle(Entity.class, "setPose", true,
EntityPose.class);
private static EnumMap<PlayerAnimation, Integer> DEFAULTS = Maps.newEnumMap(PlayerAnimation.class);
static {
DEFAULTS.put(PlayerAnimation.ARM_SWING, 0);
Expand Down

0 comments on commit 72c7332

Please sign in to comment.