/** Validate that the specified user is the owner of this pet. */ protected void validateOwnership(MemberObject owner) throws InvocationException { // TODO: if pet is updated or changes hands, we need to update the resolved PetObject if (_petobj.pet.ownerId != owner.getMemberId()) { // TODO: allow support personnel? log.warning( "Pet handling by non-owner", "who", owner.who(), "pet", this, "ownerId", _petobj.pet.ownerId); throw new InvocationException(PetCodes.E_INTERNAL_ERROR); } }
/** Validate that the specified user is the room owner or the owner of the pet. */ protected void validateRoomOrPetOwnership(MemberObject owner) throws InvocationException { // normally I always do the cheap compare first, but I'd rather not duplicate // the code from validateOwnership. PlaceManager plmgr = _placeReg.getPlaceManager(owner.getPlaceOid()); if ((plmgr instanceof RoomManager) && ((RoomManager) plmgr).canManage(owner)) { return; // they check out, they're room owners } // otherwise... validateOwnership(owner); }
/** * Sets up the necessary bits to follow our owner. If this is not possible, we'll throw an * invocation exception and shut ourselves down. */ protected void startFollowing(MemberObject owner) throws InvocationException { if (_follow != null) { log.warning( "Asked to follow but we're already following!", "pet", this, "target", owner.who(), "following", _follow.who()); throw new InvocationException(PetCodes.E_INTERNAL_ERROR); } if (owner.walkingId != 0) { shutdown(false); throw new InvocationException(PetCodes.E_ALREADY_WALKING); } reinitFollowing(owner); }
/** Called when we are transferred to a new server, rewires up our following listener. */ public void reinitFollowing(MemberObject owner) { if (_follow != null) { log.warning( "Requested to reinit following but we're already following!?", "pet", this, "target", owner.who()); return; } _follow = owner; _follow.setWalkingId(_petobj.pet.itemId); _follow.addListener( _follist = new ObjectDeathListener() { public void objectDestroyed(ObjectDestroyedEvent event) { // our followee logged off, shut ourselves down shutdown(false); } }); }
/** Handles an order from the specified user on this pet. */ public void orderPet(MemberObject owner, int order) throws InvocationException { // first validate the permissions if (order == Pet.ORDER_SLEEP) { validateRoomOrPetOwnership(owner); } else { validateOwnership(owner); } // then enact the order switch (order) { case Pet.ORDER_SLEEP: if (_petobj.pet.ownerId != owner.getMemberId()) { // a non-owner sent the pet to sleep, let's report that MemberObject realOwner = _locator.lookupMember(_petobj.pet.ownerId); if (realOwner != null) { SpeakUtil.sendInfo( realOwner, MsoyCodes.GENERAL_MSGS, MessageUtil.tcompose("m.pet_ordered4_room_mgr", owner.getVisibleName())); } } stopFollowing(); updateUsage(Item.UsedAs.NOTHING, 0); shutdown(false); break; case Pet.ORDER_FOLLOW: startFollowing(owner); break; case Pet.ORDER_GO_HOME: stopFollowing(); updateUsage(Item.UsedAs.PET, owner.homeSceneId); if (ScenePlace.getSceneId(_petobj) == owner.homeSceneId) { // we're already home, yay! } else if (_sceneReg.getSceneManager(owner.homeSceneId) != null) { enterRoom(owner.homeSceneId); } else { // TODO: if home room is resolved (on any server), instruct it to resolve pet shutdown(false); } break; case Pet.ORDER_STAY: // make sure the requester is in a room that they own PlaceManager plmgr = _placeReg.getPlaceManager(owner.getPlaceOid()); if (!(plmgr instanceof RoomManager)) { log.info("Owner no longer in a room?", "who", owner.who(), "in", plmgr); throw new InvocationException(PetCodes.E_INTERNAL_ERROR); } ((RoomManager) plmgr).requireCanAddPet(owner); // potentially stop following our owner stopFollowing(); // note that we want to autoload in this room updateUsage(Item.UsedAs.PET, ((RoomManager) plmgr).getScene().getId()); break; default: log.warning("Received unknown pet order", "from", owner.who(), "order", order); throw new InvocationException(PetCodes.E_INTERNAL_ERROR); } }