Skip to content

Commit 986136b

Browse files
committed
Missing OOP examples
1 parent d80e343 commit 986136b

File tree

76 files changed

+761
-55
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+761
-55
lines changed

functions/Account/addAccount.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ server:
1010
- Maximum account password length was 30 characters until version **1.5.4-11138**. Currently there is no upper limit.
1111
oop:
1212
element: Account
13+
static: true
1314
method: add
1415
description: This function adds an [[account]] to the list of registered accounts of the current server.
1516
parameters:
@@ -30,11 +31,14 @@ server:
3031
description: Returns an [[account]] or false if the account already exists or an error occured.
3132
examples:
3233
- path: 'examples/addAccount-1.lua'
33-
description: This enables players to register on your server by using /register <password> in the chat window.
34+
description: This enables players to register on your server by using <code>/register &lt;password&gt;</code> in the chat window.
3435
- path: 'examples/addAccount-2.lua'
35-
description: This enables players to register on your server by using /register <username> <password> in the chat window.
36+
description: This enables players to register on your server by using <code>/register &lt;username&gt; &lt;password&gt;</code> in the chat window.
3637
- path: 'examples/addAccount-3.lua'
37-
description: This code differs again so the user can only register once /register <username> <password>.
38+
description: This code differs again so the user can only register once <code>/register &lt;username&gt; &lt;password&gt;</code>.
39+
- path: examples/addAccount_OOP-1.lua
40+
description: This enables players to register on your server by using <code>/register &lt;password&gt;</code> in the chat window.
41+
oop: true
3842
meta:
3943
- changelog:
4044
- version: 1.5.4-11138
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function registerPlayer ( source, commandName, password )
2+
-- Check if the password field is blank or not (only blank if they didnt enter one)
3+
if ( password and #password > 0 ) then
4+
--Attempt to add the account, and save its value in a var
5+
local accountAdded = Account.add(source.name, password)
6+
if ( accountAdded ) then
7+
-- Tell the user all is done
8+
source:outputChat ( "Thank you " .. source.name .. ", you're now registed, you can login with /login" )
9+
else
10+
-- There was an error making the account, tell the user
11+
source:outputChat ( "Error creating account, contact the server admin" )
12+
end
13+
else
14+
-- There was an error in the syntax, tell the user the correct syntax.
15+
source:outputChat ( "Error creating account, correct syntax: /register <password>" )
16+
end
17+
end
18+
addCommandHandler ( "register", registerPlayer ) -- add the command handler
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
addCommandHandler("checkaccount",
2-
function(player, cmd, account)
3-
if hasObjectPermissionTo(player, "function.banPlayer" ) then -- if the player typing /checkaccount command has permission to banPlayer
4-
if account and account ~= "" then -- if the account name was mentioned
5-
if getAccount(account) then -- if the account exists
6-
outputChatBox("Account "..account.." exists in the database!", player, 0, 255, 0)
7-
else -- if the account doesn't exist
8-
outputChatBox("Account "..account.." does not exist in database", player, 0, 255, 0)
9-
end
10-
else
11-
outputChatBox("Syntax is /checkaccount [account name]", player, 255, 0, 0)
12-
end
13-
end
14-
end
15-
)
1+
addCommandHandler("checkaccount", function(player, cmd, account)
2+
if hasObjectPermissionTo(player, "function.banPlayer" ) then -- if the player typing /checkaccount command has permission to banPlayer
3+
if (account and account ~= "") then -- if the account name was mentioned
4+
if (getAccount(account)) then -- if the account exists
5+
outputChatBox("Account "..account.." exists in the database!", player, 0, 255, 0)
6+
else -- if the account doesn't exist
7+
outputChatBox("Account "..account.." does not exist in database", player, 0, 255, 0)
8+
end
9+
else
10+
outputChatBox("Syntax is /checkaccount [account name]", player, 255, 0, 0)
11+
end
12+
end
13+
end)

functions/Account/examples/getAccountByID-1.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
addCommandHandler("getAccount", function (player, cmd, id)
22
local id = tonumber(id)
3-
local account = getAccountByID(id)
3+
local account = getAccountByID(id)
44
if (account) then
5-
outputChatBox("The name of the account with that ID is: "..getAccountName(account), player)
5+
outputChatBox("The name of the account with that ID is: "..getAccountName(account), player)
66
else
77
outputChatBox("There is no account with this ID.", player)
88
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
addCommandHandler("checkaccount", function(player, cmd, account)
2+
if ACL.hasObjectPermissionTo(player, "function.banPlayer" ) then -- if the player typing /checkaccount command has permission to banPlayer
3+
if (account and account ~= "") then -- if the account name was mentioned
4+
if (Account(account)) then -- if the account exists
5+
player:outputChat("Account "..account.." exists in the database!", 0, 255, 0)
6+
else -- if the account doesn't exist
7+
player:outputChat("Account "..account.." does not exist in database", 0, 255, 0)
8+
end
9+
else
10+
player:outputChat("Syntax is /checkaccount [account name]", 255, 0, 0)
11+
end
12+
end
13+
end)

functions/Account/getAccount.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ server:
2424
examples:
2525
- path: 'examples/getAccount-1.lua'
2626
description: This function checks if the account mentioned exists in the internal.db database file.
27+
- path: 'examples/getAccount_OOP-1.lua'
28+
description: This function checks if the account mentioned exists in the internal.db database file.
29+
oop: true
2730

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local newFile = File.new("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
newFile:write("This is a test file!") -- write a text line
4+
newFile:close() -- close the file once you're done with it
5+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addEventHandler("onClientResourceStart", resourceRoot, function(res)
2+
local filePath = ":"..res.name.."/test.txt"
3+
File.new(filePath) --create the file in this resource and name it 'test.txt'.
4+
if File.copy(filePath,":"..res.name.."/test1.txt") then
5+
outputChatBox("File was successfully copied!", 0, 100, 0)
6+
else
7+
outputChatBox("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0)
8+
end
9+
end)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addEventHandler("onResourceStart", resourceRoot, function(res)
2+
local filePath = ":"..res.name.."/test.txt"
3+
File.new(filePath) --create the file in this resource and name it 'test.txt'.
4+
if File.copy(filePath, ":"..res.name.."/test1.txt") then
5+
root:outputChat("File was successfully copied!", 0, 100, 0)
6+
else
7+
root:outputChat("File was not successfully copied, probably because it doesn't exist.", 100, 0, 0)
8+
end
9+
end)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local newFile = File.new("test.txt") -- attempt to create a new file
2+
if (newFile) then -- check if the creation succeeded
3+
newFile:write("This is a test file!") -- write a text line
4+
newFile:close() -- close the file once you're done with it
5+
end

0 commit comments

Comments
 (0)