• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

PlayerColors

Level 11
Joined
Nov 4, 2007
Messages
337
Requirements: TimerUtils

Well, to me this is useful.
The PlayerColors library does actually the same stuff several other libraries do: It colors the name of a player.
With one difference: Instead of needing additional functions to get the player name colored,
this hooks GetPlayerName and makes it return a colored player name. Instead of having a GetPlayerNameColored function, this library provides GetPlayerNameUncolored.

JASS:
library PlayerColors initializer init requires TimerUtils
//===========================================================================
// Information:
//==============
//
// There are already several libraries that support functions like GetPlayerNameColored,
// I know. This does actually the same thing, but the other way round.
// It hooks GetPlayerName so that it will always return the player name colored nicely
// and has got a GetPlayerNameUncolored function instead of that.
//
// Since this uses native hooking, you don't have to go through old maps and replace
// every 'GetPlayerName' with 'GetPlayerNameColored'. And colored player names
// make a map seem much more professional, believe me.
//
// This is pretty stable and still works correctly if you use functions like SetPlayerName and
// SetPlayerColor.
//
//===========================================================================
// Implementation:
//===============
//
// 1. Download a tool called 'JassNewGen', and unpack it somewhere. You need that
// edit to use this tool. The 'JassNewGen' is used very commonly and offers other
// nice features. You can find it at:
// [url]http://www.wc3c.net/showthread.php?t=90999[/url]
// You should also update to the latest JassHelper.
//
// 2. Make a new trigger, and convert it to custom text. Insert everything
// the library contains into that trigger.
//
// 3. Download TimerUtils from this link:
//
// and do the same implemenation stuff with it.
//
// 3. Save your map.
//
//===========================================================================

    globals
        private string array ColorString
        private string array PlayerNames
        private boolean IsChangingColor = false
        private boolean Init
    endglobals

    function GetPlayerNameUncolored takes player p returns string
        return PlayerNames[GetPlayerId(p)]
    endfunction

    private function ResetColor takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer ID = GetTimerData(t)
        set IsChangingColor = true
        call SetPlayerName(Player(ID),PlayerNames[ID])
        call ReleaseTimer(t)
        set IsChangingColor = false
    endfunction

    private function TempColorName takes player p returns nothing
        local integer ID
        local timer t
        if Init == false then
            set IsChangingColor = true
            set ID = GetPlayerId(p)
            set t = NewTimer()
            call SetPlayerName(p,ColorString[ID]+PlayerNames[ID]+"|r")
            call SetTimerData(t,ID)
            call TimerStart(t,0.00,false,function ResetColor)
            set IsChangingColor = false
        endif
    endfunction

    private function UpdateColor takes player p, playercolor c returns nothing
        set ColorString[GetPlayerId(p)] = ColorString[GetHandleId(c)]
    endfunction

    private function UpdateName takes player p, string name returns nothing
        if IsChangingColor == false then
            set PlayerNames[GetPlayerId(p)] = name
        endif
    endfunction

    hook GetPlayerName TempColorName
    hook SetPlayerColor UpdateColor
    hook SetPlayerName UpdateName

    private function init takes nothing returns nothing
        set ColorString[0] = "|cffff0000" //red
        set ColorString[1] = "|cff0000ff" //blue
        set ColorString[2] = "|cff00f5ff" //Teal
        set ColorString[3] = "|cff551A8B" //Purple
        set ColorString[4] = "|cffffff00" //Yellow
        set ColorString[5] = "|cffEE9A00" //Orange
        set ColorString[6] = "|cff00CD00" //Green
        set ColorString[7] = "|cffFF69B4" //Pink
        set ColorString[8] = "|cffC0C0C0" //Gray
        set ColorString[9] = "|cffB0E2FF" //Light Blue
        set ColorString[10] = "|cff006400" //Dark Green
        set ColorString[11] = "|cff8B4513" //Brown

        set Init = true
        set PlayerNames[0] = GetPlayerName(Player(0))
        set PlayerNames[1] = GetPlayerName(Player(1))
        set PlayerNames[2] = GetPlayerName(Player(2))
        set PlayerNames[3] = GetPlayerName(Player(3))
        set PlayerNames[4] = GetPlayerName(Player(4))
        set PlayerNames[5] = GetPlayerName(Player(5))
        set PlayerNames[6] = GetPlayerName(Player(6))
        set PlayerNames[7] = GetPlayerName(Player(7))
        set PlayerNames[8] = GetPlayerName(Player(8))
        set PlayerNames[9] = GetPlayerName(Player(9))
        set PlayerNames[10] = GetPlayerName(Player(10))
        set PlayerNames[11] = GetPlayerName(Player(11))
        set Init = false

    endfunction
endlibrary

Here an example:

 
Last edited:
JASS:
    private function UpdateColor takes player p, playercolor c returns nothing
        if c == PLAYER_COLOR_RED then
            set ColorString[GetPlayerId(p)] = ColorString[0]
        elseif c == PLAYER_COLOR_BLUE then
            set ColorString[GetPlayerId(p)] = ColorString[1]
        elseif c == PLAYER_COLOR_CYAN then
            set ColorString[GetPlayerId(p)] = ColorString[2]
        elseif c == PLAYER_COLOR_PURPLE then
            set ColorString[GetPlayerId(p)] = ColorString[3]
        elseif c == PLAYER_COLOR_YELLOW then
            set ColorString[GetPlayerId(p)] = ColorString[4]
        elseif c == PLAYER_COLOR_ORANGE then
            set ColorString[GetPlayerId(p)] = ColorString[5]
        elseif c == PLAYER_COLOR_GREEN then
            set ColorString[GetPlayerId(p)] = ColorString[6]
        elseif c == PLAYER_COLOR_PINK then
            set ColorString[GetPlayerId(p)] = ColorString[7]
        elseif c == PLAYER_COLOR_LIGHT_GRAY then
            set ColorString[GetPlayerId(p)] = ColorString[8]
        elseif c == PLAYER_COLOR_LIGHT_BLUE then
            set ColorString[GetPlayerId(p)] = ColorString[9]
        elseif c == PLAYER_COLOR_AQUA then
            set ColorString[GetPlayerId(p)] = ColorString[10]
        elseif c == PLAYER_COLOR_BROWN then
            set ColorString[GetPlayerId(p)] = ColorString[11]
        endif
    endfunction
JASS:
    private function UpdateColor takes player p, playercolor c returns nothing
            set ColorString[GetPlayerId(p)] = ColorString[GetHandleId(c)]
    endfunction
 
Level 8
Joined
Oct 3, 2008
Messages
367
What you should be using is an integer stack with a 0 second timer running, but pause the timer when not in use. Resume the timer when you need to fire something.

TimerUtils is totally unneeded.
 
Top