Advertisement
Rochet2

Enchant display system

Apr 22nd, 2013
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.06 KB | None | 0 0
  1. -- Enchant display system:
  2.  
  3. --[[
  4. -- SQL, execute to characters database:
  5.     CREATE TABLE `custom_item_enchant_visuals` (
  6.         `iguid` INT(10) UNSIGNED NOT NULL COMMENT 'item DB guid',
  7.         `display` INT(10) UNSIGNED NOT NULL COMMENT 'enchantID',
  8.         PRIMARY KEY (`iguid`)
  9.     )
  10.     COMMENT='stores the enchant IDs for the visuals'
  11.     COLLATE='latin1_swedish_ci'
  12.     ENGINE=InnoDB;
  13. ]]
  14.  
  15. -- script variables:
  16. local EQUIPMENT_SLOT_MAINHAND = 15
  17. local EQUIPMENT_SLOT_OFFHAND = 16
  18. local PLAYER_VISIBLE_ITEM_1_ENCHANTMENT = 284
  19. local PERM_ENCHANTMENT_SLOT = 0
  20. local DD = {}
  21.  
  22. -- functions
  23. local LoadDB, setVisual, applyVisuals, LOGIN
  24.  
  25. function LoadDB()
  26.     DD = {}
  27.     CharDBQuery("DELETE FROM custom_item_enchant_visuals WHERE NOT EXISTS(SELECT 1 FROM item_instance WHERE custom_item_enchant_visuals.iguid = item_instance.guid)")
  28.     local Q = CharDBQuery("SELECT iguid, display FROM custom_item_enchant_visuals")
  29.     if(Q) then
  30.         repeat
  31.             local iguid, display = Q:GetUInt32(0), Q:GetUInt32(1)
  32.             DD[iguid] = display
  33.         until not Q:NextRow()
  34.     end
  35. end
  36. LoadDB()
  37.  
  38. function setVisual(player, item, display)
  39.     if(not player or not item) then return false end
  40.     local iguid = item:GetGUIDLow()
  41.     local enID = item:GetEnchantmentId(PERM_ENCHANTMENT_SLOT) or 0
  42.     if(enID ~= 0) then
  43.         CharDBExecute("DELETE FROM custom_item_enchant_visuals WHERE iguid = "..iguid)
  44.         DD[iguid] = nil
  45.         display = enID
  46.     elseif(not display) then
  47.         if(not DD[iguid]) then return false end
  48.         display = DD[iguid]
  49.     else
  50.         CharDBExecute("REPLACE INTO custom_item_enchant_visuals (iguid, display) VALUES ("..iguid..", "..display..")")
  51.         DD[iguid] = display
  52.     end
  53.     if(item:IsEquipped()) then
  54.         player:SetUInt16Value(PLAYER_VISIBLE_ITEM_1_ENCHANTMENT + (item:GetSlot() * 2), 0, display)
  55.     end
  56.     return true
  57. end
  58.  
  59. function applyVisuals(player)
  60.     if(not player) then return end
  61.     for i = EQUIPMENT_SLOT_MAINHAND, EQUIPMENT_SLOT_OFFHAND do
  62.         setVisual(player, player:GetItemByPos(-1, i))
  63.     end
  64. end
  65.  
  66. function LOGIN(event, player)
  67.     applyVisuals(player)
  68. end
  69.  
  70. RegisterPlayerEvent(3, LOGIN)
  71. RegisterPlayerEvent(29, function(e,p,i,b,s) setVisual(p, i) end)
  72.  
  73. -- Enchant IDs
  74. local E = {3789, 3854, 3273, 3225, 3870, 1899, 2674, 2675, 2671, 2672, 3365, 2673, 2343, 425, 3855, 1894, 1103, 1898, 3345, 1743, 3093, 1900, 3846, 1606, 283, 1, 3265, 2, 3, 3266, 1903, 13, 26, 7, 803, 1896, 2666, 25}
  75. local SubClasses = {
  76.     [0] = true,
  77.     [1] = true,
  78.     [4] = true,
  79.     [5] = true,
  80.     [6] = true,
  81.     [7] = true,
  82.     [8] = true,
  83.     [10] = true,
  84.     [11] = true,
  85.     [12] = true,
  86.     [14] = true,
  87.     [15] = true,
  88. }
  89.  
  90. math.randomseed(os.time())
  91. local function ONITEMLOOT(event, player, item, count, guid)
  92.     if(item:GetClass() == 2 and SubClasses[item:GetSubClass()]) then
  93.         if(math.random(4) == 1) then -- 25% of looted weapons get the visuals
  94.             setVisual(player, item, E[math.random(#E)])
  95.         end
  96.     end
  97. end
  98.  
  99. RegisterPlayerEvent(32, ONITEMLOOT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement