View Single Post
12-19-17, 09:07 PM   #12
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
It's not something the addon api really supports, no. There is AddonLoader, if that still works, but when your goal is to prevent your addon from being loaded, achieving that by instead loading a different addon probably isn't ideal (and requiring users to download a separate addon is even less practical than just having them disable it )

Just in case this solution can interest you, not sure if this is the best way to do it (I'm not sure if it could potentially introduce taint in rare cases? idk)

lua Code:
  1. StaticPopupDialogs["YOURADDONCONFIRMRELOAD"] = {
  2.     text = "YourAddonName is meant for a different class and has been disabled. Would you like to /reload for it to take effect?",
  3.     button1 = YES,
  4.     button2 = NO,
  5.     OnAccept = function()
  6.         ReloadUI()
  7.     end,
  8.     timeout = 0,
  9.     whileDead = true,
  10.     hideOnEscape = true,
  11.     preferredIndex = STATICPOPUP_NUMDIALOGS,
  12. }
  13.  
  14. local _, class = UnitClass("player")
  15. if class ~= "ROGUE" then
  16.     DisableAddon("YourAddonName")
  17.     StaticPopup_Show("YOURADDONCONFIRMRELOAD")
  18.     return
  19. end

Not tested, but in theory this should prevent your class-specific stuff from running, and it'll give a popup prompt for the user to /reload so they don't have to manually type /reload (or log out/back in) themselves. They also have the option to just say No if they don't care, and the popup won't ever bother them again unless they re-enabled the addon anyway.


Edit: Actually, if you want to think out of the box. You can probably have a separate addon bundled with your actual addon, whose only job is to load the addon itself if the character being played is a rogue.
If that works for addons that are bundled with external libraries, at least.

Something like this:

MyAddonLoader.toc
Code:
## Interface: 70300
## Title: MyAddonLoader
## Notes: Loads the addon if the character is a rogue

MyAddonLoader.lua
MyAddonLoader.lua
lua Code:
  1. local _, class = UnitClass("player")
  2. if class == "ROGUE" then
  3.     LoadAddon(MyAddon)
  4. end

MyAddon.toc
Code:
## Interface: 70300
## Title: MyAddon
## Notes: The actual addon
## LoadOnDemand: 1
## RequiredDeps: MyAddonLoader <-- Maybe not actually required, but w/e

MyAddon.lua
MyAddon.lua
lua Code:
  1. -- Passed class test :)
  2. code
  3. code
  4. code

See if that works, or if that causes issues. All that would be in MyAddonLoader is the .toc and the basic class check test in the .lua, the libraries and everything would be in MyAddon and should only be loaded if the character being played is a rogue?
If that doesn't work, then I guess prompting the user to /reload or having them deal with the addon being loaded once during first login would be the only real solution.

You may want to put the code from MyAddonLoader.lua inside an event handler which fires at PLAYER_LOGIN too (or whenever you are certain that UnitClass can be accessed), in case that could fail and return nil otherwise. But if everything works then you probably don't need to.

Also it looks like you may also have to add the included library dependencies to MyAddon.toc, but I'm not sure which ones you are using or which ones you need (and tbh I've never toyed with that), so you'll have to keep in mind to add that yourself.

Last edited by Ammako : 12-19-17 at 09:30 PM.
  Reply With Quote