All pastes #262368 Raw Edit

Mine

public text v1 · immutable
#262368 ·published 2006-11-30 13:28 UTC
rendered paste body
StanceLink = {}

StanceLink.Const = {

  Cmd         = {"/stancelink", "/slnk"},

  Opt         = {
    {
      option  = "set",
      desc    = "links your currently equipped arms to your stance, so that whenever you enter that stance, you regain those arms.",
      method  = "Add"
    },
    {
      option  = "clear",
      desc    = "removes any link you might have with your current stance, so no arms will be equipped in future on changing to it.",
      method  = "Clear"
    },
    {
      option  = "list",
      desc    = "details whatever links you have setup between stances and arms.",
      method  = "List"
    }
  },

  Title   = "StanceLink",
  Version = "r1b1",
  Desc    = "Automatically equip weapons on warrior stance changes.",

  Chat            = {
     SetArmLink   = "%s (main-hand) and %s (secondary-hand) will now be armed when you enter %s.",
     ClearArmLink = "The link for your %s has been cleared."
  },

  Stance = {
     Ba  = "Battle Stance",
     Bk  = "Berserker Stance",
     De  = "Defensive Stance"
  },
  
  Slot   = {
    MH   = "Main-hand",
    SH   = "Secondary-hand"
  }
}

local const = StanceLink.Const

StanceLink.Defaults = {
  [const.Stance.Ba] = {},
  [const.Stance.Bk] = {},
  [const.Stance.De] = {}
}

StanceLink.Obj  = AceAddon:new{
  name          = const.Title,
  version       = const.Version,
  description   = const.Desc,
  author        = "Rowne",
  aceCompatible = "100",
  category      = ACE_CATEGORY_COMBAT,
  db            = AceDbClass:new("StanceLinkDB"),
  defaults      = StanceLink.Defaults,
  cmd           = AceChatCmdClass:new(const.Cmd, const.Opt),

  Enable = function(self)
    self:RegisterEvent("PLAYER_AURAS_CHANGED", "ArmsSwap")
  end,

  Add = function(self)
    local db = self:DetermineStance()
    if not db then return end
    local h1 = GetInventoryItemName(CharacterMainHandSlot:GetID()) 
    local h2 = GetInventoryItemName(CharacterSecondaryHandSlot:GetID())
    self.cmd:msg(const.Chat.SetArmLink, h1, h2, db)
    db                   = self:Get(db)
    db[const.Slot.MH]    = h1
    db[const.Slot.SH]    = h2
  end,

  Clear = function(self)
    local db = self:DetermineStance()
    if not db then return end
    self.cmd:msg(const.Chat.ClearArmLink, db)
    db = self:Get(db) db = {}
  end,

  List = function(self)
    local doList = function(t)
      local db = {}
      for k, v in self:Get(t) do
        tinsert(db, {text = k, val = v})
      end
      self.cmd:report(t.."...", db)
    end
    doList(const.Stance.Ba)
    doList(const.Stance.Bk)
    doList(const.Stance.De)
  end,
   
  DetermineStance = function(self)
    return self:Buffs(const.Stance.Ba, true)
        or self:Buffs(const.Stance.Bk, true)
        or self:Buffs(const.Stance.De, true)
  end,
   
  ArmsSwap = function(self)
    local db = self:DetermineStance()
    if not db then return end
    db       = self:Get(db)
    local h1 = db[const.Slot.MH]
    local h2 = db[const.Slot.SH]
    h1       = self:FindItemByName(h1)
    h2       = self:FindItemByName(h2)
    if h1 then PickupContainerItem(h1.x, h1.y) end
    if CursorHasItem() then EquipCursorItem(16) end
    if h2 then PickupContainerItem(h2.x, h2.y) end
    if CursorHasItem() then EquipCursorItem(17) end
  end
}

StanceLink.Obj:RegisterForLoad()

ace:RegisterFunctions(StanceLink.Obj, {
  version  = 1.0,

  Get     = function(self, var)
    return self.db:get(self.profilePath, var)
  end,

  Buffs   = function(b, n)
    local t = CreateFrame("GameTooltip",
                          "TempBuffTip",
                          WorldFrame,
                          "GameTooltipTemplate")
    t:SetOwner(WorldFrame, "ANCHOR_NONE")
    for k, v in b do
      for j = 1, 10 do
        local chk = function(f)
          t[f](t, "player", j)
          local m = t:GetName()
                m = getglobal(m.."TextLeft1"):GetText()
          if type(m) == "string" and strfind(m or "", v) then
            return n and b or TRUE
          end
        end
        chk("SetUnitBuff") chk("SetUnitDebuff")
      end
    end
  end,

  FindItemByName = function(n)
    for x = 0,NUM_BAG_FRAMES do
      for y = 1, GetContainerNumSlots(x) do
        if strfind(GetContainerItemLink(x, y) or "", n or "") then
          return {x = x, y = y}
        end
      end
    end
  end
})