Skip to content

Commit

Permalink
apply dimensional height offset similar to the fabric mod
Browse files Browse the repository at this point in the history
                    // Make people in other dimensions far away so that they're muted.
                    val yAxisAdjuster = (world.registryKey.value.stableHash % 2048) * config.clientDimensionYAxisAdjust
                    camPos[1] += yAxisAdjuster

/**
 * A stable hash function designed for world IDs.
 * Different clients should be able to run this on the same world ID and get the same result.
 *
 * Based on the `djb2` hash function: [Hash Functions](http://www.cse.yorku.ca/~oz/hash.html)
 */
val Identifier.stableHash: Int
    get() {
        var hash = 5381

        for (c in this.toString()) {
            hash += (hash shl 5) + c.code
        }

        return hash
    }

fixes #35
  • Loading branch information
zsawyer committed Jan 24, 2023
1 parent b2cfed9 commit 82114ce
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mod/src/main/java/zsawyer/mods/mumblelink/mumble/UpdateData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package zsawyer.mods.mumblelink.mumble;

import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import zsawyer.mods.mumblelink.MumbleLinkImpl;
import zsawyer.mods.mumblelink.api.IdentityManipulator;
Expand All @@ -36,6 +38,8 @@
* @author zsawyer
*/
public class UpdateData {
private static final int HEIGHT_INDEX = 1;


float[] fAvatarPosition = {0, 0, 0}; // [3]
float[] fAvatarFront = {0, 0, 0}; // [3]
Expand Down Expand Up @@ -135,6 +139,7 @@ public void set(Minecraft game) {
(float) game.player.getPosition(1f).y() * fAvatarPositionY,
(float) game.player.getPosition(1f).z() * fAvatarPositionZ
};
applyDimensionalOffset(game, fAvatarPosition);

// Unit vector pointing out of the avatar's eyes (here Front looks
// into scene).
Expand All @@ -159,6 +164,7 @@ public void set(Minecraft game) {
(float) game.player.getPosition(1f).y() * fCameraPositionY,
(float) game.player.getPosition(1f).z() * fCameraPositionZ
};
applyDimensionalOffset(game, fCameraPosition);

fCameraFront = new float[]{
(float) lookDirection.x * fCameraFrontX,
Expand Down Expand Up @@ -211,4 +217,27 @@ protected String generateContext(Minecraft game, int maxLength) {
private Vec3 getTopVec(Minecraft game) {
return game.player.getUpVector(1f);
}


/**
* Make people in other dimensions far away so that they're muted.
* <p>
* reimplementation of https://github.com/magneticflux-/fabric-mumblelink-mod/blob/12727324ae9ecfc9c6b0ab5b604e824d43cfffa1/src/main/kotlin/com/skaggsm/mumblelinkmod/client/ClientMumbleLinkMod.kt#L136
*
* @param game the source to get live data from
* @param originalPosition the original position to be offset
*/
public static void applyDimensionalOffset(Minecraft game, float[] originalPosition) {
ResourceKey<Level> dimension = game.player.level.dimension();
if (dimension == null) {
// silently ignoring because it would become too spammy
return;
}

int configuredOffset = MumbleLinkImpl.dimensionalHeight();
int hash = LinkAPIHelper.stableHash(dimension.toString());
float heightOffset = (hash % 2048) * configuredOffset;

originalPosition[HEIGHT_INDEX] += heightOffset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.PrimitiveIterator;

/**
* @author zsawyer
Expand Down Expand Up @@ -54,6 +55,28 @@ public static ByteBuffer parseToByteBuffer(int capacity, String value) {
return buffer;
}

/**
* A stable hash function designed for world IDs.
* Different clients should be able to run this on the same world ID and get the same result.
* <p>
* Based on the `djb2` hash function: [Hash Functions](http://www.cse.yorku.ca/~oz/hash.html)
* <p>
* reimplementation of https://github.com/magneticflux-/fabric-mumblelink-mod/blob/3866317c64f9b7f5b9b4f17e88cd51c0a717b993/src/main/kotlin/com/skaggsm/mumblelinkmod/client/Utils.kt#L21
*
* @param hashingTarget the strings whose character sequence should be hashed
* @return the stable hash built for the given input
*/
public static int stableHash(String hashingTarget) {
int hash = 5381;
PrimitiveIterator.OfInt characterIterator = hashingTarget.chars().iterator();

while (characterIterator.hasNext()) {
hash += (hash << 5) + characterIterator.nextInt();
}

return hash;
}

public synchronized LinkAPILibrary getLibraryInstance() {
return libraryInstance;
}
Expand Down

0 comments on commit 82114ce

Please sign in to comment.