- Joined
- Nov 30, 2007
- Messages
- 1,202
I'm using a string hash as an Id to save commands that are to be executed later. But because that can generate negative numbers or very large numbers it's not gonna work. Should i use ordinary hashtables here or another solution?
JASS:
library CommandUtils initializer Init uses Table
globals
private constant string PREFIX = "-"
private constant integer MAX_WORDS = 120
public PlayerChat array lastPlayerChat[24]
private Table table
endglobals
struct PlayerChat
public string chatStr
public string subStr
public string array word[MAX_WORDS]
public integer wordCount
public integer length
method update takes string chat returns nothing
local string char = ""
local integer i = 0
loop
exitwhen i == .wordCount
set .word[i] = null
set i = i + 1
endloop
set .length = StringLength(chat)
set .chatStr = chat
set .wordCount = 0
set i = 0
loop
exitwhen i > .length
set char = SubString(chat, i, i + 1)
if char != " " then
set .word[.wordCount] = .word[.wordCount] + char
else
set .wordCount = .wordCount + 1
endif
set i = i + 1
endloop
set .wordCount = .wordCount + 1
endmethod
method getWord takes integer index returns string
if (index < 0 or index >= .wordCount) then
return null
endif
return .word[index]
endmethod
endstruct
function RegisterCommand takes trigger trg, string cmd returns nothing
set table.trigger[StringHash(cmd)] = trg
endfunction
function DeregisterCommand takes string cmd returns nothing
call table.trigger.remove(StringHash(cmd))
endfunction
function GetLastPlayerChat takes integer playerId returns PlayerChat
return lastPlayerChat[playerId]
endfunction
private function Main takes nothing returns nothing
local trigger t
local integer i
local integer cid
local integer pid = GetPlayerId(GetTriggerPlayer())
call lastPlayerChat[pid].update(GetEventPlayerChatString())
set cid = StringHash(lastPlayerChat[pid].word[0])
if (not(table.trigger.has(cid))) then
return
endif
set lastPlayerChat[pid].subStr = SubString(lastPlayerChat[pid].chatStr, StringLength(lastPlayerChat[pid].word[0]), lastPlayerChat[pid].length)
set t = table.trigger[cid]
if TriggerEvaluate(t) then
call TriggerExecute(t)
endif
set t = null
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
local player p
local integer i = 0
loop
set p = Player(i)
if (GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER) then
call TriggerRegisterPlayerChatEvent(t, p, PREFIX, false)
set lastPlayerChat[i] = PlayerChat.create()
endif
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
call TriggerAddAction(t, function Main)
set table = Table.create()
set p = null
set t = null
endfunction
endlibrary
Last edited: