summaryrefslogtreecommitdiff
path: root/lua/autorun/zone_serverchanger.lua
blob: afb92a633ceac8d83da9d3b35f46cd6085e182cc (plain)
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
80
81
82
83
84
--[[
	A server changer will "teleport" the player between instances
]]

zones.RegisterClass("artery_serverchange",Color(255,255,255))

--Use this to set default properties. Only called on server.
hook.Add("OnZoneCreated","artery_serverchange",function(zone,class,zoneID)
	if class == "artery_serverchange" then
		zone.toserver = game.GetIPAddress()
		zone.topos = Vector(0,0,0)
		
	end
end)

-- Use this hook to let a player change a zone after making it or with the edit tool.
-- class is zone.class, zone is the zone's full table, DPanel is a panel to parent your things to, zoneID is the zone's ID, DFrame is the whole frame.
-- Return your preferred width and height for the panel and the frame will size to it.
hook.Add("ShowZoneOptions","artery_serverchange",function(zone,class,DPanel,zoneID,DFrame) 
	if class == "artery_serverchange" then
		local w,h = 500, 400
		
		local tobl = Label("To Server:")
		tobl:SetParent(DPanel)
		tobl:Dock(TOP)
		tobl:SetTextColor(color_black)
		tobl:SizeToContents()
		
		local to = vgui.Create("DTextEntry",DPanel) --parent to the panel.
		to:Dock(TOP)
		to:SetValue(zone.toserver)
		function to:OnChange(new)
			print("server's onvaluechanged has been called")
			net.Start("artery_serverchange")
				net.WriteFloat(zoneID)
				net.WriteString(new)
			net.SendToServer()
		end
		
		local poslbl = Label("To position:")
		poslbl:SetParent(DPanel)
		poslbl:Dock(TOP)
		poslbl:SetTextColor(color_black)
		poslbl:SizeToContents()
		
		for k,v in pairs({"x","y","z"}) do
			local post = vgui.Create("DTextEntry",DPanel)
			post:SetValue(zone.topos[v])
			post:Dock(TOP)
			post:SetNumeric( true )
			function post:OnChange(new)
				print("Sending pos change")
				net.Start("artery_poschange")
				net.WriteFloat(zoneID)
				net.WriteString(v)
				net.WriteString(self:GetText())
				net.SendToServer()
			end
		end
		return w, h -- Specify the width and height for the DPanel container. The frame will resize accordingly.
		
	end
end)

if SERVER then
	util.AddNetworkString("artery_serverchange")
	util.AddNetworkString("artery_poschange")
	net.Receive("artery_serverchange",function(len,ply)
		print("Server change received!")
		local id, new = net.ReadFloat(), net.ReadString()
		if not ply:IsAdmin() then return end
		zones.List[id].toserver = new
		zones.Sync()
	end)
	net.Receive("artery_poschange",function(len,ply)
		print("poschange received!")
		local id,v,w = net.ReadFloat(), net.ReadString(), net.ReadString()
		if not ply:IsAdmin() then return end
		zones.List[id].topos[v] = tonumber(w) or 0
		zones.Sync()
		print("zone",id,"is now set to")
		PrintTable(zones.List[id])
	end)
end