Advertisement
Laurea

Userdata Attachments

Feb 26th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.16 KB | None | 0 0
  1. --Made by Laurea (the Insane)
  2.  
  3. local backupValues = false; --(Eluna only) Set to false if you don't want values to carry over an engine reload or server restart.
  4. local instFunc, guidFunc, eluna = nil;
  5.  
  6. if (type(GetLuaEngine) ~= "function") then
  7.     assert(false, "This script is intended for use with ArcEmu with ALE or TrinityCore with Eluna.");
  8. elseif (GetLuaEngine() == "ALE") then
  9.     instFunc = "GetInstanceID";
  10.     guidFunc = "GetGUID";
  11.     eluna = false;
  12. elseif (GetLuaEngine():sub(1, 5) == "Eluna") then
  13.     instFunc = "GetInstanceId";
  14.     guidFunc = Unit.GetGUIDLow and "GetGUIDLow" or "GetGUID";
  15.     eluna = true;
  16. else
  17.     assert(false, "This script requires either ALE (ArcEmu Lua Engine) or Eluna to function properly.\nALE comes with ArcEmu by default, and Eluna is a TrinityCore project.");
  18. end
  19.  
  20. local DataValue = {
  21.     _player = {},
  22.     _creature = {},
  23.     _gameobject = {},
  24.     _guild = {},
  25.     _group = {},
  26.     _item = {},
  27.     _map = {},
  28.     _instance = {},
  29. };
  30.  
  31. local PLAYER, UNIT, GOBJ, ITEM, GUILD, GROUP = nil;
  32. if (eluna) then
  33.     UNIT = Unit;
  34.     GOBJ = GameObject;
  35.     ITEM = Item;
  36.     GUILD = Guild;
  37.     GROUP = Group;
  38.    
  39.     assert(UNIT);
  40.     assert(GOBJ);
  41.     assert(ITEM);
  42.     assert(GUILD);
  43.     assert(GROUP);
  44. else
  45.     PLAYER = LCF.PlayerMethods;
  46.     UNIT = LCF.CreatureMethods;
  47.     GOBJ = LCF.GOMethods;
  48.    
  49.     assert(PLAYER);
  50.     assert(UNIT);
  51.     assert(GOBJ);
  52. end
  53.  
  54. local function err(val, exp, func, arg, lvl)
  55.     local tmp = {"Bad argument #", arg, " to ", func, " (", exp, " expected, got ", type(val), ")."};
  56.     error(table.concat(tmp), lvl);
  57. end
  58. -----------------------------------------------------------------------------------------------------
  59. local function Set(tbl, field, value, obj, extra)
  60.     if (type(field) ~= "string" and type(field) ~= "number") then
  61.         local pos = 1;
  62.         local func = "SetValue";
  63.         if (extra == 1) then
  64.             pos = 2;
  65.             func = "SetMapValue";
  66.         elseif (extra == 2) then
  67.             pos = 2;
  68.             func = "SetInstanceValue";
  69.         elseif (extra == 3) then
  70.             func = "SetMemberValue";
  71.         end
  72.         err(field, "string or number", func, pos, eluna and 3 or 4);
  73.         return nil;
  74.     end
  75.     if (tbl[obj] == nil) then
  76.         tbl[obj] = {};
  77.     end
  78.     tbl[obj][field] = value;
  79.     return 1;
  80. end
  81. --Set UNIT
  82. function UNIT:SetValue(field, value)
  83.     if (eluna and self:GetUnitType() == "Player") then
  84.         return Set(DataValue._player, field, value, self[guidFunc](self));
  85.     end
  86.     return Set(DataValue._creature, field, value, self[guidFunc](self));
  87. end
  88. function UNIT:SetMapValue(field, value)
  89.     return Set(DataValue._map, field, value, self:GetMapId(), 1);
  90. end
  91. function UNIT:SetInstanceValue(field, value)
  92.     return Set(DataValue._instance, field, value, self[instFunc](self), 2);
  93. end
  94. --Set GOBJ
  95. function GOBJ:SetValue(field, value)
  96.     return Set(DataValue._gameobject, field, value, self[guidFunc](self));
  97. end
  98. function GOBJ:SetMapValue(field, value)
  99.     return Set(DataValue._map, field, value, self:GetMapId(), 1);
  100. end
  101. function GOBJ:SetInstanceValue(field, value)
  102.     return Set(DataValue._instance, field, value, self[instFunc](self), 2);
  103. end
  104. --Set Global
  105. function SetMapValue(map, field, value)
  106.     if (type(map) ~= "number") then err(map, "number", "SetMapValue", 1, 3); end
  107.     return Set(DataValue._map, field, value, map, 1);
  108. end
  109. function SetInstanceValue(instance, field, value)
  110.     if (type(instance) ~= "number") then err(instance, "number", "SetInstanceValue", 1, 3); end
  111.     return Set(DataValue._instance, field, value, instance, 2);
  112. end
  113.  
  114. if (eluna) then
  115.     --Set ITEM
  116.     function ITEM:SetValue(field, value)
  117.         return Set(DataValue._item, field, value, self[guidFunc](self));
  118.     end
  119.     function ITEM:SetMapValue(field, value)
  120.         return Set(DataValue._map, field, value, self:GetOwner():GetMapId(), 1);
  121.     end
  122.     function ITEM:SetInstanceValue(field, value)
  123.         return Set(DataValue._instance, field, value, self:GetOwner()[instFunc](self:GetOwner()), 2);
  124.     end
  125.     --Set GUILD
  126.     function GUILD:SetValue(field, value)
  127.         return Set(DataValue._guild, field, value, self:GetId());
  128.     end
  129.     function GUILD:SetMemberValue(field, value)
  130.         for _, player in pairs (self:GetMembers()) do
  131.             Set(DataValue._player, field, value, player[guidFunc](player), 3);
  132.         end
  133.     end
  134.     --Set GROUP
  135.     function GROUP:SetValue(field, value)
  136.         return Set(DataValue._group, field, value, self[guidFunc](self));
  137.     end
  138.     function GROUP:SetMemberValue(field, value)
  139.         for _, player in pairs (self:GetMembers()) do
  140.             Set(DataValue._player, field, value, player[guidFunc](player), 3);
  141.         end
  142.     end
  143. else
  144.     --Set PLAYER
  145.     function PLAYER:SetValue(field, value)
  146.         return Set(DataValue._player, field, value, self[guidFunc](self));
  147.     end
  148.     function PLAYER:SetMapValue(field, value)
  149.         return Set(DataValue._map, field, value, self:GetMapId(), 1);
  150.     end
  151.     function PLAYER:SetInstanceValue(field, value)
  152.         return Set(DataValue._instance, field, value, self[instFunc](self), 2);
  153.     end
  154. end
  155. -----------------------------------------------------------------------------------------------------
  156. local function Get(tbl, field, obj, extra)
  157.     if (type(field) ~= "string" and type(field) ~= "number") then
  158.         local pos = 1;
  159.         local func = "GetValue";
  160.         if (extra == 1) then
  161.             pos = 2;
  162.             func = "GetMapValue";
  163.         elseif (extra == 2) then
  164.             pos = 2;
  165.             func = "GetInstanceValue";
  166.         end
  167.         err(field, "string or number", func, pos, eluna and 3 or 4);
  168.         return nil;
  169.     end
  170.     if (tbl[obj] == nil) then
  171.         return nil;
  172.     end
  173.     return tbl[obj][field];
  174. end
  175. --Get UNIT
  176. function UNIT:GetValue(field)
  177.     if (eluna and self:GetUnitType() == "Player") then
  178.         return Get(DataValue._player, field, self[guidFunc](self));
  179.     end
  180.     return Get(DataValue._creature, field, self[guidFunc](self));
  181. end
  182. function UNIT:GetMapValue(field)
  183.     return Get(DataValue._map, field, self:GetMapId(), 1);
  184. end
  185. function UNIT:GetInstanceValue(field)
  186.     return Get(DataValue._instance, field, self[instFunc](self), 2);
  187. end
  188. --Get GOBJ
  189. function GOBJ:GetValue(field)
  190.     return Get(DataValue._gameobject, field, self[guidFunc](self));
  191. end
  192. function GOBJ:GetMapValue(field)
  193.     return Get(DataValue._map, field, self:GetMapId(), 1);
  194. end
  195. function GOBJ:GetInstanceValue(field)
  196.     return Get(DataValue._instance, field, self[instFunc](self), 2);
  197. end
  198. --Get Global
  199. function GetMapValue(map, field)
  200.     if (type(map) ~= "number") then err(map, "number", "GetMapValue", 1, 3); end
  201.     return Get(DataValue._map, field, map, 1);
  202. end
  203. function GetInstanceValue(instance, field)
  204.     if (type(instance) ~= "number") then err(instance, "number", "GetInstanceValue", 1, 3); end
  205.     return Get(DataValue._instance, field, instance, 2);
  206. end
  207.  
  208. if (eluna) then
  209.     --Get ITEM
  210.     function ITEM:GetValue(field)
  211.         return Get(DataValue._item, field, self[guidFunc](self));
  212.     end
  213.     function ITEM:GetMapValue(field)
  214.         return Get(DataValue._map, field, self:GetOwner():GetMapId(), 1);
  215.     end
  216.     function ITEM:GetInstanceValue(field)
  217.         return Get(DataValue._instance, field, self:GetOwner()[instFunc](self:GetOwner()), 2);
  218.     end
  219.     --Get GUILD
  220.     function GUILD:GetValue(field)
  221.         return Get(DataValue._guild, field, self:GetId());
  222.     end
  223.     --Get GROUP
  224.     function GROUP:GetValue(field, value)
  225.         return Get(DataValue._group, field, self[guidFunc](self));
  226.     end
  227. else
  228.     --Get PLAYER
  229.     function PLAYER:GetValue(field)
  230.         return Get(DataValue._player, field, self[guidFunc](self));
  231.     end
  232.     function PLAYER:GetMapValue(field)
  233.         return Get(DataValue._map, field, self:GetMapId(), 1);
  234.     end
  235.     function PLAYER:GetInstanceValue(field)
  236.         return Get(DataValue._instance, field, self[instFunc](self), 2);
  237.     end
  238. end
  239. -----------------------------------------------------------------------------------------------------
  240. local function Mod(tbl, field, value, obj, extra)
  241.     if (value == nil) then return nil; end
  242.     local t = type(value);
  243.     if (type(field) ~= "string" and type(field) ~= "number") then
  244.         local pos = 1;
  245.         local func = "ModValue";
  246.         if (extra == 1) then
  247.             pos = 2;
  248.             func = "ModMapValue";
  249.         elseif (extra == 2) then
  250.             pos = 2;
  251.             func = "ModInstanceValue";
  252.         elseif (extra == 3) then
  253.             func = "ModMemberValue";
  254.         end
  255.         err(field, "string or number", func, pos, eluna and 3 or 4);
  256.         return nil;
  257.     elseif (t ~= "string" and t ~= "number" and t ~= "table") then
  258.         local pos = 2;
  259.         local func = "ModValue";
  260.         if (extra == 1) then
  261.             pos = 3;
  262.             func = "ModMapValue";
  263.         elseif (extra == 2) then
  264.             pos = 3;
  265.             func = "ModInstanceValue";
  266.         elseif (extra == 3) then
  267.             func = "ModMemberValue";
  268.         end
  269.         err(field, "string, number or table", func, pos, eluna and 3 or 4);
  270.         return nil;
  271.     end
  272.     if (tbl[obj] == nil) then
  273.         tbl[obj] = {};
  274.     end
  275.     if (t ~= type(tbl[obj][field])) then
  276.         tbl[obj][field] = value;
  277.     else
  278.         local val = tbl[obj][field];
  279.         if (t == "number") then
  280.             tbl[obj][field] = val + value;
  281.         elseif (t == "string") then
  282.             tbl[obj][field] = val..value;
  283.         elseif (t == "table") then
  284.             for k, v in pairs (value) do
  285.                 tbl[obj][field][k] = v;
  286.             end
  287.         end
  288.     end
  289.     return tbl[obj][field];
  290. end
  291. --Mod UNIT
  292. function UNIT:ModValue(field, value)
  293.     if (eluna and self:GetUnitType() == "Player") then
  294.         return Mod(DataValue._player, field, value, self[guidFunc](self));
  295.     end
  296.     return Mod(DataValue._creature, field, value, self[guidFunc](self));
  297. end
  298. function UNIT:ModMapValue(field, value)
  299.     return Mod(DataValue._map, field, value, self:GetMapId(), 1);
  300. end
  301. function UNIT:ModInstanceValue(field, value)
  302.     return Mod(DataValue._instance, field, value, self[instFunc](self), 2);
  303. end
  304. --Mod GOBJ
  305. function GOBJ:ModValue(field, value)
  306.     return Mod(DataValue._gameobject, field, value, self[guidFunc](self));
  307. end
  308. function GOBJ:ModMapValue(field, value)
  309.     return Mod(DataValue._map, field, value, self:GetMapId(), 1);
  310. end
  311. function GOBJ:ModInstanceValue(field, value)
  312.     return Mod(DataValue._instance, field, value, self[instFunc](self), 2);
  313. end
  314. --Mod Global
  315. function ModMapValue(map, field, value)
  316.     if (type(map) ~= "number") then err(map, "number", "ModMapValue", 1, 3); end
  317.     return Mod(DataValue._map, field, value, map, 1);
  318. end
  319. function ModInstanceValue(instance, field, value)
  320.     if (type(instance) ~= "number") then err(instance, "number", "ModInstanceValue", 1, 3); end
  321.     return Mod(DataValue._instance, field, value, instance, 2);
  322. end
  323.  
  324. if (eluna) then
  325.     --Mod ITEM
  326.     function ITEM:ModValue(field, value)
  327.         return Mod(DataValue._item, field, value, self[guidFunc](self));
  328.     end
  329.     function ITEM:ModMapValue(field, value)
  330.         return Mod(DataValue._map, field, value, self:GetOwner():GetMapId(), 1);
  331.     end
  332.     function ITEM:ModInstanceValue(field, value)
  333.         return Mod(DataValue._instance, field, value, self:GetOwner()[instFunc](self:GetOwner()), 2);
  334.     end
  335.     --Mod GUILD
  336.     function GUILD:ModValue(field, value)
  337.         return Mod(DataValue._guild, field, value, self:GetId());
  338.     end
  339.     function GUILD:ModMemberValue(field, value)
  340.         for _, player in pairs (self:GetMembers()) do
  341.             Mod(DataValue._player, field, value, player[guidFunc](player), 3);
  342.         end
  343.     end
  344.     --Mod GROUP
  345.     function GROUP:ModValue(field, value)
  346.         return Mod(DataValue._group, field, value, self[guidFunc](self));
  347.     end
  348.     function GROUP:ModMemberValue(field, value)
  349.         for _, player in pairs (self:GetMembers()) do
  350.             Mod(DataValue._player, field, value, player[guidFunc](player), 3);
  351.         end
  352.     end
  353. else
  354.     --Mod PLAYER
  355.     function PLAYER:ModValue(field, value)
  356.         return Mod(DataValue._player, field, value, self[guidFunc](self));
  357.     end
  358.     function PLAYER:ModMapValue(field, value)
  359.         return Mod(DataValue._map, field, value, self:GetMapId(), 1);
  360.     end
  361.     function PLAYER:ModInstanceValue(field, value)
  362.         return Mod(DataValue._instance, field, value, self[instFunc](self), 2);
  363.     end
  364. end
  365. -----------------------------------------------------------------------------------------------------
  366. if (eluna and backupValues) then
  367.     function DataValue.Save(event)
  368.         local f = io.open("scripts/Data Values.save", "w+");
  369.         f:flush();
  370.         f:close();
  371.     end
  372.    
  373.     function DataValue.Load()
  374.         local f = io.open("scripts/Data Values.save");
  375.         if (f == nil) then
  376.             return;
  377.         end
  378.         --local str = f:read("*a");
  379.         f:close();
  380.     end
  381.    
  382.     CreateLuaEvent(DataValue.Load, 100, 1);
  383.     RegisterServerHook(54, DataValue.Save);
  384.     RegisterServerHook(55, DataValue.Save);
  385. end
  386.  
  387. function SetDataValue()
  388.     error("SetDataValue has been removed.", eluna and 2 or 3);
  389. end
  390. function GetDataValue()
  391.     error("GetDataValue has been removed.", eluna and 2 or 3);
  392. end
  393. function ModDataValue()
  394.     error("ModDataValue has been removed.", eluna and 2 or 3);
  395. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement