| 1 | package net.minecraft.client.renderer.item.properties.numeric;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.GlobalPos;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.util.StringRepresentable;
|
| 14 | import net.minecraft.world.entity.ItemOwner;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class CompassAngleState extends NeedleDirectionHelper {
|
| 22 | public static final MapCodec<CompassAngleState> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 23 | i -> i.group(
|
| 24 | Codec.BOOL.optionalFieldOf("wobble", true).forGetter(NeedleDirectionHelper::wobble),
|
| 25 | CompassAngleState.CompassTarget.CODEC.fieldOf("target").forGetter(CompassAngleState::target)
|
| 26 | )
|
| 27 | .apply(i, CompassAngleState::new)
|
| 28 | );
|
| 29 | private final NeedleDirectionHelper.Wobbler wobbler;
|
| 30 | private final NeedleDirectionHelper.Wobbler noTargetWobbler;
|
| 31 | private final CompassAngleState.CompassTarget compassTarget;
|
| 32 | private final RandomSource random = RandomSource.create();
|
| 33 |
|
| 34 | public CompassAngleState(final boolean wobble, final CompassAngleState.CompassTarget compassTarget) {
|
| 35 | super(wobble);
|
| 36 | this.wobbler = this.newWobbler(0.8F);
|
| 37 | this.noTargetWobbler = this.newWobbler(0.8F);
|
| 38 | this.compassTarget = compassTarget;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected float calculate(final ItemStack itemStack, final ClientLevel level, final int seed, final ItemOwner owner) {
|
| 43 | GlobalPos compassTargetPos = this.compassTarget.get(level, itemStack, owner);
|
| 44 | long gameTime = level.getGameTime();
|
| 45 | return !isValidCompassTargetPos(owner, compassTargetPos)
|
| 46 | ? this.getRandomlySpinningRotation(seed, gameTime)
|
| 47 | : this.getRotationTowardsCompassTarget(owner, gameTime, compassTargetPos.pos());
|
| 48 | }
|
| 49 |
|
| 50 | private float getRandomlySpinningRotation(final int seed, final long gameTime) {
|
| 51 | if (this.noTargetWobbler.shouldUpdate(gameTime)) {
|
| 52 | this.noTargetWobbler.update(gameTime, this.random.nextFloat());
|
| 53 | }
|
| 54 |
|
| 55 | float targetRotation = this.noTargetWobbler.rotation() + hash(seed) / 2.1474836E9F;
|
| 56 | return Mth.positiveModulo(targetRotation, 1.0F);
|
| 57 | }
|
| 58 |
|
| 59 | private float getRotationTowardsCompassTarget(final ItemOwner owner, final long gameTime, final BlockPos compassTargetPos) {
|
| 60 | float angleToTarget = (float)getAngleFromEntityToPos(owner, compassTargetPos);
|
| 61 | float ownerYRotation = getWrappedVisualRotationY(owner);
|
| 62 | float targetRotation;
|
| 63 | if (owner.asLivingEntity() instanceof Player player && player.isLocalPlayer() && player.level().tickRateManager().runsNormally()) {
|
| 64 | if (this.wobbler.shouldUpdate(gameTime)) {
|
| 65 | this.wobbler.update(gameTime, 0.5F - (ownerYRotation - 0.25F));
|
| 66 | }
|
| 67 |
|
| 68 | targetRotation = angleToTarget + this.wobbler.rotation();
|
| 69 | } else {
|
| 70 | targetRotation = 0.5F - (ownerYRotation - 0.25F - angleToTarget);
|
| 71 | }
|
| 72 |
|
| 73 | return Mth.positiveModulo(targetRotation, 1.0F);
|
| 74 | }
|
| 75 |
|
| 76 | private static boolean isValidCompassTargetPos(final ItemOwner owner, final @Nullable GlobalPos positionToPointTo) {
|
| 77 | return positionToPointTo != null
|
| 78 | && positionToPointTo.dimension() == owner.level().dimension()
|
| 79 | && !(positionToPointTo.pos().distToCenterSqr(owner.position()) < 1.0E-5F);
|
| 80 | }
|
| 81 |
|
| 82 | private static double getAngleFromEntityToPos(final ItemOwner owner, final BlockPos position) {
|
| 83 | Vec3 target = Vec3.atCenterOf(position);
|
| 84 | Vec3 ownerPosition = owner.position();
|
| 85 | return Math.atan2(target.z() - ownerPosition.z(), target.x() - ownerPosition.x()) / (float) (Math.PI * 2);
|
| 86 | }
|
| 87 |
|
| 88 | private static float getWrappedVisualRotationY(final ItemOwner owner) {
|
| 89 | return Mth.positiveModulo(owner.getVisualRotationYInDegrees() / 360.0F, 1.0F);
|
| 90 | }
|
| 91 |
|
| 92 | private static int hash(final int input) {
|
| 93 | return input * 1327217883;
|
| 94 | }
|
| 95 |
|
| 96 | protected CompassAngleState.CompassTarget target() {
|
| 97 | return this.compassTarget;
|
| 98 | }
|
| 99 |
|
| 100 | @Environment(EnvType.CLIENT)
|
| 101 | public enum CompassTarget implements StringRepresentable {
|
| 102 | NONE("NONE", 0, "none"),
|
| 103 | LODESTONE("LODESTONE", 1, "lodestone"),
|
| 104 | SPAWN("SPAWN", 2, "spawn"),
|
| 105 | RECOVERY("RECOVERY", 3, "recovery");
|
| 106 |
|
| 107 | public static final Codec<CompassAngleState.CompassTarget> CODEC = StringRepresentable.fromEnum(CompassAngleState.CompassTarget::values);
|
| 108 | private final String name;
|
| 109 |
|
| 110 | CompassTarget(final String name) {
|
| 111 | this.name = name;
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public String getSerializedName() {
|
| 116 | return this.name;
|
| 117 | }
|
| 118 |
|
| 119 | public abstract @Nullable GlobalPos get(final ClientLevel level, final ItemStack itemStack, final @Nullable ItemOwner entity);
|
| 120 | }
|
| 121 | }
|
| 122 |
|