summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/MagicCarpet/plugin.lua
blob: 27dcdf45d99aa967c06b94a68582d59654e4826e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local PLUGIN = {}
local Carpets = {}

function Initialize( Plugin )
	PLUGIN = Plugin

	Plugin:SetName( "MagicCarpet" )
	Plugin:SetVersion( 1 )

	cPluginManager.AddHook(cPluginManager.HOOK_PLAYER_MOVING, OnPlayerMoving)
	cPluginManager.AddHook(cPluginManager.HOOK_DISCONNECT,    OnDisconnect)
	
	PluginManager:BindCommand("/mc", "magiccarpet", HandleCarpetCommand, " - Spawns a magical carpet");

	LOG( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() )
	return true
end





function OnDisable()
	LOG( PLUGIN:GetName() .. " v." .. PLUGIN:GetVersion() .. " is shutting down..." )
	for i, Carpet in pairs( Carpets ) do
		Carpet:remove()
	end
end





function HandleCarpetCommand( Split, Player )
	Carpet = Carpets[ Player ]
	if( Carpet == nil ) then
		Carpets[ Player ] = cCarpet:new()
		Player:SendMessage(cChatColor.Green .. "[INFO] " .. cChatColor.White .. "You're on a magic carpet!" )
		Player:SendMessage(cChatColor.Yellow .. "[INFO] " .. cChatColor.White .. "Look straight down to descend. Jump to ascend!" )
	else
		Carpet:remove()
		Carpets[ Player ] = nil
		Player:SendMessage(cChatColor.Green .. "[INFO] " .. cChatColor.White .. "The carpet vanished!" )
	end

	return true
end





function OnDisconnect( Reason, Player )
	local Carpet = Carpets[ Player ]
	if( Carpet ~= nil )	 then
		Carpet:remove()
	end
	Carpets[ Player ] = nil
end





function OnPlayerMoving(Player)
	local Carpet = Carpets[ Player ]
	if( Carpet == nil ) then
		return
	end

	if( Player:GetPitch() == 90 ) then
		Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY() - 1, Player:GetPosZ() ) )
	else
		if( Player:GetPosY() < Carpet:getY() ) then
			Player:TeleportToCoords(Player:GetPosX(), Carpet:getY() + 0.2, Player:GetPosZ())
		end
		Carpet:moveTo( cLocation:new( Player:GetPosX(), Player:GetPosY(), Player:GetPosZ() ) )
	end
end