Let a compass lock onto a lodestone

Using a compass on a lodestone now sets its lodestone target (in place for a single compass in survival, otherwise one is split off), and the target clears when the lodestone is gone. The target also travels in item packets on the server thread.
This commit is contained in:
Kacpherek
2026-10-26 19:40:59 +01:00
parent 7f8a6680a8
commit c439b806b8
6 changed files with 148 additions and 1 deletions
+2
View File
@@ -3762,6 +3762,7 @@ impl Game<'_> {
|| self.place_minecart(hit, &stack)
|| self.place_painting(hit, &stack)
|| self.place_item_frame(hit, &stack)
|| self.use_lodestone_compass(hit, &stack)
|| self.place_armor_stand(hit, &stack)
|| self.place_cushion(hit, &stack)
|| self.place_end_crystal(hit, &stack))
@@ -4661,6 +4662,7 @@ impl Game<'_> {
horizon_client::spans::time("tick_thunder", || self.tick_thunder());
horizon_client::spans::time("tick_lecterns", || self.tick_lecterns());
horizon_client::spans::time("tick_maps", || self.tick_maps());
self.tick_lodestone_compasses();
horizon_client::spans::time("tick_bolts", || self.tick_bolts());
horizon_client::spans::time("tick_fishing", || self.tick_fishing());
horizon_client::spans::time("tick_tridents", || self.tick_tridents());
@@ -1983,6 +1983,7 @@ impl Game<'_> {
self.as_guest(host, conn, |g| {
g.tick_fishing();
g.tick_maps();
g.tick_lodestone_compasses();
g.count_tick_stats(sleeping);
g.tick_advancements();
g.update_player_attributes();
+87
View File
@@ -714,6 +714,93 @@ impl Game<'_> {
self.swing_arm();
true
}
/// `CompassItem.useOn`: a compass used on a lodestone locks onto it (a
/// single one in survival changes in place, else one is split off).
pub(super) fn use_lodestone_compass(&mut self, hit: &super::Hit, stack: &ItemStack) -> bool {
if &*stack.item != "minecraft:compass" {
return false;
}
let [x, y, z] = hit.pos;
if self
.res
.facts()
.get(self.world.block(x, y, z))
.name
.as_ref()
!= "minecraft:lodestone"
{
return false;
}
let at = [x, y, z].map(|v| f64::from(v) + 0.5);
self.audio
.play("minecraft:item.lodestone_compass.lock", Some(at), 1.0, 1.0);
let target = Tag::compound([(
"target",
Tag::compound([
("dimension", Tag::String(self.dimension_id().into())),
("pos", Tag::IntArray(vec![x, y, z])),
]),
)]);
if !self.creative && stack.count == 1 {
if let Some(held) = self.held_mut() {
held.data_mut()
.set_other("minecraft:lodestone_tracker", Some(target));
}
} else {
let mut locked = stack.clone();
locked.count = 1;
locked
.data_mut()
.set_other("minecraft:lodestone_tracker", Some(target));
self.consume_held();
self.give_or_drop_stack(locked);
}
self.stats.add("used", &stack.item, 1);
self.swing_arm();
true
}
/// `CompassItem.inventoryTick` / `LodestoneTracker.tick`: a compass whose
/// lodestone is gone loses its target. A target in an unloaded chunk waits.
pub(super) fn tick_lodestone_compasses(&mut self) {
let dim = self.dimension_id();
let gone = |game: &Self, stack: &ItemStack| -> bool {
let Some(tracker) = stack
.data()
.and_then(|d| d.other("minecraft:lodestone_tracker"))
else {
return false;
};
let (Some(target), true) = (
tracker.get("target"),
horizon_client::save::number(tracker.get("tracked")).is_none_or(|v| v != 0),
) else {
return false;
};
let Some(Tag::IntArray(pos)) = target.get("pos") else {
return false;
};
let [x, y, z] = [pos[0], pos[1], pos[2]];
horizon_client::save::text(target.get("dimension")).as_deref() == Some(dim)
&& game.world.chunks.contains_key(&(x >> 4, z >> 4))
&& game
.res
.facts()
.get(game.world.block(x, y, z))
.name
.as_ref()
!= "minecraft:lodestone"
};
for slot in (0..self.inventory.len()).chain([super::shield::OFFHAND_SLOT]) {
if self.stack_in(slot).is_some_and(|s| gone(self, s))
&& let Some(s) = self.stack_in_mut(slot)
{
s.data_mut().set_other(
"minecraft:lodestone_tracker",
Some(Tag::Compound(Default::default())),
);
}
}
}
fn give_or_drop_stack(&mut self, stack: ItemStack) {
if let Some(rest) = self.give(stack)
&& let Some(eye) = self.eye_position()
+16
View File
@@ -95,6 +95,22 @@ fn components_tag(s: &Stack, builtin: &Builtin, synced: &Dynamic) -> Option<Tag>
),
]),
Component::FireworkExplosion(e) => fireworks_tag(e),
Component::LodestoneTracker { target, tracked } => {
let mut entries = Vec::new();
if let Some((dimension, pos)) = target {
entries.push((
"target",
Tag::compound([
("dimension", Tag::String(dimension.as_str().into())),
("pos", Tag::IntArray(pos.to_vec())),
]),
));
}
if !tracked {
entries.push(("tracked", Tag::Byte(0)));
}
Tag::compound(entries)
}
Component::Enchantments(list) => {
// Enchantments are a synced (data-driven) registry: the
// ids index the server's RegistryData, not the builtin
+15
View File
@@ -576,6 +576,21 @@ fn network_stack(
));
}
stack.added.extend(firework_components(d));
// The compass's lodestone target.
if let Some(t) = d.other("minecraft:lodestone_tracker") {
let target = t.get("target").and_then(|g| {
let Some(horizon_world::nbt::Tag::IntArray(pos)) = g.get("pos") else {
return None;
};
let dimension = crate::save::text(g.get("dimension"))?;
Some((dimension, [*pos.first()?, *pos.get(1)?, *pos.get(2)?]))
});
let tracked = crate::save::number(t.get("tracked")).is_none_or(|v| v != 0);
stack.added.push((
horizon_protocol::item::component::LODESTONE_TRACKER,
horizon_protocol::item::Component::LodestoneTracker { target, tracked },
));
}
// PotionContents: the potion by its `minecraft:potion` id.
if let Some(id) = (d.potion.as_deref()).and_then(|potion| {
synced_id(
+27 -1
View File
@@ -3,7 +3,7 @@
//! client sends back in container clicks (HashedStack).
use crate::{
Result,
buf::{Codec, Reader, Writer},
buf::{Codec, Reader, Writer, pack_block_pos, unpack_block_pos},
};
use horizon_world::nbt::Tag;
@@ -40,6 +40,7 @@ pub mod component {
pub const WRITABLE_BOOK_CONTENT: i32 = 56;
pub const WRITTEN_BOOK_CONTENT: i32 = 57;
pub const TRIM: i32 = 58;
pub const LODESTONE_TRACKER: i32 = 69;
pub const FIREWORK_EXPLOSION: i32 = 70;
pub const FIREWORKS: i32 = 71;
}
@@ -99,6 +100,12 @@ pub enum Component {
},
/// `FireworkExplosion` (a firework star).
FireworkExplosion(FireworkExplosion),
/// `LodestoneTracker`: the target's dimension and block, and whether it
/// follows the lodestone.
LodestoneTracker {
target: Option<(String, [i32; 3])>,
tracked: bool,
},
/// `ArmorTrim` naming its material and pattern by registry id (`trim_material`,
/// `trim_pattern`); a trim with a direct holder stays [`Component::Raw`].
Trim {
@@ -316,6 +323,17 @@ impl Component {
}
}
FIREWORK_EXPLOSION => Self::FireworkExplosion(FireworkExplosion::read(r)?),
LODESTONE_TRACKER => {
let target = if r.bool()? {
Some((r.string(32767)?, unpack_block_pos(r.i64()?)))
} else {
None
};
Self::LodestoneTracker {
target,
tracked: r.bool()?,
}
}
// ArmorTrim.STREAM_CODEC: two holders, each a reference's id plus
// one. A direct material or pattern has no registry name: read past.
TRIM => {
@@ -437,6 +455,14 @@ impl Component {
explosions.iter().for_each(|e| e.write(w));
}
Self::FireworkExplosion(e) => e.write(w),
Self::LodestoneTracker { target, tracked } => {
w.bool(target.is_some());
if let Some((dimension, pos)) = target {
w.string(dimension);
w.i64(pack_block_pos(*pos));
}
w.bool(*tracked);
}
Self::WritableBook(pages) => {
w.varint(pages.len() as i32);
for page in pages {