local config = { maxNameLength = 8, -- Do not touch without asking me !! minHomes = 1, delimeter = "|" -- Don't touch } function onSay(cid, words, param, channel) words = string.gsub(words, "/", "") local homes = db.getResult("SELECT * FROM `homeTowns` WHERE `player_id`=".. getPlayerGUID(cid)) if(words == 'home') then if(homes:getID() ~= -1) then local homeTable = string.explode(homes:getDataString("homes"), config.delimeter) if(numberOfHomes(homeTable) > config.minHomes) then if(param == '') then local playerHomes = "Your Home Locations:\n\n" for i = 1, table.maxn(homeTable), 1 do local homeData = string.explode(homeTable[i], "=") if(tostring(homeData[1]) ~= "nil") then playerHomes = playerHomes .. tostring(homeData[1]) .."\n" end end doPlayerPopupFYI(cid, playerHomes) else local homeExists = false for i = 1, table.maxn(homeTable), 1 do local homeData = string.explode(homeTable[i], "=") if(tostring(homeData[1]) ~= "nil") then if param == tostring(homeData[1]) then homeExists = true local pos = string.explode(homeData[2], ",") doTeleportThing(cid, {x = pos[1], y = pos[2], z = pos[3]}) if(not isPlayerGhost(cid)) then doSendMagicEffect(getCreaturePosition(cid), 10) end end end end if not homeExists then doPlayerSendCancel(cid, "There is no home with name " .. tostring(param)) return true end end else if(homeTable[1] == nil) then doPlayerSendCancel(cid, "You don't have any saved locations. use /sethome to add current location.") return true end local homeData = string.explode(homeTable[1], "=") if param ~= homeData[1] and param ~= "" then doPlayerSendCancel(cid, "There is no home with name " .. tostring(param)) return true end local pos = string.explode(homeData[2], ",") doTeleportThing(cid, {x = pos[1], y = pos[2], z = pos[3]}) if(not isPlayerGhost(cid)) then doSendMagicEffect(getCreaturePosition(cid), 10) end end else -- doPlayerSendCancel(cid, "You don't have any saved locations. use /sethome to add current location.") return true end elseif(words == 'sethome') then if param == '' then doPlayerSendCancel(cid, "Home name missing! Usage: /sethome home123") return true end if string.len(param) > config.maxNameLength then doPlayerSendCancel(cid, "Home name can only be 8 characters long!") return true end -- check if player already has houses saved if(homes:getID() ~= -1) then local homeTable = string.explode(homes:getDataString("homes"), config.delimeter) if(numberOfHomes(homeTable) == 10) then doPlayerSendCancel(cid, "Can not add new home, you have reached maximum home limit : 10.") return true end local pos = getCreaturePosition(cid) local homename = param param = param .. "=" .. tostring(pos.x) .. "," .. tostring(pos.y) .. "," .. tostring(pos.z) .. config.delimeter local update = db.executeQuery('UPDATE `homeTowns` SET `homes` = "' .. tostring(homes:getDataString("homes")) .. tostring(param) .. '" WHERE `player_id`='..getPlayerGUID(cid)) if(not update) then error("Failed to update player homes :".. getPlayerName(cid) .. ", " .. tostring(param)) return true end doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(homename) .. " has been added to your home locations.") else local pos = getCreaturePosition(cid) local homename = param param = param .. "=" .. tostring(pos.x) .. "," .. tostring(pos.y) .. "," .. tostring(pos.z) .. config.delimeter local insert = db.executeQuery('INSERT INTO `homeTowns`(`player_id`,`homes`) VALUES('..getPlayerGUID(cid)..', "'.. tostring(param) ..'")') if(not insert) then error("Failed to insert new home to database :".. getPlayerName(cid) .. ", " .. tostring(param)) return true end doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(homename) .. " has been added to your home locations.") end elseif(words == 'deletehome') then if param == '' then doPlayerSendCancel(cid, "Invalid home name! Usage: /deletehome ") return true end local homeString = string.explode(tostring(homes:getDataString("homes")), config.delimeter) local finalString = "" for i = 1, table.maxn(homeString), 1 do local homeX = string.explode(tostring(homeString[i]), "=") if(homeX[1] ~= param and homeX[1] ~= nil) then finalString = finalString .. tostring(homeString[i]) .. "|" end end local update = db.executeQuery('UPDATE `homeTowns` SET `homes` = "' .. finalString .. '" WHERE `player_id`='..getPlayerGUID(cid)) if(not update) then error("Failed to update player homes :".. getPlayerName(cid) .. ", " .. tostring(param)) end doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(param) .. " has been deleted from your home locations.") end return true end function numberOfHomes(homes) return table.getn(homes)-1 end