Skip to content

Commit

Permalink
Merge VRBodyPart to One Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed Jan 17, 2025
1 parent b0711ba commit 6bb5499
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 97 deletions.
17 changes: 7 additions & 10 deletions common/src/main/java/org/vivecraft/api/data/VRBodyPart.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.vivecraft.api.data;

/**
* The device tracking a specific body part.
*/
public enum VRBodyPart {
HMD,
MAIN_HAND,
OFF_HAND,
RIGHT_FOOT,
Expand All @@ -13,10 +9,11 @@ public enum VRBodyPart {
RIGHT_KNEE,
LEFT_KNEE,
RIGHT_ELBOW,
LEFT_ELBOW;
LEFT_ELBOW,
HMD;

/**
* @return The opposite body part to this one, or the same body part if it has no opposite.
* @return the opposite limb
*/
public VRBodyPart opposite() {
return switch (this) {
Expand All @@ -34,14 +31,14 @@ public VRBodyPart opposite() {

/**
* Whether this body part type is available in the provided full-body tracking mode.
* @param mode The full-body tracking mode to check.
* @param fbtMode The full-body tracking mode to check.
* @return Whether this body part has available data in the provided mode.
*/
public boolean availableInMode(FBTMode mode) {
public boolean availableInMode(FBTMode fbtMode) {
return switch (this) {
case HMD, MAIN_HAND, OFF_HAND -> true;
case RIGHT_FOOT, LEFT_FOOT, WAIST -> mode != FBTMode.ARMS_ONLY;
case RIGHT_KNEE, LEFT_KNEE, RIGHT_ELBOW, LEFT_ELBOW -> mode == FBTMode.WITH_JOINTS;
case RIGHT_FOOT, LEFT_FOOT, WAIST -> fbtMode != FBTMode.ARMS_ONLY;
case RIGHT_KNEE, LEFT_KNEE, RIGHT_ELBOW, LEFT_ELBOW -> fbtMode == FBTMode.WITH_JOINTS;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.vivecraft.common.CommonDataHolder;
import org.vivecraft.common.VRServerPerms;
import org.vivecraft.common.network.CommonNetworkHelper;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.common.network.VrPlayerState;
import org.vivecraft.common.network.packet.c2s.*;
import org.vivecraft.common.network.packet.s2c.*;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class ClientNetworking {
private static float CAPTURED_YAW;
private static float CAPTURED_PITCH;
private static boolean OVERRIDE_ACTIVE;
public static BodyPart LAST_SENT_BODY_PART = BodyPart.MAIN_HAND;
public static VRBodyPart LAST_SENT_BODY_PART = VRBodyPart.MAIN_HAND;

public static boolean NEEDS_RESET = true;

Expand Down Expand Up @@ -193,11 +193,11 @@ public static int getTeleportHorizLimit() {

public static void sendActiveHand(InteractionHand hand) {
if (SERVER_WANTS_DATA) {
sendActiveBodyPart(hand == InteractionHand.MAIN_HAND ? BodyPart.MAIN_HAND : BodyPart.OFF_HAND);
sendActiveBodyPart(hand == InteractionHand.MAIN_HAND ? VRBodyPart.MAIN_HAND : VRBodyPart.OFF_HAND);
}
}

public static void sendActiveBodyPart(BodyPart bodyPart) {
public static void sendActiveBodyPart(VRBodyPart bodyPart) {
if (SERVER_WANTS_DATA) {
// only send if the hand is different from last time, don't need to spam packets
if (bodyPart != LAST_SENT_BODY_PART) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.vivecraft.client_vr.Vector3fHistory;
import org.vivecraft.client_vr.provider.MCVR;
import org.vivecraft.api.data.FBTMode;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.common.utils.MathUtils;
import org.vivecraft.data.BlockTags;
import org.vivecraft.data.ItemTags;
Expand Down Expand Up @@ -235,7 +235,7 @@ public void doProcess(LocalPlayer player) {
// this.mc.physicalGuiManager.preClickAction();

if (!EpicFightHelper.isLoaded() || !EpicFightHelper.attack()) {
ClientNetworking.sendActiveBodyPart(BodyPart.values()[i]);
ClientNetworking.sendActiveBodyPart(VRBodyPart.values()[i]);
// only attack if epic fight didn't trigger
this.mc.gameMode.attack(player, entity);
} else {
Expand Down Expand Up @@ -349,7 +349,7 @@ else if (blockstate.getBlock() instanceof NoteBlock ||
// this.mc.physicalGuiManager.preClickAction();

// send hitting hand
ClientNetworking.sendActiveBodyPart(BodyPart.values()[i]);
ClientNetworking.sendActiveBodyPart(VRBodyPart.values()[i]);

// this will either destroy the block if in creative or set it as the current block.
// does nothing in survival if you are already hitting this block.
Expand Down Expand Up @@ -391,7 +391,7 @@ else if (blockstate.getBlock() instanceof NoteBlock ||
}

// reset hitting hand
ClientNetworking.sendActiveBodyPart(BodyPart.MAIN_HAND);
ClientNetworking.sendActiveBodyPart(VRBodyPart.MAIN_HAND);

this.mc.getProfiler().pop();
}
Expand Down
44 changes: 0 additions & 44 deletions common/src/main/java/org/vivecraft/common/network/BodyPart.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.world.phys.Vec3;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.api.data.FBTMode;
import org.vivecraft.api.data.VRBodyPartData;
import org.vivecraft.client.network.ClientNetworking;
Expand Down Expand Up @@ -196,7 +197,7 @@ public static VrPlayerState deserialize(FriendlyByteBuf buffer, int bytesAfter)
* @return Pose of the {@code bodyPart}, or {@code null} if the body part is not valid for the current FBT mode
*/
@Nullable
public Pose getBodyPartPose(BodyPart bodyPart) {
public Pose getBodyPartPose(VRBodyPart bodyPart) {
return switch(bodyPart) {
case MAIN_HAND -> this.mainHand;
case OFF_HAND -> this.offHand;
Expand All @@ -207,6 +208,7 @@ public Pose getBodyPartPose(BodyPart bodyPart) {
case LEFT_KNEE -> this.leftKnee;
case RIGHT_KNEE -> this.rightKnee;
case WAIST -> this.waist;
case HMD -> this.hmd;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.network.FriendlyByteBuf;
import org.vivecraft.client.network.ClientNetworking;
import org.vivecraft.common.network.CommonNetworkHelper;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.api.data.FBTMode;
import org.vivecraft.common.network.packet.PayloadIdentifier;

Expand All @@ -12,7 +12,7 @@
*
* @param bodyPart the active BodyPart
*/
public record ActiveBodyPartPayloadC2S(BodyPart bodyPart) implements VivecraftPayloadC2S {
public record ActiveBodyPartPayloadC2S(VRBodyPart bodyPart) implements VivecraftPayloadC2S {

@Override
public PayloadIdentifier payloadId() {
Expand All @@ -23,16 +23,16 @@ public PayloadIdentifier payloadId() {
public void write(FriendlyByteBuf buffer) {
buffer.writeByte(payloadId().ordinal());
if (ClientNetworking.USED_NETWORK_VERSION < CommonNetworkHelper.NETWORK_VERSION_DUAL_WIELDING &&
!this.bodyPart.isValid(FBTMode.ARMS_ONLY))
!this.bodyPart.availableInMode(FBTMode.ARMS_ONLY))
{
// old plugins only support main and offhand
buffer.writeByte(BodyPart.MAIN_HAND.ordinal());
buffer.writeByte(VRBodyPart.MAIN_HAND.ordinal());
} else {
buffer.writeByte(this.bodyPart.ordinal());
}
}

public static ActiveBodyPartPayloadC2S read(FriendlyByteBuf buffer) {
return new ActiveBodyPartPayloadC2S(BodyPart.values()[buffer.readByte()]);
return new ActiveBodyPartPayloadC2S(VRBodyPart.values()[buffer.readByte()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.common.utils.MathUtils;
import org.vivecraft.server.ServerVRPlayers;
import org.vivecraft.server.ServerVivePlayer;
Expand All @@ -24,7 +24,7 @@ public class PlayerPredicateMixin {
if (player.getUseItem().is(Items.SPYGLASS) && ServerVRPlayers.isVRPlayer(player)) {
vivePlayer.set(ServerVRPlayers.getVivePlayer(player));
if (!vivePlayer.get().isSeated()) {
return vivePlayer.get().getBodyPartPos(BodyPart.values()[player.getUsedItemHand().ordinal()]);
return vivePlayer.get().getBodyPartPos(VRBodyPart.values()[player.getUsedItemHand().ordinal()]);
}
}
return original.call(player);
Expand All @@ -37,7 +37,7 @@ public class PlayerPredicateMixin {
{
if (vivePlayer.get() != null && !vivePlayer.get().isSeated()) {
return vivePlayer.get()
.getBodyPartVectorCustom(BodyPart.values()[player.getUsedItemHand().ordinal()], MathUtils.DOWN);
.getBodyPartVectorCustom(VRBodyPart.values()[player.getUsedItemHand().ordinal()], MathUtils.DOWN);
} else {
return original.call(player, partialTick);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.vivecraft.client.network.ClientNetworking;
import org.vivecraft.client_vr.ClientDataHolderVR;
import org.vivecraft.client_vr.VRState;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;

/**
* we override the players look direction so the server handles any interactions as if the player looked at the interacted block
Expand Down Expand Up @@ -66,12 +66,12 @@ public class MultiPlayerGameModeVRMixin {
if (VRState.VR_RUNNING && ClientNetworking.SERVER_ALLOWS_DUAL_WIELDING) {
// check if main or offhand items match the started item, we want to limit abuse of this,
// but still make both items work
BodyPart lastBodyPart = ClientNetworking.LAST_SENT_BODY_PART;
VRBodyPart lastBodyPart = ClientNetworking.LAST_SENT_BODY_PART;

ClientNetworking.LAST_SENT_BODY_PART = BodyPart.MAIN_HAND;
ClientNetworking.LAST_SENT_BODY_PART = VRBodyPart.MAIN_HAND;
boolean sameItem = original.call(pos);

ClientNetworking.LAST_SENT_BODY_PART = BodyPart.OFF_HAND;
ClientNetworking.LAST_SENT_BODY_PART = VRBodyPart.OFF_HAND;
sameItem |= original.call(pos);

ClientNetworking.LAST_SENT_BODY_PART = lastBodyPart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.vivecraft.client.network.ClientNetworking;
import org.vivecraft.client_vr.VRState;
import org.vivecraft.common.network.CommonNetworkHelper;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.server.ServerVRPlayers;
import org.vivecraft.server.ServerVivePlayer;
import org.vivecraft.server.config.ServerConfig;
Expand All @@ -44,7 +44,7 @@ public class InventoryMixin {

@Unique
private ItemStack vivecraft$activeItem(ItemStack original) {
BodyPart bodyPart = null;
VRBodyPart bodyPart = null;
// server side
if (this.player instanceof ServerPlayer serverPlayer && ServerConfig.DUAL_WIELDING.get()) {
if (ServerVRPlayers.isVRPlayer(serverPlayer)) {
Expand All @@ -61,9 +61,9 @@ else if (this.player.isLocalPlayer() && VRState.VR_RUNNING && ClientNetworking.S
}

if (bodyPart != null) {
if (bodyPart == BodyPart.OFF_HAND) {
if (bodyPart == VRBodyPart.OFF_HAND) {
return this.offhand.get(0);
} else if (bodyPart != BodyPart.MAIN_HAND) {
} else if (bodyPart != VRBodyPart.MAIN_HAND) {
// feet
return ItemStack.EMPTY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.vivecraft.common.CommonDataHolder;
import org.vivecraft.common.network.CommonNetworkHelper;
import org.vivecraft.api.data.FBTMode;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.common.network.VrPlayerState;
import org.vivecraft.common.network.packet.PayloadIdentifier;
import org.vivecraft.common.network.packet.c2s.*;
Expand Down Expand Up @@ -194,7 +194,7 @@ public static void handlePacket(
player.connection.aboveGroundTickCount = 0;
}
case ACTIVEHAND -> {
BodyPart newBodyPart = vivePlayer.isSeated() ? BodyPart.MAIN_HAND : ((ActiveBodyPartPayloadC2S) c2sPayload).bodyPart();
VRBodyPart newBodyPart = vivePlayer.isSeated() ? VRBodyPart.MAIN_HAND : ((ActiveBodyPartPayloadC2S) c2sPayload).bodyPart();
if (vivePlayer.activeBodyPart != newBodyPart) {
// handle equipment changes
ItemStack oldItem = player.getItemBySlot(EquipmentSlot.MAINHAND);
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/java/org/vivecraft/server/ServerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.joml.Quaternionfc;
import org.joml.Vector3f;
import org.vivecraft.client.utils.UpdateChecker;
import org.vivecraft.common.network.BodyPart;
import org.vivecraft.api.data.VRBodyPart;
import org.vivecraft.common.utils.MathUtils;
import org.vivecraft.server.config.ConfigBuilder;
import org.vivecraft.server.config.ServerConfig;
Expand Down Expand Up @@ -338,8 +338,8 @@ public static void registerCommands(
*/
public static void debugParticleAxes(ServerVivePlayer vivePlayer) {
if (vivePlayer.isVR() && vivePlayer.vrPlayerState != null) {
for(BodyPart bodyPart : BodyPart.values()) {
if (bodyPart.isValid(vivePlayer.vrPlayerState.fbtMode())) {
for(VRBodyPart bodyPart : VRBodyPart.values()) {
if (bodyPart.availableInMode(vivePlayer.vrPlayerState.fbtMode()) && bodyPart != VRBodyPart.HMD) {
debugParticleAxes(
vivePlayer.player.serverLevel(),
vivePlayer.getBodyPartPos(bodyPart),
Expand Down
Loading

0 comments on commit 6bb5499

Please sign in to comment.