Prozix
New Member
- Reaction score
- 7
Hello there, here I am again.
Since I have been unable to find a CCS that simplifies adding commands to your project I figured I had to write it myself. Here I am presenting to you: The ChatCommandSystem written by Prozix (requires vJass)
And here is how to use it:
Suggestions are welcome, enjoy:thup:
Update v1.01 - Added ListCommand functions. Passing invalid parameters to a boolean command now doesn't execute the function
Update v1.02 - Added player specific prefixes. Uploaded a demo map
Update v1.03 - fixed a silly silly thing regarding the boolean return type
Since I have been unable to find a CCS that simplifies adding commands to your project I figured I had to write it myself. Here I am presenting to you: The ChatCommandSystem written by Prozix (requires vJass)
JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Chat Command System ~~ By Prozix ~~ Version 1.03 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// =What is Chat Command System=
// - Chat Command System allows you to easily add chat commands to your map
//
// =Pros=
// - Easy
// - Time saver
// - Built in command listing
//
// =Cons=
// - Less efficient than doing everything manually
// - Only one parameter handled within the system, for more parameters use the string return type
// - If you use waits withing a command that has a return type, create a local variable and set it to the argument first
// newly called commands could overwrite the variables!
//
// =Functions=
// function CCS_SetPrefixPlayer takes string s , player p returns nothing Allows you to change the prefix for one player
//
// function CCS_SetPrefixAll takes string s returns nothing Changes the prefix for all players
//
// function CCS_GetPrefix takes nothing returns string Returns the prefix as a string
//
// function CCS_SetIntegerLength takes integer i returns nothing Up to how many characters will be read and converted to an integer after a command
//
// function CCS_SetRealLength takes integer i returns nothing Up to how many characters will be read and converted to a real after a command
//
// function CCS_SetStringLength takes integer i returns nothing Up to how many characters will be read and used as a string after a command
//
// function CCS_DisableChatCommand takes string command returns nothing Disables a command, the function you have assigned doesn't run anymore
//
// function CCS_EnableChatCommand takes string command returns nothing Enables a command
//
// function CCS_ListEnabledCommands takes player p returns nothing Prints the enabled commands for a player
//
// function CCS_ListAllCommands takes player p returns nothing Prints all commands for a player
//
// function CCS_GetChattingPlayer takes nothing returns player Returns the player that entered the chat message
//
// function CCS_GetBool takes nothing returns boolean If your command is a boolean command, this returns true or false
//
// function CCS_GetInt takes nothing returns integer If your command is an integer command, this returns the integer after the command
//
// function CCS_GetReal takes nothing returns real If your command is a real command, this returns the real after the command
//
// function CCS_GetStr takes nothing returns string If your command is a string command, this returns the string after the command
//
// function CCS_RegisterChatCommand takes string command, boolean enabled, integer commandType, code commandFunction returns nothing
// This function is used to register a chat command
// =Command types=
// - CCS_TYPE_NONE a chat command without parameters
// - CCS_TYPE_BOOL a chat command with an boolean value as a parameter
// - CCS_TYPE_INT a chat command with an integer as a parameter
// - CCS_TYPE_REAL a chat command with a real as a parameter
// - CCS_TYPE_STR a chat command with a string as a parameter
//
// =Customizing=
// - If you wan't boolean equivalents of "true" other than "yes", "on" or "1" you can change them in the globals section
//
// =How to import=
// - Create a trigger named CCS
// - Convert it to custom text and replace the whole trigger text with this.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library ChatCommandSystem initializer Init
globals
private constant integer CCS_MAXCOMMANDS = 2000
private constant integer CCS_MAX_PLAYERS = 12
//command prefix
private constant string CCS_DEFAULT_PREFIX = "-"
private string array CCS_CommandPrefix[CCS_MAX_PLAYERS]
private player CCS_ChattingPlayer
private boolean CCS_Bool
private integer CCS_Int
private real CCS_Real
private string CCS_Str
private integer CCS_Index = 0 //the index of the command struct
//yes equivalents, no array because it are only 4 vars and i want to initialize them here
constant string CCS_YES1 = "yes"
constant string CCS_YES2 = "true"
constant string CCS_YES3 = "on"
constant string CCS_YES4 = "1"
//no equivalents
constant string CCS_NO1 = "no"
constant string CCS_NO2 = "false"
constant string CCS_NO3 = "off"
constant string CCS_NO4 = "0"
//type enum
constant integer CCS_TYPE_NONE = 0
constant integer CCS_TYPE_BOOL = 1
constant integer CCS_TYPE_INT = 2
constant integer CCS_TYPE_REAL = 3
constant integer CCS_TYPE_STR = 4
//type length
private integer CCS_TYPE_NONE_LENGTH = 0
private integer CCS_TYPE_BOOL_LENGTH = 0 //initialized at init function
private integer CCS_TYPE_INT_LENGTH = 10 //up to how much characters
private integer CCS_TYPE_REAL_LENGTH = 10 //we will search for the type starting from Length(prefix+command)
private integer CCS_TYPE_STR_LENGTH = 20
endglobals
private struct ChatCommand extends array
string command
boolean enabled
integer commandType
trigger action
endstruct
//===SYSTEM FUNCTIONS================================================================
//system variables
function CCS_SetPrefixPlayer takes string s , player p returns nothing
set CCS_CommandPrefix[GetPlayerId(p)] = s
endfunction
function CCS_SetPrefixAll takes string s returns nothing
local integer i = 0
loop
set CCS_CommandPrefix<i> = s
set i = i + 1
exitwhen i==CCS_MAX_PLAYERS
endloop
endfunction
function CCS_GetPrefix takes player p returns string
return CCS_CommandPrefix[GetPlayerId(p)]
endfunction
function CCS_SetIntegerLength takes integer i returns nothing
set CCS_TYPE_INT_LENGTH = i
endfunction
function CCS_SetRealLength takes integer i returns nothing
set CCS_TYPE_REAL_LENGTH = i
endfunction
function CCS_SetStringLength takes integer i returns nothing
set CCS_TYPE_STR_LENGTH = i
endfunction
//enable/disable (very inefficient but you don't need to keep track of command ID's this way)
function CCS_DisableChatCommand takes string command returns nothing
local integer i = 0
loop
if ChatCommand<i>.command == command and ChatCommand<i>.enabled then
set ChatCommand<i>.enabled = false
endif
set i = i+1
exitwhen i==CCS_Index
endloop
endfunction
function CCS_EnableChatCommand takes string command returns nothing
local integer i = 0
loop
if ChatCommand<i>.command == command and not ChatCommand<i>.enabled then
set ChatCommand<i>.enabled = true
endif
set i = i+1
exitwhen i==CCS_Index
endloop
endfunction
//print commands for a player
function CCS_ListEnabledCommands takes player p returns nothing
local integer i = 0
local string s
loop
if ChatCommand<i>.enabled then
set s = "("+I2S(i)+"): |cffff0000"+CCS_CommandPrefix[GetPlayerId(p)]+ChatCommand<i>.command+"|r"
if ChatCommand<i>.commandType == CCS_TYPE_BOOL then
set s = s + "|cff5555ff type: boolean|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_INT then
set s = s + "|cff5555ff type: integer|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_REAL then
set s = s + "|cff5555ff type: real|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_STR then
set s = s + "|cff5555ff type: string|r"
endif
call DisplayTimedTextToPlayer(p, 0, 0, 30.0, s)
endif
set i = i+1
exitwhen i==CCS_Index
endloop
endfunction
function CCS_ListAllCommands takes player p returns nothing
local integer i = 0
local string s
loop
set s = "("+I2S(i)+"): |cffff0000"+CCS_CommandPrefix[GetPlayerId(p)]+ChatCommand<i>.command+"|r"
if ChatCommand<i>.commandType == CCS_TYPE_BOOL then
set s = s + "|cff5555ff type: boolean|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_INT then
set s = s + "|cff5555ff type: integer|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_REAL then
set s = s + "|cff5555ff type: real|r"
elseif ChatCommand<i>.commandType == CCS_TYPE_STR then
set s = s + "|cff5555ff type: string|r"
endif
call DisplayTimedTextToPlayer(p, 0, 0, 30.0, s)
set i = i+1
exitwhen i==CCS_Index
endloop
endfunction
//get functions for use in chat actions
function CCS_GetChattingPlayer takes nothing returns player
return CCS_ChattingPlayer
endfunction
function CCS_GetBool takes nothing returns boolean
return CCS_Bool
endfunction
function CCS_GetInt takes nothing returns integer
return CCS_Int
endfunction
function CCS_GetReal takes nothing returns real
return CCS_Real
endfunction
function CCS_GetStr takes nothing returns string
return CCS_Str
endfunction
function CCS_RegisterChatCommand takes string command, boolean enabled, integer commandType, code commandFunction returns nothing
set ChatCommand[CCS_Index].command = command
set ChatCommand[CCS_Index].enabled = enabled
set ChatCommand[CCS_Index].commandType = commandType
set ChatCommand[CCS_Index].action = CreateTrigger()
call TriggerAddAction(ChatCommand[CCS_Index].action, commandFunction)
set CCS_Index = CCS_Index+1
if CCS_Index == CCS_MAXCOMMANDS then
call BJDebugMsg("|cffff0000Chat Command System Warning: Too much commands registered, rendered the system unable to function properly|r")
set CCS_Index = 0
endif
//call BJDebugMsg("Registered command("+I2S(CCS_Index)+"): "+CCS_CommandPrefix+ChatCommand[CCS_Index-1].command)
endfunction
//============================================================================================
private function Actions takes nothing returns nothing
local string EnteredChat = GetEventPlayerChatString()
local string ChatSub = ""
local integer SubLength = 0
local integer i = 0
local boolean execute
local integer pId
set CCS_ChattingPlayer = GetTriggerPlayer()
set pId = GetPlayerId(CCS_ChattingPlayer)
loop
if ChatCommand<i>.enabled then
set SubLength = StringLength(CCS_CommandPrefix[pId]+ChatCommand<i>.command)
set ChatSub = SubString(EnteredChat, 0, SubLength)
if ChatSub == CCS_CommandPrefix[pId]+ChatCommand<i>.command then
set execute = true
if ChatCommand<i>.commandType == CCS_TYPE_BOOL then
set ChatSub = SubString(EnteredChat, SubLength + 1, SubLength + CCS_TYPE_BOOL_LENGTH + 1) //+1 for the space
if ChatSub == CCS_YES1 or ChatSub == CCS_YES2 or ChatSub == CCS_YES3 or ChatSub == CCS_YES4 then //allow 1 extra space before the parameter
set CCS_Bool = true
elseif ChatSub == CCS_NO1 or ChatSub == CCS_NO2 or ChatSub == CCS_NO3 or ChatSub == CCS_NO4 then //allow 1 extra space before the parameter
set CCS_Bool = false
else
call DisplayTimedTextToPlayer(GetTriggerPlayer(), 0, 0, 10, "|cffff0000false parameters for "+CCS_CommandPrefix[pId]+ChatCommand<i>.command+"|r") //you can outcomment this
set execute = false
endif
elseif ChatCommand<i>.commandType == CCS_TYPE_INT then
set CCS_Int = S2I(SubString(EnteredChat, SubLength + 1, SubLength + CCS_TYPE_INT_LENGTH + 1))
elseif ChatCommand<i>.commandType == CCS_TYPE_REAL then
set CCS_Real = S2R(SubString(EnteredChat, SubLength + 1, SubLength + CCS_TYPE_REAL_LENGTH + 1))
elseif ChatCommand<i>.commandType == CCS_TYPE_STR then
set CCS_Str = SubString(EnteredChat, SubLength + 1, SubLength + CCS_TYPE_STR_LENGTH + 1)
endif
if execute then
call TriggerExecute(ChatCommand<i>.action)
endif
endif
endif
set i = i + 1
exitwhen i == CCS_Index
endloop
endfunction
//============================================================================================
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
loop
call TriggerRegisterPlayerChatEvent(t, Player(i), "", false)
set i = i+1
exitwhen i==CCS_MAX_PLAYERS
endloop
call CCS_SetPrefixAll(CCS_DEFAULT_PREFIX)
call TriggerAddAction(t, function Actions)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_YES1)
if StringLength(CCS_YES2) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_YES2)
endif
if StringLength(CCS_YES3) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_YES3)
endif
if StringLength(CCS_YES4) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_YES4)
endif
if StringLength(CCS_NO1) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_NO1)
endif
if StringLength(CCS_NO2) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_NO2)
endif
if StringLength(CCS_NO3) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_NO3)
endif
if StringLength(CCS_NO4) > CCS_TYPE_BOOL_LENGTH then //NEAT (H)
set CCS_TYPE_BOOL_LENGTH = StringLength(CCS_NO4)
endif // /end_the_con's_of_non_array_arrays_^o^
endfunction
endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
And here is how to use it:
JASS:
scope AllPlayerChatCommands initializer Init
//command 1
private function HelpActions takes nothing returns nothing
call DisplayTextToPlayer(CCS_GetChattingPlayer(), 0, 0, "|cff00ff00commands: |r")
call CCS_ListEnabledCommands(CCS_GetChattingPlayer())
call DisplayTextToPlayer(CCS_GetChattingPlayer(), 0, 0, "|cff00ff00----------|r")
endfunction
//command 2
private function BoolActions takes nothing returns nothing
if CCS_GetBool() then
call BJDebugMsg("Yes, it039;s true")
else
call BJDebugMsg("No, it039;s not true")
endif
endfunction
//command 3
private function AddGold takes nothing returns nothing
call AdjustPlayerStateBJ(CCS_GetInt(), CCS_GetChattingPlayer(), PLAYER_STATE_RESOURCE_GOLD)
endfunction
//command 4
private function ShowReal takes nothing returns nothing
call DisplayTimedTextToPlayer(CCS_GetChattingPlayer(), 0, 0, 30.0, "You have entered: "+R2S(CCS_GetReal()))
endfunction
//command 5
private function Disabled takes nothing returns nothing
call BJDebugMsg("This will not show <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" />")
endfunction
//command 6
private function TauntEnemies takes nothing returns nothing
local integer i = 0
local player p = CCS_GetChattingPlayer()
loop
if IsPlayerEnemy(p, Player(i)) then
call DisplayTimedTextToPlayer(Player(i), 0, 0, 30.0, GetPlayerName(p)+": I039;m going to crush you MUAHAHA")
endif
set i = i+1
exitwhen i==12
endloop
endfunction
//command 7
private function GoodJob takes nothing returns nothing
local integer i = 0
local player p = CCS_GetChattingPlayer()
loop
if IsPlayerAlly(p, Player(i)) then
if p==Player(i) then //Player(i) is yourself
call DisplayTimedTextToPlayer(Player(i), 0, 0, 30.0, "You praise everyone on your team for doing a good job. ")
else
call DisplayTimedTextToPlayer(Player(i), 0, 0, 30.0, GetPlayerName(p)+": Good job "+GetPlayerName(Player(i)))
endif
endif
set i = i+1
exitwhen i==12
endloop
endfunction
//command 8
private function SetName takes nothing returns nothing
call SetPlayerName(CCS_GetChattingPlayer(), CCS_GetStr())
endfunction
//command 9
private function SetPrefix takes nothing returns nothing
call CCS_SetPrefixPlayer(CCS_GetStr(), CCS_GetChattingPlayer())
endfunction
//command 10
private function CommandFog takes nothing returns nothing
if CCS_GetBool() then
call FogEnableOn()
call FogMaskEnableOn()
else
call FogEnableOff()
call FogMaskEnableOff()
endif
endfunction
//command registering
private function Init takes nothing returns nothing
call CCS_SetPrefixAll("-") //redundant, default is "-", just to show you can use this
call CCS_RegisterChatCommand("help", true, CCS_TYPE_NONE, function HelpActions)
call CCS_RegisterChatCommand("bool", true, CCS_TYPE_BOOL, function BoolActions)
call CCS_RegisterChatCommand("addgold", true, CCS_TYPE_INT, function AddGold)
call CCS_RegisterChatCommand("real", true, CCS_TYPE_REAL, function ShowReal)
call CCS_RegisterChatCommand("disabled", true, CCS_TYPE_NONE, function Disabled)
call CCS_DisableChatCommand("disabled")
call CCS_RegisterChatCommand("taunt", false, CCS_TYPE_NONE, function TauntEnemies)
call CCS_EnableChatCommand("taunt")
call CCS_RegisterChatCommand("gj", true, CCS_TYPE_NONE, function GoodJob)
call CCS_RegisterChatCommand("setname", true, CCS_TYPE_STR, function SetName)
call CCS_RegisterChatCommand("setprefix", true, CCS_TYPE_STR, function SetPrefix)
call CCS_RegisterChatCommand("fog", true, CCS_TYPE_BOOL, function CommandFog)
endfunction
endscope
Suggestions are welcome, enjoy:thup:
Update v1.01 - Added ListCommand functions. Passing invalid parameters to a boolean command now doesn't execute the function
Update v1.02 - Added player specific prefixes. Uploaded a demo map
Update v1.03 - fixed a silly silly thing regarding the boolean return type