Skip to content

Commit b0012da

Browse files
Started adding /ride
1 parent cae6fec commit b0012da

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
lines changed

API/entity.lua

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,39 @@ function better_commands.entity_from_alias(alias, list)
105105
return entities[math.random(1, #entities)]
106106
end
107107

108-
---Handles rotation when summoning/teleporting
108+
---Handles Vector2 (yaw/pitch) rotation in split_param
109+
---@param base_rot vector.Vector
110+
---@param split_param splitParam
111+
---@param i integer
112+
---@param no_relative boolean?
113+
---@return vector.Vector?
114+
---@return string?
115+
function better_commands.handle_vector2_rot(base_rot, split_param, i, no_relative)
116+
base_rot = table.copy(base_rot)
117+
if split_param[i] then
118+
if not split_param[i+1] then
119+
return nil, S("Missing second rotation coordinate")
120+
end
121+
if split_param[i].type == "number" then
122+
base_rot.y = math.rad(tonumber(split_param[i][3]) or 0)
123+
elseif split_param[i].type == "relative" and not no_relative then
124+
base_rot.y = base_rot.y+math.rad(tonumber(split_param[i][3]:sub(2,-1)) or 0)
125+
else
126+
return nil, S("Invalid rotation coordinate @1", split_param[i][3])
127+
end
128+
if split_param[i+1].type == "number" then
129+
base_rot.x = math.rad(tonumber(split_param[i+1][3]) or 0)
130+
elseif split_param[i+1].type == "relative" and not no_relative then
131+
base_rot.x = base_rot.x+math.rad(tonumber(split_param[i+1][3]:sub(2,-1)) or 0)
132+
else
133+
return nil, S("Invalid rotation coordinate @1", split_param[i+1][3])
134+
end
135+
return base_rot
136+
end
137+
return base_rot
138+
end
139+
140+
---Handles rotation in various commands
109141
---@param context contextTable
110142
---@param victim minetest.ObjectRef|vector.Vector
111143
---@param split_param splitParam[]
@@ -118,21 +150,17 @@ function better_commands.get_tp_rot(context, victim, split_param, i)
118150
if split_param[i] then
119151
local yaw_pitch
120152
local facing
121-
if split_param[i].type == "number" then
122-
victim_rot.y = math.rad(tonumber(split_param[i][3]) or 0)
153+
if split_param[i].type == "number" or split_param[i].type == "relative" then
123154
yaw_pitch = true
124155
elseif split_param[i].type == "relative" then
125-
victim_rot.y = victim_rot.y+math.rad(tonumber(split_param[i][3]:sub(2,-1)) or 0)
126156
yaw_pitch = true
127157
elseif split_param[i][3] == "facing" then
128158
facing = true
159+
else
160+
return nil, S("Invalid rotation")
129161
end
130-
if yaw_pitch and split_param[i+1] then
131-
if split_param[i+1].type == "number" then
132-
victim_rot.x = math.rad(tonumber(split_param[i+1][3]) or 0)
133-
elseif split_param[i+1].type == "relative" then
134-
victim_rot.x = victim_rot.x+math.rad(tonumber(split_param[i+1][3]:sub(2,-1)) or 0)
135-
end
162+
if yaw_pitch then
163+
return better_commands.handle_vector2_rot(victim_rot, split_param, i)
136164
elseif facing and split_param[i+1] then
137165
if split_param[i+1].type == "selector" then
138166
local targets, err = better_commands.parse_selector(split_param[i+1], context, true)

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Removed `anchored` execute subcommand (if you want to have rotation anchors, make a PR, because I can't think of any good way to do it)
3737
* Command results are now shown to other players as well, unless `better_commands.send_command_feedback` is disabled
3838
* Error messages are now red
39-
* Some command me2ssages now match ACOVG's better
39+
* Some command messages now match ACOVG's better
4040
### Bugfixes
4141
* Fixed a bug with the `/kill` command (it should work now).
4242
* The `rm`/`r` selector arguments now actually treat their values as numbers (not strings), and are now inclusive as intended.

COMMANDS/COMMANDS.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local command_files = {
2020
"settings",
2121
"stop",
2222
"weather",
23+
"ride",
2324
}
2425

2526
local mcl_only = {

COMMANDS/ride.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
S = minetest.get_translator(minetest.get_current_modname())
2+
3+
better_commands.register_command("ride", {
4+
description = "Allows entities to mount or dismount other entities.",
5+
params = "<target> <action> [vehicle|entityType] [bone] [pos] [rot] [teleportRules] [nameTag] [rideRules]",
6+
privs = {server = true},
7+
func = function (name, param, context)
8+
local split_param, err = better_commands.parse_params(param)
9+
if err or not split_param then return false, better_commands.error(err), 0 end
10+
local target_selector = split_param[1]
11+
if not target_selector then return false, better_commands.error(S("Missing target")), 0 end
12+
local action = split_param[2] and split_param[2][3]
13+
if not action then return false, better_commands.error(S("Missing action")), 0 end
14+
if action == "mount" or action == "start_riding" then
15+
local vehicle_selector = split_param[3]
16+
local teleport_ride, bone, pos, rot, err =
17+
false, "", vector.zero(), vector.zero()
18+
if not vehicle_selector then return false, better_commands.error(S("Missing vehicle")), 0 end
19+
bone = split_param[4] and split_param[4][3]
20+
if not bone or bone == "_" then bone = "" end
21+
if split_param[5] then
22+
pos, err = better_commands.parse_pos(split_param, 5, context)
23+
if err or not pos then return false, better_commands.error(err), 0 end
24+
if split_param[8] then
25+
rot, err = better_commands.handle_vector2_rot(rot, split_param, 8, false)
26+
if err or not rot then return false, better_commands.error(err), 0 end
27+
local teleport_rules = split_param[10] and split_param[10][3]
28+
if teleport_rules then
29+
if teleport_rules == "teleport_ride" then
30+
teleport_ride = true
31+
elseif teleport_rules ~= "teleport_rider" then
32+
return false, better_commands.error(S("Expected teleport_ride|teleport_rider, got @1", teleport_rules)), 0
33+
end
34+
end
35+
end
36+
end
37+
local target, err = better_commands.parse_selector(target_selector, context, true)
38+
if err or not target then return false, better_commands.error(err), 0 end
39+
local vehicle, err = better_commands.parse_selector(vehicle_selector, context, true)
40+
if err or not vehicle then return false, better_commands.error(err), 0 end
41+
if target[1] == vehicle[1] then
42+
return false, better_commands.error("Cannot attach entity to itself"), 0
43+
end
44+
target[1]:set_attach(vehicle[1], bone, pos, rot)
45+
return true, S("Attached @1 to @2", better_commands.get_entity_name(target[1]), better_commands.get_entity_name(vehicle[1])), 1
46+
end
47+
return false, nil, 0
48+
end
49+
})

COMMANDS/weather.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ better_commands.register_command("weather", {
6464
(better_commands.mcl and " [<duration>])" or ""),
6565
func = function (name, param, context)
6666
local split_param, err = better_commands.parse_params(param)
67-
if err or not split_param then return false, err, 0 end
67+
if err or not split_param then return false, better_commands.error(err), 0 end
6868
if not split_param[1] then
6969
return false, nil, 0
7070
end
@@ -84,7 +84,7 @@ better_commands.register_command("weather", {
8484
return false, better_commands.error(S("Unexpected argument: @1", split_param[2][3])), 0
8585
else
8686
local duration, err = better_commands.parse_time_string(split_param[2][3], true)
87-
if err or not duration then return false, err, 0 end
87+
if err or not duration then return false, better_commands.error(err), 0 end
8888
local tps = tonumber(minetest.settings:get("time_speed"))
8989
-- Don't ask how the math works; I already forgot.
9090
local duration_s = duration*24000/(tps/3.6)
@@ -94,7 +94,7 @@ better_commands.register_command("weather", {
9494
set_weather(w, end_time)
9595
return true, S("Set weather to @1", split_param[1][3])
9696
else
97-
return false, S("Invalid weather: @1", split_param[1][3])
97+
return false, better_commands.error(S("Invalid weather: @1", split_param[1][3]))
9898
end
9999
end
100100
})
@@ -109,7 +109,7 @@ better_commands.register_command("toggledownfall", {
109109
if new_weather then
110110
set_weather(new_weather)
111111
else
112-
return false, S("No weather called 'rain'"), 0
112+
return false, better_commands.error(S("No weather called 'rain'")), 0
113113
end
114114
else
115115
set_weather("none")

TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A place for me to write out my future plans. I decide what to do next [randomly]
55
Things that *must* be done before the next release
66
- [ ] `/give <player> <item_without_metadata> <count>` only gives 1 item.
77
- Items with metadata work fine.
8-
8+
- [ ] Test potentially broken summon/tp rotation
99

1010

1111

@@ -103,7 +103,7 @@ Things that I hope to do *eventually*.
103103
- [ ] `testforblock`
104104
- [ ] `testforblocks`
105105
- [ ] `tickingarea`?
106-
- [ ] `toggledownfall` (depending on mods)
107-
- [ ] `weather` (depending on mods)
106+
- [x] `toggledownfall` (depending on mods)
107+
- [x] `weather` (depending on mods)
108108
- [ ] `whitelist`
109109
- [ ] `worldborder`? (maybe not visible, probably no collision)

0 commit comments

Comments
 (0)