Skip to content

Commit b2286b4

Browse files
v3.0
1 parent 835c9ce commit b2286b4

16 files changed

+201
-152
lines changed

API/API.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ end)
5454

5555
for _, file in ipairs(api_files) do
5656
better_commands.run_file(file, "API")
57+
end
58+
59+
---Runs a Better Command (useful for debugging and stuff)
60+
---@param input string
61+
---@param player string?
62+
function better_commands.run(input, executor_name)
63+
local command, param = input:match("%/?(%S+)%s+(.*)$")
64+
local context
65+
if executor_name then
66+
context = {executor = executor_name}
67+
else
68+
context = {executor = {x=0,y=0,z=0}, command_block = true, pos = {x=0,y=0,z=0}, dir = {x=0,y=0,z=0}}
69+
end
70+
if better_commands.commands[command] then
71+
better_commands.commands[command].func(executor_name or "", param, context)
72+
end
5773
end

API/damage.lua

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
---@param reason table?
77
---@param damage_immortal? boolean
88
function better_commands.deal_damage(target, damage, reason, damage_immortal)
9+
core.log(dump({damage=damage,reason=reason}))
910

1011
local luaentity = target:get_luaentity()
1112

12-
if luaentity then
13+
if luaentity then -- (not player)
1314
if luaentity.deal_damage then -- Mobs Redo/Mobs MC
1415
--core.log("deal_damage")
1516
luaentity:deal_damage(damage, reason or {type = "generic"})
@@ -25,28 +26,22 @@ function better_commands.deal_damage(target, damage, reason, damage_immortal)
2526
if luaentity.health > 0 then
2627
--core.log("luaentity.health")
2728
luaentity.health = luaentity.health - damage
28-
luaentity:check_for_death(reason)
29+
if better_commands.mcl2 then
30+
luaentity:check_for_death(reason and reason.type or "set_hp", reason)
31+
else
32+
luaentity:check_for_death(reason, damage)
33+
end
2934
--core.log(luaentity.health)
3035
end
3136
return
3237
end
3338
end
3439

40+
-- Player
3541
local hp = target:get_hp()
3642
local armorgroups = target:get_armor_groups()
3743

3844
if hp > 0 and armorgroups and (damage_immortal or not armorgroups.immortal) then
39-
--core.log("set_hp")
4045
target:set_hp(hp - damage, reason)
4146
end
42-
--core.log("nothing")
43-
end
44-
45-
-- Make sure players always die when /killed
46-
core.register_on_player_hpchange(function(player, hp_change, reason)
47-
if reason.better_commands == "kill" then
48-
---@diagnostic disable-next-line: redundant-return-value
49-
return math.min(hp_change, -1000000000), true
50-
end
51-
return hp_change
52-
end, true)
47+
end

API/entity.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function better_commands.set_entity_rotation(obj, rotation)
6565
obj:set_look_vertical(rotation.x)
6666
obj:set_look_horizontal(rotation.y)
6767
elseif obj.set_rotation then
68-
obj:set_rotation(rotation)
68+
-- I have absolutely no idea why the x-axis needs to be flipped.
69+
obj:set_rotation({x=-rotation.x, y=rotation.y, z=rotation.z})
6970
end
7071
end
7172

API/parsing.lua

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ local S = core.get_translator(core.get_current_modname())
55
---@param str string
66
---@return splitParam[] split_param
77
function better_commands.parse_params(str)
8-
str = " "..str -- Don't ask
8+
str = " "..str -- Adds a space to the beginning so I don't have to check for the beginning separately
99
local i = 1
1010
local tmp
1111
local found = {}
1212
-- selectors, @?[data]
1313
repeat
14-
-- p, a, r, s, e, and n are obviously the selectors but I just like the fact that they include the word "parse" (it was even better before I added @n)
15-
tmp = {str:find("(@[parsen])%s*(%[.-%])", i)}
14+
-- Obviously the selectors but I just like the fact that they include the word "parse" (it was even better before @n was added)
15+
tmp = {str:find("%s(@[nparse])%s*(%[.-%])", i)}
1616
if tmp[1] then
1717
i = tmp[2] + 1
18+
tmp[2] = tmp[2] - 1 -- Account for the extra space at the beginning
1819
tmp.type = "selector"
1920
tmp.extra_data = true
2021
table.insert(found, table.copy(tmp))
@@ -27,7 +28,6 @@ function better_commands.parse_params(str)
2728
-- modname:id[data] count wear (everything but id and data optional)
2829
tmp = {str:find("%s([_%w]*:?[_%w]+)%s*(%[.-%])%s*(%d*)%s*(%d*)", i)}
2930
if tmp[1] then
30-
tmp[1] = tmp[1] + 1 -- ignore the space
3131
local overlap
3232
for _, thing in pairs(found) do
3333
if tmp[1] >= thing[1] and tmp[1] <= thing[2]
@@ -38,6 +38,7 @@ function better_commands.parse_params(str)
3838
end
3939
end
4040
i = tmp[2] + 1
41+
tmp[2] = tmp[2] - 1 -- Account for the extra space at the beginning
4142
if not overlap then
4243
if better_commands.handle_alias(tmp[3]) then
4344
tmp.type = "item"
@@ -58,7 +59,6 @@ function better_commands.parse_params(str)
5859
-- modname:id count wear (everything but id optional)
5960
tmp = {str:find("%s([_%w]*:?[_%w]+)%s*(%d*)%s*(%d*)", i)}
6061
if tmp[1] then
61-
tmp[1] = tmp[1] + 1 -- ignore the space
6262
local overlap
6363
for _, thing in pairs(found) do
6464
if tmp[1] >= thing[1] and tmp[1] <= thing[2]
@@ -69,6 +69,7 @@ function better_commands.parse_params(str)
6969
end
7070
end
7171
i = tmp[2] + 1
72+
tmp[2] = tmp[2] - 1 -- Account for the extra space at the beginning
7273
if not overlap then
7374
if better_commands.handle_alias(tmp[3]) then
7475
tmp.type = "item"
@@ -84,9 +85,10 @@ function better_commands.parse_params(str)
8485
-- everything else
8586
i = 1
8687
repeat
87-
tmp = {str:find("(%S+)", i)}
88+
tmp = {str:find("%s(%S+)", i)}
8889
if tmp[1] then
8990
i = tmp[2] + 1
91+
tmp[2] = tmp[2] - 1 -- Account for the extra space at the beginning
9092
local overlap
9193
for _, thing in pairs(found) do
9294
if tmp[1] >= thing[1] and tmp[1] <= thing[2]
@@ -503,7 +505,11 @@ function better_commands.parse_item(item_data, ignore_count)
503505
if item_data.type == "item" and not item_data.extra_data then
504506
local stack = ItemStack(item_data[3])
505507
if not ignore_count then
506-
stack:set_count(tonumber(item_data[4]) or 1)
508+
local count = tonumber(item_data[4]) or 1
509+
if count > 65535 then
510+
return nil, S("Maximum count is 65535, got @1", count)
511+
end
512+
stack:set_count(count)
507513
end
508514
stack:set_wear(tonumber(item_data[5]) or 0)
509515
return stack
@@ -527,7 +533,11 @@ function better_commands.parse_item(item_data, ignore_count)
527533
end
528534
end
529535
if not ignore_count then
530-
stack:set_count(tonumber(item_data[5]) or 1)
536+
local count = tonumber(item_data[5]) or 1
537+
if count > 65535 then
538+
return nil, S("Maximum count is 65535, got @1", count)
539+
end
540+
stack:set_count(count)
531541
end
532542
stack:set_wear(tonumber(item_data[6]) or stack:get_wear())
533543
return stack

API/scoreboard.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ local sidebar_template = {
9292
}
9393

9494
local function sort_scores(a, b)
95-
return (a.score == b.score) and (a.name < b.name) or (tonumber(a.score) > tonumber(b.score))
95+
local a_score, b_score, a_name, b_name = core.strip_colors(a.score), core.strip_colors(b.score),
96+
core.strip_colors(a.name), core.strip_colors(b.name)
97+
return (a_score == b_score) and (a_name < b_name) or (tonumber(a_score) > tonumber(b_score))
9698
end
9799

98100
function better_commands.format_score(objective, name)
@@ -161,7 +163,6 @@ function better_commands.update_hud()
161163
local count = 0
162164
local sortable_scores = {}
163165
for name in pairs(scores) do
164-
count = count + 1
165166
local display_name = better_commands.format_name(name)
166167
local score = better_commands.format_score(objective, name) or "???"
167168
local width = #core.strip_colors(display_name) + #core.strip_colors(score)
@@ -174,8 +175,11 @@ function better_commands.update_hud()
174175
table.sort(sortable_scores, function(...) return not sort_scores(...) end)
175176
end
176177
for _, data in ipairs(sortable_scores) do
177-
name_text = name_text..data.name.."\n"
178-
score_text = score_text..data.score.."\n"
178+
if core.strip_colors(data.name):sub(1,1) ~= "#" then
179+
count = count + 1
180+
name_text = name_text..data.name.."\n"
181+
score_text = score_text..data.score.."\n"
182+
end
179183
end
180184
if not title then
181185
if sidebar.title then

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22

33
## v3.0
4-
This is a small update, mostly for the bugfixes.
4+
This is a small update, mostly composed of bugfixes. Lots of bugfixes. It's been over a year since I last updated this, and now I'm finally back.
55
### New features
66
* Added `/stop` command
77
* Added `/weather` command
@@ -14,7 +14,8 @@ This is a small update, mostly for the bugfixes.
1414
* Fixed unnecessary logging
1515
* Fixed `/scoreboard objectives remove <objective>` causing a crash
1616
* Maybe fixed command registration issue? For some reason, I can't reproduce it anymore, so I probably just fixed it months ago and then completely forgot about it.
17-
* Fixed a few other things, some caused by MCL updates, others caused by me making mistakes.
17+
* Fixed wrong argument order for `/scoreboard players operation ...`
18+
* Fixed a LOT of random things.
1819

1920
## v2.0 (May 31, 2024)
2021
### New features

COMMANDS/clear.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ better_commands.register_command("clear", {
1414
targets, err = better_commands.parse_selector(selector, context)
1515
if err or not targets then return false, better_commands.error(err), 0 end
1616
end
17-
local filter, group
17+
local filter, group, remove_max
1818
if split_param[2] then
19-
if split_param[2][5] then
20-
split_param[3] = split_param[2][5]
21-
end
22-
split_param[2][5] = nil
23-
split_param[2][6] = nil
24-
2519
if split_param[2][3] == "*" then
2620
filter = "*"
2721
elseif split_param[2][3]:sub(1,6) == "group:" then
@@ -31,10 +25,19 @@ better_commands.register_command("clear", {
3125
filter, err = better_commands.parse_item(split_param[2], true)
3226
if err or not filter then return false, better_commands.error(err), 0 end
3327
end
34-
end
35-
local remove_max = tonumber(split_param[3] and split_param[3][3])
36-
if split_param[3] and not remove_max then
37-
return false, better_commands.error(S("maxCount must be a number")), 0
28+
local remove_max_str
29+
if split_param[2].type == "item" then
30+
local count_index = split_param[2].extra_data and 5 or 4
31+
remove_max_str = split_param[2][count_index]
32+
elseif split_param[2].type == "string" then
33+
remove_max_str = split_param[3] and split_param[3][3]
34+
end
35+
if remove_max_str and remove_max_str ~= "" then
36+
remove_max = tonumber(remove_max_str)
37+
if not remove_max then
38+
return false, better_commands.error(S("maxCount must be a number")), 0
39+
end
40+
end
3841
end
3942
if remove_max then
4043
remove_max = math.floor(remove_max)
@@ -60,7 +63,7 @@ better_commands.register_command("clear", {
6063
if all then
6164
match_count = match_count + stack:get_count()
6265
elseif group then
63-
if core.get_item_group(stack:get_name(), filter) then
66+
if core.get_item_group(stack:get_name(), filter) ~= 0 then
6467
match_count = match_count + stack:get_count()
6568
end
6669
elseif split_param[2].extra_data then
@@ -78,7 +81,7 @@ better_commands.register_command("clear", {
7881
if all then
7982
matches = true
8083
elseif group then
81-
if core.get_item_group(stack:get_name(), filter) then
84+
if core.get_item_group(stack:get_name(), filter) ~= 0 then
8285
matches = true
8386
end
8487
elseif split_param[2].extra_data then
@@ -131,7 +134,7 @@ better_commands.register_command("clear", {
131134
if all and remove_max == -1 then
132135
return true, S("Removed all items from player @1", last), 1
133136
elseif match_total < 1 then
134-
return false, better_commands.error(S("No items were found on player @1", last)), 0
137+
return false, better_commands.error(S("No items were found on player @1", better_commands.get_entity_name(targets[1]))), 0
135138
elseif remove_max == 0 then
136139
return true, S("Found @1 matching items(s) on player @2", match_total, last), match_total
137140
else

COMMANDS/command_runners.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ better_commands.register_command("old", {
3737
local missing
3838
if not privs then privs, missing = core.check_player_privs(name, def.privs) end
3939
if privs then
40-
return def.real_func(name, command_param, context)
40+
return def.func(name, command_param, context)
4141
else
4242
return false, better_commands.error(S("You don't have permission to run this command (missing privileges: @1)", table.concat(missing, ", "))), 0
4343
end

COMMANDS/damage.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ better_commands.register_command("remove", {
9292
if count < 1 then
9393
return false, better_commands.error(S("No entity was found")), 0
9494
elseif count == 1 then
95-
return true, S("Killed @1", last), count
95+
return true, S("Removed @1", last), count
9696
else
97-
return true, S("Killed @1 entities", count), count
97+
return true, S("Removed @1 entities", count), count
9898
end
9999
end
100100
})
@@ -111,23 +111,26 @@ better_commands.register_command("damage", {
111111
amount = math.floor(amount)
112112
if amount < 1 then return false, better_commands.error(S("<amount> must not be negative")), 0 end
113113
local damage_type = split_param[3] and split_param[3][3] or "set_hp"
114-
local cause = (split_param[4] and split_param[4][3] == "by") and split_param[5]
114+
local cause
115+
if split_param[4] then
116+
if split_param[4][3] ~= "by" then
117+
return false, better_commands.error(S("Expected 'by <cause>', got '@1'", split_param[4][3])), 0
118+
end
119+
if split_param[5] then
120+
cause = split_param[5]
121+
else
122+
return false, better_commands.error(S("Missing cause")), 0
123+
end
124+
end
115125
local damage_source
116126
if cause then
117127
local results, err = better_commands.parse_selector(cause, context, true)
118128
if err or not results then return false, better_commands.error(err), 0 end
119129
damage_source = results[1]
120130
end
121131
local reason = {
122-
type = damage_type,
123132
object = damage_source,
124133
}
125-
if better_commands.mcl2 and damage_type then
126-
if mcl_damage.types[damage_type] then
127-
reason._mcl_reason = table.copy(reason)
128-
reason.type = nil
129-
end
130-
end
131134
local targets, err = better_commands.parse_selector(selector, context)
132135
if err or not targets then return false, better_commands.error(err), 0 end
133136
local count = 0

0 commit comments

Comments
 (0)