WIP: command and datapack tweaks

Work in progress on commands, functions and scoreboard handling carried over from the interrupted run.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-10 08:59:08 +02:00
parent 40802dd9af
commit f0d33d3cf8
8 changed files with 282 additions and 0 deletions
@@ -40,6 +40,7 @@ pub struct Registries {
/// Boss bar ids and datapack predicate ids.
pub bossbars: Vec<String>,
pub predicates: Vec<String>,
pub item_modifiers: Vec<String>,
pub schedules: Vec<String>,
pub stopwatches: Vec<String>,
pub recipes: Vec<String>,
@@ -1936,6 +1937,7 @@ impl ArgType {
"templates" => reg.templates.clone(),
"random_sequences" => reg.random_sequences.clone(),
"predicates" => reg.predicates.clone(),
"item_modifiers" => reg.item_modifiers.clone(),
"time_markers" => reg.time_markers.clone(),
"advancements" => reg.advancements.clone(),
_ => Vec::new(),
@@ -222,6 +222,14 @@ fn data_and_tag_commands_parse() {
assert_eq!(id, "data.get");
assert_eq!(args, ["target", "path", "scale"]);
assert_eq!(run("data get block ~ ~ ~ Items[0]").unwrap().0, "data.get");
assert_eq!(
run("item modify entity @s weapon.mainhand a:b").unwrap().0,
"item.modify"
);
assert_eq!(
run("item modify block 1 2 3 container.0 a:b").unwrap().0,
"item.modify"
);
assert_eq!(run("data get storage my:store").unwrap().0, "data.get");
assert_eq!(run("data merge storage a:b {x:1}").unwrap().0, "data.merge");
assert_eq!(
@@ -151,6 +151,18 @@ pub fn register(d: &mut Dispatcher, root: usize) {
fn item(d: &mut Dispatcher, root: usize) {
let item = d.literal(root, "item");
d.requires(item, GAMEMASTERS);
let modify = d.literal(item, "modify");
for provider in ["entity", "block"] {
let p = d.literal(modify, provider);
let target = if provider == "entity" {
d.argument(p, "target", ENTITIES)
} else {
d.argument(p, "targetPos", ArgType::BlockPos)
};
let slot = d.argument(target, "slot", ArgType::Slot);
let modifier = d.argument(slot, "modifier", ArgType::Id("item_modifiers"));
d.executes(modifier, "item.modify");
}
for mode in ["replace", "fill", "override"] {
let m = d.literal(item, mode);
for provider in ["entity", "block"] {
+7
View File
@@ -280,6 +280,8 @@ pub struct DataStore {
/// Function tag id -> entries (`ns:f`, `#ns:tag`), with `required` flags.
pub function_tags: BTreeMap<String, Vec<(String, bool)>>,
pub predicates: BTreeMap<String, serde_json::Value>,
/// `item_modifier` files, for `/item modify`.
pub item_modifiers: BTreeMap<String, serde_json::Value>,
/// Functions that failed to parse (id, message), for logging.
pub errors: Vec<(String, String)>,
}
@@ -322,6 +324,11 @@ impl DataStore {
&& let Ok(v) = serde_json::from_slice(&bytes)
{
s.predicates.insert(id, v);
} else if let Some(id) = resource_id(&f, "item_modifier", ".json")
&& let Some(bytes) = pack.read(&f)
&& let Ok(v) = serde_json::from_slice(&bytes)
{
s.item_modifiers.insert(id, v);
}
}
}
@@ -536,6 +536,7 @@ impl Game<'_> {
"setworldspawn" => self.cmd_setworldspawn(ctx),
"clear" => self.cmd_clear(ctx),
"item" => self.cmd_item(ctx),
"item.modify" => self.cmd_item_modify(ctx),
"place" => self.cmd_place(ctx),
"recipe.give" | "recipe.take" => self.cmd_recipe(id, ctx),
"damage" => self.cmd_damage(ctx),
@@ -312,6 +312,239 @@ impl Game<'_> {
extra,
}))
}
/// One item modifier (a typed object, a list or a datapack reference)
/// applied to a stack; `None` when the stack ends up empty.
fn apply_item_modifier(
&self,
m: &serde_json::Value,
stack: Option<ItemStack>,
rng: &mut horizon_core::random::LegacyRandom,
this: Option<&horizon_client::advancements::EntityView>,
origin: [f64; 3],
depth: u32,
) -> Option<ItemStack> {
use horizon_core::random::RandomSource;
use serde_json::Value;
if depth > 16 {
return stack;
}
let int = |v: &Value, rng: &mut horizon_core::random::LegacyRandom| -> i32 {
if let Some(n) = v.as_f64() {
return n as i32;
}
let min = v["min"].as_f64().unwrap_or(0.) as i32;
let max = v["max"].as_f64().unwrap_or(0.) as i32;
match v["type"].as_str().unwrap_or("minecraft:constant") {
"minecraft:uniform" | "uniform" => rng
.next_int_between_inclusive(min, max.max(min))
.unwrap_or(min),
_ => v["value"].as_f64().unwrap_or(0.) as i32,
}
};
match m {
Value::Array(list) => list.iter().fold(stack, |s, f| {
self.apply_item_modifier(f, s, rng, this, origin, depth + 1)
}),
Value::String(id) => match self.fns.store.item_modifiers.get(id) {
Some(f) => self.apply_item_modifier(f, stack, rng, this, origin, depth + 1),
None => stack,
},
Value::Object(_) => {
let mut stack = stack?;
if !m["conditions"].is_null()
&& !self.conditions_hold(&m["conditions"], this, origin)
{
return Some(stack);
}
match m["function"].as_str().unwrap_or("") {
"minecraft:sequence" => {
return m["functions"]
.as_array()
.into_iter()
.flatten()
.fold(Some(stack), |s, f| {
self.apply_item_modifier(f, s, rng, this, origin, depth + 1)
});
}
"minecraft:set_count" => {
let n = int(&m["count"], rng);
let base = if m["add"].as_bool() == Some(true) {
stack.count as i32
} else {
0
};
let max = self.res.items.get(&stack.item).map_or(64, |i| i.max_stack);
stack.count = (base + n).clamp(0, max as i32) as u32;
}
"minecraft:limit_count" => {
let min = m["limit"]["min"].as_i64().or(m["limit"].as_i64());
let max = m["limit"]["max"].as_i64().or(m["limit"].as_i64());
if let Some(min) = min {
stack.count = stack.count.max(min as u32);
}
if let Some(max) = max {
stack.count = stack.count.min(max as u32);
}
}
"minecraft:set_damage" => {
let max = self.res.items.get(&stack.item).map_or(0, |i| i.max_damage);
if max > 0 {
let f = match m["damage"].as_f64() {
Some(f) => f as f32,
None => {
let lo = m["damage"]["min"].as_f64().unwrap_or(0.) as f32;
let hi = m["damage"]["max"].as_f64().unwrap_or(0.) as f32;
lo + rng.next_float() * (hi - lo)
}
};
let base = if m["add"].as_bool() == Some(true) {
1. - stack.damage as f32 / max as f32
} else {
0.
};
let frac = (base + f).clamp(0., 1.);
stack.damage = ((1. - frac) * max as f32).floor() as u32;
}
}
"minecraft:set_name" => {
let t = horizon_client::packs::component(&m["name"]);
stack.extra.get_or_insert_with(Default::default).custom_name =
Some(t.plain());
}
"minecraft:set_enchantments" => {
let add = m["add"].as_bool() == Some(true);
let d = stack.extra.get_or_insert_with(Default::default);
let target = if &*stack.item == "minecraft:enchanted_book" {
&mut d.stored_enchantments
} else {
&mut d.enchantments
};
for (id, level) in m["enchantments"].as_object().into_iter().flatten() {
let n = int(level, rng).clamp(0, 255) as u32;
let id: std::sync::Arc<str> = if id.contains(':') {
id.as_str().into()
} else {
format!("minecraft:{id}").into()
};
match target.iter_mut().find(|(e, _)| *e == id) {
Some(e) => e.1 = if add { e.1 + n } else { n },
None => target.push((id, n)),
}
target.retain(|(_, l)| *l > 0);
}
}
"minecraft:set_potion" => {
if let Some(p) = m["id"].as_str() {
stack.extra.get_or_insert_with(Default::default).potion =
Some(p.into());
}
}
_ => {}
}
(stack.count > 0).then_some(stack)
}
_ => stack,
}
}
/// `/item modify (entity|block) <target> <slot> <modifier>`.
pub(super) fn cmd_item_modify(&mut self, ctx: &Context) -> R<i32> {
self.ensure_datapacks();
let targets = self.owner_of(ctx, "target", true)?;
let Some(Arg::Str(slot_name)) = ctx.get("slot").cloned() else {
return Ok(0);
};
let Some(Arg::Str(id)) = ctx.get("modifier").cloned() else {
return Ok(0);
};
let id = if id.contains(':') {
id
} else {
format!("minecraft:{id}")
};
let Some(modifier) = self.fns.store.item_modifiers.get(&id).cloned() else {
return Err(err("argument.item_modifier.unknown", &[&id]));
};
let source = self.command_source.unwrap_or_else(|| self.player_source());
let view = source.entity.map(|t| self.entity_view_of(t));
let seed = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos() as i64);
let mut rng = horizon_core::random::LegacyRandom::new(seed);
let mut tracker = Tracker::default();
let mut selected = 0;
for o in &targets {
let slots = self.slots_named(o, &slot_name);
if slots.is_empty() {
if let Owner::Block(p) = o
&& !self.world.block_entities.contains_key(&(p[0], p[1], p[2]))
{
return Err(err(
"commands.item.target.not_a_container",
&[&p[0], &p[1], &p[2]],
));
}
continue;
}
let mut changed = 0;
for slot in &slots {
// SlotSelector.NON_EMPTY_SLOTS
let Some(current) = self.slot_get(slot) else {
continue;
};
selected += 1;
let new = self.apply_item_modifier(
&modifier,
Some(current.clone()),
&mut rng,
view.as_ref(),
source.pos,
0,
);
if new.as_ref() != Some(&current) && self.slot_set(slot, new) {
changed += 1;
}
}
let h = self.owner_holder(o);
tracker.track(&h, changed);
}
if selected == 0 {
return Err(err("commands.item.target.no_such_slot", &[&slot_name]));
}
let pos = match targets.first() {
Some(Owner::Block(p)) => Some(*p),
_ => None,
};
let (message, total) = tracker.message(
false,
Some(err("commands.item.target.failed", &[])),
|h, count| {
let n = Text::literal(count.to_string());
match pos {
Some(p) => Text::translate(
"commands.item.block.modify.success",
&[
n,
Text::literal(p[0].to_string()),
Text::literal(p[1].to_string()),
Text::literal(p[2].to_string()),
],
),
None => Text::translate(
"commands.item.entity.modify.success.single",
&[n, h.text.clone()],
),
}
},
|c, _| {
Text::translate(
"commands.item.entity.modify.success.multiple",
&[Text::literal(c.to_string())],
)
},
)?;
self.feedback(message);
Ok(total)
}
/// `/item replace|fill|override <target> <slots> (with <item> [count] | from <source> <slots>)`.
pub(super) fn cmd_item(&mut self, ctx: &Context) -> R<i32> {
let mode = ctx
@@ -123,6 +123,7 @@ impl Game<'_> {
reg.functions = self.fns.store.functions.keys().cloned().collect();
reg.function_tags = self.fns.store.function_tags.keys().cloned().collect();
reg.predicates = self.fns.store.predicates.keys().cloned().collect();
reg.item_modifiers = self.fns.store.item_modifiers.keys().cloned().collect();
reg.schedules = self.fns.schedule.iter().map(|s| s.id.clone()).collect();
reg.datapacks_enabled = std::iter::once("vanilla".to_owned())
.chain(self.fns.enabled.iter().cloned())
@@ -227,6 +227,24 @@ impl Game<'_> {
}
}
}
if ok && !s.predicates.is_empty() {
// EntityPredicates: the datapack predicate with the entity as
// `this` and the entity's position as origin.
self.ensure_datapacks();
let view = self.entity_view_of(t);
let origin = self.target_position(t).unwrap_or_default();
for (id, negated) in &s.predicates {
let matches = self
.fns
.store
.predicates
.get(id)
.is_some_and(|json| self.conditions_hold(json, Some(&view), origin));
if matches == *negated {
ok = false;
}
}
}
if ok && !s.advancements.is_empty() {
ok = t == Target::Player && self.advancements_match(&s.advancements);
}