Skip to content

Commit

Permalink
Fix triggerHapticPulse (closes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed May 22, 2023
1 parent c980b0a commit 866a7c4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
15 changes: 14 additions & 1 deletion common/src/main/java/net/blf02/vrapi/client/VRDataGrabber.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class VRDataGrabber {

// vr field in net.minecraft.client.Minecraft
public static Field Minecraft_vr; // Type MCVR
public static Object Minecraft_vr_Instance; // Type MCVR
// Not guaranteed to be non-null. Must run initMinecraftVRInstanceIfNeeded before using
public static Object Minecraft_vr_Instance; // Type MCVR (if null, needs to initialize at call time)
public static Object vrHolder;

// VRPlayer from Vivecraft
Expand Down Expand Up @@ -91,6 +92,7 @@ public static void init() {
vrHolder = cdhInstance;
}

// Not guaranteed to be non-null. Must run initMinecraftVRInstanceIfNeeded before using
Minecraft_vr_Instance = Minecraft_vr.get(vrHolder);

VRSettings_seated = getField(ReflectionConstants.VRSettings, "seated");
Expand Down Expand Up @@ -216,6 +218,17 @@ public static void isSelf(Player player) {
}
}

public static void initMinecraftVRInstanceIfNeeded() {
if (Minecraft_vr_Instance == null) {
try {
Minecraft_vr_Instance = Minecraft_vr.get(vrHolder);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

}
}

public static Field getField(Class<?> clazz, String field) {
try {
return clazz.getField(field);
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/net/blf02/vrapi/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Constants {

// Version {major, minor, patch}
public static final int[] version = new int[]{3, 0, 2};
public static final int[] version = new int[]{3, 0, 3};

// Debugging
public static final boolean doDebugging = false;
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/net/blf02/vrapi/common/VRAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public void triggerHapticPulse(int controllerNum, float durationSeconds, float f
@Nullable ServerPlayer player) {
if (Platform.getEnvironment() == Env.CLIENT) {
try {
VRDataGrabber.initMinecraftVRInstanceIfNeeded();
VRDataGrabber.MCVR_triggerHapticPulse.invoke(VRDataGrabber.Minecraft_vr_Instance,
ReflectionConstants.ControllerType_ENUMS[controllerNum], durationSeconds, frequency, amplitude, delaySeconds);
} catch (IllegalAccessException | InvocationTargetException e) {
Expand Down
18 changes: 3 additions & 15 deletions common/src/main/java/net/blf02/vrapi/debug/DebugSubscriber.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package net.blf02.vrapi.debug;

import net.blf02.vrapi.api.data.IVRData;
import net.blf02.vrapi.common.VRAPI;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.entity.player.Player;

public class DebugSubscriber {

public static void onPlayerTick(Player player) {
if (VRAPI.VRAPIInstance.playerInVR(player)) {
IVRData hmd = VRAPI.VRAPIInstance.getVRPlayer(player).getHMD();
player.level.addParticle(ParticleTypes.SNOWFLAKE,
hmd.position().x, hmd.position().y, hmd.position().z,
0.01, 0.01, 0.01);
IVRData left = VRAPI.VRAPIInstance.getVRPlayer(player).getController1();
player.level.addParticle(ParticleTypes.ANGRY_VILLAGER,
left.position().x, left.position().y, left.position().z,
0.01, 0.01, 0.01);
IVRData right = VRAPI.VRAPIInstance.getVRPlayer(player).getController0();
player.level.addParticle(ParticleTypes.FALLING_WATER,
right.position().x, right.position().y, right.position().z,
0.01, 0.01, 0.01);
if (VRAPI.VRAPIInstance.playerInVR(player) && player.level.isClientSide) {
VRAPI.VRAPIInstance.triggerHapticPulse(1, 0.05f,
null);
}
}
}
4 changes: 3 additions & 1 deletion forge/src/main/java/net/blf02/forge/APIProviderInit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.blf02.forge;

import net.blf02.vrapi.VRAPIMod;
import net.blf02.vrapi.common.VRAPI;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.forgespi.language.ModFileScanData;
Expand All @@ -8,6 +9,7 @@
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -43,7 +45,7 @@ protected static VRAPIPluginProvider initPlugin(final Type type) {
final Constructor<? extends VRAPIPluginProvider> constructor = ((Class<? extends VRAPIPluginProvider>) clazz).getConstructor();
return constructor.newInstance();
} catch (final ReflectiveOperationException | ClassCastException e) {
System.out.println("Could not initialize plugin " + type.getClassName());
VRAPIMod.LOGGER.log(Level.WARNING, "Could not initialize plugin " + type.getClassName());
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.19.2
enabled_platforms=quilt,fabric,forge

archives_base_name=vrapi
mod_version=3.0.2
mod_version=3.0.3
maven_group=net.blf02.vrapi

architectury_version=6.2.43
Expand Down

0 comments on commit 866a7c4

Please sign in to comment.