Function Creating
#1
Not sure if this goes here but I thought I would create a function to display server rules and decided to use the motd function from core as a template for it since it basically does the same thing. Now I've added it to the core.deproj file, info.lua file, and created a rules.lua file with some modifications. Everything works except one thing, it reads from the motd.txt and doesn't create rules.txt like I wanted it to. Is there something I'm missing like a variable defined elsewhere? I would also like to add I'm completely new here and just started using this today because I despise java and am so happy I found a C++ alternative. I'm also completely new to LUA but I do have a little knowledge in C++, PHP, and JS/jQuery so I'm not completely dumb.

core.deproj code:
  <file>
    <filename>rules.lua</filename>
  </file>
info.lua code:
		["/rules"] = 
		{
			Permission = "core.rules",
			Handler = HandleRulesCommand,
			HelpString = " - Show rules",
		},
rules.lua code:
function HandleRulesCommand( Split, Player )
	ShowRulesTo( Player )
	return true
end

function LoadRules()

	local File = io.open( "rules.txt", "r" )

	-- Check if the file 'rules.txt' exists, else create it.
	if not File then
		CreateFile = io.open( "rules.txt", "w" )
		CreateFile:write("@6Server Rules")
		CreateFile:close()
	else
		File:close()
	end

	for line in io.lines( "rules.txt" ) do
		local TempMessage = line
		-- Do a for loop that goes to each char in the line.
		for I=1, string.len( TempMessage ) do
			-- If the char is a '@' then check if the next char represents a color.
			if string.sub( TempMessage, I, I ) == "@" then
				local Char = string.sub( TempMessage, I + 1, I + 1 )
				local Color = ReturnColorFromChar( TempMessage, Char )
				-- If the next char represented a color then put the color in the string.
				if Color ~= nil then
					TempMessage = string.gsub( TempMessage, "@" .. Char, Color )
				end
			end
		end
		-- Add the message to the list of messages.
		Messages[#Messages + 1] = TempMessage
	end

end

function ShowRulesTo( Player )
	for I=1, #Messages do
		Player:SendMessage(Messages[I])
	end
end
Reply
Thanks given by:
#2
Hey,

Okay, I miss the Initialize function. For an example plugin take a look here
http://mc-server.xoft.cz/LuaAPI/Writing-...lugin.html

Good luck,
Seadragon91
Reply
Thanks given by:
#3
Hmmm, I didn't see that for the motd one. Is that one coded into the server itself?
Reply
Thanks given by:
#4
Hello, welcome to the forum. (I've moved your question to a more appropriate section)
I think your color-processing code is wrong, especially line 29. What is it exactly that you want to do with the colors?
I'd try to comment out the processing code for now, and simply send the contents of the file, as is, and work from there.
I'd also either use an IDE such as ZeroBrane studio or Decoda, those two are known to support live debugging within MCServer. See the articles in the API help for how to set those up:
http://mc-server.xoft.cz/LuaAPI/SettingUpDecoda.html
http://mc-server.xoft.cz/LuaAPI/SettingUpZeroBrane.html
If that is not an option, consider using a LOG() function in key places to see the values of your variables.
I suppose there's no error report in the server log?

@Seadragon91: He's adding this code to Core, so there is already an Initialize function there.
Reply
Thanks given by:
#5
Each plugin needs to have exactly one Initialize function, if you're adding code to an existing plugin, then you shouldn't provide another one, there's already one in the plugin.
The Initialize function is the first thing that the server calls after it loads the plugin.
Reply
Thanks given by:
#6
Thanks for moving it to the appropriate area and I apologize for that. For the color processing code I didn't modify that, maybe that's the issue? Also would the LOG() function need anything to be passed and where could I find syntax (if needed) for it?
Reply
Thanks given by:
#7
No problem.
After reading the color processing over once again, I think it may work, although it's extremely poorly written.
The LOG function takes a single string as the argument and outputs the string into the server's console and logfile. Similar with LOGINFO, LOGWARNING and LOGERROR, each used for the appropriate severity of the logged message. They're documented in the API docs in the Globals section:
http://mc-server.xoft.cz/LuaAPI/Globals.html#functions

Oh wait. Who calls the LoadRules() function? Someone needs to call it once on server initialization - most likely in the Initialize function. Without it, the messages are not initialized so they won't display.
Also, you'll need to initialize the Messages variable. As it stands, Lua parses it as "global variable", thus it has the value nil by default, and using it as an (array-)table will crash the plugin.
I recommend making as much stuff as possible local:
local Rules = {}  -- the container for the rules, empty array by default
local function ShowRulesTo(Player)
...
function HandleRulesCommand(...)  -- must be global because it's called from another file
...
function LoadRules(...)  -- must be global because it's called from another file.
Reply
Thanks given by:
#8
Is there a way to add the function to RegisterPluginInfoCommands();? Also, I tried making things local and when I call the function on load everything works however it still shows the motd with it.

local Rules = {}
function HandleRulesCommand( Split, Player )
	ShowRulesTo( Player )
	return true
end
function LoadRules()

	local Rules = io.open( "rules.txt", "r" )

	-- Check if the file 'rules.txt' exists, else create it.
	if not Rules then
		CreateRule = io.open( "rules.txt", "w" )
		CreateRule:write("@6It works!")
		CreateRule:close()
	else
		Rules:close()
	end
	
	for line in io.lines( "rules.txt" ) do
		local rools = line
		-- Do a for loop that goes to each char in the line.
		for I=1, string.len( rools ) do
			-- If the char is a '@' then check if the next char represents a color.
			if string.sub( rools, I, I ) == "@" then
				local Char = string.sub( rools, I + 1, I + 1 )
				local Color = ReturnColorFromChar( rools, Char )
				-- If the next char represented a color then put the color in the string.
				if Color ~= nil then
					rools = string.gsub( rools, "@" .. Char, Color )
				end
			end
		end
		-- Add the message to the list of messages.
		Messages[#Messages + 1] = rools
	end

end

function ShowRulesTo( Player )
	for I=1, #Messages do
		Player:SendMessage(Messages[I])
	end
end
Reply
Thanks given by:
#9
That's because you're reading the text into a global variable Messages which is then overwritten by the motd.lua file. Read the texts into Rules (the one at the top) and display those. This is exactly the reason for making everything local - to avoid silent collisions with other files.
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)