-- Search-A-Card -- made by: Chr1Z -- description: spawns the card(s) with the specified name information = { version = "1.2", last_updated = "12.11.2022" } require("playercards/PlayerCardSpawner") local buttonParameters = {} buttonParameters.function_owner = self buttonParameters.height = 200 buttonParameters.width = 1200 buttonParameters.font_size = 75 local BUTTON_LABELS = {} BUTTON_LABELS["spawn"] = {} BUTTON_LABELS["spawn"][true] = "Mode: Spawn all matching cards " BUTTON_LABELS["spawn"][false] = "Mode: Spawn first matching card" BUTTON_LABELS["search"] = {} BUTTON_LABELS["search"][true] = "Mode: Name matches search term" BUTTON_LABELS["search"][false] = "Mode: Name contains search term" local inputParameters = {} inputParameters.label = "Click to enter card name" inputParameters.input_function = "input_func" inputParameters.function_owner = self inputParameters.alignment = 2 inputParameters.position = { 0, 0.05, -1.6 } inputParameters.width = 1200 inputParameters.height = 130 inputParameters.font_size = 107 local ALL_CARDS_GUID = "15bb07" -- main code function onSave() return JSON.encode({ spawnAll, searchExact, inputParameters.value }) end function onLoad(saved_data) local loaded_data = JSON.decode(saved_data) spawnAll = loaded_data[1] or false searchExact = loaded_data[2] or false inputParameters.value = loaded_data[3] or "" -- index 0: button for spawn mode buttonParameters.click_function = "search" buttonParameters.label = "Spawn matching card(s)!" buttonParameters.position = { 0, 0.06, 1.15 } self.createButton(buttonParameters) -- index 1: button for spawn mode buttonParameters.click_function = "spawnMode" buttonParameters.label = BUTTON_LABELS["spawn"][spawnAll] buttonParameters.position[3] = buttonParameters.position[3] + 0.4 self.createButton(buttonParameters) -- index 2: button for search mode buttonParameters.click_function = "searchMode" buttonParameters.label = BUTTON_LABELS["search"][searchExact] buttonParameters.position[3] = buttonParameters.position[3] + 0.4 self.createButton(buttonParameters) self.createInput(inputParameters) self.addContextMenuItem("More Information", function() printToAll("------------------------------", "White") printToAll("Search-A-Card v" .. information["version"] .. " by Chr1Z", "Orange") printToAll("last updated: " .. information["last_updated"], "White") end) end function spawnMode() spawnAll = not spawnAll self.editButton({ index = 1, label = BUTTON_LABELS["spawn"][spawnAll] }) end function searchMode() searchExact = not searchExact self.editButton({ index = 2, label = BUTTON_LABELS["search"][searchExact] }) end -- if "Enter press" (\n) is found, start search and recreate input function input_func(_, _, input, stillEditing) if not stillEditing then inputParameters.value = input elseif string.find(input, "%\n") ~= nil then inputParameters.value = input.gsub(input, "%\n", "") search() self.removeInput(0) self.createInput(inputParameters) end end function search() if inputParameters.value == nil or string.len(inputParameters.value) == 0 then printToAll("Please enter a search string.", "Yellow") return end if string.len(inputParameters.value) < 3 then printToAll("Please enter a longer search string.", "Yellow") return end local allCardsBag = getObjectFromGUID(ALL_CARDS_GUID) if allCardsBag == nil then printToAll("Player card bag couldn't be found.", "Red") return end -- search all objects in bag local cardList = allCardsBag.call("getCardsByName", { name = inputParameters.value, exact = searchExact }) if cardList == nil or #cardList == 0 then printToAll("No match found.", "Red") return end if (#cardList > 100) then printToAll("Matched more than 100 cards, please try a more specific search.", "Yellow") return end -- sort table by name (reverse for multiple results, because bottom card spawns first) table.sort(cardList, function(k1, k2) return spawnAll == (k1.data.Nickname > k2.data.Nickname) end) local rot = self.getRotation() local pos = self.positionToWorld(Vector(0, 2, -0.225)) Spawner.spawnCards(cardList, pos, rot, true) end