Skip to content

Commit

Permalink
(experimental) attach command no_rotate arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 28, 2020
1 parent f4786ed commit 9397c78
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
Expand Up @@ -65,21 +65,15 @@ public AnimateCommand() {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

AnimationHelper animationHelper = NMSHandler.getAnimationHelper();

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
// Entity arg
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}

if (!scriptEntry.hasObject("animation") &&
!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("nms_animation")) {

if (arg.matchesEnum(PlayerAnimation.values())) {
scriptEntry.addObject("animation", PlayerAnimation.valueOf(arg.getValue().toUpperCase()));
}
Expand All @@ -91,13 +85,9 @@ else if (animationHelper.hasEntityAnimation(arg.getValue())) {
}
}
}

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}

if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("animation") && !scriptEntry.hasObject("nms_animation")) {
throw new InvalidArgumentsException("Must specify a valid animation!");
}
Expand All @@ -107,30 +97,20 @@ else if (animationHelper.hasEntityAnimation(arg.getValue())) {
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
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;

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)) +
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()));
}

// Go through all the entities and animate them
for (EntityTag entity : entities) {
if (entity.isSpawned()) {

try {
if (animation != null && entity.getBukkitEntity() instanceof Player) {

Player player = (Player) entity.getBukkitEntity();

animation.play(player);
}
else if (effect != null) {
Expand All @@ -143,7 +123,7 @@ else if (effect != null) {
}
catch (Exception e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Error playing that animation!");
} // We tried!
}
}
}
}
Expand Down
Expand Up @@ -21,16 +21,16 @@ public class AttachCommand extends AbstractCommand {

public AttachCommand() {
setName("attach");
setSyntax("attach [<entity>|...] [to:<entity>/cancel] (offset:<offset>) (relative) (yaw_offset:<#.#>) (pitch_offset:<#.#>) (sync_server) (for:<player>|...)");
setRequiredArguments(2, 8);
setSyntax("attach [<entity>|...] [to:<entity>/cancel] (offset:<offset>) (relative) (yaw_offset:<#.#>) (pitch_offset:<#.#>) (sync_server) (no_rotate) (for:<player>|...)");
setRequiredArguments(2, 9);
isProcedural = false;
}

// <--[command]
// @Name attach
// @Syntax attach [<entity>|...] [to:<entity>/cancel] (offset:<offset>) (relative) (yaw_offset:<#.#>) (pitch_offset:<#.#>) (sync_server) (for:<player>|...)
// @Syntax attach [<entity>|...] [to:<entity>/cancel] (offset:<offset>) (relative) (yaw_offset:<#.#>) (pitch_offset:<#.#>) (sync_server) (no_rotate) (for:<player>|...)
// @Required 2
// @Maximum 8
// @Maximum 9
// @Short Attaches a list of entities to another entity, for client-visible motion sync.
// @Group entity
//
Expand All @@ -53,6 +53,8 @@ public AttachCommand() {
// This can reduce some visual artifacts (such as entity unloading at distance), but may produce unintended functional artifacts.
// Note that you should generally only use 'sync_server' when you exclude the 'for' argument.
//
// Optionally specify 'no_rotate' to retain the attached entity's own rotation and ignore the target rotation.
//
// @Tags
// None.
//
Expand Down Expand Up @@ -83,6 +85,10 @@ else if (!scriptEntry.hasObject("sync_server")
&& arg.matches("sync_server")) {
scriptEntry.addObject("sync_server", new ElementTag(true));
}
else if (!scriptEntry.hasObject("no_rotate")
&& arg.matches("no_rotate")) {
scriptEntry.addObject("no_rotate", new ElementTag(true));
}
else if (!scriptEntry.hasObject("yaw_offset")
&& arg.matchesPrefix("yaw_offset")
&& arg.matchesFloat()) {
Expand Down Expand Up @@ -120,6 +126,7 @@ else if (!scriptEntry.hasObject("entities")
scriptEntry.defaultObject("cancel", new ElementTag(false));
scriptEntry.defaultObject("relative", new ElementTag(false));
scriptEntry.defaultObject("sync_server", new ElementTag(false));
scriptEntry.defaultObject("no_rotate", new ElementTag(false));
scriptEntry.defaultObject("yaw_offset", new ElementTag(0f));
scriptEntry.defaultObject("pitch_offset", new ElementTag(0f));
}
Expand All @@ -134,6 +141,7 @@ public void execute(final ScriptEntry scriptEntry) {
ElementTag cancel = scriptEntry.getElement("cancel");
ElementTag relative = scriptEntry.getElement("relative");
ElementTag sync_server = scriptEntry.getElement("sync_server");
ElementTag no_rotate = scriptEntry.getElement("no_rotate");
ElementTag yaw_offset = scriptEntry.getElement("yaw_offset");
ElementTag pitch_offset = scriptEntry.getElement("pitch_offset");
boolean shouldCancel = cancel.asBoolean();
Expand All @@ -145,6 +153,7 @@ public void execute(final ScriptEntry scriptEntry) {
+ yaw_offset.debug()
+ pitch_offset.debug()
+ sync_server.debug()
+ no_rotate.debug()
+ (forPlayers == null ? "" : ArgumentHelper.debugList("for", forPlayers)));
}
BiConsumer<EntityTag, UUID> procPlayer = (entity, player) -> {
Expand All @@ -161,6 +170,7 @@ public void execute(final ScriptEntry scriptEntry) {
attachment.pitchAngleOffset = pitch_offset.asFloat();
attachment.syncServer = sync_server.asBoolean();
attachment.forPlayer = player;
attachment.noRotate = no_rotate.asBoolean();
EntityAttachmentHelper.registerAttachment(attachment);
}
};
Expand Down
Expand Up @@ -28,6 +28,8 @@ public static class AttachmentData {

public boolean syncServer;

public boolean noRotate;

public BukkitTask checkTask;

public UUID forPlayer;
Expand Down Expand Up @@ -57,6 +59,11 @@ public void run() {
if (positionalOffset != null) {
goal = fixedForOffset(goal.toVector(), goal.getYaw(), goal.getPitch()).toLocation(goal.getWorld());
}
if (noRotate) {
Location attachLoc = attached.getLocation();
goal.setYaw(attachLoc.getYaw());
goal.setPitch(attachLoc.getPitch());
}
attached.teleport(goal);
}
}
Expand Down Expand Up @@ -152,6 +159,11 @@ public static byte adaptedCompressedAngle(byte angle, float offset) {
return (byte)((int)(angleF * (256F / 360F)));
}

public static byte compressAngle(float angle) {
angle %= 180;
return (byte)((int)(angle * (256F / 360F)));
}

public static Vector fixOffset(Vector offset, double yaw, double pitch) {
yaw = Math.toRadians(yaw);
pitch = Math.toRadians(pitch);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.server.v1_16_R2.*;
import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -231,10 +232,23 @@ public boolean processAttachToForPacket(Packet<?> packet) {
ENTITY_ID_PACKENT.setInt(pNew, att.attached.getEntityId());
if (att.positionalOffset != null && (packet instanceof PacketPlayOutEntity.PacketPlayOutRelEntityMove || packet instanceof PacketPlayOutEntity.PacketPlayOutRelEntityMoveLook)) {
boolean isRotate = packet instanceof PacketPlayOutEntity.PacketPlayOutRelEntityMoveLook;
byte yaw = 0, pitch = 0;
byte yaw, pitch;
if (att.noRotate) {
Entity attachedEntity = ((CraftEntity) att.attached).getHandle();
yaw = EntityAttachmentHelper.compressAngle(attachedEntity.yaw);
pitch = EntityAttachmentHelper.compressAngle(attachedEntity.pitch);
}
else if (isRotate) {
yaw = YAW_PACKENT.getByte(packet);
pitch = PITCH_PACKENT.getByte(packet);
}
else {
yaw = EntityAttachmentHelper.compressAngle(e.yaw);
pitch = EntityAttachmentHelper.compressAngle(e.pitch);
}
if (isRotate) {
yaw = EntityAttachmentHelper.adaptedCompressedAngle(YAW_PACKENT.getByte(packet), att.positionalOffset.getYaw());
pitch = EntityAttachmentHelper.adaptedCompressedAngle(PITCH_PACKENT.getByte(packet), att.positionalOffset.getPitch());
yaw = EntityAttachmentHelper.adaptedCompressedAngle(yaw, att.positionalOffset.getYaw());
pitch = EntityAttachmentHelper.adaptedCompressedAngle(pitch, att.positionalOffset.getPitch());
}
Vector goalPosition = att.fixedForOffset(new Vector(e.locX(), e.locY(), e.locZ()), e.yaw, e.pitch);
Vector oldPos = att.visiblePosition;
Expand All @@ -254,10 +268,8 @@ public boolean processAttachToForPacket(Packet<?> packet) {
POS_X_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getX());
POS_Y_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getY());
POS_Z_PACKTELENT.setDouble(newTeleportPacket, goalPosition.getZ());
if (isRotate) {
YAW_PACKTELENT.setByte(newTeleportPacket, yaw);
PITCH_PACKTELENT.setByte(newTeleportPacket, pitch);
}
YAW_PACKTELENT.setByte(newTeleportPacket, yaw);
PITCH_PACKTELENT.setByte(newTeleportPacket, pitch);
oldManager.sendPacket(newTeleportPacket);
}
else {
Expand Down Expand Up @@ -314,8 +326,18 @@ else if (packet instanceof PacketPlayOutEntityTeleport) {
Vector resultPos = new Vector(POS_X_PACKTELENT.getDouble(pNew), POS_Y_PACKTELENT.getDouble(pNew), POS_Z_PACKTELENT.getDouble(pNew));
if (att.positionalOffset != null) {
resultPos = att.fixedForOffset(resultPos, e.yaw, e.pitch);
byte yaw = EntityAttachmentHelper.adaptedCompressedAngle(YAW_PACKTELENT.getByte(packet), att.positionalOffset.getYaw());
byte pitch = EntityAttachmentHelper.adaptedCompressedAngle(PITCH_PACKTELENT.getByte(packet), att.positionalOffset.getPitch());
byte yaw, pitch;
if (att.noRotate) {
Entity attachedEntity = ((CraftEntity) att.attached).getHandle();
yaw = EntityAttachmentHelper.compressAngle(attachedEntity.yaw);
pitch = EntityAttachmentHelper.compressAngle(attachedEntity.pitch);
}
else {
yaw = YAW_PACKTELENT.getByte(packet);
pitch = PITCH_PACKTELENT.getByte(packet);
}
yaw = EntityAttachmentHelper.adaptedCompressedAngle(yaw, att.positionalOffset.getYaw());
pitch = EntityAttachmentHelper.adaptedCompressedAngle(pitch, att.positionalOffset.getPitch());
POS_X_PACKTELENT.setDouble(pNew, resultPos.getX());
POS_Y_PACKTELENT.setDouble(pNew, resultPos.getY());
POS_Z_PACKTELENT.setDouble(pNew, resultPos.getZ());
Expand Down

0 comments on commit 9397c78

Please sign in to comment.