MCServer Script Tips & Tricks

The custom C++ server (mc.ae-c.net:7777)
GO HERE INSTEAD -> http://forum.mc-server.org/

Moderator: FakeTruth

Locked
Tybor
Posts: 13
Joined: Mon Jan 24, 2011 5:34 pm
Minecraft IGN: Tybor

MCServer Script Tips & Tricks

Post by Tybor » Mon Jan 24, 2011 5:44 pm

Hi,

I want to start this for a collection of useful information and code snippets for MCServer scripting.
So contribute anything you think it goes with the topic.

Script Template (empty with only necessary functions):

Code: Select all

-- some defines
local PLUGIN_NAME = "Template"
local PLUGIN_VERSION = 1
--

local TemplatePlugin = {}
TemplatePlugin.__index = TemplatePlugin

function TemplatePlugin:new()
	local t = {}
	setmetatable(t, TemplatePlugin)
	local w = Lua__cPlugin:new()
	tolua.setpeer(w, t)
	w:tolua__set_instance(w)
	return w
end

function TemplatePlugin:OnDisable()
	Log( self:GetName() .. " v." .. self:GetVersion() .. " is shutting down..." )
end

function TemplatePlugin:Initialize()
	self:SetName( PLUGIN_NAME )
	self:SetVersion( PLUGIN_VERSION )

	PluginManager = cPluginManager:GetPluginManager()
	-- add to some hooks
  -- PluginManager:AddHook( self, cPluginManager.E_PLUGIN_CHAT )
	-- PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_DIG )
	-- PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_PLACE )

	-- add some commands
  -- self:AddCommand("/test", " - test command")

	Log( "Initialized " .. self:GetName() .. " v." .. self:GetVersion() )
	return true;
end

-- create new instance of plugin
cPluginManager:GetPluginManager():AddPlugin( TemplatePlugin:new() )
Lua Replacement for LuaStringSplit (I learned a lot finding out why I can't use # on LuaStringSplit result ;-):

Code: Select all

-- from http://lua-users.org/wiki/SplitJoin
function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end
Alternative OnChat implementation (Lua only):

Code: Select all

function TemplatePlugin:OnChat( Message, Player )
  if( Message:byte(1) ~= string.byte('/',1) ) then -- Not a command
		return false
	end

	local SplitMsg = Split(Message, " +")
	if(SplitMsg[1] == "/area") then
Hope this helps someone anytime :P

greetz,
Tybor

User avatar
ker
Posts: 18
Joined: Wed Jan 19, 2011 2:18 pm
Minecraft IGN: ker_

Re: MCServer Script Tips & Tricks

Post by ker » Mon Jan 24, 2011 6:04 pm

oh templates :D
here's what mine has more than yours:

Code: Select all

-- called when a player placed a block
function TEMPLATE_NAME:OnBlockPlace( PacketData, Player )
	local X = PacketData.m_PosX
	local Y = PacketData.m_PosY
	local Z = PacketData.m_PosZ
	local X2, local Y2, local Z2 = AddDirection( X, Y, Z, PacketData.m_Direction ) -- the actual position of the placed block
	local item_type = PacketData.m_ItemType; -- the type of item used to call the place command
	local item_amount = PacketData.m_Count; -- the number of items in that slot
	local item_uses = PacketData.m_Uses; -- the number of uses the item has left before it breaks
	return false
end

-- called while a player is digging
function TEMPLATE_NAME:OnBlockDig( PacketData, Player )
	local X = PacketData.m_PosX
	local Y = PacketData.m_PosY
	local Z = PacketData.m_PosZ
	local X2, local Y2, local Z2 = AddDirection( X, Y, Z, PacketData.m_Direction ); -- the block which is on the side where digging is happening
	if(PacketData.m_Status == 3) then -- block broken
	elseif(PacketData.m_Status == 0) then -- started digging
	elseif(PacketData.m_Status == 1) then -- dig update
	elseif(PacketData.m_Status == 2) then -- dig aborted
	end
	return false
end

-- called many times per second
function TEMPLATE_NAME:Tick( DeltaTime )
	if( self.NumTicks < 50 ) then	-- only repeat this every 50 ticks
		self.NumTicks = self.NumTicks + 1
		return
	end
	self.NumTicks = 0
	local debug = true; -- a debug variable is always useful

	local World = cWorld:GetWorld() -- access to the world
end




User avatar
FakeTruth
Site Admin
Posts: 1802
Joined: Tue Oct 26, 2010 10:29 pm
Minecraft IGN: FakeTruth

Re: MCServer Script Tips & Tricks

Post by FakeTruth » Mon Jan 24, 2011 9:36 pm

Nice, nice. I stickied this.

However, your LuaStringSplit version is gonna be deprecated in next version :D
I finally found a way to return tables instead of class objects.

Before, LuaStringSplit returned a cStringVector object ( that's why you can't use # on it, it's an object like Player ). But now I return a lua table, which you can iterate through with

Code: Select all

for i,v in ipairs( LuaStringSplit(Message, " ") ) do
    Log( v )
end
And you can also do

Code: Select all

Log( #Split )
:D You can do anything you can do with tables :D yaay~

Be warned that this will break existing loops and such, if you use indexing (eg. [1] or [2] ) for the Split, there's no problem. Using Split:size() will give an error though
Image

Locked

Who is online

Users browsing this forum: No registered users and 1 guest