[COLOR="#808080"]
local ChatMsg1 = "#Menu";
local ChatMsg2 = "#menu";
local HearthStone = {};
local function LoadLocations()
local Q = CharDBQuery("SELECT * FROM characters.hearthstone;") -- gathers the data from each row of the sql table
if(Q)then
repeat
HearthStone[Q:GetUInt32(0)] = {
map = Q:GetUInt32(1),
x = Q:GetFloat(2),
y = Q:GetFloat(3),
z = Q:GetFloat(4),
o = Q:GetFloat(5),
};
until not Q:NextRow();
end
end
LoadLocations(); -- starts the loading function
local function UpdateLoc(key,...)
local KEY = {
[1] = "INSERT INTO characters.hearthstone SET `guid`='%s';", -- creates a new entry
[2] = "UPDATE characters.hearthstone SET `%s`='%s' WHERE `%s`='%s';", -- updates an existing entry
};
if(key == 1)then -- used for new entry creation
local guid = ...;
local qs = string.format(KEY[key], guid);
WorldDBQuery(qs);
HearthStone[guid] = {map = nil, x = nil, y = nil, z = nil, o = nil,};
end
if(key == 2)then -- used for updating an existing entry
local a,b,c,d,e = ...;
local qs = string.format(KEY[key],a,b,c,d);
WorldDBQuery(qs);
HearthStone[d][a] = b;
end
end
local function FirstLogin(event, player)
local guid = player:GetGUIDLow();
if not(HearthStone[guid])then
local map,x,y,z,o = player:GetMapId(),player:GetLocation();
UpdateLoc(1,guid); -- key 1 pushes new entry data to UpdateLoc function
UpdateLoc(2,"map",map,"guid",guid); -- key 2 Pushes update data to UpdateLoc function
UpdateLoc(2,"x",x,"guid",guid); -- key 2 Pushes update data to UpdateLoc function
UpdateLoc(2,"y",y,"guid",guid); -- key 2 Pushes update data to UpdateLoc function
UpdateLoc(2,"z",z,"guid",guid); -- key 2 Pushes update data to UpdateLoc function
UpdateLoc(2,"o",o,"guid",guid); -- key 2 Pushes update data to UpdateLoc function
end
end
local function Login(event, player) -- normal login
local guid = player:GetGUIDLow();
if not(HearthStone[guid].map)then -- to verify no null data
FirstLogin(30, player); -- if null data then re-push the data
end
end
local function Hello(event, player)
player:GossipClearMenu();
player:GossipMenuAddItem(0, "Hearhstone", 0, 1);
player:GossipSendMenu(1, player, 100);
end
local function OnSelect(event, player, unit, sender, intid, code)
local guid = player:GetGUIDLow();
local map,x,y,z,o = player:GetMapId(),player:GetLocation();
if(intid == 1)then -- HearthStone sub-menu
player:GossipClearMenu();
player:GossipMenuAddItem(0, "Save my location", 0, 9)
player:GossipMenuAddItem(0, "Teleport me", 0, 10)
player:GossipSendMenu(1, player, 100);
end
if(intid == 9) then -- stores current coordinates
UpdateLoc(2,"map",map,"guid",guid);
UpdateLoc(2,"x",x,"guid",guid);
UpdateLoc(2,"y",y,"guid",guid);
UpdateLoc(2,"z",z,"guid",guid);
UpdateLoc(2,"o",o,"guid",guid);
player:GossipComplete();
player:SendAreaTriggerMessage("Your location has been stored.")
player:SendBroadcastMessage("Home Bind location updated.")
end
if(intid == 10) then -- teleports to stored coordinates
player:Teleport(HearthStone[guid].map, HearthStone[guid].x, HearthStone[guid].y, HearthStone[guid].z, HearthStone[guid].o)
player:GossipComplete();
end
end
local function Chat(event, player, msg, lang, type)
if(msg == ChatMsg1)or(msg == ChatMsg2)then
Hello(event, player)
end
end
RegisterPlayerEvent(30, FirstLogin)
RegisterPlayerEvent(3, FirstLogin)
RegisterPlayerEvent(18, Chat)
RegisterPlayerGossipEvent(100, 2, OnSelect)
[/COLOR]