Custom scripts

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

Moderator: FakeTruth

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

Custom scripts

Post by ker » Thu Jan 20, 2011 3:16 pm

Since noone has started a thread yet, here it is.

If you got any useful scripts, post them here :)

Water that behaves (almost) just like Minecraft Water and doesn't take up any computation time once a flow has found it's path to flow along at.

2nd script is for some cheats, like /give [item id] [item amount]

Code: Select all

local WaterPlugin = {}
WaterPlugin.__index = WaterPlugin

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

function WaterPlugin:OnDisable()
	Log( Plugin:GetName() .. " v." .. Plugin:GetVersion() .. " is shutting down..." )
end

function WaterPlugin:Initialize()
	self:SetName( "Water" )
	self:SetVersion( 1 )

	PluginManager = cPluginManager:GetPluginManager()
	PluginManager:AddHook( self, cPluginManager.E_PLUGIN_TICK )
	PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_DIG )
	PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_PLACE )
	self.WaterBlocks = {};
	self.WaterBlocks = {};

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

function IsFreeForWater(block)
	return block == E_BLOCK_AIR 
		or block == E_BLOCK_TORCH 
		or block == E_BLOCK_MINECART_TRACKS 
		or block == E_BLOCK_REDSTONE_TORCH_ON 
		or block == E_BLOCK_REDSTONE_TORCH_OFF;
end

function WaterPlugin:OnBlockPlace( PacketData, Player )

	local X = PacketData.m_PosX
	local Y = PacketData.m_PosY
	local Z = PacketData.m_PosZ
	X, Y, Z = AddDirection( X, Y, Z, PacketData.m_Direction )
	local t = PacketData.m_ItemType; -- the type of item used to call the place command
	Log("water: " .. X .. "," .. Y .. "," .. Z .. " -- adding due to Place... " .. t);

	local World = cWorld:GetWorld();
	if(t == 326) then -- water bucket
		World:SetBlock(X, Y, Z, E_BLOCK_WATER, 0);
	elseif(t == 8) then
		World:SetBlock(X, Y, Z, E_BLOCK_WATER, 0);
	end
	self:AddSurroundings(X, Y, Z);
	self:AddBlock(X, Y, Z);
	return false
end

function WaterPlugin:AddSurroundings(X, Y, Z)
	local level_points = {
		{X-1, Y, Z},
		{X+1, Y, Z},
		{X, Y, Z-1},
		{X, Y, Z+1},
		{X, Y+1, Z},
		{X, Y-1, Z},
		};
	for key,val in pairs(level_points) do
		self:AddBlock(val[1], val[2], val[3]);
	end
end

function WaterPlugin:AddBlock(X, Y, Z)
	local World = cWorld:GetWorld();
	local t = World:GetBlock(X, Y, Z);
	if(t == E_BLOCK_WATER or t == E_BLOCK_STATIONARY_WATER) then
		local found = false;
		for key,val in pairs(self.WaterBlocks) do
			if(val[1] == X and val[2] == Y and val[3] == Z) then
				found = true;
				Log("addblock found");
				break;
			end
		end
		if(not found) then
			Log("addblock adding");
			table.insert(self.WaterBlocks, {X, Y, Z});
		end
	end
end

function WaterPlugin:OnBlockDig( PacketData, Player )
	local X = PacketData.m_PosX
	local Y = PacketData.m_PosY
	local Z = PacketData.m_PosZ
	if(PacketData.m_Status == 3) then -- block broken
		Log("water: " .. X .. "," .. Y .. "," .. Z .. " -- adding due to dig... status is: " .. PacketData.m_Status);
		self:AddSurroundings(X, Y, Z);
	end
	return false
end

function WaterPlugin:Tick( DeltaTime )
	if( self.NumTicks < 5 ) then	-- Only spread every 10 ticks, to make sure it doesnt happen too fast
		self.NumTicks = self.NumTicks + 1
		return
	end
	local debug = true;
	self.NumTicks = 0

	local World = cWorld:GetWorld()

	local OldBlocks = self.WaterBlocks;
	self.WaterBlocks = {};
	for key,val in pairs(OldBlocks) do
		local X = val[1]
		local Y = val[2]
		local Z = val[3]
		local t = World:GetBlock(X, Y, Z);
		Log("processing: " .. X .. "," .. Y .. "," .. Z .. " -- " .. t);
		if(t == E_BLOCK_WATER) then
			local isFed = false;
			local f = World:GetBlockMeta(X, Y, Z);
			local f2 = f;
			if(f == 8) then
				local t2 = World:GetBlock(X, Y+1, Z);
				if(t2 == E_BLOCK_WATER) then
					isFed = true;
					local t3 = World:GetBlock(X, Y-1, Z);
					local f3 = World:GetBlockMeta(X, Y-1, Z);
					if(t3 == E_BLOCK_WATER and f3 == 8) then
						-- I'm falling, don't spread sideways
					else
						f = 0;
					end
				end
			elseif(f == 0) then
				isFed = true;
			else
				f2 = GetMaximumWaterAround(X, Y, Z);
				if(f2 < f) then
					isFed = true;
				end
			end
			if(isFed) then
				Log(" isFed");
				local down = World:GetBlock(X, Y-1, Z);
				if(IsFreeForWater(down)) then
					Log("  down");
					World:SetBlock(X, Y-1, Z, E_BLOCK_WATER, 8);
					self:AddBlock(X, Y-1, Z);
				else
					Log("  around");
					if(f == 8) then
						-- I'm falling
					elseif(f2+1 < f) then
						World:SetBlock(X, Y, Z, E_BLOCK_WATER, f2+1);
						self:AddBlock(X, Y, Z);
					elseif(f < 7) then
						Log("   enough power");
						local points = GetLowestPoints(X, Y, Z);
						for k,v in pairs(points) do
							local t2 = World:GetBlock(v[1], v[2], v[3]);
							if(t2 ~= E_BLOCK_WATER) then
								if(v[2] == Y) then
									World:SetBlock(v[1], v[2], v[3], E_BLOCK_WATER, f+1);
								else
									World:SetBlock(v[1], v[2], v[3], E_BLOCK_WATER, 8);
								end
								self:AddBlock(v[1], v[2], v[3]);
							else -- is water
								local f3 = World:GetBlockMeta(v[1], v[2], v[3]);
								if(f3 ~= 8 and f3 > f+1) then
									self:AddBlock(v[1], v[2], v[3]);
								end
							end
						end
					end
				end
			else
				Log(" not isFed");
				World:SetBlock(X, Y, Z, E_BLOCK_AIR, 0);
				self:AddSurroundings(X, Y, Z);
			end
		else
			Log("changed before tick, not processing none-water");
		end
	end
end

function GetMaximumWaterAround(X, Y, Z)
	local mx = 8;
	local level_points = {
		{X-1, Y, Z},
		{X+1, Y, Z},
		{X, Y, Z-1},
		{X, Y, Z+1},
		};
	local World = cWorld:GetWorld();
	for key,val in pairs(level_points) do
		local t = World:GetBlock(val[1], val[2], val[3]);
		if(t == E_BLOCK_STATIONARY_WATER) then
			mx = 0;
			break;
		elseif(t == E_BLOCK_WATER) then
			local f = World:GetBlockMeta(val[1], val[2], val[3]);
			if(f < mx) then
				mx = f;
			elseif(f == 8) then
				mx = 0;
			end
			if(mx == 0) then
				break;
			end
		end
	end
	return mx;
end

function GetLowestPoints(X, Y, Z)
	local World = cWorld:GetWorld()
	local t = World:GetBlock(X, Y-1, Z);
	local points = {};
	if(IsFreeForWater(t)) then
		table.insert(points, {X, Y-1, Z});
		return points;
	end
	local lower_points = {
		{X-1, Y-1, Z},
		{X+1, Y-1, Z},
		{X, Y-1, Z-1},
		{X, Y-1, Z+1},
		};
	local waterFound = false;
	for key,val in pairs(lower_points) do
		local t = World:GetBlock(val[1], val[2], val[3]);
		local t2 = World:GetBlock(val[1], Y, val[3]);
		if(IsFreeForWater(t) and IsFreeForWater(t2)) then
			table.insert(points, val);
			val[2] = Y;
			table.insert(points, val);
		elseif(t2 == E_BLOCK_WATER and (IsFreeForWater(t) or t == E_BLOCK_WATER)) then
			waterFound = true;
		end
	end
	if(#points == 0 and not waterFound) then
		local level_points = {
			{X-1, Y, Z},
			{X+1, Y, Z},
			{X, Y, Z-1},
			{X, Y, Z+1},
			};
		for key,val in pairs(level_points) do
			local t = World:GetBlock(val[1], val[2], val[3]);
			if(IsFreeForWater(t) or t == E_BLOCK_WATER) then
				table.insert(points, val);
			end
		end
	end
	return points;
end

Plugin = WaterPlugin:new()
cPluginManager:GetPluginManager():AddPlugin( Plugin )

And the Cheat-Plugin (will be extended to support more than just /give)

Code: Select all

local CheatPlugin = {}
CheatPlugin.__index = CheatPlugin

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

function CheatPlugin:OnDisable()
	Log( Plugin:GetName() .. " v." .. Plugin:GetVersion() .. " is shutting down..." )
end

function CheatPlugin:Initialize()
	self:SetName( "Cheat" )
	self:SetVersion( 1 )

	PluginManager = cPluginManager:GetPluginManager()
	PluginManager:AddHook( self, cPluginManager.E_PLUGIN_CHAT )

	self:AddCommand("/give", "[ware id] [ware amount]")

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

function CheatPlugin:OnChat( Message, Player )
	if( GetChar(Message, 0) ~= '/' ) then -- Not a command
		return
	end

	Server = cServer:GetServer()

	Split = LuaStringSplit(Message, " ")
	if(Split[0] == "/give") then		-- Cheating
		if(Split:size() ~= 3) then
			Server:SendMessage( cChatColor.Green .. "Usage: /give [ware id] [ware amount]", Player )
			return true
		end

		World = cWorld:GetWorld()
		if(not isValidItem(Split[1])) then
			Server:SendMessage( cChatColor.Green .. "Invalid Item Id", Player)
			return false;
		end

		ItemAmount = tonumber(Split[2]);
		if(ItemAmount < 1) then
			Server:SendMessage( cChatColor.Green .. "cannot give you less than 1 of something", Player)
		end

		if(ItemAmount > 64) then
			ItemAmount = 64
		end
		Item = cItem:new(Split[1], ItemAmount)
		Player:GetInventory():AddItem(Item);
		Server:SendMessage( cChatColor.Green .. "You now got " .. ItemAmount .. " of " .. Split[1], Player)
		return true
	end

	return false
end

Plugin = CheatPlugin:new()
cPluginManager:GetPluginManager():AddPlugin( Plugin )


Last edited by ker on Tue Jan 25, 2011 9:30 am, edited 4 times in total.

User avatar
knutremi
Posts: 2177
Joined: Tue Nov 30, 2010 7:59 pm
Minecraft IGN: knutremi
Location: Norway
Contact:

Re: Custom scripts

Post by knutremi » Thu Jan 20, 2011 8:20 pm

Pretty cool. Doesn't look like any scripting language I've ever seen before though. Is it a new language?
Don't beat yourself up.

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

Re: Custom scripts

Post by ker » Thu Jan 20, 2011 8:27 pm

lua is like a century old or sth... dunnow, look it up on wikipedia, but I've been using it for years

the plugin interface is somewhat nontrivial to use, but copypasta of the fire-plugin or teleport-plugin should easily get you started

User avatar
knutremi
Posts: 2177
Joined: Tue Nov 30, 2010 7:59 pm
Minecraft IGN: knutremi
Location: Norway
Contact:

Re: Custom scripts

Post by knutremi » Thu Jan 20, 2011 8:55 pm

I had a look at the Wikipedia article and boy, am I gonna implement Lua scripting in applications I write in the future. As I've learned basic programming in C++, Lua looks like it's gonna be a breeze to use.

So there's an actual chance I'll write plugins for MCServer sometime in the future.
Don't beat yourself up.

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

Re: Custom scripts

Post by FakeTruth » Fri Jan 21, 2011 12:00 am

Cool !!

It can pretty much flood your world, but pretty awesome plugin nonetheless :D
Image

uniwersel1
Posts: 71
Joined: Tue Dec 21, 2010 3:30 am
Minecraft IGN: uniwersel1

Re: Custom scripts

Post by uniwersel1 » Fri Jan 21, 2011 8:52 am

i dont have time to do things i want 1:play minecraft for more than 5 minutes 2:learn java and scripts and the whole thing just dont have time its dissapointing im missing the whole coding world

User avatar
knutremi
Posts: 2177
Joined: Tue Nov 30, 2010 7:59 pm
Minecraft IGN: knutremi
Location: Norway
Contact:

Re: Custom scripts

Post by knutremi » Fri Jan 21, 2011 10:43 am

Do like me. Study or take a course.

Imagine you're writing a guide for a robot cashier on how to expedite a customer. And you have to use things like say something, remember information, if, else, math, do again and conditions. Then imagine you're that robot, trying to follow the guide. You can only use cold hard logic, step-by-step instructions and references. No intuition.

The context can be swapped but you're always writing a guide for the same robot.

If you can do that you've already learned basic programming. Learning a programming language will be a breeze.
Don't beat yourself up.

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

Re: Custom scripts

Post by FakeTruth » Fri Jan 21, 2011 10:58 am

pfffffwahaha, u so funny XD
Image

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

Re: Custom scripts

Post by ker » Fri Jan 21, 2011 11:51 am

FakeTruth wrote:Cool !!

It can pretty much flood your world, but pretty awesome plugin nonetheless :D
xD

yes I know, once you implement GetMetaData() or sth like that I'll happily fix that problem

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

Re: Custom scripts

Post by FakeTruth » Fri Jan 21, 2011 12:04 pm

ker wrote:
FakeTruth wrote:Cool !!

It can pretty much flood your world, but pretty awesome plugin nonetheless :D
xD

yes I know, once you implement GetMetaData() or sth like that I'll happily fix that problem
It is!! In version 166, which I released yesterday.

click hero for zeh new and improved interface

Code: Select all

char GetBlockMeta( int a_X, int a_Y, int a_Z );
Image

Locked

Who is online

Users browsing this forum: No registered users and 1 guest