-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteleport.lua
126 lines (124 loc) · 7.31 KB
/
teleport.lua
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--local bc = better_commands
local S = minetest.get_translator(minetest.get_current_modname())
-- some duplicate code
better_commands.register_command("teleport", {
params = S("[<targets>] <location/entity> [<rot>]"),
description = S("Teleports and rotates things"),
privs = {server = true},
func = function(name, param, context)
local split_param = better_commands.parse_params(param)
if not split_param[1] then return false, nil, 0 end
if split_param[1].type == "selector" then
if not split_param[2] then
if not context.executor.is_player then
return false, better_commands.error(S("Command blocks can't teleport (although I did consider making it possible)")), 0
end
local targets, err = better_commands.parse_selector(split_param[1], context, true)
if err or not targets then return false, better_commands.error(err), 0 end
local target_pos = targets[1].is_player and targets[1]:get_pos() or targets[1]
context.executor:set_pos(target_pos)
context.executor:add_velocity(-context.executor:get_velocity())
local rotation = better_commands.get_entity_rotation(targets[1])
better_commands.set_entity_rotation(context.executor, rotation)
return true, S("Teleported @1 to @2", better_commands.get_entity_name(context.executor), better_commands.get_entity_name(targets[1])), 1
elseif split_param[2].type == "selector" then
if not context.executor.is_player and split_param[1][3] == "@s" then
return false, better_commands.error(S("Command blocks can't teleport (although I did consider making it possible)")), 0
end
local victims, err = better_commands.parse_selector(split_param[1], context)
if err or not victims then return false, better_commands.error(err), 0 end
if #victims == 0 then
return false, better_commands.error(S("No entity was found")), 0
end
local targets, err = better_commands.parse_selector(split_param[2], context, true)
if err or not targets then return false, better_commands.error(err), 0 end
local target_pos = targets[1].is_player and targets[1]:get_pos() or targets[1]
local count = 0
local last
for _, victim in ipairs(victims) do
if victim.is_player then
count = count + 1
last = better_commands.get_entity_name(victim)
victim:set_pos(target_pos)
victim:add_velocity(-victim:get_velocity())
local rotation = better_commands.get_entity_rotation(targets[1])
better_commands.set_entity_rotation(victim, rotation)
end
end
if count < 1 then
return false, better_commands.error(S("No entities found")), 0
elseif count == 1 then
return true, S(
"Teleported @1 to @2",
last,
better_commands.get_entity_name(targets[1])
),
1
else
return true, S(
"Teleported @1 entities to @2",
count,
better_commands.get_entity_name(targets[1])
),
count
end
elseif split_param[2].type == "number" or split_param[2].type == "relative" or split_param[2].type == "look_relative" then
if not context.executor.is_player and split_param[1][3] == "@s" then
return false, better_commands.error(S("Command blocks can't teleport (although I did consider making it possible)")), 0
end
local victims, err = better_commands.parse_selector(split_param[1], context)
if err or not victims then return false, better_commands.error(err), 0 end
local target_pos, err = better_commands.parse_pos(split_param, 2, context)
if err then return false, better_commands.error(err), 0 end
local count = 0
local last
for _, victim in ipairs(victims) do
if victim.is_player then
count = count+1
last = better_commands.get_entity_name(victim)
victim:set_pos(target_pos)
if not (split_param[2].type == "look_relative"
or split_param[2].type == "relative"
or split_param[3].type == "relative"
or split_param[4].type == "relative") then
victim:add_velocity(-victim:get_velocity())
end
local victim_rot, err = better_commands.get_tp_rot(context, victim, split_param, 5)
if err then return false, better_commands.error(err), 0 end
if victim_rot then
better_commands.set_entity_rotation(victim, victim_rot)
end
end
end
if count < 1 then
return false, better_commands.error(S("No entities found")), 0
elseif count == 1 then
return true, S("Teleported @1 to @2", last, minetest.pos_to_string(target_pos, 1)), 1
else
return true, S("Teleported @1 entities to @2", count, minetest.pos_to_string(target_pos, 1)), count
end
end
elseif split_param[1].type == "number" or split_param[1].type == "relative" or split_param[1].type == "look_relative" then
if not context.executor.is_player and split_param[1][3] == "@s" then
return false, better_commands.error(S("Command blocks can't teleport (although I did consider making it possible)")), 0
end
local target_pos, err = better_commands.parse_pos(split_param, 1, context)
if err then
return false, better_commands.error(err), 0
end
context.executor:set_pos(target_pos)
if not (split_param[1].type == "look_relative"
or split_param[1].type == "relative"
or split_param[2].type == "relative"
or split_param[3].type == "relative") then
context.executor:add_velocity(-context.executor:get_velocity())
end
local victim_rot, err = better_commands.get_tp_rot(context, context.executor, split_param, 4)
if err or not victim_rot then return false, better_commands.error(err), 0 end
better_commands.set_entity_rotation(context.executor, victim_rot)
return true, S("Teleported @1 to @2", better_commands.get_entity_name(context.executor), minetest.pos_to_string(target_pos, 1)), 1
end
return false, nil, 0
end
})
better_commands.register_command_alias("tp", "teleport")