Skip to content

Commit

Permalink
add animate for:<player>
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 15, 2021
1 parent 7b62812 commit 6323939
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Expand Up @@ -79,4 +79,8 @@ default void removeNoCollideTeam(Player player, UUID noCollide) {
default void sendEntityMetadataFlagsUpdate(Player player) {
throw new UnsupportedOperationException();
}

default void sendEntityEffect(Player player, Entity entity, byte effectId) {
throw new UnsupportedOperationException();
}
}
@@ -1,5 +1,6 @@
package com.denizenscript.denizen.scripts.commands.entity;

import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.AnimationHelper;
Expand All @@ -21,16 +22,16 @@ public class AnimateCommand extends AbstractCommand {

public AnimateCommand() {
setName("animate");
setSyntax("animate [<entity>|...] [animation:<name>]");
setRequiredArguments(2, 2);
setSyntax("animate [<entity>|...] [animation:<name>] (for:<player>|...)");
setRequiredArguments(2, 3);
isProcedural = false;
}

// <--[command]
// @Name Animate
// @Syntax animate [<entity>|...] [animation:<name>]
// @Syntax animate [<entity>|...] [animation:<name>] (for:<player>|...)
// @Required 2
// @Maximum 2
// @Maximum 3
// @Plugin Citizens
// @Short Makes a list of entities perform a certain animation.
// @Group entity
Expand All @@ -45,6 +46,7 @@ public AnimateCommand() {
//
// All entities also have available Bukkit's entity effect list:
// <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/EntityEffect.html>
// These EntityEffect options can optionally be played only for specific players with the "for:" argument input.
//
// In addition, Denizen adds a few new entity animations:
// SKELETON_START_SWING_ARM, SKELETON_STOP_SWING_ARM, POLAR_BEAR_START_STANDING, POLAR_BEAR_STOP_STANDING, HORSE_BUCK
Expand All @@ -59,14 +61,19 @@ public AnimateCommand() {
// - animate <player> animation:hurt
//
// @Usage
// Use to make a wolf NPC shake
// Use to make a wolf NPC shake.
// - animate <npc> animation:wolf_shake
// -->

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
AnimationHelper animationHelper = NMSHandler.getAnimationHelper();
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("for")
&& arg.matchesPrefix("for")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("for", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
Expand Down Expand Up @@ -97,14 +104,16 @@ else if (animationHelper.hasEntityAnimation(arg.getValue())) {
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for");
PlayerAnimation animation = scriptEntry.hasObject("animation") ? (PlayerAnimation) scriptEntry.getObject("animation") : null;
EntityEffect effect = scriptEntry.hasObject("effect") ? (EntityEffect) scriptEntry.getObject("effect") : null;
String nmsAnimation = scriptEntry.hasObject("nms_animation") ? (String) scriptEntry.getObject("nms_animation") : null;
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(animation != null ? ArgumentHelper.debugObj("animation", animation.name()) :
effect != null ? ArgumentHelper.debugObj("effect", effect.name()) : ArgumentHelper.debugObj("animation", nmsAnimation)) +
ArgumentHelper.debugObj("entities", entities.toString()));
ArgumentHelper.debugList("entities", entities)
+ (forPlayers != null ? ArgumentHelper.debugList("for", forPlayers) : ""));
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
Expand All @@ -114,7 +123,14 @@ public void execute(final ScriptEntry scriptEntry) {
animation.play(player);
}
else if (effect != null) {
entity.getBukkitEntity().playEffect(effect);
if (forPlayers != null) {
for (PlayerTag player : forPlayers) {
NMSHandler.getPacketHelper().sendEntityEffect(player.getPlayerEntity(), entity.getBukkitEntity(), effect.getData());
}
}
else {
entity.getBukkitEntity().playEffect(effect);
}
}
else {
EntityAnimation entityAnimation = NMSHandler.getAnimationHelper().getEntityAnimation(nmsAnimation);
Expand Down
Expand Up @@ -362,6 +362,11 @@ public void sendEntityMetadataFlagsUpdate(Player player) {
sendPacket(player, new PacketPlayOutEntityMetadata(player.getEntityId(), dw, true));
}

@Override
public void sendEntityEffect(Player player, Entity entity, byte effectId) {
sendPacket(player, new PacketPlayOutEntityStatus(((CraftEntity) entity).getHandle(), effectId));
}

public static void sendPacket(Player player, Packet packet) {
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
Expand Down

0 comments on commit 6323939

Please sign in to comment.