-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
PlayerAnimationImpl.java
148 lines (139 loc) · 6.58 KB
/
PlayerAnimationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package net.citizensnpcs.nms.v1_14_R1.util;
import java.lang.invoke.MethodHandle;
import java.util.EnumMap;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.ArmorStandTrait;
import net.citizensnpcs.trait.SitTrait;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.PlayerAnimation;
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;
import net.minecraft.server.v1_14_R1.PacketPlayOutEntityMetadata;
public class PlayerAnimationImpl {
public static void play(PlayerAnimation animation, Player bplayer, int radius) {
final EntityPlayer player = (EntityPlayer) NMSImpl.getHandle(bplayer);
if (DEFAULTS.containsKey(animation)) {
playDefaultAnimation(player, radius, DEFAULTS.get(animation));
return;
}
switch (animation) {
case SIT:
if (player instanceof NPCHolder) {
((NPCHolder) player).getNPC().getOrAddTrait(SitTrait.class).setSitting(true);
return;
}
player.getBukkitEntity().setMetadata("citizens.sitting",
new FixedMetadataValue(CitizensAPI.getPlugin(), true));
NPCRegistry registry = CitizensAPI.getNamedNPCRegistry("PlayerAnimationImpl");
if (registry == null) {
registry = CitizensAPI.createNamedNPCRegistry("PlayerAnimationImpl", new MemoryNPCDataStore());
}
final NPC holder = registry.createNPC(EntityType.ARMOR_STAND, "");
holder.getOrAddTrait(ArmorStandTrait.class).setAsPointEntity();
holder.spawn(player.getBukkitEntity().getLocation());
new BukkitRunnable() {
@Override
public void cancel() {
super.cancel();
holder.destroy();
}
@Override
public void run() {
if (player.dead || !player.valid || !player.getBukkitEntity().hasMetadata("citizens.sitting")
|| !player.getBukkitEntity().getMetadata("citizens.sitting").get(0).asBoolean()) {
cancel();
return;
}
if (!NMS.getPassengers(holder.getEntity()).contains(player.getBukkitEntity())) {
NMS.mount(holder.getEntity(), player.getBukkitEntity());
}
}
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
break;
case SLEEP:
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.SLEEPING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case SNEAK:
player.getBukkitEntity().setSneaking(true);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player,
radius);
break;
case START_ELYTRA:
player.J();
break;
case START_USE_MAINHAND_ITEM:
player.c(EnumHand.MAIN_HAND);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player,
radius);
break;
case START_USE_OFFHAND_ITEM:
player.c(EnumHand.OFF_HAND);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player,
radius);
break;
case STOP_SITTING:
if (player instanceof NPCHolder) {
((NPCHolder) player).getNPC().getOrAddTrait(SitTrait.class).setSitting(false);
return;
}
player.getBukkitEntity().setMetadata("citizens.sitting",
new FixedMetadataValue(CitizensAPI.getPlugin(), false));
NMS.mount(player.getBukkitEntity(), null);
break;
case STOP_SLEEPING:
try {
ENTITY_SETPOSE_METHOD.invoke(player, EntityPose.STANDING);
} catch (Throwable e) {
e.printStackTrace();
}
break;
case STOP_SNEAKING:
player.getBukkitEntity().setSneaking(false);
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player,
radius);
break;
case STOP_USE_ITEM:
player.clearActiveItem();
sendPacketNearby(new PacketPlayOutEntityMetadata(player.getId(), player.getDataWatcher(), true), player,
radius);
break;
default:
throw new UnsupportedOperationException();
}
}
protected static void playDefaultAnimation(EntityPlayer player, int radius, int code) {
PacketPlayOutAnimation packet = new PacketPlayOutAnimation(player, code);
sendPacketNearby(packet, player, radius);
}
protected static void sendPacketNearby(Packet<?> packet, EntityPlayer player, int radius) {
NMSImpl.sendPacketNearby(player.getBukkitEntity(), player.getBukkitEntity().getLocation(), packet, radius);
}
private static EnumMap<PlayerAnimation, Integer> DEFAULTS = Maps.newEnumMap(PlayerAnimation.class);
private static final MethodHandle ENTITY_SETPOSE_METHOD = NMS.getMethodHandle(Entity.class, "setPose", true,
EntityPose.class);
static {
DEFAULTS.put(PlayerAnimation.ARM_SWING, 0);
DEFAULTS.put(PlayerAnimation.HURT, 1);
DEFAULTS.put(PlayerAnimation.EAT_FOOD, 2);
DEFAULTS.put(PlayerAnimation.ARM_SWING_OFFHAND, 3);
DEFAULTS.put(PlayerAnimation.CRIT, 4);
DEFAULTS.put(PlayerAnimation.MAGIC_CRIT, 5);
}
}