summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/MountMuzzle/mountmuzzle.lua
blob: 95ab08037be35e12fc2e48d902023b9c1b22f2ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
--[[
Copyright © 2018, Sjshovan (Apogee)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Mount Muzzle nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Sjshovan (Apogee) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]

_addon.name = 'Mount Muzzle'
_addon.description = 'Change or remove the default mount music.'
_addon.author = 'Sjshovan (Apogee) sjshovan@gmail.com'
_addon.version = '0.9.5'
_addon.commands = {'mountmuzzle', 'muzzle', 'mm'}

local _logger = require('logger')
local _config = require('config')
local _packets = require('packets')

require('constants')
require('helpers')

local needs_inject = false

local defaults = {
    muzzle = muzzles.silent.name
}

local settings = _config.load(defaults)

local help = {
    commands = {
        buildHelpSeperator('=', 26),
        buildHelpTitle('Commands'),
        buildHelpSeperator('=', 26),
        buildHelpCommandEntry('list', 'Display the available muzzle types.'),
        buildHelpCommandEntry('set <muzzle>', 'Set the current muzzle to the given muzzle type.'),
        buildHelpCommandEntry('get', 'Display the current muzzle.'),
        buildHelpCommandEntry('default', 'Set the current muzzle to the default (Silent).'),
        buildHelpCommandEntry('unload', 'Unload Mount Muzzle.'),
        buildHelpCommandEntry('reload', 'Reload Mount Muzzle.'),
        buildHelpCommandEntry('about', 'Display information about Mount Muzzle.'),
        buildHelpCommandEntry('help', 'Display Mount Muzzle commands.'),
        buildHelpSeperator('=', 26),
    },
    types = {
        buildHelpSeperator('=', 23),
        buildHelpTitle('Types'),
        buildHelpSeperator('=', 23),
        buildHelpTypeEntry(muzzles.silent.name:ucfirst(), muzzles.silent.description),
        buildHelpTypeEntry(muzzles.mount.name:ucfirst(), muzzles.mount.description),
        buildHelpTypeEntry(muzzles.chocobo.name:ucfirst(), muzzles.chocobo.description),
        buildHelpTypeEntry(muzzles.zone.name:ucfirst(), muzzles.zone.description),
        buildHelpSeperator('=', 23),
    },
    about = {
        buildHelpSeperator('=', 23),
        buildHelpTitle('About'),
        buildHelpSeperator('=', 23),
        buildHelpTypeEntry('Name', _addon.name),
        buildHelpTypeEntry('Description', _addon.description),
        buildHelpTypeEntry('Author', _addon.author),
        buildHelpTypeEntry('Version', _addon.version),
        buildHelpSeperator('=', 23),
    },
    aliases = {
        muzzles = {
            s = muzzles.silent.name,
            m = muzzles.mount.name,
            c = muzzles.chocobo.name,
            z = muzzles.zone.name
        }
    }
}

function display_help(table_help)
    for index, command in pairs(table_help) do
        displayResponse(command)
    end
end

function getMuzzle()
    return settings.muzzle
end

function getPlayerBuffs() 
    return T(windower.ffxi.get_player().buffs)
end

function resolveCurrentMuzzle()
    local current_muzzle = getMuzzle()
    
    if not muzzleValid(current_muzzle) then
        current_muzzle = muzzles.silent.name
        setMuzzle(current_muzzle)
        displayResponse(
            'Note: Muzzle found in settings was not valid and is now set to the default (%s).':format('Silent':color(colors.secondary)),
            colors.warn
        )
    end
    
    return muzzles[current_muzzle]
end

function setMuzzle(muzzle)
    settings.muzzle = muzzle
    settings:save()
end

function playerInReive()
    return getPlayerBuffs():contains(player.buffs.reiveMark)
end

function playerIsMounted()
    local _player = windower.ffxi.get_player()
    
    if _player then
        return _player.status == player.statuses.mounted or getPlayerBuffs():contains(player.buffs.mounted)
    end
    
    return false 
end

function muzzleValid(muzzle)
    return muzzles[muzzle] ~= nil
end

function injectMuzzleMusic()
    injectMusic(music.types.mount, resolveCurrentMuzzle().song)
end

function injectMusic(bgmType, songID) 
    _packets.inject(_packets.new('incoming', packets.inbound.music_change.id, {
        ['BGM Type'] = bgmType,
        ['Song ID'] = songID,
    }))
end

function requestInject()
    needs_inject = true
end

function handleInjectionNeeds() 
    if needs_inject and playerIsMounted() then
        injectMuzzleMusic()
        needs_inject = false; 
    end
end

function tryInject()
    requestInject()
    handleInjectionNeeds()
end

windower.register_event('login', 'load', 'zone change', function() 
    tryInject()
end)

windower.register_event('addon command', function(command, ...)
    if command then
        command = command:lower()
    else 
        return display_help(help.commands)
    end
    
    local command_args = {...}
    local respond = false
    local response_message = ''
    local success = true
    
    if command == 'list' or command == 'l' then
        display_help(help.types)

    elseif command == 'set' or command == 's' then
        respond = true
        
        local muzzle = tostring(command_args[1]):lower()
        local from_alias = help.aliases.muzzles[muzzle]
        
        if (from_alias ~= nil) then
            muzzle = from_alias
        end

        if not muzzleValid(muzzle) then
            success = false
            response_message = 'Muzzle type not recognized.'
        else
            requestInject()
            setMuzzle(muzzle)
            response_message = 'Updated current muzzle to %s.':format(muzzle:ucfirst():color(colors.secondary))
        end

    elseif command == 'get' or command == 'g' then
        respond = true
        response_message = 'Current muzzle is %s.':format(getMuzzle():ucfirst():color(colors.secondary))

    elseif command == 'default' or command == 'd' then
        respond = true
        requestInject()

        setMuzzle(muzzles.silent.name)
        response_message = 'Updated current muzzle to the default (%s).':format('Silent':color(colors.secondary))

    elseif command == 'reload' or command == 'r' then
        windower.send_command('lua r mountmuzzle')
    
    elseif command == 'unload' or command == 'u' then
        respond = true
        response_message = 'Thank you for using Mount Muzzle. Goodbye.'
        injectMusic(music.types.mount, muzzles.zone.song)
        windower.send_command('lua unload mountmuzzle')

    elseif command == 'about' or command == 'a' then
        display_help(help.about)
        
    elseif command == 'help' or command == 'h' then
        display_help(help.commands)
    else
        display_help(help.commands)
    end

    if respond then
        displayResponse(
            buildCommandResponse(response_message, success)
        )
    end
    
    handleInjectionNeeds()
end)

windower.register_event('incoming chunk', function(id, data)
     if id == packets.inbound.music_change.id then
        local packet = _packets.parse('incoming', data)
        
        if packet['BGM Type'] == music.types.mount then
            packet['Song ID'] = resolveCurrentMuzzle().song
            return _packets.build(packet)
        end
        
        tryInject()
    end
end)