From b66153b9cb3df27af9a4802dbc8e0442a4788443 Mon Sep 17 00:00:00 2001
From: Dante Ruiz <danteruiz102@gmail.com>
Date: Thu, 16 Nov 2017 15:07:00 -0800
Subject: [PATCH] fixing setting incorrect parent

---
 .../controllerModules/nearParentGrabEntity.js | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/scripts/system/controllers/controllerModules/nearParentGrabEntity.js b/scripts/system/controllers/controllerModules/nearParentGrabEntity.js
index 178d459b1d..7a62a9b395 100644
--- a/scripts/system/controllers/controllerModules/nearParentGrabEntity.js
+++ b/scripts/system/controllers/controllerModules/nearParentGrabEntity.js
@@ -33,6 +33,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
         this.lastUnequipCheckTime = 0;
         this.autoUnequipCounter = 0;
         this.lastUnexpectedChildrenCheckTime = 0;
+        this.robbed = false;
 
         this.parameters = makeDispatcherModuleParameters(
             500,
@@ -75,6 +76,14 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
             return false;
         };
 
+        this.getOtherModule = function() {
+            return this.hand === RIGHT_HAND ? leftNearParentingGrabEntity : rightNearParentingGrabEntity;
+        };
+
+        this.otherHandIsParent = function(props) {
+            return this.getOtherModule().thisHandIsParent(props);
+        };
+
         this.startNearParentingGrabEntity = function (controllerData, targetProps) {
             Controller.triggerHapticPulse(HAPTIC_PULSE_STRENGTH, HAPTIC_PULSE_DURATION, this.hand);
 
@@ -100,6 +109,11 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
                 // this should never happen, but if it does, don't set previous parent to be this hand.
                 this.previousParentID[targetProps.id] = null;
                 this.previousParentJointIndex[targetProps.id] = -1;
+            } else if (this.otherHandIsParent(targetProps)) {
+                var otherModule = this.getOtherModule();
+                this.previousParentID[this.grabbedThingID] = otherModule.previousParentID[this.grabbedThingID];
+                this.previousParentJointIndex[this.grabbedThingID] = otherModule.previousParentJointIndex[this.grabbedThingID];
+                otherModule.robbed = true;
             } else {
                 this.previousParentID[targetProps.id] = targetProps.parentID;
                 this.previousParentJointIndex[targetProps.id] = targetProps.parentJointIndex;
@@ -119,7 +133,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
         this.endNearParentingGrabEntity = function (controllerData) {
             this.hapticTargetID = null;
             var props = controllerData.nearbyEntityPropertiesByID[this.targetEntityID];
-            if (this.thisHandIsParent(props)) {
+            if (this.thisHandIsParent(props) && !this.robbed) {
                 if (this.previousParentID[this.targetEntityID] === Uuid.NULL || this.previousParentID === undefined) {
                     Entities.editEntity(this.targetEntityID, {
                         parentID: this.previousParentID[this.targetEntityID],
@@ -145,6 +159,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
             }));
             this.grabbing = false;
             this.targetEntityID = null;
+            this.robbed = false;
         };
 
         this.checkForChildTooFarAway = function (controllerData) {
@@ -253,12 +268,14 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
             if (controllerData.triggerValues[this.hand] < TRIGGER_OFF_VALUE &&
                 controllerData.secondaryValues[this.hand] < TRIGGER_OFF_VALUE) {
                 this.checkForUnexpectedChildren(controllerData);
+                this.robbed = false;
                 return makeRunningValues(false, [], []);
             }
 
             if (targetProps) {
                 if ((propsArePhysical(targetProps) || propsAreCloneDynamic(targetProps)) &&
                     targetProps.parentID === Uuid.NULL) {
+                    this.robbed = false;
                     return makeRunningValues(false, [], []); // let nearActionGrabEntity handle it
                 } else {
                     this.targetEntityID = targetProps.id;
@@ -266,6 +283,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
                 }
             } else {
                 this.hapticTargetID = null;
+                this.robbed = false;
                 return makeRunningValues(false, [], []);
             }
         };
@@ -284,6 +302,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
                     this.grabbing = false;
                     this.targetEntityID = null;
                     this.hapticTargetID = null;
+                    this.robbed = false;
                     return makeRunningValues(false, [], []);
                 }
 
@@ -300,6 +319,7 @@ Script.include("/~/system/libraries/cloneEntityUtils.js");
                 // still searching / highlighting
                 var readiness = this.isReady(controllerData);
                 if (!readiness.active) {
+                    this.robbed = false;
                     return readiness;
                 }
                 if (controllerData.triggerClicks[this.hand] || controllerData.secondaryValues[this.hand] > BUMPER_ON_VALUE) {