Skip to content

Commit

Permalink
Fixed bugs will cause NaN and crash
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisQuepierts committed Aug 6, 2024
1 parent b11d649 commit c011463
Show file tree
Hide file tree
Showing 62 changed files with 711 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.quepierts.simpleanimator.api.animation.Animator;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
public interface IAnimateHandler {
boolean simpleanimator$isRunning();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import net.quepierts.simpleanimator.core.animation.RequestHolder;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
public interface IInteractHandler {
boolean simpleanimator$invite(@NotNull Player target, @NotNull ResourceLocation interact, boolean update);
boolean simpleanimator$accept(@NotNull Player requester, boolean update);
boolean simpleanimator$accept(@NotNull Player requester, boolean update, boolean forced);

void simpleanimator$cancel(boolean update);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import java.util.Optional;

public class Animation {
private static final String KEY_REQUEST = "request";
private static final String KEY_REQUEST = "invite";
private static final String KEY_WAITING = "waiting";
private static final String KEY_CANCEL = "cancel";
private static final String KEY_ENTER = "enter";
private static final String KEY_LOOP = "main";
private static final String KEY_EXIT = "exit";
Expand Down Expand Up @@ -58,9 +59,10 @@ public static Animation[] serialize(JsonObject json) {

if (!waiting.repeatable())
throw new RuntimeException("\"waiting\" should be looped!");
final AnimationSection cancel = AnimationSection.fromJsonObject(object.getAsJsonObject(KEY_CANCEL), Type.INVITE);

animations[0] = new Animation(
request, waiting, null,
request, waiting, cancel,
override, movable, true, Type.INVITE
);
animations[1] = new Animation(
Expand Down Expand Up @@ -108,7 +110,7 @@ private Animation(
}

private static boolean isInteractiveAnimation(JsonObject json) {
return json.has("request");
return json.has(KEY_REQUEST);
}

private static boolean getBoolean(JsonObject json, String key, boolean def) {
Expand All @@ -124,11 +126,12 @@ public AnimationSection get(AnimationState state) {
}

public float getFadeIn(ClientAnimator animator) {
AnimationState nextState = animator.getNextState();
AnimationSection animation = get(animator.getCurState());

if (nextState == AnimationState.IDLE) {
if (animator.getNextState() == AnimationState.IDLE) {
return animation.getFadeOut();
} else if (animator.getCurState() == AnimationState.IDLE) {
return get(animator.getNextState()).getFadeIn();
} else {
return animation.getFadeIn();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.quepierts.simpleanimator.api.animation;

import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -9,14 +8,16 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.Mth;
import net.minecraft.util.StringUtil;
import net.quepierts.simpleanimator.core.animation.AnimationState;
import net.quepierts.simpleanimator.core.animation.LerpMode;
import net.quepierts.simpleanimator.core.animation.ModelBone;
import net.quepierts.simpleanimator.core.client.ClientAnimator;
import net.quepierts.simpleanimator.core.client.state.IAnimationState;
import org.joml.Vector3f;

import java.util.*;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class AnimationSection {
public static final Vector3f ZERO = new Vector3f(0);
Expand Down Expand Up @@ -266,7 +267,7 @@ public void update(ModelBone bone, ModelPart part, ClientAnimator animator, floa
final KeyFrame[] rotFrames = data.rotation();

if (animator.isTransferring()) {
final float time = Mth.clamp(animator.getTimer() / fadeIn, 0.0f, 1.0f);
final float time = fadeIn == 0 ? 1.0f : Mth.clamp(animator.getTimer() / fadeIn, 0.0f, 1.0f);

if (posFrames.length != 0) {
cache.position().set(linearLerp(
Expand Down Expand Up @@ -301,39 +302,6 @@ public void update(ModelBone bone, ModelPart part, ClientAnimator animator, floa
}
}

public void update(ModelBone bone, PartPose pose, ClientAnimator.Cache cache, AnimationState state, float time) {
BoneData data = this.keyFrames.get(bone);
if (data == null) {
cache.position().set(pose.x, pose.y, pose.z);
cache.rotation().set(pose.xRot, pose.yRot, pose.zRot);
return;
}

Vector3f position = new Vector3f(pose.x, pose.y, pose.z);
Vector3f rotation = new Vector3f(pose.xRot, pose.yRot, pose.zRot);
float localT;
KeyFrame[] posFrames = data.position();
KeyFrame[] rotFrames = data.rotation();
switch (state) {
case ENTER:
localT = time / fadeIn;
if (posFrames.length != 0)
cache.position().set(linearLerp(position, posFrames[0].vec3(), localT));
if (rotFrames.length != 0)
cache.rotation().set(linearLerp(rotation, rotFrames[0].vec3(), localT));
break;
case LOOP:
interpolate(posFrames, cache.position(), time);
interpolate(rotFrames, cache.rotation(), time);
break;
case EXIT:
localT = time / fadeOut;
cache.position().set(linearLerp(cache.position(), position, localT));
cache.rotation().set(linearLerp(cache.rotation(), rotation, localT));
break;
}
}

public static void interpolate(KeyFrame[] keyframes, Vector3f out, float time) {
KeyFrame kf0 = null, kf1 = null, kf2 = null, kf3 = null;
for (int i = 0; i < keyframes.length - 1; i++) {
Expand Down Expand Up @@ -418,23 +386,22 @@ public static void toNetwork(FriendlyByteBuf byteBuf, AnimationSection animation
byteBuf.writeFloat(animation.length);
byteBuf.writeFloat(animation.fadeIn);
byteBuf.writeFloat(animation.fadeOut);
byteBuf.writeMap(
animation.keyFrames,
FriendlyByteBuf::writeEnum,
BoneData::toNetwork
);

for (ModelBone value : ModelBone.values()) {
byteBuf.writeOptional(Optional.ofNullable(animation.keyFrames.get(value)), BoneData::toNetwork);
}
}

public static AnimationSection fromNetwork(FriendlyByteBuf byteBuf) {
final boolean repeat = byteBuf.readBoolean();
final float length = byteBuf.readFloat();
final float fadeIn = byteBuf.readFloat();
final float fadeOut = byteBuf.readFloat();
final EnumMap<ModelBone, BoneData> map = byteBuf.readMap(
i -> Maps.newEnumMap(ModelBone.class),
buf -> buf.readEnum(ModelBone.class),
BoneData::fromNetwork
);
final EnumMap<ModelBone, BoneData> map = new EnumMap<>(ModelBone.class);
for (ModelBone value : ModelBone.values()) {
Optional<BoneData> optional = byteBuf.readOptional(BoneData::fromNetwork);
optional.ifPresent(boneData -> map.put(value, boneData));
}
return new AnimationSection(repeat, length, fadeIn, fadeOut, map);
}

Expand All @@ -455,17 +422,30 @@ public static KeyFrame fromNetwork(FriendlyByteBuf byteBuf) {

public record BoneData(KeyFrame[] rotation, KeyFrame[] position) {
public static void toNetwork(FriendlyByteBuf byteBuf, BoneData data) {
byteBuf.writeCollection(Arrays.asList(data.rotation), KeyFrame::toNetwork);
byteBuf.writeCollection(Arrays.asList(data.position), KeyFrame::toNetwork);
writeArray(byteBuf, data.rotation);
writeArray(byteBuf, data.position);
}

public static BoneData fromNetwork(FriendlyByteBuf byteBuf) {
List<KeyFrame> rotation = byteBuf.readList(KeyFrame::fromNetwork);
List<KeyFrame> position = byteBuf.readList(KeyFrame::fromNetwork);
return new BoneData(
rotation.toArray(new KeyFrame[0]),
position.toArray(new KeyFrame[0])
);
KeyFrame[] rotate = readArray(byteBuf);
KeyFrame[] position = readArray(byteBuf);
return new BoneData(rotate, position);
}

private static void writeArray(FriendlyByteBuf byteBuf, KeyFrame[] arr) {
byteBuf.writeVarInt(arr.length);
for (KeyFrame keyFrame : arr) {
KeyFrame.toNetwork(byteBuf, keyFrame);
}
}

private static KeyFrame[] readArray(FriendlyByteBuf byteBuf) {
int i = byteBuf.readVarInt();
KeyFrame[] arr = new KeyFrame[i];
for (int i1 = 0; i1 < i; i1++) {
arr[i1] = KeyFrame.fromNetwork(byteBuf);
}
return arr;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public Animator(UUID uuid) {
}

public void sync(AnimatorDataPacket packet) {
SimpleAnimator.LOGGER.debug("Update: {} {} ,", this.getClass().getSimpleName(), packet);
this.animationLocation = packet.animationLocation;
this.animation = SimpleAnimator.getProxy().getAnimationManager().getAnimation(packet.animationLocation);
this.curState = packet.curState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.quepierts.simpleanimator.core;

import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;

public class PlayerUtils {
public static boolean isRiding(Player player) {
public static boolean isRiding(Entity player) {
return player != null && player.getVehicle() != null;
}

Expand All @@ -25,18 +26,23 @@ public static float normalizeRadians(double rad) {
return (float) Math.toRadians(normalizeAngle((float) Math.toDegrees(rad)));
}

public static Vec3 getRelativePosition(Player player, double forward, double left) {
public static Vec3 getRelativePositionWorldSpace(Player player, double forward, double left) {
Vec2 vec2 = new Vec2(0, player.yBodyRot);
Vec3 vec3 = player.position();
float f = Mth.cos((vec2.y + 90.0F) * ((float)Math.PI / 180F));
float f1 = Mth.sin((vec2.y + 90.0F) * ((float)Math.PI / 180F));
Vec3 vec31 = new Vec3(f, 0, f1);
Vec3 vec33 = new Vec3(-f1, 0, f);
double d0 = vec31.x * forward + vec33.x * left;
double d2 = vec31.z * forward + vec33.z * left;
final float f = Mth.cos((vec2.y + 90.0F) * ((float)Math.PI / 180F));
final float f1 = Mth.sin((vec2.y + 90.0F) * ((float)Math.PI / 180F));
final double d0 = f * forward - f1 * left;
final double d2 = f1 * forward + f * left;
return new Vec3(vec3.x + d0, vec3.y, vec3.z + d2);
}

public static Vec3 getRelativePosition(Player player, double forward, double left) {
Vec2 vec2 = new Vec2(0, player.yBodyRot);
final float f = Mth.cos((vec2.y + 90.0F) * ((float)Math.PI / 180F));
final float f1 = Mth.sin((vec2.y + 90.0F) * ((float)Math.PI / 180F));
return new Vec3(f * forward - f1 * left, 0, f1 * forward + f * left);
}

public static double distanceSqr2D(Vec3 src, Vec3 dest) {
double x = dest.x - src.x;
double z = dest.z - src.z;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package net.quepierts.simpleanimator.core;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.logging.LogUtils;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.quepierts.simpleanimator.core.network.INetwork;
import net.quepierts.simpleanimator.core.proxy.ClientProxy;
import net.quepierts.simpleanimator.core.proxy.CommonProxy;
Expand All @@ -9,6 +13,7 @@
public abstract class SimpleAnimator {
public static final String MOD_ID = "simple_animator";
public static final Logger LOGGER = LogUtils.getLogger();
public static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

private static CommonProxy proxy;
private static INetwork network;
Expand All @@ -23,6 +28,7 @@ public static CommonProxy getProxy() {
return proxy;
}

@Environment(EnvType.CLIENT)
public static ClientProxy getClient() {
return (ClientProxy) proxy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.mojang.datafixers.util.Pair;
import com.mojang.logging.LogUtils;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.FileToIdConverter;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -108,7 +107,6 @@ private CompletableFuture<List<Pair<ResourceLocation, Animation[]>>> load(Resour
list.add(CompletableFuture.supplyAsync(() -> {
try (Reader reader = resource.openAsReader()) {
Animation[] animations = Animation.fromStream(reader);
LOGGER.warn("Loaded animation {} from {} in data pack {}", resourceLocation, location, resource.sourcePackId());
return Pair.of(resourceLocation, animations);
} catch (IOException e) {
LOGGER.warn("Couldn't read animation {} from {} in data pack {}", resourceLocation, location, resource.sourcePackId());
Expand Down Expand Up @@ -200,9 +198,6 @@ private static void load(
}

public void sync(ServerPlayer player) {
if (Minecraft.getInstance().player != null && Minecraft.getInstance().player.getUUID().equals(player.getUUID()))
return;

LOGGER.info("Send Animations to Client");
SimpleAnimator.getNetwork().sendToPlayer(new ClientUpdateAnimationPacket(this.animations), player);
SimpleAnimator.getNetwork().sendToPlayer(new ClientUpdateInteractionPacket(this.interactions), player);
Expand All @@ -211,20 +206,10 @@ public void sync(ServerPlayer player) {
public void sync(PlayerList list) {
ClientUpdateAnimationPacket animationPacket = new ClientUpdateAnimationPacket(this.animations);
ClientUpdateInteractionPacket interactionPacket = new ClientUpdateInteractionPacket(this.interactions);
if (Minecraft.getInstance().player != null) {
UUID uuid = Minecraft.getInstance().player.getUUID();
for (ServerPlayer player : list.getPlayers()) {
if (player.getUUID().equals(uuid))
continue;
SimpleAnimator.getNetwork().sendToPlayer(animationPacket, player);
SimpleAnimator.getNetwork().sendToPlayer(interactionPacket, player);
}

} else {
for (ServerPlayer player : list.getPlayers()) {
SimpleAnimator.getNetwork().sendToPlayer(animationPacket, player);
SimpleAnimator.getNetwork().sendToPlayer(interactionPacket, player);
}
for (ServerPlayer player : list.getPlayers()) {
SimpleAnimator.getNetwork().sendToPlayer(animationPacket, player);
SimpleAnimator.getNetwork().sendToPlayer(interactionPacket, player);
}
}

Expand Down
Loading

0 comments on commit c011463

Please sign in to comment.