In AttachTo actions, we enforce the nearby requirement with a precondition requiring the direct object to be in the same immediate container as the indirect object, and vice versa. In moveWhileAttached(), we enforce the rule by detaching the objects if one is being moved away from the other's immediate container.
NearbyAttachable : Attachable
This method returns a list with three elements. The first element is the target location for 'self', and the second is the target location for 'other', the object we're attaching to. The third element is an integer giving the priority; a higher number means higher priority.
The priority is an arbitrary value that we use to determine which of the two objects involved in the attach gets to decide on the target locations. We call this method on both of the two objects being attached to one another, then we use the target locations returned by the object that claims the higher priority. If the two priorities are equal, we pick one arbitrarily.
The default implementation chooses my own immediate container as the target location for both objects. However, if the other object is non-portable, we'll choose its immediate location instead, since we obviously can't move it to our container.