include library.sui; include library.utils; const string_id SID_LOCK_MENU = new string_id("spam", "sid_lock_menu"); const string_id SID_MENU_REMOVE_LOCK = new string_id("spam", "sid_menu_lock_remove"); const string_id SID_MENU_ADD_ACCESS_PLAYER = new string_id("spam", "sid_menu_lock_add_player"); const string_id SID_MENU_REMOVE_ACCESS_PLAYER = new string_id("spam", "sid_menu_lock_remove_player"); const string_id SID_MENU_ADD_ACCESS_GUILD = new string_id("spam", "sid_menu_lock_add_guild"); const string_id SID_MENU_REMOVE_ACCESS_GUILD = new string_id("spam", "sid_menu_lock_remove_guild"); const string_id SID_LOCK_VIEW_ACCESS_LIST = new string_id("spam", "sid_menu_lock_view_access_list"); const string_id SID_LOCK_ADDED_PLAYER = new string_id("spam", "sid_lock_added_player"); const string_id SID_LOCK_ADDED_GUILD = new string_id("spam", "sid_lock_added_guild"); const string_id SID_LOCK_GUILD_ALREADY_ADDED = new string_id("spam", "sid_lock_guild_already_added"); const string_id SID_LOCK_REMOVED_PLAYER = new string_id("spam", "sid_lock_removed_player"); const string_id SID_LOCK_REMOVED_GUILD = new string_id("spam", "sid_lock_removed_guild"); const string_id SID_LOCK_GUILD_NOT_ADDED = new string_id("spam", "sid_lock_guild_not_added"); const string_id SID_LOCK_GUILD_NOT_FOUND = new string_id("spam", "sid_lock_guild_not_found"); const string_id SID_LOCK_REMOVED = new string_id("spam", "sid_lock_removed"); const string SID_SUI_ADD_PLAYER_PROMPT = "@spam:sid_sui_add_player_prompt"; const string SID_SUI_ADD_PLAYER_TITLE = "@spam:sid_sui_add_player_title"; const string SID_SUI_ADD_GUILD_PROMPT = "@spam:sid_sui_add_guild_prompt"; const string SID_SUI_ADD_GUILD_TITLE = "@spam:sid_sui_add_guild_title"; const string SID_SUI_REMOVE_GUILD_PROMPT = "@spam:sid_sui_remove_guild_prompt"; const string SID_SUI_REMOVE_GUILD_TITLE = "@spam:sid_sui_remove_guild_title"; const string SID_SUI_REMOVE_PLAYER_PROMPT = "@spam:sid_sui_remove_player_prompt"; const string SID_SUI_REMOVE_PLAYER_TITLE = "@spam:sid_sui_remove_player_title"; const string SID_SUI_LOCK_ACCESSORS_PROMPT = "@spam:sid_sui_lock_accessors_prompt"; const string SID_SUI_LOCK_ACCESSORS_TITLE = "@spam:sid_sui_lock_accessors_title"; const string_id SID_LOCK_ADD_ACCESSOR_NOT_FOUND = new string_id("spam", "sid_lock_add_accessor_not_found"); const string_id SID_LOCK_REMOVE_ACCESSOR_NOT_FOUND = new string_id("spam", "sid_lock_remove_accessor_not_found"); const string OBJVAR_PLAYER_ACCESS_LIST = "tangible_object.accessList"; const string OBJVAR_GUILD_ACCESS_LIST = "tangible_object.guildAccessList"; void addGuildToAccess(obj_id container, int guildId) { if(!isIdValid(container) || !exists(container) || guildId == 0) { return; } addGuildToAccessList(container, guildId); } void removeGuildFromAccess(obj_id container, int guildId) { if(!isIdValid(container) || !exists(container) || guildId == 0) { return; } removeGuildFromAccessList(container, guildId); } void clearGuildAccess(obj_id container) { if(!isIdValid(container) || !exists(container)) { return; } clearGuildAccessList(container); } int [] getGuildAccess(obj_id container) { if(!isIdValid(container) || !exists(container)) { return null; } return getIntArrayObjVar(container, OBJVAR_GUILD_ACCESS_LIST); } void addPlayerToAccess(obj_id container, obj_id player) { if(!isIdValid(container) || !exists(container) || !isIdValid(player) || !exists(player)) { return; } addUserToAccessList(container, player); } void removePlayerFromAccess(obj_id container, obj_id player) { if(!isIdValid(container) || !exists(container) || !isIdValid(player) || !exists(player)) { return; } removeUserFromAccessList(container, player); } void clearPlayerAccess(obj_id container) { if(!isIdValid(container) || !exists(container)) { return; } clearUserAccessList(container); } obj_id [] getPlayerAccess(obj_id container) { if(!isIdValid(container) || !exists(container)) { return null; } return getObjIdArrayObjVar(container, OBJVAR_PLAYER_ACCESS_LIST); } trigger OnGetAttributes(obj_id player, string[] names, string[] attribs) { int idx = utils.getValidAttributeIndex(names); if(idx == -1) return SCRIPT_CONTINUE; if(!exists(self)) { return SCRIPT_CONTINUE; } string name = getStringObjVar(self, "lock_owner_name"); if(name != null && name.length() > 0) { names[idx] = "locked_by"; attribs[idx] = name; idx++; } return SCRIPT_CONTINUE; } trigger OnObjectMenuRequest(obj_id player, menu_info mi) { obj_id lockedBy = getObjIdObjVar(self, "lock_owner"); if(isIdValid(lockedBy) && player == lockedBy) { int lockMenu = mi.addRootMenu(menu_info_types.SERVER_MENU1, SID_LOCK_MENU); mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU2, SID_MENU_REMOVE_LOCK); // TODO: Add confirmation mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU3, SID_MENU_ADD_ACCESS_PLAYER); obj_id [] accessors = getUserAccessList(self); boolean anyAccess = false; if(accessors != null && accessors.length > 0) { mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU4, SID_MENU_REMOVE_ACCESS_PLAYER); anyAccess = true; } int guildId = getGuildId(player); int [] guildsWithAccess = getGuildAccess(self); if(guildsWithAccess != null && guildsWithAccess.length > 0) { anyAccess = true; } // Player is in a guild, but no guilds have access to this container. The player may add access. mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU5, SID_MENU_ADD_ACCESS_GUILD); // Player may or may not be in a guild, but a guild does have access. The player may remove access. if(guildsWithAccess != null && guildsWithAccess.length > 0) { mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU6, SID_MENU_REMOVE_ACCESS_GUILD); } if(anyAccess) { mi.addSubMenu(lockMenu, menu_info_types.SERVER_MENU7, SID_LOCK_VIEW_ACCESS_LIST); } } return SCRIPT_CONTINUE; } trigger OnObjectMenuSelect(obj_id player, int item) { // debugSpeakMsg(player, "locked_container OnObjectMenuSelect"); obj_id lockedBy = getObjIdObjVar(self, "lock_owner"); if(isIdValid(lockedBy) && player == lockedBy && item == menu_info_types.SERVER_MENU2) { obj_id container = utils.getInventoryContainer(player); // TODO: Check inventory capacity and deny if it is too full. obj_id craftedItem = makeCraftedItem("object/draft_schematic/furniture/furniture_house_container_lock.iff", 100.0f, container); if(isIdValid(craftedItem)) { removeObjVar(self, "lock_owner"); removeObjVar(self, "lock_owner_name"); clearCondition(self, CONDITION_LOCKED); clearPlayerAccess(self); clearGuildAccess(self); sendSystemMessage(player, SID_LOCK_REMOVED); detachScript(self, "structure.locked_container"); } return SCRIPT_CONTINUE; } if(isIdValid(lockedBy) && player == lockedBy && item == menu_info_types.SERVER_MENU3) { int pid = sui.inputbox( self, player, SID_SUI_ADD_PLAYER_PROMPT, sui.OK_CANCEL, SID_SUI_ADD_PLAYER_TITLE, sui.INPUT_NORMAL, null, "lockAddPlayer"); return SCRIPT_CONTINUE; } if(isIdValid(lockedBy) && player == lockedBy && item == menu_info_types.SERVER_MENU4) { int pid = sui.inputbox( self, player, SID_SUI_REMOVE_PLAYER_PROMPT, sui.OK_CANCEL, SID_SUI_REMOVE_PLAYER_TITLE, sui.INPUT_NORMAL, null, "lockRemovePlayer"); sendSystemMessage(player, SID_MENU_REMOVE_ACCESS_PLAYER); return SCRIPT_CONTINUE; } int guildId = getGuildId(player); int [] guildsWithAccess = getGuildAccess(self); // Player is in a guild, but no guilds have access to this container. The player may add access. if(isIdValid(lockedBy) && player == lockedBy && item == menu_info_types.SERVER_MENU5) { int pid = sui.inputbox( self, player, SID_SUI_ADD_GUILD_PROMPT, sui.OK_CANCEL, SID_SUI_ADD_GUILD_TITLE, sui.INPUT_NORMAL, null, "lockAddGuild"); return SCRIPT_CONTINUE; } // Player may or may not be in a guild, but a guild does have access. The player may remove access. if(isIdValid(lockedBy) && player == lockedBy && guildsWithAccess != null && guildsWithAccess.length > 0 && item == menu_info_types.SERVER_MENU6) { int pid = sui.inputbox( self, player, SID_SUI_REMOVE_GUILD_PROMPT, sui.OK_CANCEL, SID_SUI_REMOVE_GUILD_TITLE, sui.INPUT_NORMAL, null, "lockRemoveGuild"); return SCRIPT_CONTINUE; } if(isIdValid(lockedBy) && player == lockedBy && item == menu_info_types.SERVER_MENU7) { obj_id [] accessors = getPlayerAccess(self); resizeable string [] accessList = new string[0]; int accessorsCount = 0; if(accessors != null) { accessorsCount = accessors.length; } int guildsCount = 0; if(guildsWithAccess != null) { guildsCount = guildsWithAccess.length; } // debugSpeakMsg(player, "accessors: " + accessorsCount + " guilds: " + guildsCount); if(guildsWithAccess != null && guildsWithAccess.length > 0) { for(int i = 0, j = guildsWithAccess.length; i < j; i++) { utils.addElement(accessList, "Guild: " + guildGetName(guildsWithAccess[i])); } } if(accessors != null && accessors.length > 0) { for(int i = 0, j = accessors.length; i < j; i++) { if(isIdValid(accessors[i])) { utils.addElement(accessList, "" + getPlayerFullName(accessors[i])); } } } // debugSpeakMsg(player, "accessList size: " + accessList.length); int pid = sui.listbox(player, player, SID_SUI_LOCK_ACCESSORS_PROMPT, sui.OK_CANCEL, SID_SUI_LOCK_ACCESSORS_TITLE, accessList, "onLockAccessList", false, true); sui.showSUIPage(pid); } return SCRIPT_CONTINUE; } messageHandler lockAddPlayer() { obj_id player = params.getObjId("player"); if(sui.getIntButtonPressed(params) == sui.BP_OK) { string accessorName = sui.getInputBoxText(params); obj_id target = utils.getNearbyPlayerByName(player, accessorName); if(!isIdValid(target)) { target = getPlayerIdFromFirstName(accessorName); } // debugSpeakMsg(player, "Name added: " + accessorName + " object: " + target); if(isIdValid(target)) { addPlayerToAccess(self, target); sendSystemMessage(player, SID_LOCK_ADDED_PLAYER); } else { prose_package pp = new prose_package(); pp.actor.set(accessorName); pp.stringId = SID_LOCK_ADD_ACCESSOR_NOT_FOUND; sendSystemMessageProse(player, pp); } } return SCRIPT_CONTINUE; } messageHandler lockRemovePlayer() { obj_id player = params.getObjId("player"); obj_id lockedBy = getObjIdObjVar(self, "lock_owner"); if(sui.getIntButtonPressed(params) == sui.BP_OK) { string accessorName = sui.getInputBoxText(params); obj_id target = utils.getNearbyPlayerByName(player, accessorName); if(!isIdValid(target)) { target = getPlayerIdFromFirstName(accessorName); } // debugSpeakMsg(player, "Name removed: " + accessorName + " object: " + target); if(lockedBy == target) { return SCRIPT_CONTINUE; } if(isIdValid(target)) { removePlayerFromAccess(self, target); sendSystemMessage(player, SID_LOCK_REMOVED_PLAYER); } else { prose_package pp = new prose_package(); pp.actor.set(accessorName); pp.stringId = SID_LOCK_REMOVE_ACCESSOR_NOT_FOUND; sendSystemMessageProse(player, pp); } } return SCRIPT_CONTINUE; } messageHandler lockAddGuild() { obj_id player = params.getObjId("player"); if(sui.getIntButtonPressed(params) == sui.BP_OK) { string accessorName = sui.getInputBoxText(params); int guildId = findGuild(accessorName); if(guildId != 0) { int [] guildsOnContainer = getGuildAccess(self); boolean addable = true; if(guildsOnContainer != null && guildsOnContainer.length > 0) { for(int i = 0, j = guildsOnContainer.length; i < j; i++) { // We do not want to add the same guild to the container repeatedly. if(guildsOnContainer[i] == guildId) { addable = false; } } } if(addable) { addGuildToAccess(self, guildId); sendSystemMessage(player, SID_LOCK_ADDED_GUILD); } else { // Already on container. sendSystemMessage(player, SID_LOCK_GUILD_ALREADY_ADDED); } } else { sendSystemMessage(player, SID_LOCK_GUILD_NOT_FOUND); } } return SCRIPT_CONTINUE; } messageHandler lockRemoveGuild() { obj_id player = params.getObjId("player"); if(sui.getIntButtonPressed(params) == sui.BP_OK) { string accessorName = sui.getInputBoxText(params); int guildId = findGuild(accessorName); if(guildId != 0) { int [] guildsOnContainer = getGuildAccess(self); boolean removable = false; if(guildsOnContainer != null && guildsOnContainer.length > 0) { for(int i = 0, j = guildsOnContainer.length; i < j; i++) { // We do not want to add the same guild to the container repeatedly. if(guildsOnContainer[i] == guildId) { removable = true; } } } if(removable) { removeGuildFromAccess(self, guildId); sendSystemMessage(player, SID_LOCK_REMOVED_GUILD); } else { // Not on container. sendSystemMessage(player, SID_LOCK_GUILD_NOT_ADDED); } } else { sendSystemMessage(player, SID_LOCK_GUILD_NOT_FOUND); } } return SCRIPT_CONTINUE; }