From f72045438e13e63c90394e2d2cf38013cb61c994 Mon Sep 17 00:00:00 2001 From: lucas lelievre Date: Sat, 30 Apr 2022 21:37:02 +0200 Subject: [PATCH 1/3] Change getTrackerById --- src/main/java/dev/slimevr/VRServer.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/slimevr/VRServer.java b/src/main/java/dev/slimevr/VRServer.java index 48bd6f30a..a54366c59 100644 --- a/src/main/java/dev/slimevr/VRServer.java +++ b/src/main/java/dev/slimevr/VRServer.java @@ -309,9 +309,11 @@ public class VRServer extends Thread { for (Tracker tracker : trackers) { if (tracker.getTrackerNum() != id.getTrackerNum()) continue; - if (tracker.getDevice() == null && id.getDeviceId() != null) - continue; - if (tracker.getDevice() != null && id.getDeviceId() == null) + // A tracker id is a combination of two data, the device id (optional) and the tracker num + // tracker.getDevice() can be null for synthetic trackers + // if device id is null but the tracker id has a device id, the tracker should be skipped + // On the other hand if the tracker has a device id but the TrackerId does not have one we should also skip + if (tracker.getDevice() == null ^ id.getDeviceId() == null) continue; if (tracker.getDevice().getId() != id.getDeviceId().getId()) continue; From 18386d57d67b7fb3795cf96b2b45b9e61231825d Mon Sep 17 00:00:00 2001 From: lucas lelievre Date: Sat, 30 Apr 2022 22:33:37 +0200 Subject: [PATCH 2/3] Better tracker id with no possible null pointer exception --- src/main/java/dev/slimevr/VRServer.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main/java/dev/slimevr/VRServer.java b/src/main/java/dev/slimevr/VRServer.java index a54366c59..43b513266 100644 --- a/src/main/java/dev/slimevr/VRServer.java +++ b/src/main/java/dev/slimevr/VRServer.java @@ -306,18 +306,23 @@ public class VRServer extends Thread { } public Tracker getTrackerById(TrackerIdT id) { + for (Tracker tracker : trackers) { - if (tracker.getTrackerNum() != id.getTrackerNum()) + if (tracker.getTrackerNum() != id.getTrackerNum()) { continue; - // A tracker id is a combination of two data, the device id (optional) and the tracker num - // tracker.getDevice() can be null for synthetic trackers - // if device id is null but the tracker id has a device id, the tracker should be skipped - // On the other hand if the tracker has a device id but the TrackerId does not have one we should also skip - if (tracker.getDevice() == null ^ id.getDeviceId() == null) + } + + // Handle synthetic devices + if (id.getDeviceId() == null) { + if (tracker.getDevice() == null) + return tracker; continue; - if (tracker.getDevice().getId() != id.getDeviceId().getId()) - continue; - return tracker; + } + // We now know that the argument is not a synthetic tracker + + if (tracker.getDevice() != null && id.getDeviceId().getId() == tracker.getDevice().getId()) { + return tracker; + } } return null; } From 413332462f8bab11397c72cb0f675fb562e0e539 Mon Sep 17 00:00:00 2001 From: lucas lelievre Date: Sat, 30 Apr 2022 23:06:14 +0200 Subject: [PATCH 3/3] more readable version of it --- src/main/java/dev/slimevr/VRServer.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/slimevr/VRServer.java b/src/main/java/dev/slimevr/VRServer.java index 43b513266..8862a3482 100644 --- a/src/main/java/dev/slimevr/VRServer.java +++ b/src/main/java/dev/slimevr/VRServer.java @@ -306,21 +306,18 @@ public class VRServer extends Thread { } public Tracker getTrackerById(TrackerIdT id) { - for (Tracker tracker : trackers) { if (tracker.getTrackerNum() != id.getTrackerNum()) { continue; } // Handle synthetic devices - if (id.getDeviceId() == null) { - if (tracker.getDevice() == null) - return tracker; - continue; + if (id.getDeviceId() == null && tracker.getDevice() == null) { + return tracker; } - // We now know that the argument is not a synthetic tracker - if (tracker.getDevice() != null && id.getDeviceId().getId() == tracker.getDevice().getId()) { + if (tracker.getDevice() != null && id.getDeviceId() != null && id.getDeviceId().getId() == tracker.getDevice().getId()) { + // This is a physical tracker, and both device id and the tracker num match return tracker; } }