Skip to content

Commit

Permalink
Make eating/blocking rotation locking smooth. Fix rotation lock when …
Browse files Browse the repository at this point in the history
…eating/blocking on a ladder. Fix #186
  • Loading branch information
tr7zw committed Nov 10, 2024
1 parent 5474067 commit 9618a17
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 56 deletions.
20 changes: 17 additions & 3 deletions src/main/java/dev/tr7zw/notenoughanimations/access/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ public interface PlayerData {

public void setSideSword(ItemStack item);

public void disableBodyRotation(boolean val);

public boolean isBodyRotationDisabled();
/**
* Disables rotationlocking, so the animation has full controll
*
* @param val
*/
public void setDisableBodyRotation(boolean val);

public boolean isDisableBodyRotation();

/**
* Overwrites rotationlocking to be active
*
* @param val
*/
public void setRotateBodyToHead(boolean val);

public boolean isRotateBodyToHead();

public ItemStack[] getLastHeldItems();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dev.tr7zw.notenoughanimations.animations.fullbody;

import dev.tr7zw.notenoughanimations.access.PlayerData;
import dev.tr7zw.notenoughanimations.api.BasicAnimation;
import dev.tr7zw.notenoughanimations.versionless.NEABaseMod;
import dev.tr7zw.notenoughanimations.versionless.animations.BodyPart;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.world.item.ItemUseAnimation;

public class ActionRotationLockAnimation extends BasicAnimation {

private BodyPart[] target = new BodyPart[] { BodyPart.BODY };

@Override
public boolean isEnabled() {
return NEABaseMod.config.enableRotationLocking;
}

@Override
public boolean isValid(AbstractClientPlayer entity, PlayerData data) {
if (entity.getUseItemRemainingTicks() > 0) {
ItemUseAnimation action = entity.getUseItem().getUseAnimation();
// Eating/Drinking
if (action == ItemUseAnimation.EAT || action == ItemUseAnimation.DRINK || action == ItemUseAnimation.BLOCK) {
return true;
}
}
return false;
}

@Override
public BodyPart[] getBodyParts(AbstractClientPlayer entity, PlayerData data) {
return target;
}

@Override
public int getPriority(AbstractClientPlayer entity, PlayerData data) {
// lower than animations like ladder
return 1250;
}

@Override
public void apply(AbstractClientPlayer entity, PlayerData data, PlayerModel model, BodyPart part, float delta,
float tickCounter) {
data.setRotateBodyToHead(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void apply(AbstractClientPlayer entity, PlayerData data, PlayerModel mode
//spotless:on
if (blockState.hasProperty(HorizontalDirectionalBlock.FACING)) {
Direction dir = blockState.getValue(HorizontalDirectionalBlock.FACING);
data.disableBodyRotation(true);
data.setDisableBodyRotation(true);
switch (dir) {
case NORTH:
entity.setYBodyRot(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class EatDrinkAnimation extends BasicAnimation {
private BodyPart[] target;
private final BodyPart[] left = new BodyPart[] { BodyPart.LEFT_ARM };
private final BodyPart[] right = new BodyPart[] { BodyPart.RIGHT_ARM };
private final BodyPart[] leftFixed = new BodyPart[] { BodyPart.LEFT_ARM, BodyPart.BODY };
private final BodyPart[] rightFixed = new BodyPart[] { BodyPart.RIGHT_ARM, BodyPart.BODY };

@Override
public boolean isEnabled() {
Expand All @@ -34,15 +32,15 @@ public boolean isValid(AbstractClientPlayer entity, PlayerData data) {
if (action == ItemUseAnimation.EAT || action == ItemUseAnimation.DRINK) {
if (entity.getUsedItemHand() == InteractionHand.MAIN_HAND) {
if (entity.getMainArm() == HumanoidArm.RIGHT) {
target = NEABaseMod.config.enableRotationLocking ? rightFixed : right;
target = right;
} else {
target = NEABaseMod.config.enableRotationLocking ? leftFixed : left;
target = left;
}
} else {
if (entity.getMainArm() == HumanoidArm.RIGHT) {
target = NEABaseMod.config.enableRotationLocking ? leftFixed : left;
target = left;
} else {
target = NEABaseMod.config.enableRotationLocking ? rightFixed : right;
target = right;
}
}
return true;
Expand All @@ -64,12 +62,6 @@ public int getPriority(AbstractClientPlayer entity, PlayerData data) {
@Override
public void apply(AbstractClientPlayer entity, PlayerData data, PlayerModel model, BodyPart part, float delta,
float tickCounter) {
if (part == BodyPart.BODY) {
data.disableBodyRotation(true);
entity.setYBodyRot(entity.getYHeadRot());
entity.yBodyRotO = entity.yHeadRotO;
return;
}
HumanoidArm arm = part == BodyPart.LEFT_ARM ? HumanoidArm.LEFT : HumanoidArm.RIGHT;
float g = entity.getUseItemRemainingTicks() - delta + 1.0F;
// float h = g / entity.getUseItem().getUseDuration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public boolean isEnabled() {
private ArmPose leftArmPose;
private final BodyPart[] left = new BodyPart[] { BodyPart.LEFT_ARM };
private final BodyPart[] right = new BodyPart[] { BodyPart.RIGHT_ARM };
private final BodyPart[] leftFixed = new BodyPart[] { BodyPart.LEFT_ARM, BodyPart.BODY };
private final BodyPart[] rightFixed = new BodyPart[] { BodyPart.RIGHT_ARM, BodyPart.BODY };

@Override
public boolean isValid(AbstractClientPlayer entity, PlayerData data) {
Expand All @@ -37,10 +35,10 @@ public boolean isValid(AbstractClientPlayer entity, PlayerData data) {
@Override
public BodyPart[] getBodyParts(AbstractClientPlayer entity, PlayerData data) {
if (ArmPose.BLOCK == leftArmPose) {
return NEABaseMod.config.enableRotationLocking ? leftFixed : left;
return left;
}
if (ArmPose.BLOCK == rightArmPose) {
return NEABaseMod.config.enableRotationLocking ? rightFixed : right;
return right;
}
// ???
return new BodyPart[0];
Expand All @@ -54,11 +52,7 @@ public int getPriority(AbstractClientPlayer entity, PlayerData data) {
@Override
public void apply(AbstractClientPlayer entity, PlayerData data, PlayerModel model, BodyPart part, float delta,
float tickCounter) {
if (part == BodyPart.BODY) {
data.disableBodyRotation(true);
entity.setYBodyRot(entity.getYHeadRot());
entity.yBodyRotO = entity.yHeadRotO;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;

import dev.tr7zw.notenoughanimations.access.PlayerData;
import dev.tr7zw.notenoughanimations.animations.fullbody.ActionRotationLockAnimation;
import dev.tr7zw.notenoughanimations.animations.fullbody.CrawlingAnimation;
import dev.tr7zw.notenoughanimations.animations.fullbody.FallingAnimation;
import dev.tr7zw.notenoughanimations.animations.fullbody.LadderAnimation;
Expand Down Expand Up @@ -111,6 +112,7 @@ private void loadAnimations() {
addAnimation(new HugAnimation());
addAnimation(new NarutoRunningAnimation());
addAnimation(new CustomBowAnimation());
addAnimation(new ActionRotationLockAnimation());
// spotless:off
//#if MC >= 11700
addAnimation(new FreezingAnimation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ public void updateModel(AbstractClientPlayer entity, PlayerModel model, float sw
interpolate(model.rightLeg, last, ENTRY_SIZE * 3, timePassed, differentFrame, speed, deltaTick);
}
}
if (entity == mc.cameraEntity && !data.isBodyRotationDisabled()) {
if (entity == mc.cameraEntity && !data.isDisableBodyRotation()) {
// For now located here due to smoothing logic being here.
if (NEABaseMod.config.rotationLock == RotationLock.SMOOTH && entity.getVehicle() == null) {

if ((NEABaseMod.config.rotationLock == RotationLock.SMOOTH
|| (NEABaseMod.config.rotationLock == RotationLock.NONE && data.isRotateBodyToHead()))
&& entity.getVehicle() == null) {
interpolateYawBodyHead(entity, last, ENTRY_SIZE * 4, timePassed, differentFrame, 0.5f);
}
if (NEABaseMod.config.rotationLock == RotationLock.FIXED && entity.getVehicle() == null
}else if (NEABaseMod.config.rotationLock == RotationLock.FIXED && entity.getVehicle() == null
&& differentFrame) {
entity.yBodyRot = entity.yHeadRot;
entity.yBodyRotO = entity.yHeadRotO;
} else {
last[ENTRY_SIZE * 4] = entity.yBodyRot;
last[ENTRY_SIZE * 4 + 1] = entity.yBodyRotO;
}
}
data.setUpdated(tickId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class LivingEntityMixin {
protected void tickHeadTurn(float f, float g, CallbackInfoReturnable<Float> info) {
if (this instanceof PlayerData) {
PlayerData data = (PlayerData) this;
if (data.isBodyRotationDisabled()) {
data.disableBodyRotation(false);
if (data.isDisableBodyRotation()) {
data.setDisableBodyRotation(false);
info.setReturnValue(g);
info.cancel();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import dev.tr7zw.notenoughanimations.logic.PlayerTransformer;
import dev.tr7zw.notenoughanimations.renderlayer.SwordRenderLayer;
import dev.tr7zw.notenoughanimations.versionless.animations.DataHolder;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
Expand All @@ -21,10 +23,19 @@
public class PlayerEntityMixin implements PlayerData {

private int armsUpdated = 0;
@Getter
@Setter
private float[] lastRotations = new float[PlayerTransformer.ENTRY_AMOUNT * PlayerTransformer.ENTRY_SIZE];
@Getter
@Setter
private ItemStack sideSword = ItemStack.EMPTY;
private ItemStack[] lastHeldItems = new ItemStack[2];
@Getter
@Setter
private boolean disableBodyRotation = false;
@Getter
@Setter
private boolean rotateBodyToHead = false;
private int itemSwapAnimationTimer = 0;
private int lastAnimationSwapTick = -1;
private Pose poseOverwrite = null;
Expand All @@ -33,6 +44,7 @@ public class PlayerEntityMixin implements PlayerData {
@Inject(method = "tick", at = @At("RETURN"))
public void tick(CallbackInfo info) {
updateRenderLayerItems();
setRotateBodyToHead(false);
}

@Override
Expand All @@ -45,35 +57,10 @@ public void setUpdated(int frameId) {
armsUpdated = frameId;
}

@Override
public float[] getLastRotations() {
return lastRotations;
}

@Override
public ItemStack getSideSword() {
return sideSword;
}

@Override
public void setSideSword(ItemStack item) {
this.sideSword = item;
}

private void updateRenderLayerItems() {
SwordRenderLayer.update((Player) (Object) this);
}

@Override
public void disableBodyRotation(boolean val) {
disableBodyRotation = val;
}

@Override
public boolean isBodyRotationDisabled() {
return disableBodyRotation;
}

@Override
public ItemStack[] getLastHeldItems() {
return lastHeldItems;
Expand Down

0 comments on commit 9618a17

Please sign in to comment.