View difference between Paste ID: G79k5NKj and Mn7HCG5K
SHOW: | | - or go back to the newest paste.
1
//    _      ______ _      _____  ___                
2
//   | | /| / / __ \ | /| / / _ )/ _ ) ___  _______ _
3
//   | |/ |/ / /_/ / |/ |/ / _  / _  |/ _ \/ __/ _ `/
4
//   |__/|__/\____/|__/|__/____/____(_)___/_/  \_, / 
5
//                                            /___/   
6
//   Script posted from WoWBB.org
7
8
#include "ScriptPCH.h"
9
 
10
class npc_enchantment : public CreatureScript
11
{
12
    public:
13
        npc_enchantment() : CreatureScript("npc_enchantment") { } 
14
15
    bool OnGossipHello(Player* player, Creature* creature)
16
    {
17
        player->ADD_GOSSIP_ITEM(0, "[Enchant Weapon]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_MAINHAND);
18
        player->ADD_GOSSIP_ITEM(0, "[Enchant Off-Hand]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_OFFHAND);
19
        player->ADD_GOSSIP_ITEM(0, "[Enchant Cloak]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_BACK);
20
        player->ADD_GOSSIP_ITEM(0, "[Enchant Chest]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_CHEST);
21
        player->ADD_GOSSIP_ITEM(0, "[Enchant Bracers]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_WRISTS);
22
        player->ADD_GOSSIP_ITEM(0, "[Enchant Gloves]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_HANDS);
23
        player->ADD_GOSSIP_ITEM(0, "[Enchant Feet]", GOSSIP_SENDER_MAIN, EQUIPMENT_SLOT_FEET);
24
25
        player->SEND_GOSSIP_MENU(1, creature->GetGUID());
26
        return true;
27
    }
28
29
    void Enchant(Player* player, uint8 equipSlot, uint32 enchantID)
30
    {
31
        Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, equipSlot);
32
        if (!item)
33
        {
34
            player->CLOSE_GOSSIP_MENU();
35
            return;
36
        }
37
38
	    // Must re-check that the currently equipped item is valid
39
        uint32 i;
40
        for (i = 0; i < sSpellMgr->GetSpellInfoStoreSize(); ++i)
41
        {
42
            SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(i);
43
            if (!spellInfo)
44
                continue;
45
            if (spellInfo->Effects[0].Effect != SPELL_EFFECT_ENCHANT_ITEM)
46
                continue;
47
            if (spellInfo->Effects[0].MiscValue != enchantID)
48
                continue;
49
50
            uint32 spellID = 0;
51
            SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellInfo->Id);
52
            for (SkillLineAbilityMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
53
			{
54
                // Not from the enchanting skill
55
                if (itr->second->skillId != 333 || itr->second->spellId != spellInfo->Id)
56
                    continue;
57
                spellID = itr->second->id;
58
                break;
59
            }
60
            if (spellID == 0)
61
                continue;
62
63
            int32 invType = (int32)pow((float)2, (float)item->GetTemplate()->InventoryType);
64
            int32 subClass = (int32)pow((float)2, (float)item->GetTemplate()->SubClass);
65
            if (item->GetTemplate()->ItemLevel < spellInfo->BaseLevel)
66
                continue;
67
            else if (spellInfo->EquippedItemClass != item->GetTemplate()->Class)
68
                continue;
69
            else if (!(spellInfo->EquippedItemSubClassMask & subClass)) 
70
                continue;
71
            else if (item->GetTemplate()->Class == ITEM_CLASS_ARMOR && item->GetTemplate()->SubClass != ITEM_SUBCLASS_ARMOR_SHIELD)
72
            {
73
                if (!(spellInfo->EquippedItemInventoryTypeMask & invType))
74
                    continue;
75
                else // Passed all checks
76
                    break;
77
            }
78
            else 
79
                break;
80
            
81
        }
82
        if (i == sSpellMgr->GetSpellInfoStoreSize())
83
        {
84
            ChatHandler(player->GetSession()).PSendSysMessage("Failed to enchant item.");
85
            return;
86
        }
87
88
	    player->ApplyEnchantment(item, PERM_ENCHANTMENT_SLOT, false);
89
        item->SetEnchantment(PERM_ENCHANTMENT_SLOT, enchantID, 0, 0);
90
	    player->ApplyEnchantment(item, PERM_ENCHANTMENT_SLOT, true);
91
92
        std::string color = "|cff";
93
	    switch (item->GetTemplate()->Quality)
94
	    {
95
            case 0: color += "9d9d9d"; break;
96
		    case 1: color += "ffffff"; break;
97
		    case 2: color += "1eff00"; break;
98
		    case 3: color += "0070dd"; break;
99
		    case 4: color += "a335ee"; break;
100
		    case 5: color += "ff8000"; break;
101
	    }
102
        ChatHandler(player->GetSession()).PSendSysMessage("|cffFFFFFF[%s%s|cffFFFFFF] |cffFF0000succesfully enchanted!", color.c_str(), item->GetTemplate()->Name1.c_str());
103
    }
104
105
    void ShowPage(Player* player, uint32 page, uint8 equipSlot)
106
    {
107
        Item* item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, equipSlot);
108
        if (!item)
109
        {
110
            player->CLOSE_GOSSIP_MENU();
111
            return;
112
        }
113
114
        ItemTemplate const* itemTemplate = item->GetTemplate();
115
        if (!itemTemplate)
116
        {
117
            player->CLOSE_GOSSIP_MENU();
118
            return;
119
        }
120
121
        player->PlayerTalkClass->ClearMenus();
122
        uint32 startPos = (page - 1) * 10;
123
        uint32 currentPos = 0;
124
125
        uint32 i;
126
        for (i = 0; i < sSpellMgr->GetSpellInfoStoreSize(); ++i)
127
        {
128
            SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(i);
129
            if (!spellInfo)
130
                continue;
131
            if (spellInfo->Effects[0].Effect != SPELL_EFFECT_ENCHANT_ITEM)
132
                continue;
133
            if (spellInfo->Id == 62257) // Ignore Titanguard
134
                continue;
135
            if (itemTemplate->ItemLevel < spellInfo->BaseLevel)
136
                continue;
137
            if (spellInfo->EquippedItemClass != itemTemplate->Class)
138
                continue;
139
            int32 subClass = (int32)pow((float)2, (float)itemTemplate->SubClass);
140
            if (!(spellInfo->EquippedItemSubClassMask & subClass)) 
141
                continue;
142
            int32 invType = (int32)pow((float)2, (float)itemTemplate->InventoryType);
143
            if (itemTemplate->Class == ITEM_CLASS_ARMOR && itemTemplate->SubClass != ITEM_SUBCLASS_ARMOR_SHIELD)
144
                if (!(spellInfo->EquippedItemInventoryTypeMask & invType))
145
                    continue;
146
147
            uint32 spellID = 0;
148
            SkillLineAbilityMapBounds bounds = sSpellMgr->GetSkillLineAbilityMapBounds(spellInfo->Id);
149
            for (SkillLineAbilityMap::const_iterator itr = bounds.first; itr != bounds.second; ++itr)
150
			{
151
                // Not from the enchanting skill
152
                if (itr->second->skillId != 333 || itr->second->spellId != spellInfo->Id)
153
                    continue;
154
                spellID = itr->second->id;
155
                break;
156
            }
157
            if (spellID == 0)
158
                continue;
159
            if (SpellItemEnchantmentEntry const* enchant = sSpellItemEnchantmentStore.LookupEntry(spellInfo->Effects[0].MiscValue))
160
            {
161
                if (enchant->requiredLevel > player->getLevel())
162
                    continue;
163
                if (currentPos >= startPos)
164
                    player->ADD_GOSSIP_ITEM(GOSSIP_ICON_INTERACT_1, enchant->description[0], equipSlot, enchant->ID);
165
166
                currentPos++;
167
                // Stop looping once we are 10 positions ahead
168
                if (currentPos == startPos + 10)
169
                    break;
170
            }
171
        }
172
173
        // Make sure there is actually another page
174
        if (currentPos == startPos + 10 && i != sSpellMgr->GetSpellInfoStoreSize())
175
            player->ADD_GOSSIP_ITEM(0, "Next Page", 100 + page + 1, equipSlot);  // Have to offset by 100 to ensure it gets pushed to the default case
176
        player->ADD_GOSSIP_ITEM(0, "Back", 100 + page - 1, equipSlot);
177
    }
178
 
179
    bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
180
    {
181
        player->PlayerTalkClass->ClearMenus();
182
        switch(sender)
183
        {
184
            case GOSSIP_SENDER_MAIN:
185
                ShowPage(player, 1, action);
186
                player->SEND_GOSSIP_MENU(1, creature->GetGUID());
187
                break;
188
            default:
189
                if (sender < 100)
190
                {
191
                    Enchant(player, sender, action);
192
                    player->CLOSE_GOSSIP_MENU();
193
                    break;
194
                }
195
                if (sender == 100)
196
                    OnGossipHello(player, creature);
197
                else
198
                {
199
                    ShowPage(player, sender - 100, action);
200
                    player->SEND_GOSSIP_MENU(1, creature->GetGUID());
201
                }
202
                break;
203
        }
204
        return true;
205
    }
206
};
207
 
208
void AddSC_npc_enchantment()
209
{
210
	new npc_enchantment();
211
}