88 lines
2.3 KiB
Lua
88 lines
2.3 KiB
Lua
---@class BenUtils
|
|
local BenUtils = {}
|
|
BenUtils.__index = BenUtils
|
|
|
|
local UEHelpers = require("UEHelpers")
|
|
local TextLib = UEHelpers:GetKismetTextLibrary()
|
|
|
|
-- Globals
|
|
local commands_registered = {}
|
|
|
|
-- End Globals
|
|
|
|
-- Internal functions
|
|
|
|
|
|
-- End internal functions
|
|
|
|
-- Utility Functions
|
|
|
|
---@return self
|
|
function BenUtils.new()
|
|
local self = setmetatable({}, BenUtils)
|
|
return self
|
|
end
|
|
|
|
function BenUtils.ShuffleInPlace(t)
|
|
for i = #t, 2, -1 do
|
|
local j = math.random(i)
|
|
t[i], t[j] = t[j], t[i]
|
|
end
|
|
end
|
|
|
|
---@param command string
|
|
---@param callback function
|
|
function BenUtils.RegisterCommand(command, callback)
|
|
commands_registered[command] = callback
|
|
-- AddRegisteredCommand(command, callback)
|
|
print(string.format("[BenUtils] Registered command %s", command))
|
|
end
|
|
|
|
---@param PlayerName string
|
|
function BenUtils.GetPlayerStateByName(PlayerName)
|
|
local crabs = FindAllOf("CrabPS")
|
|
for _, v in ipairs(crabs) do
|
|
-- local crab = v:get()
|
|
if v.PlayerNamePrivate:ToString() == PlayerName then
|
|
return v
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
---@param input string
|
|
function BenUtils.StringToFString(input)
|
|
-- I have to first convert the input to an FText
|
|
-- Then I can call the Conv_TextToString function to get an FSTring
|
|
local ftext_obj = FText(input)
|
|
return TextLib:Conv_TextToString(ftext_obj)
|
|
end
|
|
|
|
---@param PlayerName FString
|
|
---@param ChatMessage FString
|
|
function BenUtils.ParseChatCommand(self, PlayerName, ChatMessage)
|
|
local playername = PlayerName:get()
|
|
local chatmessage = ChatMessage:get()
|
|
local firstchar = string.sub(chatmessage:ToString(), 1, 1)
|
|
---@type table
|
|
local command = {}
|
|
if firstchar == "/" then
|
|
for i in string.gmatch(chatmessage:ToString(), "%S+") do
|
|
table.insert(command, i)
|
|
end
|
|
local cmd = string.sub(command[1], 2, #command[1])
|
|
-- local args = table.remove(command, 1)
|
|
if commands_registered[cmd] then
|
|
commands_registered[cmd](playername:ToString(), table.unpack(command, 2, #command))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- End utility functions
|
|
|
|
-- Register Util hooks here
|
|
RegisterHook("/Script/CrabChampions.CrabPC:ClientOnReceivedChatMessage", BenUtils.ParseChatCommand)
|
|
-- End registry hooks
|
|
|
|
return BenUtils
|