Merge pull request #823 from ludovic111/integ/mp-sh

🤖 Agent: merged modpacks-4's #753–#765 and server-host-5's #759–#790, combined on main and checked locally (clippy, workspace tests, 9 harnesses).
This commit is contained in:
Ludovic Marie
2026-10-18 07:25:41 +02:00
committed by GitHub
63 changed files with 3851 additions and 285 deletions
@@ -120,22 +120,8 @@ fn main() -> Result<()> {
step("mesh tables");
let collision = collisions::load(&root, assets.registry)?;
step("collision");
let motion = MotionResources::load(&root, &assets)?;
let motion = MotionResources::load_with_facts(&root, &assets, &facts)?;
step("motion");
for name in [
"worldgen/blocks/block-properties.nbt",
"worldgen/blocks/cached-collision.nbt",
"worldgen/blocks/cached-support.nbt",
"cached-collision.nbt",
"occlusion.nbt",
"movement-properties.nbt",
] {
let bytes = std::fs::read(root.join(name))?;
let tree = horizon_world::nbt::read_named(&bytes, horizon_world::nbt::Limits::tables())?;
step(&format!(" tree of {name}"));
drop((tree, bytes));
step(" dropped");
}
let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, 1234)?;
step("worldgen");
println!(
+4 -10
View File
@@ -15,24 +15,18 @@ use horizon_world::{
entity_obstruction::EntityIdentity,
fluid::{FluidKind, FluidState},
liquid_collision::LiquidCollisionWorld,
nbt::{self, Limits},
powder_snow_collision::{PowderSnowEntity, PowderSnowWorld},
registry::BlockRegistry,
registry::{BlockRegistry, BlockTableFile},
world_movement::{
CollisionCheckRequest, EntityMovementWorld, MovementRequest, no_world_collision,
resolve_world_movement,
},
};
use std::{fs, io, path::Path, sync::Arc};
use std::{io, path::Path, sync::Arc};
pub fn load<'r>(root: &Path, registry: &'r BlockRegistry) -> Result<BlockCollisionRegistry<'r>> {
let data = nbt::read_named(
&fs::read(root.join("cached-collision.nbt"))?,
Limits::tables(),
)?
.0
.tag;
let cached = CachedCollisionRegistry::from_nbt(&data, registry)?;
let file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?;
let cached = CachedCollisionRegistry::from_table(file.table("collision")?)?;
if !cached.has_canonical_identities() {
return Err("scene collisions require canonical shape identities".into());
}
+10 -7
View File
@@ -535,15 +535,16 @@ impl ArmorStand {
Vec3::new(p.x + w, p.y + h, p.z + w),
)
}
/// LivingEntity.travel with gravity 0.08 and 0.98 drag, falling until it
/// lands (armor stands have no other movement).
/// LivingEntity.travelInAir (armor stands have no other movement): move
/// by the velocity (a landing stops the fall,
/// `Block.updateEntityMovementAfterFallOn`), then gravity 0.08 and the
/// 0.98 drag, so a stand at rest on the ground keeps -0.0784 as vanilla's.
pub fn tick(&mut self, scene: &mut crate::collisions::SceneCollisions) {
if self.marker {
return;
}
let mut v = self.velocity;
v.y -= 0.08;
let wanted = v;
let wanted = self.velocity;
let mut y = wanted.y;
if let Ok(moved) = scene.resolve(self.bounds(), wanted, 0.0, self.on_ground) {
self.position = Vec3::new(
self.position.x + moved.x,
@@ -551,9 +552,11 @@ impl ArmorStand {
self.position.z + moved.z,
);
self.on_ground = wanted.y < 0.0 && moved.y > wanted.y;
v = Vec3::new(0.0, if moved.y != wanted.y { 0.0 } else { v.y * 0.98 }, 0.0);
if moved.y != wanted.y {
y = 0.0;
}
}
self.velocity = v;
self.velocity = Vec3::new(0.0, (y - 0.08) * f64::from(0.98f32), 0.0);
}
}
+19 -2
View File
@@ -65,6 +65,7 @@ mod jukebox;
mod lan;
mod lan_actions;
mod lan_crafting;
mod lan_entities;
mod lan_mobs;
mod lan_pvp;
mod lan_screen;
@@ -108,6 +109,8 @@ mod powder_snow;
mod raids;
mod recipe_book;
mod remote;
mod remote_objects;
mod remote_projectiles;
mod render_control;
mod render_tick;
mod respawn_anchor;
@@ -648,6 +651,8 @@ impl LevelAccess for GameLevel<'_> {
/// FallingBlockEntity carrying a block state.
#[derive(Clone, Debug)]
struct FallingBlock {
/// The entity id a server tracks it by (0 until it is tracked).
id: u32,
state: BlockStateId,
/// `DATA_START_POS`: spawn position used to select weighted block models.
start_pos: BlockPos,
@@ -985,6 +990,8 @@ struct Game<'a> {
/// A server's players on their way to another level, each waiting for
/// the destination to load around them.
server_trips: Vec<server_travel::Trip>,
/// Players a command sends to another level, once it has run.
level_teleports: Vec<server_travel::LevelTeleport>,
/// A server's levels other than the one in place (game/levels.rs).
levels: levels::Levels,
/// Which pass of a server tick runs (`tick_server`): the first one reads
@@ -1158,6 +1165,10 @@ struct Game<'a> {
/// returns: the line, whether operators get it and whether the console
/// logs it.
admin_feedback: Vec<(horizon_client::text::Text, bool, bool)>,
/// The server console is running a command: whether its feedback goes to
/// the operators (`DedicatedServer.shouldInformAdmins`,
/// `broadcast-console-to-ops`).
console_broadcast: Option<bool>,
/// State of `/spectate`, `/posteffect`, `/debug`, `/waypoint` and friends.
cmds: commands::extra::CommandExtras,
ui: chat::UiState,
@@ -2055,6 +2066,7 @@ impl Game<'_> {
f64::from(pos.z) + 0.5,
);
self.falling.push(FallingBlock {
id: 0,
state,
start_pos: pos,
position: p,
@@ -6545,7 +6557,10 @@ impl Game<'_> {
res.threads,
)
} else {
let inner = Arc::new(VanillaTerrain::for_dimension(&self.options.resources.join("worldgen"), pack.assets.registry, &res.biomes, seed, dim.id()).map_err(|e| {
// WorldOptions.generateStructures, from the world's settings.
let structures =
horizon_client::save::WorldGen::read(world_root).is_none_or(|w| w.structures);
let inner = Arc::new(VanillaTerrain::for_world(&self.options.resources.join("worldgen"), pack.assets.registry, &res.biomes, seed, dim.id(), structures).map_err(|e| {
format!("{e}\nPrepare the worldgen bundle with the launcher, or: horizon-prepare --prepare")
})?);
self.end.set_locator(Some(inner.clone()));
@@ -10283,6 +10298,7 @@ impl<'a> Game<'a> {
world_root: PathBuf::new(),
travelling: None,
server_trips: Vec::new(),
level_teleports: Vec::new(),
levels: Default::default(),
level_pass: Default::default(),
portals: Default::default(),
@@ -10392,6 +10408,7 @@ impl<'a> Game<'a> {
lan_acting_uuid: None,
chat_capture: None,
admin_feedback: Vec::new(),
console_broadcast: None,
cmds: Default::default(),
ui: Default::default(),
combat: Default::default(),
@@ -10538,7 +10555,7 @@ fn load_resources(
on_stage(5);
let registry_owner = assets.clone();
let collision = collisions::load(&root, registry_owner.registry)?;
let motion = MotionResources::load(&root, &registry_owner)?;
let motion = MotionResources::load_with_facts(&root, &registry_owner, &facts)?;
log::info!(
"load collision+motion: {:.2}s",
started.elapsed().as_secs_f32()
@@ -357,6 +357,7 @@ impl Game<'_> {
silent: false,
store: None,
elsewhere: false,
level: None,
block: Some(pos),
};
// performPrefixedCommand: a leading slash is allowed.
+70 -16
View File
@@ -165,14 +165,20 @@ impl Game<'_> {
}
self.broadcast_to_admins(message);
}
/// CommandSourceStack.broadcastToAdmins, for a guest's command on a
/// dedicated server (`sendSuccess` with `broadcast`): `[name: message]`,
/// gray and italic, for the other operators when `send_command_feedback`
/// and the console when `log_admin_commands`. Queued while the guest acts;
/// `send_admin_feedback` sends it.
fn broadcast_to_admins(&mut self, message: Text) {
if self.lan_acting.is_none()
|| self.options.dedicated.is_none()
/// CommandSourceStack.broadcastToAdmins (`sendSuccess` with `broadcast`)
/// for a guest's command, the host's in a world open to LAN and the
/// dedicated server console's when `broadcast-console-to-ops` is on:
/// `[name: message]`, gray and italic, for the other operators when
/// `send_command_feedback`, and for a player's command on a dedicated
/// server the console when `log_admin_commands`. Queued while the
/// command runs; `send_admin_feedback` sends it.
pub(super) fn broadcast_to_admins(&mut self, message: Text) {
let informs = match self.console_broadcast {
Some(broadcast) => broadcast,
// While a guest acts the host's LanHost is out of `self.lan`.
None => self.lan_acting.is_some() || self.lan.is_some(),
};
if !informs
|| message
.translation_key()
.is_some_and(|k| admin::QUIET.contains(&k))
@@ -180,20 +186,46 @@ impl Game<'_> {
return;
}
let ops = self.ui.rules.bool("send_command_feedback");
let console = self.ui.rules.bool("log_admin_commands");
// MinecraftServer.sendSystemMessage, unless the server is the source.
let console = self.console_broadcast.is_none()
&& self.lan_acting.is_some()
&& self.options.dedicated.is_some()
&& self.ui.rules.bool("log_admin_commands");
if !ops && !console {
return;
}
// getDisplayName: the entity the command runs as (`execute as`).
let who = self
.command_source
.and_then(|s| s.entity)
.unwrap_or(Target::Player);
let line = Text::translate("chat.type.admin", &[self.target_name(who), message])
// getDisplayName: the entity the command runs as (`execute as`), or
// the server's own source, "Server".
let who = match self.command_source.and_then(|s| s.entity) {
Some(entity) => self.target_name(entity),
None if self.console_broadcast.is_some() => Text::literal("Server".to_owned()),
None => self.player_display_name(),
};
let line = Text::translate("chat.type.admin", &[who, message])
.with_color(text::GRAY)
.italic();
self.admin_feedback.push((line, ops, console));
}
/// Player.getDisplayName: the name (team-formatted), suggesting a
/// `/tell`, showing the player on hover, inserting the name.
fn player_display_name(&self) -> Text {
let name = self.acting_player_name().to_owned();
let Some(uuid) = self.lan_acting_uuid else {
return self.target_name(Target::Player);
};
self.target_name(Target::Player).with_component_fields(vec![
(
"click_event".into(),
serde_json::json!({"action": "suggest_command", "command": format!("/tell {name} ")}),
),
(
"hover_event".into(),
serde_json::json!({"action": "show_entity", "id": "minecraft:player",
"uuid": horizon_protocol::crypto::uuid_string(uuid), "name": name}),
),
("insertion".into(), name.into()),
])
}
fn source_position(&self) -> [f64; 3] {
if let Some(s) = &self.command_source {
return s.pos;
@@ -1124,7 +1156,23 @@ impl Game<'_> {
(-(dy.atan2(horizontal).to_degrees())) as f32,
]);
}
// `execute in` another dimension: a server's player moves to that
// level (ServerPlayer.teleport into another level), after the command.
let level = self
.command_source
.and_then(|s| s.level)
.filter(|d| *d != self.dim && self.options.dedicated.is_some());
for &t in &targets {
if let (Some(dim), Target::Player, Some(conn)) = (level, t, self.lan_acting) {
self.level_teleports
.push(super::server_travel::LevelTeleport {
conn,
dim,
at: pos,
rotation,
});
continue;
}
self.teleport_to(t, pos, rotation);
}
let [x, y, z] = pos.map(|v| Text::literal(format_double(v)));
@@ -1481,7 +1529,13 @@ impl Game<'_> {
self.carts.push(cart);
name = Text::translate(&format!("entity.minecraft.{k}"), &[]);
}
_ => return Err(err("commands.summon.failed", &[])),
// SummonCommand.createEntity: any other type from an empty compound.
_ => {
let Some(target) = self.summon_with_nbt(&kind, pos, "{}")? else {
return Err(err("commands.summon.failed", &[]));
};
name = self.target_name(target);
}
}
}
self.feedback(Text::translate("commands.summon.success", &[name]));
@@ -26,6 +26,9 @@ pub(in crate::game) struct Source {
/// `execute in` another dimension than the one Horizon has loaded: none
/// of its chunks are loaded.
pub elsewhere: bool,
/// The dimension `execute in` named, if any: a server teleports its
/// players there (`TeleportCommand` into the source's level).
pub level: Option<super::super::Dim>,
/// The command block whose output this source is
/// (`BaseCommandBlock.CloseableCommandBlockSource`).
pub block: Option<(i32, i32, i32)>,
@@ -77,6 +80,7 @@ impl Game<'_> {
silent: false,
store: None,
elsewhere: false,
level: None,
block: None,
}
}
@@ -90,6 +94,7 @@ impl Game<'_> {
silent: true,
store: None,
elsewhere: false,
level: None,
block: None,
}
}
@@ -441,6 +446,7 @@ impl Game<'_> {
pos: self.target_position(t)?,
rot: self.target_rotation(t),
elsewhere: false,
level: None,
..s
})
})
@@ -544,6 +550,7 @@ impl Game<'_> {
}
vec![Source {
elsewhere: dim != self.dim.id(),
level: Some(super::super::Dim::from_id(dim)),
..s
}]
}
@@ -758,6 +758,10 @@ impl Game<'_> {
.map(|e| self.shown_name_tags(glam::DVec3::from_array(e))),
});
status["physical_keys"] = json!(physical_keys);
// Boats, minecarts, armor stands, item frames and paintings.
status["objects"] = json!(self.objects_status());
// Primed TNT, falling blocks, projectiles, tridents and end crystals.
status["projectiles"] = json!(self.projectiles_status());
status
}
}
+51 -4
View File
@@ -402,8 +402,32 @@ struct ServerState {
/// When the last passes of the loop ended, for the tick rate horizon-server shows.
ticks: std::collections::VecDeque<Instant>,
paused: bool,
/// `broadcast-console-to-ops`.
broadcast_console: bool,
}
/// The dedicated-only commands' replies vanilla sends with `broadcast`
/// (`sendSuccess(..., true)`): the operators hear of them.
const BROADCAST: &[&str] = &[
"commands.ban.success",
"commands.banip.info",
"commands.banip.success",
"commands.deop.success",
"commands.kick.success",
"commands.op.success",
"commands.pardon.success",
"commands.pardonip.success",
"commands.save.disabled",
"commands.save.enabled",
"commands.save.success",
"commands.stop.stopping",
"commands.whitelist.add.success",
"commands.whitelist.disabled",
"commands.whitelist.enabled",
"commands.whitelist.reloaded",
"commands.whitelist.remove.success",
];
impl Game<'_> {
/// DedicatedServer.initServer and MinecraftServer.runServer.
pub(super) fn run_dedicated(&mut self) -> Result<()> {
@@ -612,6 +636,7 @@ impl Game<'_> {
empty_ticks: 0,
ticks: Default::default(),
paused: false,
broadcast_console: props.broadcast_console_to_ops,
};
let pause_after = props.pause_when_empty_seconds * 20;
let idle_timeout = props.player_idle_timeout;
@@ -619,7 +644,7 @@ impl Game<'_> {
while !state.stop {
let tick_start = Instant::now();
while let Ok(line) = state.console.try_recv() {
self.console_command(&mut state, &line);
self.console_line(&mut state, &line);
}
self.poll_server_control(&mut state);
let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len());
@@ -767,14 +792,36 @@ impl Game<'_> {
})
});
if found.is_none() {
self.console_reply(Text::translate("argument.player.unknown", &[]));
let unknown = Text::translate("argument.player.unknown", &[]);
log("INFO", &format!("System chat: {}", unknown.plain()));
}
found
}
/// MinecraftServer.sendSystemMessage: what the console is told.
fn console_reply(&self, text: Text) {
/// MinecraftServer.sendSystemMessage: what the console is told; while
/// the console runs a command, the replies vanilla broadcasts go to the
/// operators too.
fn console_reply(&mut self, text: Text) {
log("INFO", &format!("System chat: {}", text.plain()));
if self.console_broadcast.is_some()
&& text
.translation_key()
.is_some_and(|k| BROADCAST.contains(&k))
{
self.broadcast_to_admins(text);
}
}
/// One console line, then the operators' share of its feedback.
fn console_line(&mut self, state: &mut ServerState, line: &str) {
self.console_broadcast = Some(state.broadcast_console);
self.console_command(state, line);
self.console_broadcast = None;
if let Some(mut host) = self.lan.take() {
self.send_admin_feedback(&mut host, None);
self.lan = Some(host);
}
self.admin_feedback.clear();
}
/// DedicatedServer.handleConsoleInputs: one console line, run as the
+6
View File
@@ -74,6 +74,8 @@ impl Respawn {
}
#[derive(Clone, Debug)]
pub(super) struct Crystal {
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
pub position: Vec3,
pub show_bottom: bool,
/// EndCrystal.time (the bob and spin), starting random.
@@ -553,6 +555,7 @@ impl Game<'_> {
}
let time = self.world.random.next_int_bound(100_000).unwrap_or(0);
self.crystals.push(Crystal {
id: 0,
position: at,
show_bottom: true,
time,
@@ -2301,6 +2304,7 @@ impl Crystal {
pub(super) fn from_nbt(tag: &Tag, time: i32) -> Option<Self> {
let (position, _, _) = horizon_client::entity_nbt::boat_from_nbt(tag)?;
Some(Self {
id: 0,
position,
show_bottom: !matches!(tag.get("ShowBottom"), Some(Tag::Byte(0))),
time,
@@ -2403,6 +2407,7 @@ impl Game<'_> {
}
let time = self.world.random.next_int_bound(100_000).unwrap_or(0);
self.crystals.push(Crystal {
id: 0,
position: Vec3::new(x + 0.5, y, z + 0.5),
show_bottom: false,
time,
@@ -2665,6 +2670,7 @@ impl Game<'_> {
.retain(|c| c.position.distance_squared(at) > 0.01);
let time = self.world.random.next_int_bound(100_000).unwrap_or(0);
self.crystals.push(Crystal {
id: 0,
position: at,
show_bottom: true,
time,
+31 -5
View File
@@ -18,21 +18,46 @@ use horizon_core::{
};
pub(super) struct Rocket {
position: Vec3,
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
pub(super) position: Vec3,
previous: Vec3,
velocity: Vec3,
pub(super) velocity: Vec3,
life: i32,
lifetime: i32,
explosions: Vec<FireworkExplosion>,
/// Boosting the player (attachedToEntity).
attached: bool,
pub(super) attached: bool,
/// `DATA_SHOT_AT_ANGLE` (dispensers): constant velocity, exploding on
/// the first block it hits.
angled: bool,
pub(super) angled: bool,
/// `DATA_ID_FIREWORKS_ITEM`: the stack it was launched from.
item: ItemStack,
pub(super) item: ItemStack,
}
impl Rocket {
/// A rocket a server sent (`handleAddEntity`): placed and moved by it.
pub(super) fn remote(id: u32, position: Vec3, velocity: Vec3) -> Self {
let item = ItemStack::new("minecraft:firework_rocket", 1);
let (_, explosions) = fireworks_of(&item);
Self {
id,
position,
previous: position,
velocity,
life: 0,
lifetime: i32::MAX,
explosions,
attached: false,
angled: false,
item,
}
}
/// One tick of a server's client: the previous position, then `step`.
pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) {
self.previous = self.position;
self.life += 1;
self.position = step(self.position);
}
/// Position and motion (the dispenser parity fixture).
pub(super) fn motion(&self) -> (Vec3, Vec3) {
(self.position, self.velocity)
@@ -56,6 +81,7 @@ impl Game<'_> {
+ r.next_int_bound(6).unwrap_or(0)
+ r.next_int_bound(7).unwrap_or(0);
Rocket {
id: 0,
position: at,
previous: at,
velocity,
+25 -9
View File
@@ -73,7 +73,7 @@ pub(super) struct Owner {
}
/// A stable UUID for an entity that has none.
fn entity_uuid(id: u32) -> u128 {
pub(super) fn entity_uuid(id: u32) -> u128 {
0x4856_5a4e_0000_0000_0000_0000_0000_0000u128 | u128::from(id)
}
@@ -144,11 +144,15 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> {
meta.push((3, serializer::BOOLEAN, Value::Bool(true)));
}
}
if m.health < m.kind.max_health {
meta.push((9, serializer::FLOAT, Value::Float(m.health.max(0.0))));
// LivingEntity.DATA_HEALTH_ID: defined as 1, then set to the max health.
let health = m.health.max(0.0);
if health != 1.0 {
meta.push((9, serializer::FLOAT, Value::Float(health)));
}
if kind == "minecraft:rabbit" && m.no_ai {
meta.push((15, serializer::BYTE, Value::Byte(1)));
// Mob.DATA_MOB_FLAGS_ID: no AI 1, left-handed 2.
let flags = i8::from(m.no_ai) | i8::from(m.extra.left_handed) << 1;
if flags != 0 {
meta.push((15, serializer::BYTE, Value::Byte(flags)));
}
if m.is_baby()
&& let Some(index) = baby_index(kind)
@@ -719,8 +723,8 @@ impl Game<'_> {
})
}
/// Mobs and items as the entities guests track.
fn lan_entities(&self, hub: &mut Hub) -> Vec<EntityState> {
/// The entities guests track: mobs, items and the rest (game/lan_entities.rs).
fn lan_entities(&self, host_entity: Option<i32>, hub: &mut Hub) -> Vec<EntityState> {
let mut out = Vec::with_capacity(self.mobs.len() + self.dropped.len() + self.orbs.len());
for m in self.mobs.iter().filter(|m| !m.removed) {
let kind = m.kind.id.as_str();
@@ -740,6 +744,7 @@ impl Game<'_> {
data: 0,
meta,
equipment,
precise: false,
});
}
for d in &self.dropped {
@@ -757,6 +762,7 @@ impl Game<'_> {
data: 0,
meta: vec![(8, serializer::ITEM_STACK, Value::Item(stack))],
equipment: Vec::new(),
precise: false,
});
}
for o in &self.orbs {
@@ -774,8 +780,10 @@ impl Game<'_> {
// ExperienceOrb.DATA_VALUE.
meta: vec![(8, serializer::INT, Value::Int(o.value as i32))],
equipment: Vec::new(),
precise: false,
});
}
self.lan_object_states(host_entity, hub, &mut out);
out
}
@@ -786,12 +794,19 @@ impl Game<'_> {
};
// What the tick sends a guest goes to its connection at the end.
let holding = lan::Holding::start();
// The host's own commands' broadcastToAdmins lines.
if !self.admin_feedback.is_empty() {
self.send_admin_feedback(&mut host, None);
}
let state = self.lan_host_state(&host);
// A dedicated server runs the hub without a host player.
if state.is_some() || host.hub.config.dedicated {
let has_guests = !host.hub.guests.is_empty();
let entities = if has_guests {
self.lan_entities(&mut host.hub)
let host_entity = state.as_ref().map(|s| s.entity_id);
self.lan_assign_ids();
self.lan_riding(host_entity, &mut host.hub.riding);
self.lan_entities(host_entity, &mut host.hub)
} else {
Vec::new()
};
@@ -974,7 +989,8 @@ impl Game<'_> {
log::error!("could not open {}: {e}", level.id());
return;
}
theirs = self.lan_entities(&mut host.hub);
// A dedicated server has no host player.
theirs = self.lan_entities(None, &mut host.hub);
&theirs[..]
} else {
entities
+16 -5
View File
@@ -728,7 +728,7 @@ impl Game<'_> {
}),
);
}
self.send_admin_feedback(host, conn);
self.send_admin_feedback(host, Some(conn));
}
}
S::Unknown(8, payload) => {
@@ -931,6 +931,8 @@ impl Game<'_> {
.map(|t| horizon_client::lan::data::component_nbt(&t.component()))
.collect::<Vec<_>>()
});
// A teleport into another level happens before its feedback is sent.
self.run_level_teleports(host);
for content in lines.unwrap_or_default() {
host.hub.send_to(
conn,
@@ -940,13 +942,19 @@ impl Game<'_> {
}),
);
}
self.send_admin_feedback(host, conn);
self.send_admin_feedback(host, Some(conn));
}
/// The `broadcastToAdmins` lines a guest's command queued: every other
/// The `broadcastToAdmins` lines a command queued (`source`: the guest
/// whose it was; none for the host's or the console's): every other
/// operator gets them, and the console logs them as the server's own
/// system messages (MinecraftServer.sendSystemMessage).
fn send_admin_feedback(&mut self, host: &mut LanHost, source: u32) {
pub(super) fn send_admin_feedback(&mut self, host: &mut LanHost, source: Option<u32>) {
// PlayerList.isOp for the host of a world open to LAN: the owner when
// the world allows commands, everyone when the LAN does.
let host_op = source.is_some()
&& !host.hub.config.dedicated
&& (self.allow_commands() || host.hub.config.guest_commands);
for (line, ops, console) in std::mem::take(&mut self.admin_feedback) {
if ops {
let content = horizon_client::lan::data::component_nbt(&line.component());
@@ -954,13 +962,16 @@ impl Game<'_> {
.hub
.guests
.iter()
.filter(|g| g.conn != source && g.permission > 0)
.filter(|g| Some(g.conn) != source && g.permission > 0)
{
g.send(&C::SystemChat(play::SystemChat {
content: content.clone(),
overlay: false,
}));
}
if host_op {
self.system_message(line.clone());
}
}
if console {
super::dedicated::log("INFO", &format!("System chat: {}", line.plain()));
@@ -0,0 +1,449 @@
//! The entities beyond mobs and items that a server tracks to its clients
//! (`ChunkMap.TrackedEntity`, `ServerEntity`): boats, minecarts, armor
//! stands, item frames, paintings and the entities Horizon keeps without
//! simulating them, each as vanilla pairs it (`getAddEntityPacket` and the
//! synched data that differs from the defaults), and who rides what.
use super::Game;
use horizon_client::lan::{
entity::{EntityState, Riding},
hub::Hub,
};
use horizon_protocol::entity_data::{Value, serializer};
use horizon_world::nbt::Tag;
/// `ArmorStand`'s default poses (head, body, left arm, right arm, left leg,
/// right leg), DATA_HEAD_POSE to DATA_RIGHT_LEG_POSE (entries 16 to 21),
/// under their `Pose` keys.
const STAND_POSES: [(&str, [f32; 3]); 6] = [
("Head", [0.0, 0.0, 0.0]),
("Body", [0.0, 0.0, 0.0]),
("LeftArm", [-10.0, 0.0, -10.0]),
("RightArm", [-15.0, 0.0, 10.0]),
("LeftLeg", [-1.0, 0.0, -1.0]),
("RightLeg", [1.0, 0.0, 1.0]),
];
fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) -> EntityState {
EntityState {
id: id as i32,
uuid: super::lan::entity_uuid(id),
kind: kind.to_owned(),
position,
yaw,
pitch: 0.0,
head_yaw: 0.0,
on_ground,
velocity: [0.0; 3],
data: 0,
meta: Vec::new(),
equipment: Vec::new(),
precise: false,
}
}
/// `VehicleEntity`'s DATA_ID_HURT, DATA_ID_HURTDIR and DATA_ID_DAMAGE
/// (entries 8 to 10) when they differ from their defaults (0, 1, 0).
fn vehicle_meta(hurt_time: i32, hurt_dir: i32, damage: f32) -> Vec<(u8, i32, Value)> {
let mut meta = Vec::new();
if hurt_time != 0 {
meta.push((8, serializer::INT, Value::Int(hurt_time)));
}
if hurt_dir != 1 {
meta.push((9, serializer::INT, Value::Int(hurt_dir)));
}
if damage != 0.0 {
meta.push((10, serializer::FLOAT, Value::Float(damage)));
}
meta
}
/// An armor stand's pose from its compound (`ArmorStandPose`), each part
/// that differs from the default.
fn stand_poses(raw: Option<&Tag>) -> Vec<(u8, i32, Value)> {
let Some(pose) = raw.and_then(|t| t.get("Pose")) else {
return Vec::new();
};
let mut meta = Vec::new();
for (i, (key, default)) in STAND_POSES.iter().enumerate() {
let Some(Tag::List(values)) = pose.get(key) else {
continue;
};
let mut r = *default;
for (k, v) in values.iter().take(3).enumerate() {
if let Tag::Float(bits) = v {
r[k] = f32::from_bits(*bits);
}
}
if r != *default {
meta.push((
16 + i as u8,
serializer::ROTATIONS,
Value::Floats(r.to_vec()),
));
}
}
meta
}
impl Game<'_> {
/// Who rides what (`Entity.getPassengers`): mobs on mobs, and the
/// host in its boat or minecart.
pub(super) fn lan_riding(&self, host_entity: Option<i32>, riding: &mut Riding) {
riding.clear();
for m in self.mobs.iter().filter(|m| !m.removed) {
if let Some(vehicle) = m.extra.mount {
riding.add(vehicle as i32, m.id as i32);
}
}
if let (Some(host), Some(vehicle)) = (host_entity, self.riding) {
riding.add(vehicle as i32, host);
}
}
/// Boats, minecarts, armor stands, item frames, paintings and the
/// entities kept unsimulated, as guests track them.
pub(super) fn lan_object_states(
&self,
host_entity: Option<i32>,
hub: &mut Hub,
out: &mut Vec<EntityState>,
) {
for b in &self.boats {
let p = b.position;
let mut e = state(b.id, &b.entity_type(), [p.x, p.y, p.z], b.yaw, b.on_ground);
e.velocity = [b.velocity.x, b.velocity.y, b.velocity.z];
e.meta = vehicle_meta(b.hurt_time, b.hurt_dir, b.damage);
// AbstractBoat.DATA_ID_PADDLE_LEFT and DATA_ID_PADDLE_RIGHT.
for (k, paddling) in b.paddling.iter().enumerate() {
if *paddling {
e.meta
.push((11 + k as u8, serializer::BOOLEAN, Value::Bool(true)));
}
}
out.push(e);
}
for c in &self.carts {
let p = c.position;
let mut e = state(c.id, c.kind.id(), [p.x, p.y, p.z], c.yaw, c.on_ground());
e.velocity = [c.velocity.x, c.velocity.y, c.velocity.z];
e.meta = vehicle_meta(c.hurt_time, c.hurt_dir, c.damage);
match c.kind {
// MinecartFurnace.DATA_ID_FUEL.
super::minecarts::CartKind::Furnace if c.fuel > 0 => {
e.meta.push((13, serializer::BOOLEAN, Value::Bool(true)));
}
// MinecartCommandBlock.DATA_ID_COMMAND_NAME.
super::minecarts::CartKind::CommandBlock => {
if let Some(Tag::String(command)) =
self.persist.raw.get(&c.id).and_then(|t| t.get("Command"))
{
let command = command.to_string_lossy();
if !command.is_empty() {
e.meta
.push((13, serializer::STRING, Value::String(command)));
}
}
}
_ => {}
}
out.push(e);
}
for a in &self.armor_stands {
let p = a.position;
let mut e = state(
a.id,
"minecraft:armor_stand",
[p.x, p.y, p.z],
a.yaw,
a.on_ground,
);
e.head_yaw = a.yaw;
e.velocity = [a.velocity.x, a.velocity.y, a.velocity.z];
if a.fire_ticks > 0 {
e.meta.push((0, serializer::BYTE, Value::Byte(1)));
}
// LivingEntity.DATA_HEALTH_ID: 1 until the stand's max health is set.
e.meta.push((9, serializer::FLOAT, Value::Float(a.health)));
// ArmorStand.DATA_CLIENT_FLAGS: small 1, arms 4, no base plate 8, marker 16.
let flags = i8::from(a.small)
| i8::from(a.show_arms) << 2
| i8::from(a.no_base_plate) << 3
| i8::from(a.marker) << 4;
if flags != 0 {
e.meta.push((15, serializer::BYTE, Value::Byte(flags)));
}
e.meta.extend(stand_poses(self.persist.raw.get(&a.id)));
// Stored main hand, off hand, feet, legs, chest, head: the wire's slots 0 to 5.
for (slot, item) in a.equipment.iter().enumerate() {
if item.is_some() {
e.equipment.push((slot as u8, hub.stack(item)));
}
}
out.push(e);
}
for f in &self.item_frames {
// ItemFrame.getAddEntityPacket: the block it hangs in and its direction.
let mut e = state(f.id, f.kind(), f.pos.map(f64::from), 0.0, false);
e.pitch = match f.facing {
0 => 90.0,
1 => -90.0,
_ => 0.0,
};
e.yaw = match f.facing {
2 => 180.0,
4 => 90.0,
5 => 270.0,
_ => 0.0,
};
e.data = i32::from(f.facing);
// HangingEntity.DATA_DIRECTION, south by default.
if f.facing != 3 {
e.meta
.push((8, serializer::DIRECTION, Value::Id(i32::from(f.facing))));
}
if f.item.is_some() {
e.meta
.push((9, serializer::ITEM_STACK, Value::Item(hub.stack(&f.item))));
}
if f.rotation != 0 {
e.meta
.push((10, serializer::INT, Value::Int(i32::from(f.rotation))));
}
out.push(e);
}
for p in &self.paintings {
let mut e = state(p.id, "minecraft:painting", p.pos.map(f64::from), 0.0, false);
// Direction.get2DDataValue (south 0, west 1, north 2, east 3) to its
// 3D data value and yaw.
let facing = [3, 4, 2, 5][usize::from(p.facing & 3)];
e.yaw = f32::from(p.facing & 3) * 90.0;
e.data = facing;
if facing != 3 {
e.meta.push((8, serializer::DIRECTION, Value::Id(facing)));
}
// Painting.DATA_PAINTING_VARIANT_ID, a registry holder (id + 1),
// set unless it is the registry's first variant (VariantUtils.getAny).
if let Some(id) = hub.registry_id("minecraft:painting_variant", &p.variant)
&& id != 0
{
e.meta
.push((9, serializer::PAINTING_VARIANT, Value::Id(id + 1)));
}
out.push(e);
}
self.lan_projectile_states(host_entity, hub, out);
for o in &self.others {
let p = o.position;
let mut e = state(o.id, &o.kind, [p.x, p.y, p.z], o.yaw, false);
e.pitch = o.pitch;
e.head_yaw = o.yaw;
out.push(e);
}
}
}
impl Game<'_> {
/// Gives the entities Horizon keeps without an id one (`Entity.id`, from
/// the same counter as the rest) before they are first tracked.
pub(super) fn lan_assign_ids(&mut self) {
let next = &mut self.next_entity;
let mut give = |id: &mut u32| {
if *id == 0 {
*next += 1;
*id = *next;
}
};
self.tnts.iter_mut().for_each(|t| give(&mut t.id));
self.falling.iter_mut().for_each(|f| give(&mut f.id));
self.creatures
.projectiles
.iter_mut()
.for_each(|p| give(&mut p.id));
self.tridents.iter_mut().for_each(|t| give(&mut t.id));
self.rockets.iter_mut().for_each(|r| give(&mut r.id));
self.crystals.iter_mut().for_each(|c| give(&mut c.id));
}
/// Primed TNT, falling blocks, projectiles, tridents, firework rockets and
/// end crystals, as guests track them.
fn lan_projectile_states(
&self,
host_entity: Option<i32>,
hub: &mut Hub,
out: &mut Vec<EntityState>,
) {
let owner = |who: Option<horizon_client::mobs::Who>| match who {
Some(horizon_client::mobs::Who::Mob(id)) => id as i32,
Some(horizon_client::mobs::Who::Player) => host_entity.unwrap_or(0),
None => 0,
};
let at = |p: horizon_core::geometry::Vec3| [p.x, p.y, p.z];
for t in self.tnts.iter().filter(|t| t.id != 0) {
let mut e = state(t.id, "minecraft:tnt", at(t.position), 0.0, t.on_ground);
e.velocity = at(t.velocity);
// PrimedTnt.DATA_FUSE_ID, 80 by default; requiresPrecisePosition
// while it touches a block.
if t.fuse != 80 {
e.meta.push((8, serializer::INT, Value::Int(t.fuse)));
}
e.precise = t.on_ground;
out.push(e);
}
for f in self.falling.iter().filter(|f| f.id != 0) {
let mut e = state(f.id, "minecraft:falling_block", at(f.position), 0.0, false);
e.velocity = at(f.velocity);
// getAddEntityPacket: the block state; DATA_START_POS.
e.data = f.state.0 as i32;
if f.no_gravity {
e.meta.push((5, serializer::BOOLEAN, Value::Bool(true)));
}
let start = [f.start_pos.x, f.start_pos.y, f.start_pos.z];
if start != [0; 3] {
e.meta.push((8, serializer::BLOCK_POS, Value::Pos(start)));
}
out.push(e);
}
for p in self.creatures.projectiles.iter().filter(|p| p.id != 0) {
use horizon_client::projectiles::ProjectileKind as K;
let kind = match &p.kind {
K::Arrow { .. } => "minecraft:arrow",
K::Snowball => "minecraft:snowball",
K::Egg => "minecraft:egg",
K::EnderPearl => "minecraft:ender_pearl",
K::SmallFireball => "minecraft:small_fireball",
K::LargeFireball { .. } => "minecraft:fireball",
K::LlamaSpit => "minecraft:llama_spit",
K::WindCharge => "minecraft:wind_charge",
K::BreezeWindCharge => "minecraft:breeze_wind_charge",
K::ShulkerBullet { .. } => "minecraft:shulker_bullet",
K::WitherSkull { .. } => "minecraft:wither_skull",
K::Potion { .. } => "minecraft:splash_potion",
};
let mut e = state(p.id, kind, at(p.position), p.yaw, false);
e.pitch = p.pitch;
e.velocity = at(p.velocity);
e.data = owner(p.owner);
match &p.kind {
K::Arrow { critical, .. } => {
let a = p.arrow.as_deref();
e.meta.extend(arrow_meta(
*critical,
a.is_some_and(|a| a.no_physics),
a.map_or(0, |a| a.pierce),
a.is_some_and(|a| a.in_ground) || p.stuck,
));
}
// WitherSkull.DATA_DANGEROUS.
K::WitherSkull { dangerous: true } => {
e.meta.push((8, serializer::BOOLEAN, Value::Bool(true)));
}
_ => {}
}
out.push(e);
}
for t in self.tridents.iter().filter(|t| t.id != 0) {
let mut e = state(t.id, "minecraft:trident", at(t.position), t.yaw, false);
e.pitch = t.pitch;
e.velocity = at(t.velocity);
e.meta = arrow_meta(false, t.no_physics, 0, t.in_ground);
// ThrownTrident.ID_LOYALTY and ID_FOIL.
if t.loyalty != 0 {
e.meta
.push((11, serializer::BYTE, Value::Byte(t.loyalty as i8)));
}
if t.stack.has_any_enchantments() {
e.meta.push((12, serializer::BOOLEAN, Value::Bool(true)));
}
out.push(e);
}
for r in self.rockets.iter().filter(|r| r.id != 0) {
let mut e = state(
r.id,
"minecraft:firework_rocket",
at(r.position),
0.0,
false,
);
e.velocity = at(r.velocity);
// DATA_ID_FIREWORKS_ITEM once set from a stack, DATA_ATTACHED_TO_TARGET,
// DATA_SHOT_AT_ANGLE.
let plain = &*r.item.item == "minecraft:firework_rocket"
&& r.item.count == 1
&& r.item.extra.is_none();
if !plain {
e.meta.push((
8,
serializer::ITEM_STACK,
Value::Item(hub.stack(&Some(r.item.clone()))),
));
}
if r.attached
&& let Some(host) = host_entity
{
e.meta
.push((9, serializer::OPTIONAL_UNSIGNED_INT, Value::Id(host + 1)));
}
if r.angled {
e.meta.push((10, serializer::BOOLEAN, Value::Bool(true)));
}
out.push(e);
}
for c in self.crystals.iter().filter(|c| c.id != 0) {
let mut e = state(c.id, "minecraft:end_crystal", at(c.position), 0.0, false);
// EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM (true by default).
if c.beam.is_some() {
e.meta
.push((8, serializer::OPTIONAL_BLOCK_POS, Value::OptPos(c.beam)));
}
if !c.show_bottom {
e.meta.push((9, serializer::BOOLEAN, Value::Bool(false)));
}
out.push(e);
}
}
}
/// `AbstractArrow`'s ID_FLAGS (critical 1, no physics 2), PIERCE_LEVEL and
/// IN_GROUND, when they differ from the defaults.
fn arrow_meta(
critical: bool,
no_physics: bool,
pierce: u8,
in_ground: bool,
) -> Vec<(u8, i32, Value)> {
let mut meta = Vec::new();
let flags = i8::from(critical) | i8::from(no_physics) << 1;
if flags != 0 {
meta.push((8, serializer::BYTE, Value::Byte(flags)));
}
if pierce != 0 {
meta.push((9, serializer::BYTE, Value::Byte(pierce as i8)));
}
if in_ground {
meta.push((10, serializer::BOOLEAN, Value::Bool(true)));
}
meta
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn armor_stand_poses_send_only_what_differs() {
let tag =
horizon_world::nbt::parse_snbt("{Pose:{Head:[10f,20f,0f],LeftArm:[-10f,0f,-10f]}}")
.unwrap()
.0;
let meta = stand_poses(Some(&tag));
assert_eq!(
meta,
vec![(
16,
serializer::ROTATIONS,
Value::Floats(vec![10.0, 20.0, 0.0])
)]
);
assert!(stand_poses(None).is_empty());
}
}
@@ -157,6 +157,10 @@ pub(super) struct Rail {
powered: bool,
}
impl Minecart {
/// `Entity.onGround`.
pub(super) fn on_ground(&self) -> bool {
self.on_ground
}
pub fn new(id: u32, kind: CartKind, position: Vec3) -> Self {
Self {
id,
+18
View File
@@ -202,6 +202,7 @@ impl Game<'_> {
{
let (no_gravity, drop_item) = entity_nbt::falling_block_flags(&tag);
self.falling.push(FallingBlock {
id: 0,
state,
// DATA_START_POS is not persisted by vanilla.
start_pos: horizon_core::position::BlockPos::default(),
@@ -220,6 +221,23 @@ impl Game<'_> {
});
}
}
// PrimedTnt.readAdditionalSaveData: its fuse (80 when absent) and motion.
"minecraft:tnt" => {
if let Some(position) = entity_nbt::position(&tag) {
let fuse = match tag.get("fuse") {
Some(Tag::Short(f)) => i32::from(*f),
_ => 80,
};
self.tnts.push(super::tnt::PrimedTnt {
id: 0,
position,
previous: position,
velocity: entity_nbt::motion(&tag),
fuse,
on_ground: matches!(tag.get("OnGround"), Some(Tag::Byte(1))),
});
}
}
k if super::boats::any_boat_wood(k)
.is_some_and(|(wood, chest)| self.boat_models.has(wood, chest)) =>
{
+44 -3
View File
@@ -82,7 +82,7 @@ pub(super) struct Remote {
pub brand: String,
builtin: Arc<Builtin>,
lang: Arc<Language>,
registries: Arc<Dynamic>,
pub(super) registries: Arc<Dynamic>,
joined: bool,
status: String,
entity_id: i32,
@@ -246,6 +246,8 @@ impl Game<'_> {
self.player = None;
self.spawn = None;
self.mobs.clear();
self.clear_remote_objects();
self.clear_remote_projectiles();
self.creatures.projectiles.clear();
self.dropped.clear();
self.particles.clear();
@@ -363,6 +365,8 @@ impl Game<'_> {
self.world = World::new(self.res.facts.air, 0);
self.player = None;
self.mobs.clear();
self.clear_remote_objects();
self.clear_remote_projectiles();
self.dropped.clear();
self.particles.clear();
self.pickups.clear();
@@ -445,7 +449,7 @@ impl Game<'_> {
.and_then(|i| i.id(name))
.unwrap_or(0)
}
fn to_stack(&self, s: &Option<Stack>) -> Option<ItemStack> {
pub(super) fn to_stack(&self, s: &Option<Stack>) -> Option<ItemStack> {
let s = s.as_ref()?;
let mut out = ItemStack {
item: self.item_name(s.item),
@@ -658,6 +662,8 @@ impl Game<'_> {
self.world.day_time = day;
self.world.weather = weather;
self.mobs.clear();
self.clear_remote_objects();
self.clear_remote_projectiles();
self.dropped.clear();
self.particles.clear();
self.pickups.clear();
@@ -1043,11 +1049,15 @@ impl Game<'_> {
if let Some(m) = mob {
m.on_ground = on_ground;
}
self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground);
self.turn_remote_projectile(id, rotation);
}
fn remove_entity(&mut self, id: i32) {
self.mobs.retain(|m| m.id != id as u32);
self.orbs.retain(|o| o.id != id as u32);
self.dropped.retain(|d| d.id != id as u32);
self.remove_remote_object(id as u32);
self.remove_remote_projectile(id as u32);
if let Some(r) = self.remote.as_mut() {
r.entities.remove(&id);
r.lerps.remove(&id);
@@ -1311,6 +1321,16 @@ impl Game<'_> {
}
}
self.spawn_entity(p.id, &kind, p.position, rotation);
if !self.spawn_remote_object(p.id, &kind, p.position, rotation[0], p.data) {
self.spawn_remote_projectile(
p.id,
&kind,
p.position,
(rotation[0], rotation[1]),
p.velocity,
p.data,
);
}
}
C::RemoveEntities(p) => {
for id in p.ids {
@@ -1419,10 +1439,13 @@ impl Game<'_> {
o.value = (*v).max(0) as u32;
}
}
self.remote_object_data(p.id, &p.values);
self.remote_projectile_data(p.id, &p.values);
}
// ClientPacketListener.handleSetEquipment: what a mob or another
// player holds and wears (EquipmentSlot ordinals).
C::SetEquipment(p) => {
self.remote_object_equipment(p.id, &p.slots);
let items: Vec<(u8, Option<Arc<str>>)> = p
.slots
.iter()
@@ -2592,8 +2615,26 @@ impl Game<'_> {
l.steps -= 1;
}
}
lerps.retain(|_, l| l.steps > 0);
head_lerps.retain(|_, (_, steps)| *steps > 0);
if running {
// Boats, minecarts, armor stands, projectiles and the rest
// interpolate like mobs.
let mut step = |id: u32, at: Vec3| match lerps.get_mut(&(id as i32)) {
Some(l) if l.steps > 0 => {
let n = f64::from(l.steps);
l.steps -= 1;
Vec3::new(
at.x + (l.target[0] - at.x) / n,
at.y + (l.target[1] - at.y) / n,
at.z + (l.target[2] - at.z) / n,
)
}
_ => at,
};
self.tick_remote_objects(&mut step);
self.tick_remote_projectiles(&mut step);
}
lerps.retain(|_, l| l.steps > 0);
if let Some(r) = self.remote.as_mut() {
for (id, l) in lerps {
r.lerps.entry(id).or_insert(l);
@@ -0,0 +1,270 @@
//! Boats, minecarts, armor stands, item frames and paintings on a server's
//! client (`ClientPacketListener.handleAddEntity`, `handleSetEntityData`,
//! `handleSetEquipment`): the server simulates them, the client places them
//! where it says and interpolates their moves like a mob's.
use super::Game;
use horizon_client::entities::{ArmorStand, ItemFrame, Painting};
use horizon_core::geometry::Vec3;
use horizon_protocol::entity_data::Value;
use std::sync::Arc;
impl Game<'_> {
/// `handleAddEntity` for an object type; false when `kind` isn't one.
/// Hanging entities come at their block, with their direction as the
/// spawn data (`Direction.from3DDataValue`).
pub(super) fn spawn_remote_object(
&mut self,
id: i32,
kind: &str,
position: [f64; 3],
yaw: f32,
data: i32,
) -> bool {
let id = id as u32;
let at = Vec3::new(position[0], position[1], position[2]);
let block = position.map(|v| v.floor() as i32);
self.remove_remote_object(id);
if let Some(cart) = super::minecarts::CartKind::from_id(kind) {
let mut c = super::minecarts::Minecart::new(id, cart, at);
c.yaw = yaw;
c.previous_yaw = yaw;
self.carts.push(c);
} else if let Some((wood, chest)) = super::boats::any_boat_wood(kind)
&& self.boat_models.has(wood, chest)
{
let mut b = super::boats::Boat::new(id, Arc::from(wood), at, yaw);
if chest {
b = b.with_chest();
}
self.boats.push(b);
} else if kind == "minecraft:armor_stand" {
self.armor_stands.push(ArmorStand::new(id, at, yaw));
} else if kind == "minecraft:item_frame" || kind == "minecraft:glow_item_frame" {
self.item_frames.push(ItemFrame {
id,
pos: block,
facing: data.clamp(0, 5) as u8,
glow: kind == "minecraft:glow_item_frame",
item: None,
rotation: 0,
drop_chance: 1.0,
fixed: false,
invisible: false,
check_interval: 0,
});
} else if kind == "minecraft:painting" {
// The variant comes with the entity data; until then the
// registry's first one (VariantUtils.getAny).
let (variant, width, height) = self
.remote_painting_variant(0)
.map_or((Arc::from("minecraft:kebab"), 1, 1), |v| {
(v.id, v.width, v.height)
});
// Direction 3D data value (north 2, south 3, west 4, east 5) to 2D.
let facing = match data {
2 => 2,
4 => 1,
5 => 3,
_ => 0,
};
self.paintings.push(Painting {
id,
pos: block,
facing,
variant,
width,
height,
check_interval: 0,
});
} else {
return false;
}
true
}
/// A `painting_variant` by its network id, from the server's registry.
fn remote_painting_variant(&self, id: i32) -> Option<super::paintings::Variant> {
let r = self.remote.as_ref()?;
let name = r.registries.name("minecraft:painting_variant", id)?;
self.painting_variants.get(name).cloned()
}
/// `handleSetEntityData` for an object: what changes how it looks.
pub(super) fn remote_object_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
let id = id as u32;
for (index, _, value) in values {
if self.item_frames.iter().any(|f| f.id == id) {
// HangingEntity.DATA_DIRECTION, ItemFrame.DATA_ITEM and DATA_ROTATION.
let item = match (index, value) {
(9, Value::Item(stack)) => Some(self.to_stack(stack)),
_ => None,
};
let Some(f) = self.item_frames.iter_mut().find(|f| f.id == id) else {
continue;
};
match (index, value) {
(8, Value::Id(d)) => f.facing = (*d).clamp(0, 5) as u8,
(10, Value::Int(r)) => f.rotation = r.rem_euclid(8) as u8,
_ => {}
}
if let Some(item) = item {
f.item = item;
}
} else if self.paintings.iter().any(|p| p.id == id) {
// Painting.DATA_PAINTING_VARIANT_ID: a registry holder (id + 1).
if let (9, Value::Id(holder)) = (index, value)
&& let Some(v) = self.remote_painting_variant(holder - 1)
&& let Some(p) = self.paintings.iter_mut().find(|p| p.id == id)
{
p.variant = v.id;
p.width = v.width;
p.height = v.height;
}
} else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) {
match (index, value) {
(0, Value::Byte(flags)) => a.invisible = flags & 0x20 != 0,
(9, Value::Float(health)) => a.health = *health,
// ArmorStand.DATA_CLIENT_FLAGS.
(15, Value::Byte(flags)) => {
a.small = flags & 1 != 0;
a.show_arms = flags & 4 != 0;
a.no_base_plate = flags & 8 != 0;
a.marker = flags & 16 != 0;
}
_ => {}
}
} else if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) {
// VehicleEntity's hurt, direction and damage; the paddles.
match (index, value) {
(8, Value::Int(v)) => b.hurt_time = *v,
(9, Value::Int(v)) => b.hurt_dir = *v,
(10, Value::Float(v)) => b.damage = *v,
(11, Value::Bool(v)) => b.paddling[0] = *v,
(12, Value::Bool(v)) => b.paddling[1] = *v,
_ => {}
}
} else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) {
match (index, value) {
(8, Value::Int(v)) => c.hurt_time = *v,
(9, Value::Int(v)) => c.hurt_dir = *v,
(10, Value::Float(v)) => c.damage = *v,
_ => {}
}
}
}
}
/// `handleSetEquipment` for an armor stand (EquipmentSlot ordinals 0 to
/// 5: main hand, off hand, feet, legs, chest, head, its storage order).
pub(super) fn remote_object_equipment(
&mut self,
id: i32,
slots: &[(u8, Option<horizon_protocol::item::Stack>)],
) {
if !self.armor_stands.iter().any(|a| a.id == id as u32) {
return;
}
let items: Vec<_> = slots
.iter()
.map(|(slot, stack)| (usize::from(*slot), self.to_stack(stack)))
.collect();
if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id as u32) {
for (slot, item) in items {
if let Some(s) = a.equipment.get_mut(slot) {
*s = item;
}
}
}
}
/// A move's rotation and ground state for an object.
pub(super) fn turn_remote_object(&mut self, id: i32, yaw: Option<f32>, on_ground: bool) {
let id = id as u32;
if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) {
if let Some(yaw) = yaw {
b.yaw = yaw;
}
b.on_ground = on_ground;
} else if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) {
if let Some(yaw) = yaw {
c.yaw = yaw;
}
} else if let Some(a) = self.armor_stands.iter_mut().find(|a| a.id == id) {
if let Some(yaw) = yaw {
a.yaw = yaw;
}
a.on_ground = on_ground;
}
}
/// One tick of interpolation: `step` moves a position a step toward
/// the entity's last sent one, if it has a move in progress.
pub(super) fn tick_remote_objects(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) {
for c in &mut self.carts {
c.previous = c.position;
c.previous_yaw = c.yaw;
c.position = step(c.id, c.position);
}
for b in &mut self.boats {
b.previous = b.position;
b.previous_yaw = b.yaw;
b.position = step(b.id, b.position);
}
for a in &mut self.armor_stands {
a.position = step(a.id, a.position);
}
}
/// The boats, minecarts, armor stands, item frames and paintings in the
/// level (a server's client: the ones it was sent), for `status`.
pub(super) fn objects_status(&self) -> Vec<serde_json::Value> {
use serde_json::json;
let at = |p: Vec3| [p.x, p.y, p.z];
let mut out = Vec::new();
for b in &self.boats {
out.push(json!({"id": b.id, "kind": b.entity_type(), "position": at(b.position), "yaw": b.yaw}));
}
for c in &self.carts {
out.push(
json!({"id": c.id, "kind": c.kind.id(), "position": at(c.position), "yaw": c.yaw}),
);
}
for a in &self.armor_stands {
let items: Vec<_> = a
.equipment
.iter()
.map(|i| i.as_ref().map(|i| &*i.item))
.collect();
out.push(json!({"id": a.id, "kind": "minecraft:armor_stand", "position": at(a.position), "yaw": a.yaw,
"small": a.small, "arms": a.show_arms, "no_base_plate": a.no_base_plate, "equipment": items}));
}
for f in &self.item_frames {
out.push(
json!({"id": f.id, "kind": f.kind(), "position": f.pos, "facing": f.facing,
"item": f.item.as_ref().map(|i| &*i.item), "rotation": f.rotation}),
);
}
for p in &self.paintings {
out.push(json!({"id": p.id, "kind": "minecraft:painting", "position": p.pos, "facing": p.facing,
"variant": &*p.variant}));
}
out
}
/// Every object a server sent (a new connection, level or dimension).
pub(super) fn clear_remote_objects(&mut self) {
self.carts.clear();
self.boats.clear();
self.armor_stands.clear();
self.item_frames.clear();
self.paintings.clear();
}
pub(super) fn remove_remote_object(&mut self, id: u32) {
self.carts.retain(|c| c.id != id);
self.boats.retain(|b| b.id != id);
self.armor_stands.retain(|a| a.id != id);
self.item_frames.retain(|f| f.id != id);
self.paintings.retain(|p| p.id != id);
}
}
@@ -0,0 +1,229 @@
//! Primed TNT, falling blocks, projectiles, tridents, firework rockets and
//! end crystals on a server's client (`ClientPacketListener.handleAddEntity`
//! and `handleSetEntityData`): the server simulates them, the client draws
//! them where it says and interpolates their moves.
use super::{FallingBlock, Game};
use horizon_client::projectiles::{Projectile, ProjectileKind};
use horizon_core::{geometry::Vec3, position::BlockPos};
use horizon_protocol::entity_data::Value;
use horizon_world::registry::BlockStateId;
/// The projectile a type id names, as Horizon models it.
fn projectile_kind(kind: &str) -> Option<ProjectileKind> {
Some(match kind {
"minecraft:arrow" | "minecraft:spectral_arrow" => ProjectileKind::Arrow {
base_damage: 2.0,
critical: false,
effect: None,
},
"minecraft:snowball" => ProjectileKind::Snowball,
"minecraft:egg" => ProjectileKind::Egg,
"minecraft:ender_pearl" => ProjectileKind::EnderPearl,
"minecraft:small_fireball" => ProjectileKind::SmallFireball,
"minecraft:fireball" => ProjectileKind::LargeFireball { power: 1 },
"minecraft:llama_spit" => ProjectileKind::LlamaSpit,
"minecraft:wind_charge" => ProjectileKind::WindCharge,
"minecraft:breeze_wind_charge" => ProjectileKind::BreezeWindCharge,
"minecraft:wither_skull" => ProjectileKind::WitherSkull { dangerous: false },
_ => return None,
})
}
impl Game<'_> {
/// `handleAddEntity` for these types; false when `kind` isn't one.
pub(super) fn spawn_remote_projectile(
&mut self,
id: i32,
kind: &str,
position: [f64; 3],
rotation: (f32, f32),
velocity: [f64; 3],
data: i32,
) -> bool {
let id = id as u32;
let at = Vec3::new(position[0], position[1], position[2]);
let velocity = Vec3::new(velocity[0], velocity[1], velocity[2]);
let (yaw, pitch) = rotation;
self.remove_remote_projectile(id);
match kind {
"minecraft:tnt" => self.tnts.push(super::tnt::PrimedTnt {
id,
position: at,
previous: at,
velocity,
fuse: 80,
on_ground: false,
}),
// FallingBlockEntity.recreateFromPacket: the block state is the data.
"minecraft:falling_block" => self.falling.push(FallingBlock {
id,
state: BlockStateId(data.max(0) as u32),
start_pos: BlockPos::default(),
position: at,
previous: at,
velocity,
age: 0,
no_gravity: false,
drop_item: false,
fall_distance: 0.0,
damage: Default::default(),
saved: None,
}),
"minecraft:trident" => {
let mut t = super::trident::ThrownTrident::remote(id, at, yaw, pitch);
t.velocity = velocity;
self.tridents.push(t);
}
"minecraft:firework_rocket" => {
self.rockets
.push(super::fireworks::Rocket::remote(id, at, velocity));
}
"minecraft:end_crystal" => self.crystals.push(super::dragon::Crystal {
id,
position: at,
show_bottom: true,
time: 0,
beam: None,
invulnerable: false,
}),
_ => {
let Some(k) = projectile_kind(kind) else {
return false;
};
let mut p = Projectile::new(k, at, None);
p.id = id;
p.velocity = velocity;
p.yaw = yaw;
p.pitch = pitch;
self.creatures.projectiles.push(p);
}
}
true
}
/// `handleSetEntityData` for these types: what changes how they look.
pub(super) fn remote_projectile_data(&mut self, id: i32, values: &[(u8, i32, Value)]) {
let id = id as u32;
for (index, _, value) in values {
if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id) {
// PrimedTnt.DATA_FUSE_ID.
if let (8, Value::Int(fuse)) = (index, value) {
t.fuse = *fuse;
}
} else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id) {
// FallingBlockEntity.DATA_START_POS (the model's seed).
if let (8, Value::Pos(p)) = (index, value) {
f.start_pos = BlockPos::new(p[0], p[1], p[2]);
}
} else if let Some(c) = self.crystals.iter_mut().find(|c| c.id == id) {
// EndCrystal.DATA_BEAM_TARGET and DATA_SHOW_BOTTOM.
match (index, value) {
(8, Value::OptPos(beam)) => c.beam = *beam,
(9, Value::Bool(show)) => c.show_bottom = *show,
_ => {}
}
} else if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) {
match (&mut p.kind, index, value) {
// AbstractArrow.ID_FLAGS (critical) and IN_GROUND.
(ProjectileKind::Arrow { critical, .. }, 8, Value::Byte(flags)) => {
*critical = flags & 1 != 0;
}
(ProjectileKind::Arrow { .. }, 10, Value::Bool(in_ground)) => {
p.stuck = *in_ground;
}
// WitherSkull.DATA_DANGEROUS.
(ProjectileKind::WitherSkull { dangerous }, 8, Value::Bool(d)) => {
*dangerous = *d;
}
_ => {}
}
}
}
}
/// A move's rotation for a projectile or trident.
pub(super) fn turn_remote_projectile(&mut self, id: i32, rotation: Option<(f32, f32)>) {
let (id, Some((yaw, pitch))) = (id as u32, rotation) else {
return;
};
if let Some(p) = self.creatures.projectiles.iter_mut().find(|p| p.id == id) {
p.yaw = yaw;
p.pitch = pitch;
} else if let Some(t) = self.tridents.iter_mut().find(|t| t.id == id) {
t.yaw = yaw;
t.pitch = pitch;
}
}
/// One tick of interpolation, as `tick_remote_objects`.
pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) {
for t in &mut self.tnts {
t.previous = t.position;
t.position = step(t.id, t.position);
}
for f in &mut self.falling {
f.previous = f.position;
f.age += 1;
f.position = step(f.id, f.position);
}
for p in &mut self.creatures.projectiles {
p.previous = p.position;
p.age += 1;
p.position = step(p.id, p.position);
}
for t in &mut self.tridents {
let id = t.id;
t.remote_step(|at| step(id, at));
}
for r in &mut self.rockets {
let id = r.id;
r.remote_step(|at| step(id, at));
}
for c in &mut self.crystals {
c.time += 1;
c.position = step(c.id, c.position);
}
}
pub(super) fn remove_remote_projectile(&mut self, id: u32) {
self.tnts.retain(|t| t.id != id);
self.falling.retain(|f| f.id != id);
self.creatures.projectiles.retain(|p| p.id != id);
self.tridents.retain(|t| t.id != id);
self.rockets.retain(|r| r.id != id);
self.crystals.retain(|c| c.id != id);
}
/// Every one a server sent (a new connection, level or dimension).
pub(super) fn clear_remote_projectiles(&mut self) {
self.tnts.clear();
self.falling.clear();
self.creatures.projectiles.clear();
self.tridents.clear();
self.rockets.clear();
self.crystals.clear();
}
/// Their kind and place, for `status`.
pub(super) fn projectiles_status(&self) -> Vec<serde_json::Value> {
use serde_json::json;
let at = |p: Vec3| [p.x, p.y, p.z];
let mut out = Vec::new();
for t in &self.tnts {
out.push(json!({"id": t.id, "kind": "minecraft:tnt", "position": at(t.position), "fuse": t.fuse}));
}
for f in &self.falling {
out.push(json!({"id": f.id, "kind": "minecraft:falling_block", "position": at(f.position), "state": f.state.0}));
}
for c in &self.crystals {
out.push(json!({"id": c.id, "kind": "minecraft:end_crystal", "position": at(c.position), "show_bottom": c.show_bottom}));
}
for t in &self.tridents {
out.push(json!({"id": t.id, "kind": "minecraft:trident", "position": at(t.position)}));
}
for p in &self.creatures.projectiles {
out.push(json!({"id": p.id, "kind": format!("{:?}", p.kind).split([' ', '{']).next().unwrap_or(""), "position": at(p.position)}));
}
out
}
}
@@ -22,6 +22,17 @@ pub(super) struct Trip {
end: bool,
/// The Nether portal plan, whose exit is found or built on arrival.
plan: Option<Plan>,
/// Sent by a command: the player is already where it goes.
placed: bool,
}
/// `TeleportCommand` into another level for a server's player: where, and
/// the rotation it sets (none keeps the player's).
pub(super) struct LevelTeleport {
pub conn: u32,
pub dim: Dim,
pub at: [f64; 3],
pub rotation: Option<[f32; 2]>,
}
/// A player's box: Player's standing dimensions.
@@ -177,7 +188,8 @@ impl Game<'_> {
};
let at = [destination.x, destination.y, destination.z];
let ctx = self.server_ctx();
host.hub.change_level(i, at, yaw, &ctx);
// TeleportTransition.PLAY_PORTAL_SOUND.
host.hub.change_level(i, at, yaw, None, true, &ctx);
self.as_guest(host, conn, |g| g.trigger_dimension(source, target));
self.server_trips.push(Trip {
conn,
@@ -185,6 +197,7 @@ impl Game<'_> {
target: destination,
end,
plan,
placed: false,
});
if let Err(e) = self.enter_level(Some(host), source) {
log::error!("could not reopen {}: {e}", source.id());
@@ -214,6 +227,9 @@ impl Game<'_> {
continue;
}
let trip = self.server_trips.remove(k);
if trip.placed {
continue;
}
self.resolve_arrivals();
let yaw = host.hub.guests[i].yaw;
let (position, yaw) = if trip.end {
@@ -248,6 +264,43 @@ impl Game<'_> {
}
}
/// The players the command just run sends to another level
/// (`ServerPlayer.teleport` from TeleportCommand): a Respawn packet
/// keeping all data and the teleport, with no portal sound. The server
/// leaves the body alone until the level has loaded around it.
pub(super) fn run_level_teleports(&mut self, host: &mut LanHost) {
for t in std::mem::take(&mut self.level_teleports) {
let Some(i) = host.hub.guests.iter().position(|g| g.conn == t.conn) else {
continue;
};
let source = self.dim;
if let Err(e) = self.enter_level(Some(host), t.dim) {
log::error!("could not open {}: {e}", t.dim.id());
continue;
}
let g = &mut host.hub.guests[i];
let [yaw, pitch] = t.rotation.unwrap_or([g.yaw, g.pitch]);
// A new body there: no fall distance or motion carried over.
g.motion = None;
let ctx = self.server_ctx();
host.hub
.change_level(i, t.at, yaw, Some(pitch), false, &ctx);
self.as_guest(host, t.conn, |g| g.trigger_dimension(source, t.dim));
self.server_trips.retain(|trip| !trip.is(t.conn));
self.server_trips.push(Trip {
conn: t.conn,
dim: t.dim,
target: Vec3::new(t.at[0], t.at[1], t.at[2]),
end: false,
plan: None,
placed: true,
});
if let Err(e) = self.enter_level(Some(host), source) {
log::error!("could not reopen {}: {e}", source.id());
}
}
}
/// The hub's view of the server for packets sent outside its own tick.
pub(super) fn server_ctx(&mut self) -> TickCtx<'_> {
let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]);
+4
View File
@@ -12,6 +12,8 @@ use horizon_world::level::flags as level_flags;
#[derive(Clone, Debug)]
pub(super) struct PrimedTnt {
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
pub position: Vec3,
pub previous: Vec3,
pub velocity: Vec3,
@@ -140,6 +142,7 @@ impl Game<'_> {
f64::from(pos.z) + 0.5,
);
self.tnts.push(PrimedTnt {
id: 0,
position,
previous: position,
velocity: Vec3::new(-rot.sin() * 0.02, f64::from(0.2f32), -rot.cos() * 0.02),
@@ -501,6 +504,7 @@ mod tests {
world.set(21, 4, 20, flowing).unwrap();
let at = Vec3::new(20.5, 4.0, 20.5);
let mut tnt = PrimedTnt {
id: 0,
position: at,
previous: at,
velocity: Vec3::ZERO,
+37 -8
View File
@@ -15,20 +15,48 @@ use horizon_core::{
};
pub(super) struct ThrownTrident {
stack: ItemStack,
position: Vec3,
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
pub(super) stack: ItemStack,
pub(super) position: Vec3,
previous: Vec3,
velocity: Vec3,
yaw: f32,
pitch: f32,
in_ground: bool,
pub(super) velocity: Vec3,
pub(super) yaw: f32,
pub(super) pitch: f32,
pub(super) in_ground: bool,
in_ground_time: u32,
dealt_damage: bool,
loyalty: u32,
no_physics: bool,
pub(super) loyalty: u32,
pub(super) no_physics: bool,
returning: u32,
creative_only: bool,
}
impl ThrownTrident {
/// A trident a server sent (`handleAddEntity`): placed and moved by it.
pub(super) fn remote(id: u32, position: Vec3, yaw: f32, pitch: f32) -> Self {
Self {
id,
stack: ItemStack::new("minecraft:trident", 1),
position,
previous: position,
velocity: Vec3::ZERO,
yaw,
pitch,
in_ground: false,
in_ground_time: 0,
dealt_damage: false,
loyalty: 0,
no_physics: false,
returning: 0,
creative_only: false,
}
}
/// One tick of a server's client: the previous position, then `step`.
pub(super) fn remote_step(&mut self, step: impl FnOnce(Vec3) -> Vec3) {
self.previous = self.position;
self.position = step(self.position);
}
}
const HALF: f64 = 0.25;
fn bounds(p: Vec3) -> Aabb {
Aabb::new(
@@ -163,6 +191,7 @@ impl Game<'_> {
on_ground,
);
self.tridents.push(ThrownTrident {
id: 0,
loyalty: thrown.enchantment_level("minecraft:loyalty"),
stack: thrown,
position: start,
+229 -51
View File
@@ -11,7 +11,7 @@ use horizon_protocol::{
packets::play::{
self, AddEntity, EntityPositionSync, MoveEntity, MoveEntityPos, MoveEntityPosRot,
MoveEntityRot, PositionPath, RotateHead, SetEntityData, SetEntityMotion, SetEquipment,
VecDelta,
SetPassengers, VecDelta,
},
};
@@ -35,6 +35,30 @@ pub struct EntityState {
/// EquipmentSlot ordinals (0 main hand, 1 off hand, 2 feet, 3 legs,
/// 4 chest, 5 head, 6 body, 7 saddle) and their stacks.
pub equipment: Vec<(u8, Option<Stack>)>,
/// `Entity.getRequiresPrecisePosition` (primed TNT and falling blocks
/// touching a block): moves are sent as full position syncs.
pub precise: bool,
}
/// `AbstractArrow`: every update sends its position and rotation.
fn is_arrow(kind: &str) -> bool {
matches!(
kind,
"minecraft:arrow" | "minecraft:spectral_arrow" | "minecraft:trident"
)
}
/// `AbstractHurtingProjectile.accelerationPower` (0.1, none for wind
/// charges), sent with each velocity update.
fn acceleration_power(kind: &str) -> Option<f64> {
match kind {
"minecraft:fireball"
| "minecraft:small_fireball"
| "minecraft:dragon_fireball"
| "minecraft:wither_skull" => Some(0.1),
"minecraft:wind_charge" | "minecraft:breeze_wind_charge" => Some(0.0),
_ => None,
}
}
/// What a client was last told about one entity (ServerEntity's fields).
@@ -54,6 +78,40 @@ struct Sent {
/// The type's update interval (0: never) and whether velocity is tracked.
interval: u32,
track_deltas: bool,
/// lastPassengers, and wasRiding: a passenger's position isn't synced,
/// and the first update after it dismounts is a full position sync.
passengers: Vec<i32>,
was_riding: bool,
}
/// Who rides what this tick (`Entity.getPassengers`, `isPassenger`).
#[derive(Default)]
pub struct Riding {
/// Each vehicle's passengers, in order.
pub passengers: crate::fast_hash::FxHashMap<i32, Vec<i32>>,
/// Each passenger's vehicle.
pub vehicle: crate::fast_hash::FxHashMap<i32, i32>,
}
impl Riding {
pub fn clear(&mut self) {
self.passengers.clear();
self.vehicle.clear();
}
/// `passenger` rides `vehicle`, after the ones already riding it.
pub fn add(&mut self, vehicle: i32, passenger: i32) {
self.passengers.entry(vehicle).or_default().push(passenger);
self.vehicle.insert(passenger, vehicle);
}
pub fn passengers(&self, id: i32) -> &[i32] {
self.passengers.get(&id).map_or(&[], Vec::as_slice)
}
fn set_passengers(&self, vehicle: i32) -> play::Clientbound {
play::Clientbound::SetPassengers(SetPassengers {
vehicle,
passengers: self.passengers(vehicle).to_vec(),
})
}
}
#[derive(Default)]
@@ -94,7 +152,12 @@ impl Tracker {
}
/// The packets that introduce an entity (ServerEntity.sendPairingData).
pub fn spawn(&mut self, e: &EntityState, entity_type: i32) -> Vec<play::Clientbound> {
pub fn spawn(
&mut self,
e: &EntityState,
entity_type: i32,
riding: &Riding,
) -> Vec<play::Clientbound> {
let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw));
let head = pack_degrees(e.head_yaw);
let (_, interval, track_deltas) = tracking::settings(&e.kind);
@@ -121,6 +184,13 @@ impl Tracker {
slots: e.equipment.clone(),
}));
}
let passengers = riding.passengers(e.id).to_vec();
if !passengers.is_empty() {
out.push(riding.set_passengers(e.id));
}
if let Some(&vehicle) = riding.vehicle.get(&e.id) {
out.push(riding.set_passengers(vehicle));
}
self.sent.insert(
e.id,
Sent {
@@ -135,6 +205,8 @@ impl Tracker {
teleport_delay: 0,
interval,
track_deltas,
passengers,
was_riding: false,
},
);
out
@@ -144,20 +216,48 @@ impl Tracker {
/// rotation every update interval (positions at least every 60 ticks, a
/// full position sync every 400 or when the entity lands or leaves the
/// ground), velocity for types that track it, changed data at once.
pub fn update(&mut self, e: &EntityState) -> Vec<play::Clientbound> {
pub fn update(&mut self, e: &EntityState, riding: &Riding) -> Vec<play::Clientbound> {
let Some(s) = self.sent.get_mut(&e.id) else {
return Vec::new();
};
let mut out = Vec::new();
let passengers = riding.passengers(e.id);
if passengers != s.passengers.as_slice() {
out.push(riding.set_passengers(e.id));
s.passengers = passengers.to_vec();
}
let dirty = e.meta != s.meta;
let due = s.interval != 0 && s.tick.is_multiple_of(s.interval);
if due || dirty {
let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw));
let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1;
let rotate = turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0);
let arrow = is_arrow(&e.kind);
let rotate =
arrow || turned(rotation.1, s.rotation.1) || turned(rotation.0, s.rotation.0);
if riding.vehicle.contains_key(&e.id) {
// A passenger: its rotation alone (its vehicle places it).
if rotate {
out.push(play::Clientbound::MoveEntityRot(MoveEntityRot(
MoveEntity {
id: e.id,
delta: None,
rotation: Some((rotation.1, rotation.0)),
on_ground: e.on_ground,
},
)));
s.rotation = rotation;
}
s.base = e.position;
s.was_riding = true;
Self::send_dirty(e, s, dirty, &mut out);
Self::send_head(e, s, &mut out);
s.tick += 1;
Self::send_equipment(e, s, &mut out);
return out;
}
s.teleport_delay += 1;
let d: f64 = (0..3).map(|k| (e.position[k] - s.base[k]).powi(2)).sum();
let send_position = d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60);
let send_position = arrow || d >= 7.629_394_5e-6 || s.tick.is_multiple_of(60);
let sync = EntityPositionSync {
id: e.id,
path: PositionPath::Linear(e.position),
@@ -166,9 +266,14 @@ impl Tracker {
on_ground: e.on_ground,
};
// ServerEntity.createMovePacket.
let packet = if s.teleport_delay > 400 || s.was_on_ground != e.on_ground {
let packet = if e.precise
|| s.teleport_delay > 400
|| s.was_riding
|| s.was_on_ground != e.on_ground
{
s.was_on_ground = e.on_ground;
s.teleport_delay = 0;
s.was_riding = false;
Some(play::Clientbound::EntityPositionSync(sync))
} else if send_position {
let delta = [0, 1, 2].map(|k| encode(e.position[k]) - encode(s.base[k]));
@@ -206,10 +311,23 @@ impl Tracker {
let still = e.velocity.iter().all(|v| *v == 0.0);
if diff > 1.0e-7 || diff > 0.0 && still {
s.velocity = e.velocity;
out.push(play::Clientbound::SetEntityMotion(SetEntityMotion {
let motion = play::Clientbound::SetEntityMotion(SetEntityMotion {
id: e.id,
velocity: e.velocity,
}));
});
match acceleration_power(&e.kind) {
// A bundle: the motion and the projectile's power.
Some(power) => out.extend([
play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty),
motion,
play::Clientbound::ProjectilePower(play::ProjectilePower {
id: e.id,
power,
}),
play::Clientbound::BundleDelimiter(horizon_protocol::packets::Empty),
]),
None => out.push(motion),
}
}
}
if let Some(packet) = packet {
@@ -227,33 +345,47 @@ impl Tracker {
s.rotation = rotation;
}
}
// ServerEntity.sendDirtyEntityData.
if dirty {
let changed: Vec<_> = e
.meta
.iter()
.filter(|m| !s.meta.contains(m))
.cloned()
.collect();
if !changed.is_empty() {
out.push(play::Clientbound::SetEntityData(SetEntityData {
id: e.id,
values: changed,
}));
}
s.meta = e.meta.clone();
}
let head = pack_degrees(e.head_yaw);
if turned(head, s.head) {
out.push(play::Clientbound::RotateHead(RotateHead {
id: e.id,
head_yaw: head,
}));
s.head = head;
}
Self::send_dirty(e, s, dirty, &mut out);
Self::send_head(e, s, &mut out);
}
s.tick += 1;
// LivingEntity.detectEquipmentUpdates: changed slots every tick.
Self::send_equipment(e, s, &mut out);
out
}
/// ServerEntity.sendDirtyEntityData.
fn send_dirty(e: &EntityState, s: &mut Sent, dirty: bool, out: &mut Vec<play::Clientbound>) {
if !dirty {
return;
}
let changed: Vec<_> = e
.meta
.iter()
.filter(|m| !s.meta.contains(m))
.cloned()
.collect();
if !changed.is_empty() {
out.push(play::Clientbound::SetEntityData(SetEntityData {
id: e.id,
values: changed,
}));
}
s.meta = e.meta.clone();
}
fn send_head(e: &EntityState, s: &mut Sent, out: &mut Vec<play::Clientbound>) {
let head = pack_degrees(e.head_yaw);
if (i32::from(head) - i32::from(s.head)).abs() >= 1 {
out.push(play::Clientbound::RotateHead(RotateHead {
id: e.id,
head_yaw: head,
}));
s.head = head;
}
}
/// LivingEntity.detectEquipmentUpdates: changed slots every tick.
fn send_equipment(e: &EntityState, s: &mut Sent, out: &mut Vec<play::Clientbound>) {
if e.equipment != s.equipment {
// Cleared slots are sent as empty stacks.
let mut slots: Vec<(u8, Option<Stack>)> = e
@@ -275,7 +407,6 @@ impl Tracker {
}
s.equipment = e.equipment.clone();
}
out
}
}
@@ -297,44 +428,91 @@ mod tests {
data: 0,
meta: Vec::new(),
equipment: Vec::new(),
precise: false,
}
}
#[test]
fn moves_go_out_every_update_interval() {
let mut t = Tracker::default();
let spawn = t.spawn(&state(0.0), 100);
let spawn = t.spawn(&state(0.0), 100, &Riding::default());
assert!(matches!(spawn[0], play::Clientbound::AddEntity(_)));
// Tick 0 is due and a multiple of 60: the position goes out unchanged.
assert!(matches!(
t.update(&state(0.0))[..],
t.update(&state(0.0), &Riding::default())[..],
[play::Clientbound::MoveEntityPos(_)]
));
// A zombie updates every third tick.
assert!(t.update(&state(0.5)).is_empty());
assert!(t.update(&state(0.5)).is_empty());
assert!(t.update(&state(0.5), &Riding::default()).is_empty());
assert!(t.update(&state(0.5), &Riding::default()).is_empty());
assert!(matches!(
t.update(&state(0.5))[..],
t.update(&state(0.5), &Riding::default())[..],
[play::Clientbound::MoveEntityPos(_)]
));
for _ in 0..2 {
t.update(&state(40.0));
t.update(&state(40.0), &Riding::default());
}
let jump = t.update(&state(40.0));
let jump = t.update(&state(40.0), &Riding::default());
assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_)));
}
#[test]
fn fireballs_bundle_their_motion_with_their_power() {
let mut t = Tracker::default();
let mut e = state(0.0);
e.kind = "minecraft:fireball".into();
e.on_ground = false;
t.spawn(&e, 100, &Riding::default());
e.velocity = [0.0, 0.0, 0.5];
let out = t.update(&e, &Riding::default());
assert!(matches!(
out[..],
[
play::Clientbound::BundleDelimiter(_),
play::Clientbound::SetEntityMotion(_),
play::Clientbound::ProjectilePower(play::ProjectilePower { power, .. }),
play::Clientbound::BundleDelimiter(_),
play::Clientbound::MoveEntityPos(_),
] if power == 0.1
));
}
#[test]
fn arrows_send_position_and_rotation_every_update() {
let mut t = Tracker::default();
let mut e = state(0.0);
e.kind = "minecraft:arrow".into();
t.spawn(&e, 100, &Riding::default());
assert!(matches!(
t.update(&e, &Riding::default())[..],
[play::Clientbound::MoveEntityPosRot(_)]
));
}
#[test]
fn precise_entities_sync_their_position() {
let mut t = Tracker::default();
let mut e = state(0.0);
e.kind = "minecraft:tnt".into();
e.precise = true;
t.spawn(&e, 100, &Riding::default());
assert!(matches!(
t.update(&e, &Riding::default())[..],
[play::Clientbound::EntityPositionSync(_)]
));
}
#[test]
fn landing_sends_a_position_sync() {
let mut t = Tracker::default();
let mut e = state(0.0);
t.spawn(&e, 100);
t.update(&e);
t.spawn(&e, 100, &Riding::default());
t.update(&e, &Riding::default());
e.on_ground = false;
t.update(&e);
t.update(&e);
t.update(&e, &Riding::default());
t.update(&e, &Riding::default());
assert!(matches!(
t.update(&e)[..],
t.update(&e, &Riding::default())[..],
[play::Clientbound::EntityPositionSync(_)]
));
}
@@ -343,11 +521,11 @@ mod tests {
fn metadata_and_equipment_send_only_the_changes() {
let mut t = Tracker::default();
let mut e = state(0.0);
t.spawn(&e, 100);
t.update(&e);
t.spawn(&e, 100, &Riding::default());
t.update(&e, &Riding::default());
e.meta.push((0, 0, Value::Byte(1)));
e.equipment.push((0, Some(Stack::new(5, 1))));
let out = t.update(&e);
let out = t.update(&e, &Riding::default());
assert!(
out.iter()
.any(|p| matches!(p, play::Clientbound::SetEntityData(_)))
@@ -356,9 +534,9 @@ mod tests {
out.iter()
.any(|p| matches!(p, play::Clientbound::SetEquipment(_)))
);
assert!(t.update(&e).is_empty());
assert!(t.update(&e, &Riding::default()).is_empty());
e.equipment.clear();
let out = t.update(&e);
let out = t.update(&e, &Riding::default());
let Some(play::Clientbound::SetEquipment(eq)) = out.first() else {
panic!("expected equipment");
};
+26 -9
View File
@@ -124,6 +124,8 @@ pub struct Hub {
/// The `player_movement_check` and `elytra_movement_check` game rules,
/// set by the game before each tick.
pub movement_checks: (bool, bool),
/// Who rides what, set by the game before each tick.
pub riding: super::entity::Riding,
last_clock: i64,
last_rate: f32,
ticking: (f32, bool),
@@ -237,6 +239,7 @@ impl Hub {
sync,
ticks: 0,
movement_checks: (true, true),
riding: Default::default(),
last_clock: i64::MIN,
last_rate: 0.0,
ticking: (20.0, false),
@@ -1375,6 +1378,7 @@ impl Hub {
data: 0,
meta,
equipment,
precise: false,
}
}
@@ -1501,14 +1505,14 @@ impl Hub {
continue;
}
if self.guests[gi].tracker.is_tracking(e.id) {
let packets = self.guests[gi].tracker.update(e);
let packets = self.guests[gi].tracker.update(e, &self.riding);
self.guests[gi].send_all(&packets);
} else if visible(&self.guests[gi], ei) {
let Some(kind) = self.entity_type(&e.kind) else {
continue;
};
// Players need their list entry first; guests got it at join.
let packets = self.guests[gi].tracker.spawn(e, kind);
let packets = self.guests[gi].tracker.spawn(e, kind, &self.riding);
self.guests[gi].send_all(&packets);
}
}
@@ -2197,7 +2201,15 @@ impl Hub {
/// names (`set_dimension`): ClientboundRespawnPacket keeping all data,
/// then vanilla's packet order. Nothing tracked in the old level is
/// removed one by one: the client drops its whole level.
pub fn change_level(&mut self, i: usize, at: [f64; 3], yaw: f32, ctx: &TickCtx) {
pub fn change_level(
&mut self,
i: usize,
at: [f64; 3],
yaw: f32,
pitch: Option<f32>,
portal_sound: bool,
ctx: &TickCtx,
) {
let mode = self.guests[i].game_mode;
let info = self.spawn_info(mode);
self.close_container_for(i);
@@ -2208,6 +2220,9 @@ impl Hub {
g.chunk_queue.clear();
g.center = None;
g.yaw = yaw;
if let Some(pitch) = pitch {
g.pitch = pitch;
}
g.position = at;
g.previous_position = at;
g.send(&C::Respawn(play::Respawn {
@@ -2239,12 +2254,14 @@ impl Hub {
// ServerPlayer.sendPostEffects.
g.send(&raw(ids::POST_EFFECTS, vec![0]));
// TeleportTransition.PLAY_PORTAL_SOUND.
g.send(&C::LevelEvent(play::LevelEvent {
kind: 1032,
pos: [0, 0, 0],
data: 0,
global: false,
}));
if portal_sound {
g.send(&C::LevelEvent(play::LevelEvent {
kind: 1032,
pos: [0, 0, 0],
data: 0,
global: false,
}));
}
// lastSentExp, lastSentHealth and lastSentFood.
g.sent_health = None;
g.sent_xp = None;
+3 -11
View File
@@ -65,17 +65,9 @@ impl LightTables {
}
/// Full tables with the vanilla occlusion shapes from `occlusion.nbt`.
pub fn load(root: &Path, registry: &BlockRegistry, facts: &BlockFacts) -> crate::Result<Self> {
use horizon_world::{
collision::CachedCollisionRegistry,
nbt::{self, Limits},
};
let data = nbt::read_named(
&std::fs::read(root.join("occlusion.nbt"))?,
Limits::tables(),
)?
.0
.tag;
let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?;
use horizon_world::{collision::CachedCollisionRegistry, registry::BlockTableFile};
let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?;
let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?;
let mut tables = Self::new(facts);
let block = VoxelShape::block();
let mut shapes: Vec<VoxelShape> = vec![VoxelShape::empty(), block.clone()];
+156 -4
View File
@@ -674,6 +674,28 @@ pub fn tag_namespaces(root: &Path) -> Vec<String> {
/// Bundles of block tag files (tag id → the tag file's text).
const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"];
/// The worldgen loader's bundle of biome tags.
const BIOME_TAGS: &str = "worldgen/biome_tags.json";
/// Worldgen registries a content mod may add to, and the bundle the
/// worldgen loader reads each from (id → the file's text): what structure
/// mods hold. Biomes, noise and density functions change the terrain and
/// are not read from mods.
const WORLDGEN_BUNDLES: [(&str, &str); 6] = [
("structure", "worldgen/structures.json"),
("structure_set", "worldgen/structure_sets.json"),
("template_pool", "worldgen/template_pools.json"),
("processor_list", "worldgen/processor_lists.json"),
("configured_feature", "worldgen/features.json"),
("placed_feature", "worldgen/placed_features.json"),
];
/// A mod file that adds to a worldgen bundle: (bundle, id).
fn worldgen_entry(file: &str) -> Option<(&'static str, String)> {
let (ns, rest) = file.strip_prefix("data/")?.split_once("/worldgen/")?;
let (kind, path) = rest.split_once('/')?;
let (_, bundle) = WORLDGEN_BUNDLES.iter().find(|(k, _)| *k == kind)?;
Some((bundle, format!("{ns}:{}", path.strip_suffix(".json")?)))
}
/// The overlay being written: `out` mirrors `base` through links, and a
/// directory becomes real only when something inside it is written.
@@ -773,6 +795,9 @@ fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result<String> {
struct Known {
blocks: std::collections::BTreeSet<String>,
items: std::collections::BTreeSet<String>,
/// The worldgen loader's biomes and biome tags.
biomes: std::collections::BTreeSet<String>,
biome_tags: std::collections::BTreeSet<String>,
/// `data/<ns>/tags/<kind>/<path>.json` files the mods add.
tag_files: std::collections::BTreeSet<String>,
}
@@ -786,14 +811,27 @@ impl Known {
items.extend(o.into_iter().map(|(k, _)| k));
}
items.extend(plan.items.iter().map(|i| i.rename.to.clone()));
let keys = |name: &str| -> Result<std::collections::BTreeSet<String>> {
Ok(match base.join(name).exists() {
true => match read_json(&base.join(name))? {
J::Obj(o) => o.into_iter().map(|(k, _)| k).collect(),
_ => Default::default(),
},
false => Default::default(),
})
};
Ok(Known {
blocks,
items,
biomes: keys("worldgen/biomes.json")?,
biome_tags: keys(BIOME_TAGS)?,
tag_files: tags.keys().cloned().collect(),
})
}
/// Whether tag `file` keeps entry `v`: a known id or an existing tag,
/// for block and item tags (other registries are kept as they are).
/// for block, item and biome tags (other registries are kept as they
/// are). The worldgen loader refuses a biome tag naming what it lacks
/// (another mod's biomes, a loader's tags), so those entries go.
fn keeps(&self, file: &str, v: &J) -> bool {
let id = match v {
J::Str(s) => s.as_str(),
@@ -803,23 +841,32 @@ impl Known {
},
_ => return true,
};
let Some(kind) = file
let Some(rest) = file
.strip_prefix("data/")
.and_then(|r| r.split_once("/tags/"))
.and_then(|(_, rest)| Some(rest.split_once('/')?.0))
.map(|(_, rest)| rest)
else {
return true;
};
let kind = match rest.strip_prefix("worldgen/biome/") {
Some(_) => "worldgen/biome",
None => rest.split_once('/').map_or("", |(k, _)| k),
};
let ids = match kind {
"block" => &self.blocks,
"item" => &self.items,
"worldgen/biome" => &self.biomes,
_ => return true,
};
match id.strip_prefix('#') {
Some(tag) => {
let (tns, path) = tag.split_once(':').unwrap_or(("minecraft", tag));
let rel = format!("data/{tns}/tags/{kind}/{path}.json");
self.tag_files.contains(&rel) || tns == "minecraft"
self.tag_files.contains(&rel)
|| match kind {
"worldgen/biome" => self.biome_tags.contains(&format!("{tns}:{path}")),
_ => tns == "minecraft",
}
}
None => ids.contains(id),
}
@@ -856,6 +903,7 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
}
// Mod files, and every tag addition gathered per tag file.
let mut tags: BTreeMap<String, Vec<J>> = BTreeMap::new();
let mut worldgen: BTreeMap<&str, Vec<(String, String)>> = BTreeMap::new();
for m in mods {
let declarations = format!("data/{}/horizon/", m.id);
for file in &m.content.files {
@@ -878,6 +926,18 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
.extend(values.iter().cloned());
} else if !file.starts_with(&declarations) {
overlay.write(file, &bytes)?;
if let Some((bundle, id)) = worldgen_entry(file) {
let text = String::from_utf8(bytes).map_err(|e| format!("{file}: {e}"))?;
worldgen.entry(bundle).or_default().push((id, text));
} else if let Some(template) = file
.strip_prefix("data/")
.and_then(|r| r.split_once("/structure/"))
.filter(|(_, path)| path.ends_with(".nbt"))
{
// The worldgen loader's templates: worldgen/structure/<ns>/<path>.nbt.
let (ns, path) = template;
overlay.write(&format!("worldgen/structure/{ns}/{path}"), &bytes)?;
}
}
}
}
@@ -921,6 +981,38 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
for (name, doc) in &bundles {
overlay.write(name, &serde_json::to_vec(doc)?)?;
}
// Biome tags, merged into the worldgen loader's bundle as block tags are.
let biome_tags: Vec<(String, &Vec<J>)> = tags
.iter()
.filter_map(|(file, values)| {
let (ns, path) = file
.strip_prefix("data/")?
.split_once("/tags/worldgen/biome/")?;
Some((format!("{ns}:{}", path.strip_suffix(".json")?), values))
})
.collect();
if !biome_tags.is_empty() && base.join(BIOME_TAGS).exists() {
let mut doc = read_json(&base.join(BIOME_TAGS))?;
for (id, values) in biome_tags {
let text = match doc.get(&id) {
Some(J::Str(t)) => Some(t.clone()),
_ => None,
};
doc.set(&id, J::Str(merge_tag(text.as_deref(), values, BIOME_TAGS)?));
}
overlay.write(BIOME_TAGS, &serde_json::to_vec(&doc)?)?;
}
// Structures and the pools, sets, processors and features they use.
for (bundle, entries) in &worldgen {
if !base.join(bundle).exists() {
continue;
}
let mut doc = read_json(&base.join(bundle))?;
for (id, text) in entries {
doc.set(id, J::Str(text.clone()));
}
overlay.write(bundle, &serde_json::to_vec(&doc)?)?;
}
// A block without its own blockstate looks like its template, so every
// state still has a model.
for m in mods {
@@ -943,15 +1035,75 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
/// The root to load: `base` itself with no content, else the mods' overlay
/// over it, built in `cache/mod-content`.
///
/// The root is built once and kept: later starts with the same build, base
/// and mod files reuse it. With ATM10's carried mods a build takes about
/// 25 s and peaks above 3 GB, more than everything the game keeps.
pub fn resource_root(base: &Path, mods: &[ContentMod], cache: &Path) -> Result<PathBuf> {
if mods.iter().all(|m| m.content.is_empty()) {
return Ok(base.to_owned());
}
let out = cache.join("mod-content");
let stamp = stamp(base, mods)?;
if fs::read(out.join(STAMP)).is_ok_and(|s| s == stamp.as_bytes()) {
return Ok(out);
}
build(base, mods, &out)?;
fs::write(out.join(STAMP), stamp)?;
Ok(out)
}
/// In a root built by [`resource_root`]: what it was built from.
const STAMP: &str = "horizon-mod-content.stamp";
/// What a content root is built from: this build of the game, the base's
/// manifest and the tables [`build`] rewrites, and every file of every mod
/// in order. Files count by size and modification time, as build tools do.
fn stamp(base: &Path, mods: &[ContentMod]) -> Result<String> {
use sha2::{Digest, Sha256};
let meta = |path: &Path| -> Result<String> {
let m = fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?;
let modified = m
.modified()?
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
Ok(format!(
"{} {} {}",
path.display(),
m.len(),
modified.as_nanos()
))
};
let mut parts = vec!["horizon mod content 1".to_owned()];
parts.push(meta(&std::env::current_exe()?)?);
let base = std::path::absolute(base)?;
parts.push(
String::from_utf8_lossy(&fs::read(base.join("horizon-resources.json"))?).into_owned(),
);
for name in JSON_TABLES.iter().chain(&NBT_TABLES).chain(&TAG_BUNDLES) {
if base.join(name).exists() {
parts.push(meta(&base.join(name))?);
}
}
for m in mods {
parts.push(m.id.clone());
let source = m.content.source();
if source.is_dir() {
for file in &m.content.files {
parts.push(meta(&source.join(file))?);
}
} else {
parts.push(meta(source)?);
}
}
let mut hasher = Sha256::new();
for part in &parts {
hasher.update((part.len() as u64).to_le_bytes());
hasher.update(part.as_bytes());
}
Ok(format!("{:x}", hasher.finalize()))
}
#[cfg(test)]
#[path = "mod_content_tests.rs"]
mod tests;
@@ -502,3 +502,119 @@ fn independent_reloads_read_changed_behaviour_json_and_report_invalid_files() {
);
fs::remove_dir_all(&scratch).unwrap();
}
/// A built root is kept for later starts, and built again when a mod's
/// files change.
#[test]
fn content_root_is_reused_until_a_mod_changes() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-reuse-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let m = ruby_mod(&scratch.join("ruby"));
let cache = scratch.join("cache");
let root = resource_root(&base, std::slice::from_ref(&m), &cache).unwrap();
// A rebuild starts from an empty folder: this file tells them apart.
let sentinel = root.join("sentinel");
fs::write(&sentinel, b"").unwrap();
assert_eq!(
resource_root(&base, std::slice::from_ref(&m), &cache).unwrap(),
root
);
assert!(sentinel.exists(), "an unchanged mod rebuilt the root");
let lang = scratch.join("ruby/assets/ruby/lang/en_us.json");
fs::write(&lang, r#"{"block.ruby.ruby_block": "Ruby Block"}"#).unwrap();
let file = fs::File::options().write(true).open(&lang).unwrap();
file.set_modified(std::time::SystemTime::now() + std::time::Duration::from_secs(5))
.unwrap();
resource_root(&base, std::slice::from_ref(&m), &cache).unwrap();
assert!(!sentinel.exists(), "a changed mod kept the old root");
assert!(
fs::read_to_string(root.join("assets/ruby/lang/en_us.json"))
.unwrap()
.contains("Ruby Block")
);
let _ = fs::remove_dir_all(&scratch);
}
/// A structure mod's worldgen reaches the worldgen loader: its structure,
/// set, pool and templates join the bundles, and its biome tag keeps only
/// what the loader knows.
#[test]
fn content_root_carries_a_structure_mod() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-worldgen-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let dir = scratch.join("huts");
let files = [
(
"horizon-mod.toml",
"id = \"huts\"\nname = \"Huts\"\nversion = \"1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n",
),
(
"data/huts/worldgen/structure/hut.json",
r##"{"type": "minecraft:jigsaw", "biomes": "#huts:has_hut", "step": "surface_structures",
"spawn_overrides": {}, "terrain_adaptation": "beard_thin", "start_pool": "huts:hut/start",
"size": 1, "start_height": {"absolute": 0}, "project_start_to_heightmap": "WORLD_SURFACE_WG",
"max_distance_from_center": 80}"##,
),
(
"data/huts/worldgen/template_pool/hut/start.json",
r#"{"fallback": "minecraft:empty", "elements": [{"weight": 1, "element": {
"element_type": "minecraft:single_pool_element", "location": "huts:hut",
"projection": "rigid", "processors": "minecraft:empty"}}]}"#,
),
(
"data/huts/worldgen/structure_set/huts.json",
r#"{"structures": [{"structure": "huts:hut", "weight": 1}],
"placement": {"type": "minecraft:random_spread", "salt": 7, "spacing": 4, "separation": 2}}"#,
),
(
"data/huts/tags/worldgen/biome/has_hut.json",
r##"{"values": ["#minecraft:is_overworld", "#c:is_plains", "othermod:glade", "minecraft:plains"]}"##,
),
];
for (name, text) in files {
let path = dir.join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
// The template: vanilla's smallest plains house, as the mod's own file.
let house =
base.join("worldgen/structure/minecraft/village/plains/houses/plains_small_house_1.nbt");
fs::create_dir_all(dir.join("data/huts/structure")).unwrap();
fs::copy(&house, dir.join("data/huts/structure/hut.nbt")).unwrap();
let package = horizon_mods::Package::read(&dir).unwrap();
let m = ContentMod {
id: "huts".into(),
content: Content::read(&dir, &package.manifest).unwrap(),
};
let root = resource_root(&base, &[m], &scratch.join("cache")).unwrap();
assert!(root.join("worldgen/structure/huts/hut.nbt").exists());
let tags: BTreeMap<String, String> =
serde_json::from_slice(&fs::read(root.join("worldgen/biome_tags.json")).unwrap()).unwrap();
let hut: serde_json::Value = serde_json::from_str(&tags["huts:has_hut"]).unwrap();
assert_eq!(
hut["values"],
serde_json::json!(["#minecraft:is_overworld", "minecraft:plains"])
);
let assets = Assets::load(&root).unwrap();
let biomes = crate::biome::Biomes::load(&root).unwrap();
let terrain = crate::vanillagen::VanillaTerrain::new(
&root.join("worldgen"),
assets.registry,
&biomes,
1234,
)
.unwrap();
let (name, at) = terrain
.locate(&["huts:hut".to_owned()], [0, 64, 0], 16)
.expect("the mod's structure generates");
assert_eq!(name, "huts:hut");
assert!(at[0].abs() < 1024 && at[2].abs() < 1024, "{at:?}");
let _ = fs::remove_dir_all(&scratch);
}
+3 -5
View File
@@ -117,13 +117,11 @@ pub fn load_visibility(
use horizon_world::{
block_render::{FaceVisibility, RenderDefinition, SkipRendering},
collision::CachedCollisionRegistry,
nbt::{self, Limits},
registry::BlockTableFile,
};
use std::{collections::BTreeMap, fs};
let data = nbt::read_named(&fs::read(root.join("occlusion.nbt"))?, Limits::tables())?
.0
.tag;
let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?;
let file = BlockTableFile::open(&root.join("occlusion.nbt"), registry)?;
let occlusion = CachedCollisionRegistry::from_table(file.table("occlusion")?)?;
let raw: BTreeMap<String, Value> =
serde_json::from_slice(&fs::read(root.join("render-definitions.json"))?)?;
let definitions = raw
+19 -10
View File
@@ -31,8 +31,7 @@ use horizon_world::{
movement_restitution::{self, RestitutionInput, RestitutionWorld},
movement_statistics::{self, MovementStat, MovementStatisticsWorld},
movement_support::{self, MovementLocation},
nbt::{self, Limits},
registry::BlockStateId,
registry::{BlockStateId, BlockTableFile},
world_movement::{self, MovementRequest},
worldgen::tags::TagDefinitions,
};
@@ -57,17 +56,27 @@ pub struct MotionResources {
pub fluids: FluidResources,
}
impl MotionResources {
/// [`Self::load_with_facts`] with the block facts decoded here.
pub fn load(root: &Path, assets: &Assets) -> Result<Self> {
Self::load_with_facts(
root,
assets,
&crate::blocks::BlockFacts::load(root, assets.registry)?,
)
}
/// The motion tables, with the block facts the game already decoded
/// (decoding `gameplay-facts.json` again costs a second or more and, with
/// a big modpack, hundreds of megabytes while it runs).
pub fn load_with_facts(
root: &Path,
assets: &Assets,
facts: &crate::blocks::BlockFacts,
) -> Result<Self> {
let attributes = Arc::new(Catalog::from_json(&serde_json::from_slice(&fs::read(
root.join("attributes.json"),
)?)?)?);
let data = nbt::read_named(
&fs::read(root.join("movement-properties.nbt"))?,
Limits::tables(),
)?
.0
.tag;
let properties = MovementProperties::from_nbt(&data, assets.registry)?;
let file = BlockTableFile::open(&root.join("movement-properties.nbt"), assets.registry)?;
let properties = MovementProperties::from_table(file.table("movement")?)?;
let bundle: BTreeMap<String, String> =
serde_json::from_slice(&fs::read(root.join("block-tags.json"))?)?;
let mut definitions = TagDefinitions::default();
@@ -91,7 +100,7 @@ impl MotionResources {
Ok(Self {
landing: crate::landing::load(root, &properties)?,
health: crate::health::HealthResources::load(root)?,
fluids: FluidResources::load(root, assets)?,
fluids: FluidResources::new(assets, facts)?,
attributes,
blocks: properties.bind(&MovementTags {
fences: tag("minecraft:fences")?,
+1 -2
View File
@@ -33,8 +33,7 @@ pub struct FluidResources {
pub shapes: Mutex<FluidShapeCache>,
}
impl FluidResources {
pub fn load(root: &Path, assets: &Assets) -> Result<Self> {
let facts = crate::blocks::BlockFacts::load(root, assets.registry)?;
pub fn new(assets: &Assets, facts: &crate::blocks::BlockFacts) -> Result<Self> {
Ok(Self {
states: FluidStateTable::new(assets.registry)?,
sturdy: facts.states.iter().map(|s| s.sturdy_full).collect(),
+3
View File
@@ -63,6 +63,8 @@ pub enum ProjectileKind {
}
#[derive(Clone, Debug, PartialEq)]
pub struct Projectile {
/// The entity id a server tracks it by (0 until it is tracked).
pub id: u32,
pub kind: ProjectileKind,
pub owner: Option<Who>,
pub position: Vec3,
@@ -128,6 +130,7 @@ impl Projectile {
pub fn new(kind: ProjectileKind, position: Vec3, owner: Option<Who>) -> Self {
let kind_is_arrow = matches!(kind, ProjectileKind::Arrow { .. });
Self {
id: 0,
kind,
owner,
position,
+12
View File
@@ -267,6 +267,18 @@ impl Text {
_ => c,
}
}
/// Style fields the spans don't model (an entity hover, an insertion),
/// set on the component sent to vanilla only.
pub fn with_component_fields(mut self, fields: Vec<(String, Value)>) -> Self {
let mut c = self.component();
if let Value::Object(o) = &mut c {
for (k, v) in fields {
o.entry(k).or_insert(v);
}
}
self.source = Some(c.into());
self
}
pub fn with_color(self, color: u32) -> Self {
self.with_style(&Style::color(color))
}
+14 -1
View File
@@ -56,6 +56,19 @@ impl VanillaTerrain {
biomes: &Biomes,
seed: i64,
dimension: &str,
) -> crate::Result<Self> {
Self::for_world(dir, registry, biomes, seed, dimension, true)
}
/// A dimension of a world whose `generate_structures` is `structures`
/// (`ChunkGenerator.createStructures` and `findNearestMapStructure` do
/// nothing without).
pub fn for_world(
dir: &Path,
registry: &'static BlockRegistry,
biomes: &Biomes,
seed: i64,
dimension: &str,
structures: bool,
) -> crate::Result<Self> {
let data: Arc<GeneratorData> = shared_data(dir, registry)?;
let biome_map = data
@@ -64,7 +77,7 @@ impl VanillaTerrain {
.map(|name| biomes.id(name))
.collect();
let generator =
OverworldGenerator::for_dimension(data, seed, "minecraft:normal", dimension)?;
OverworldGenerator::for_world(data, seed, "minecraft:normal", dimension, structures)?;
let unsupported = generator.unsupported_features();
if !unsupported.is_empty() {
log::warn!(
+1
View File
@@ -422,6 +422,7 @@ fn guests_see_each_other_and_the_world_entities() {
data: 0,
meta: Vec::new(),
equipment: Vec::new(),
precise: false,
};
let first = thread::spawn(move || {
let mut bot = connect(port, "Alex", false)?;
+7
View File
@@ -192,6 +192,13 @@ impl Content {
Ok(content)
}
/// Where the files are read from: the mod's folder or its `.hmod`.
pub fn source(&self) -> &Path {
match &self.source {
Source::Dir(path) | Source::Zip(path) => path,
}
}
/// No files: a mod with code only.
pub fn is_empty(&self) -> bool {
self.files.is_empty()
+76 -1
View File
@@ -25,6 +25,9 @@ pub struct Report {
pub unreadable: BTreeMap<String, String>,
pub recipes_upgraded: usize,
pub loot_tables_upgraded: usize,
/// Blocks whose jar has no loot table but drop themselves in the oracle:
/// the mod registers that table in code, and carry-over writes it.
pub loot_tables_added: usize,
/// Worldgen and structure files (`data/<id>/worldgen/`, `structure/`).
pub worldgen: usize,
/// Recipes left as they were: custom types, NeoForge conditions.
@@ -44,6 +47,11 @@ pub struct Report {
pub items_unknown: BTreeMap<String, String>,
/// Convention (`c:`) tags the recipes use, filled in from the oracle.
pub convention_tags: usize,
/// Structure, placement and pool element types rewritten as vanilla's,
/// and those left as they are.
pub worldgen_types: crate::worldgen::Upgrade,
/// The loader's convention biome tags the structures name, carried.
pub convention_biome_tags: usize,
}
/// Vanilla blocks by their sorted property names.
@@ -330,6 +338,7 @@ pub fn carry(
out: &Path,
facts: Option<&Facts>,
allowed: &dyn Fn(&str) -> bool,
conventions: &BTreeMap<String, Value>,
) -> Result<Report, String> {
let id = &info.mod_id;
if id.is_empty()
@@ -351,6 +360,8 @@ pub fn carry(
let mut blockstates: BTreeMap<String, Value> = BTreeMap::new();
let mut item_models = BTreeSet::new();
let mut convention = BTreeSet::new();
let mut block_loot = BTreeSet::new();
let mut biome_conventions = BTreeSet::new();
for i in 0..zip.len() {
let mut entry = zip.by_index(i).map_err(|e| e.to_string())?;
let name = entry.name().to_owned();
@@ -400,7 +411,31 @@ pub fn carry(
report.recipes_kept.push(file.to_owned());
}
}
} else if let Some(kind) = rel
.strip_prefix("worldgen/")
.and_then(|r| r.split_once('/'))
.map(|(kind, _)| kind)
.filter(|_| name.starts_with("data/") && name.ends_with(".json"))
{
if let Ok(mut v) = serde_json::from_slice::<Value>(&bytes) {
if kind == "structure" {
crate::worldgen::convention_tags(&v["biomes"], &mut biome_conventions);
}
if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types) {
bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes);
}
}
} else if rel.starts_with("tags/worldgen/biome/") && name.starts_with("data/") {
if let Ok(v) = serde_json::from_slice::<Value>(&bytes) {
crate::worldgen::convention_tags(&v, &mut biome_conventions);
}
} else if rel.starts_with("loot_table/") && name.starts_with("data/") {
if let Some(b) = rel
.strip_prefix("loot_table/blocks/")
.and_then(|b| b.strip_suffix(".json"))
{
block_loot.insert(b.to_owned());
}
if let Ok(mut table) = serde_json::from_slice::<Value>(&bytes) {
let before = table.clone();
upgrade_loot(&mut table);
@@ -435,6 +470,18 @@ pub fn carry(
report.item_definitions_added += 1;
}
}
// The loader's convention biome tags the structures and their tags name
// (NeoForge's `c:is_plains`...): the game has no loader, so they come along.
for tag in crate::worldgen::with_included(&biome_conventions, conventions) {
let file = inside.join(format!("data/c/tags/worldgen/biome/{tag}.json"));
if !file.exists() {
write(
&file,
&serde_json::to_vec_pretty(&conventions[&tag]).unwrap_or_default(),
)?;
report.convention_biome_tags += 1;
}
}
let decl = inside.join(format!("data/{id}/horizon"));
let write_decl = |kind: &str, name: &str, v: &Value| {
let text = serde_json::to_vec_pretty(v).unwrap_or_default();
@@ -470,6 +517,34 @@ pub fn carry(
.push(format!("its item is {}", block.item));
} else {
declared.insert(full.clone());
let drop = (!block_loot.contains(b))
.then(|| block.drops_itself(full))
.flatten();
if let Some(drop) = drop {
let mut entry = json!({"type": "minecraft:item", "name": full});
if let crate::oracle::SelfDrop::When(property, value) = drop {
entry["condition"] = json!({
"type": "minecraft:match_block",
"blocks": full,
"state": {property: value}
});
}
let table = json!({
"type": "minecraft:block",
"pools": [{
"condition": {"type": "minecraft:survives_explosion"},
"entries": [entry],
"rolls": 1
}],
"random_sequence": format!("{id}:blocks/{b}")
});
let file = inside.join(format!("data/{id}/loot_table/blocks/{b}.json"));
write(
&file,
&serde_json::to_vec_pretty(&table).unwrap_or_default(),
)?;
report.loot_tables_added += 1;
}
}
write_decl("block", b, &v)?;
report.blocks.insert(b.to_owned(), choice.template);
@@ -781,7 +856,7 @@ mod tests {
);
assert_eq!(info.census.classes, 1);
let out = dir.join("work/mods/gem");
let report = carry(&jar, &info, &out, None, &|_| true).unwrap();
let report = carry(&jar, &info, &out, None, &|_| true, &BTreeMap::new()).unwrap();
assert_eq!(report.copied, 13);
let unreadable: Vec<&str> = report.unreadable.keys().map(String::as_str).collect();
assert_eq!(
+22
View File
@@ -139,6 +139,28 @@ pub fn fetch(
}
fetched.files.push(o);
}
// The loader's own data (its convention tags), from its own repository.
if let Some((dest, url)) = crate::worldgen::loader_jar(work, &pack.loader, &pack.loader_version)
.filter(|(dest, _)| !dest.exists())
{
let sha1 = ureq::get(&format!("{url}.sha1"))
.set("User-Agent", modrinth::AGENT)
.call()
.ok()
.and_then(|r| r.into_string().ok())
.map(|s| s.trim().to_owned());
let made = dest.parent().map(fs::create_dir_all);
match made
.unwrap_or(Ok(()))
.map_err(|e| e.to_string())
.and_then(|()| download(&url, sha1.as_deref(), &dest))
{
Ok(()) => log(format!("loader: {}", dest.display())),
Err(e) => log(format!(
"loader: {e} (structures keep only vanilla biome tags)"
)),
}
}
write_json(&work.join("fetch.json"), &fetched)?;
log(format!(
"jars: {} downloaded, {} copied, {} already here, {} need a CurseForge key, {} not found, {} failed",
+1
View File
@@ -15,3 +15,4 @@ pub mod modrinth;
pub mod oracle;
pub mod pipeline;
pub mod report;
pub mod worldgen;
+119
View File
@@ -60,6 +60,10 @@ pub struct State {
pub sound: Value,
pub outline: Value,
pub collision: Value,
/// What breaking the state drops, per tool (`hand`, `diamond_pickaxe`,
/// `shears`, `silk_touch_pickaxe`...): item id → count.
#[serde(default)]
pub drops: BTreeMap<String, BTreeMap<String, u32>>,
}
#[derive(Debug, Deserialize)]
@@ -76,6 +80,23 @@ pub struct Item {
const VANILLA_PACKAGE: &str = "net.minecraft.";
/// A state key with its properties sorted, so keys compare across blocks.
/// Which states of a block drop the block itself.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelfDrop {
/// Every state: `dropSelf`.
Always,
/// Only the states where this property has this value.
When(String, String),
}
/// A state key's `property=value` pairs.
fn pairs(key: &str) -> BTreeSet<(String, String)> {
key.split(',')
.filter_map(|p| p.split_once('='))
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
}
pub fn state_key(key: &str) -> String {
let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect();
parts.sort_unstable();
@@ -96,6 +117,45 @@ impl Block {
.first()
.is_some_and(|c| !c.starts_with(VANILLA_PACKAGE))
}
/// Every state drops one `item` (the block's own) with every tool, except
/// where the state needs a tool and gets none: vanilla's `dropSelf`. Or
/// only the states where one property has one value do, and the others
/// drop nothing with any tool: a door's lower half (`createDoorTable`).
/// Mods often register these tables in code, so their jar has no file.
pub fn drops_itself(&self, item: &str) -> Option<SelfDrop> {
let one = BTreeMap::from([(item.to_owned(), 1)]);
let (mut dropping, mut nothing) = (Vec::new(), Vec::new());
for (key, state) in &self.states {
if state.drops.is_empty() {
continue;
}
let mut drops = false;
let mut short = false;
for got in state.drops.values() {
if *got == one {
drops = true;
} else if got.is_empty() {
short = true;
} else {
return None;
}
}
match (drops, short) {
(true, true) if !state.requires_tool => return None,
(true, _) => dropping.push(pairs(key)),
(false, _) => nothing.push(pairs(key)),
}
}
let (first, rest) = dropping.split_first()?;
if nothing.is_empty() {
return Some(SelfDrop::Always);
}
first
.iter()
.filter(|pair| rest.iter().all(|s| s.contains(*pair)))
.find(|pair| nothing.iter().all(|s| !s.contains(*pair)))
.map(|(p, v)| SelfDrop::When(p.clone(), v.clone()))
}
fn default_state(&self) -> Option<&State> {
self.states
.get(&self.default)
@@ -373,6 +433,65 @@ mod tests {
assert_eq!(tile.differences[0], "sounds: no vanilla block has them");
}
#[test]
fn drops_itself_is_vanillas_drop_self() {
let cube = json!([[0, 0, 0, 1, 1, 1]]);
let with = |requires_tool: bool, drops: Value| {
let mut s = state(1.5, "stone", cube.clone());
s["requires_tool"] = json!(requires_tool);
s["drops"] = drops;
let b: Block = serde_json::from_value(block(&[], json!({}), json!({"": s}))).unwrap();
b.drops_itself("m:tile")
};
let me = json!({"m:tile": 1});
let always = Some(SelfDrop::Always);
assert_eq!(with(false, json!({"hand": me, "shears": me})), always);
// A state that needs a tool drops nothing by hand, as dropSelf does.
assert_eq!(
with(true, json!({"hand": {}, "diamond_pickaxe": me})),
always
);
assert_eq!(
with(false, json!({"hand": {}, "diamond_pickaxe": me})),
None
);
// Two of itself (a double slab), or something else, is not dropSelf.
assert_eq!(with(false, json!({"hand": {"m:tile": 2}})), None);
assert_eq!(with(false, json!({"hand": {"m:other": 1}})), None);
assert_eq!(with(false, json!({})), None);
}
#[test]
fn a_door_drops_from_its_lower_half() {
let cube = json!([[0, 0, 0, 1, 1, 1]]);
let me = json!({"hand": {"m:door": 1}, "shears": {"m:door": 1}});
let none = json!({"hand": {}, "shears": {}});
let door = |upper: &Value| {
let mut states = serde_json::Map::new();
for (key, drops) in [
("half=lower,open=false", &me),
("half=lower,open=true", &me),
("half=upper,open=false", upper),
("half=upper,open=true", upper),
] {
let mut s = state(3.0, "wood", cube.clone());
s["drops"] = drops.clone();
states.insert(key.to_owned(), s);
}
let props = json!({"half": ["upper", "lower"], "open": ["true", "false"]});
let b: Block =
serde_json::from_value(block(&[], props, Value::Object(states))).unwrap();
b.drops_itself("m:door")
};
assert_eq!(
door(&none),
Some(SelfDrop::When("half".to_owned(), "lower".to_owned()))
);
assert_eq!(door(&me), Some(SelfDrop::Always));
// Something else from the upper half: not a door table.
assert_eq!(door(&json!({"hand": {"m:other": 1}})), None);
}
#[test]
fn state_keys_sort() {
assert_eq!(
+4 -1
View File
@@ -119,6 +119,7 @@ pub fn carry(
.ok()
});
let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b));
let conventions = crate::worldgen::loader_biome_tags(work);
let mut done = 0;
for r in &records {
let chosen = if only.is_empty() {
@@ -154,16 +155,18 @@ pub fn carry(
&work.join("mods").join(&r.key),
facts.as_ref().filter(seen),
&allowed,
&conventions,
) {
Ok(report) => {
done += 1;
log(format!(
"{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside",
"{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded ({} added), {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside",
r.key,
report.copied,
report.recipes_upgraded,
report.recipes_kept.len(),
report.loot_tables_upgraded,
report.loot_tables_added,
report.item_definitions_added,
report.blocks.len(),
report.blocks_unknown.len(),
+262
View File
@@ -0,0 +1,262 @@
//! Structure mods' worldgen, carried over: the structure, placement and pool
//! element types of the common structure libraries rewritten as vanilla's,
//! and the loader's convention biome tags (`c:`) the structures name.
//!
//! The libraries' types add rules on top of vanilla's (a structure that may
//! not start in liquid, a minimum distance from the world origin, a count
//! cap per piece). Mapped to vanilla, those rules are lost: the structure
//! keeps its pieces and its spacing but can start where the mod would not.
//! Each rewrite is counted in the carry report, and every type left as it is
//! is listed, so the coverage report can say what generates and how closely.
use serde_json::Value;
use std::{
collections::{BTreeMap, BTreeSet},
io::Read,
path::{Path, PathBuf},
};
/// Structure types that are vanilla's jigsaw with extra start rules.
const JIGSAWS: [&str; 3] = [
"lithostitched:jigsaw",
"moogs_structures:moogs_structures_generic_jigsaw_structure",
"repurposed_structures:generic_jigsaw_structure",
];
/// Placements that are vanilla's random spread with extra rules.
const SPREADS: [&str; 2] = [
"moogs_structures:advanced_random_spread",
"repurposed_structures:advanced_random_spread",
];
/// Pool elements that are vanilla's with extra fields: (mod type, vanilla type).
const ELEMENTS: [(&str, &str); 3] = [
(
"moogs_structures:versioned_single_pool_element",
"minecraft:single_pool_element",
),
(
"repurposed_structures:legacy_ocean_bottom_single_pool_element",
"minecraft:legacy_single_pool_element",
),
("empty_pool_element", "minecraft:empty_pool_element"),
];
/// What carry-over did to a mod's worldgen files.
#[derive(Debug, Default, serde::Serialize)]
pub struct Upgrade {
/// Files rewritten to vanilla types.
pub mapped: usize,
/// Types left as they are (Horizon skips what it cannot run), by type.
pub unsupported: BTreeMap<String, usize>,
}
/// Rewrites one `data/<ns>/worldgen/<kind>/...` file in place; whether it
/// changed.
pub fn upgrade(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool {
let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1;
let changed = match kind {
"structure" => match v["type"].as_str() {
Some(t) if JIGSAWS.contains(&t) => {
v["type"] = "minecraft:jigsaw".into();
true
}
Some(t) if !t.starts_with("minecraft:") => {
unsupported(t);
false
}
_ => false,
},
"structure_set" => match v["placement"]["type"].as_str() {
Some(t) if SPREADS.contains(&t) => {
v["placement"]["type"] = "minecraft:random_spread".into();
true
}
Some(t) if !t.starts_with("minecraft:") => {
unsupported(t);
false
}
_ => false,
},
"template_pool" => {
let mut changed = false;
let mut elements: Vec<&mut Value> = Vec::new();
for e in v["elements"].as_array_mut().into_iter().flatten() {
let element = &mut e["element"];
// A list element holds elements of its own.
if element.get("elements").is_some_and(Value::is_array) {
elements.extend(element["elements"].as_array_mut().into_iter().flatten());
} else {
elements.push(element);
}
}
for element in elements {
let Some(t) = element["element_type"].as_str() else {
continue;
};
if let Some((_, to)) = ELEMENTS.iter().find(|(from, _)| *from == t) {
element["element_type"] = (*to).into();
changed = true;
} else if !t.starts_with("minecraft:") {
unsupported(t);
}
}
changed
}
_ => false,
};
report.mapped += usize::from(changed);
changed
}
/// The `#c:` biome tags `v` names.
pub fn convention_tags(v: &Value, out: &mut BTreeSet<String>) {
match v {
Value::String(s) => {
if let Some(tag) = s.strip_prefix("#c:") {
out.insert(tag.to_owned());
}
}
Value::Array(a) => a.iter().for_each(|x| convention_tags(x, out)),
Value::Object(o) => o.values().for_each(|x| convention_tags(x, out)),
_ => {}
}
}
/// The loader's own convention biome tags (`data/c/tags/worldgen/biome/`),
/// by path, read from the loader jar `fetch` keeps in `<work>/loader/`.
pub fn loader_biome_tags(work: &Path) -> BTreeMap<String, Value> {
let mut out = BTreeMap::new();
let Ok(dir) = std::fs::read_dir(work.join("loader")) else {
return out;
};
for jar in dir.flatten().map(|e| e.path()) {
let Some(mut zip) = std::fs::File::open(&jar)
.ok()
.and_then(|f| zip::ZipArchive::new(f).ok())
else {
continue;
};
for i in 0..zip.len() {
let Ok(mut entry) = zip.by_index(i) else {
continue;
};
let Some(path) = entry
.name()
.strip_prefix("data/c/tags/worldgen/biome/")
.and_then(|p| p.strip_suffix(".json"))
.map(str::to_owned)
else {
continue;
};
let mut text = String::new();
if entry.read_to_string(&mut text).is_ok() {
if let Ok(v) = serde_json::from_str(&text) {
out.insert(path, v);
}
}
}
}
out
}
/// `tags` and the convention tags they include, in `conventions`.
pub fn with_included(
tags: &BTreeSet<String>,
conventions: &BTreeMap<String, Value>,
) -> BTreeSet<String> {
let mut all = BTreeSet::new();
let mut todo: Vec<String> = tags.iter().cloned().collect();
while let Some(tag) = todo.pop() {
if !conventions.contains_key(&tag) || !all.insert(tag.clone()) {
continue;
}
let mut more = BTreeSet::new();
convention_tags(&conventions[&tag], &mut more);
todo.extend(more);
}
all
}
/// Where `fetch` keeps the pack's loader jar, and where it comes from:
/// NeoForge's own Maven repository. Only NeoForge packs need it today.
pub fn loader_jar(work: &Path, loader: &str, version: &str) -> Option<(PathBuf, String)> {
(loader == "neoforge" && !version.is_empty()).then(|| {
(
work.join("loader").join(format!("neoforge-{version}-universal.jar")),
format!(
"https://maven.neoforged.net/releases/net/neoforged/neoforge/{version}/neoforge-{version}-universal.jar"
),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn maps_library_types_to_vanillas() {
let mut report = Upgrade::default();
let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"});
assert!(upgrade("structure", &mut s, &mut report));
assert_eq!(s["type"], "minecraft:jigsaw");
let mut nether =
json!({"type": "moogs_structures:moogs_structures_generic_nether_jigsaw_structure"});
assert!(!upgrade("structure", &mut nether, &mut report));
let mut set = json!({"placement": {"type": "moogs_structures:advanced_random_spread",
"spacing": 10, "separation": 5, "min_distance_from_world_origin": 250}});
assert!(upgrade("structure_set", &mut set, &mut report));
assert_eq!(set["placement"]["type"], "minecraft:random_spread");
assert_eq!(set["placement"]["spacing"], 10);
let mut pool = json!({"elements": [
{"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x"}},
{"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [
{"element_type": "empty_pool_element"}]}},
{"weight": 1, "element": {"element_type": "yungsapi:yung_single_element"}}
]});
assert!(upgrade("template_pool", &mut pool, &mut report));
assert_eq!(
pool["elements"][0]["element"]["element_type"],
"minecraft:single_pool_element"
);
assert_eq!(
pool["elements"][1]["element"]["elements"][0]["element_type"],
"minecraft:empty_pool_element"
);
assert_eq!(report.mapped, 3);
assert_eq!(
report.unsupported,
BTreeMap::from([
(
"moogs_structures:moogs_structures_generic_nether_jigsaw_structure".into(),
1
),
("yungsapi:yung_single_element".into(), 1),
])
);
}
#[test]
fn convention_tags_include_theirs() {
let conventions = BTreeMap::from([
(
"is_hot".to_owned(),
json!({"values": ["#c:is_desert", "#minecraft:is_badlands"]}),
),
(
"is_desert".to_owned(),
json!({"values": ["minecraft:desert"]}),
),
("is_cold".to_owned(), json!({"values": []})),
]);
let mut used = BTreeSet::new();
convention_tags(
&json!({"biomes": ["#c:is_hot", "#c:unknown", "#minecraft:x"]}),
&mut used,
);
assert_eq!(used, BTreeSet::from(["is_hot".into(), "unknown".into()]));
assert_eq!(
with_included(&used, &conventions),
BTreeSet::from(["is_hot".into(), "is_desert".into()])
);
}
}
@@ -567,6 +567,10 @@ packet!(PlayerPosition {
pitch: f32 => Float,
relatives: i32 => Int,
});
packet!(
/// ClientboundProjectilePowerPacket: an AbstractHurtingProjectile's accelerationPower.
ProjectilePower { id: i32 => VarInt, power: f64 => Double }
);
packet!(PlayerRotation { yaw: f32 => Float, relative_yaw: bool => Bool, pitch: f32 => Float, relative_pitch: bool => Bool });
packet!(RemoveEntities { ids: Vec<i32> => List<VarInt> });
packet!(Respawn { spawn: SpawnInfo => SpawnInfo, keep: i8 => Byte });
@@ -590,6 +594,10 @@ packet!(SetCursorItem { item: Option<Stack> => Slot });
packet!(SetDefaultSpawnPosition { pos: GlobalPos => GlobalPos, yaw: f32 => Float, pitch: f32 => Float });
packet!(SetEntityData { id: i32 => VarInt, values: Vec<(u8, i32, Value)> => EntityData });
packet!(SetEntityMotion { id: i32 => VarInt, velocity: [f64; 3] => LpVec3 });
packet!(
/// ClientboundSetPassengersPacket: a vehicle and every entity riding it.
SetPassengers { vehicle: i32 => VarInt, passengers: Vec<i32> => List<VarInt> }
);
/// ClientboundSetEquipmentPacket: (EquipmentSlot ordinal, stack) pairs.
pub struct Equipment;
impl Codec<Vec<(u8, Option<Stack>)>> for Equipment {
@@ -1426,6 +1434,8 @@ packet_enum!(Clientbound, crate::ids::play::clientbound {
SetHealth(SetHealth) = SET_HEALTH,
SetHeldSlot(SetHeldSlot) = SET_HELD_SLOT,
SetObjective(SetObjective) = SET_OBJECTIVE,
SetPassengers(SetPassengers) = SET_PASSENGERS,
ProjectilePower(ProjectilePower) = PROJECTILE_POWER,
SetPlayerInventory(SetPlayerInventory) = SET_PLAYER_INVENTORY,
SetPlayerTeam(SetPlayerTeam) = SET_PLAYER_TEAM,
SetScore(SetScore) = SET_SCORE,
@@ -2052,6 +2062,11 @@ mod tests {
id: 2,
velocity: [0.0; 3],
}),
Clientbound::SetPassengers(SetPassengers {
vehicle: 2,
passengers: vec![3, 300],
}),
Clientbound::ProjectilePower(ProjectilePower { id: 4, power: 0.1 }),
Clientbound::SetEquipment(SetEquipment {
id: 2,
slots: vec![(0, None), (5, Some(Stack::new(1, 1)))],
+18 -17
View File
@@ -2,11 +2,10 @@
//! collision shapes. Replacement with an item/fluid context is separate behavior.
use crate::{
DATA_VERSION,
chunk::BlockState,
collision::CachedCollisionRegistry,
compression::invalid,
nbt::Tag,
registry::{BlockRegistry, BlockStateId},
registry::{BlockRegistry, BlockStateId, BlockTable},
};
use horizon_core::geometry::{Direction, ShapeError, ShapeOp, VoxelShape};
use std::{
@@ -112,21 +111,25 @@ pub struct BlockPropertiesRegistry<'r> {
}
impl<'r> BlockPropertiesRegistry<'r> {
pub fn from_nbt(tag: &Tag, collisions: &CachedCollisionRegistry<'r>) -> io::Result<Self> {
Self::from_table(
BlockTable::check(tag, collisions.registry(), "block property")?,
collisions,
)
}
pub fn from_table(
table: BlockTable<'_, '_>,
collisions: &CachedCollisionRegistry<'r>,
) -> io::Result<Self> {
let registry = collisions.registry();
if !std::ptr::eq(table.registry(), registry) {
return Err(invalid(
"block properties and collisions use other registries",
));
}
let tag = table.tag();
if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) {
return Err(invalid("block properties DataVersion mismatch"));
}
let Some(Tag::List(states)) = tag.get("states") else {
return Err(invalid("missing block property state identities"));
};
if states.len() != registry.len() {
return Err(invalid("block property registry size mismatch"));
}
for (id, state) in states.iter().enumerate() {
if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) {
return Err(invalid("block property state identities differ"));
}
}
let Some(Tag::Compound(blocks)) = tag.get("blocks") else {
return Err(invalid("missing block definition properties"));
};
@@ -150,10 +153,8 @@ impl<'r> BlockPropertiesRegistry<'r> {
},
);
}
let occlusion = CachedCollisionRegistry::from_nbt(
tag.get("occlusion")
.ok_or_else(|| invalid("missing block occlusion shapes"))?,
registry,
let occlusion = CachedCollisionRegistry::from_table(
table.nested("occlusion", "block occlusion shapes")?,
)?;
let mut result = Vec::with_capacity(registry.len());
let mut faces = Vec::with_capacity(registry.len());
+12 -4
View File
@@ -61,7 +61,7 @@ use crate::{
collision::CachedCollisionRegistry,
compression::invalid,
nbt::Tag,
registry::{BlockRegistry, BlockStateId},
registry::{BlockRegistry, BlockStateId, BlockTable},
};
use std::{collections::HashMap, io};
/// Cached branch of BlockState.isFaceSturdy. Dynamic support-shape selection
@@ -72,10 +72,14 @@ pub struct CachedBlockSupportRegistry<'r> {
}
impl<'r> CachedBlockSupportRegistry<'r> {
pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> {
if tag.get("Kind") != Some(&Tag::String("block_support".into())) {
Self::from_table(BlockTable::check(tag, registry, "support")?)
}
pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> {
if table.get("Kind") != Some(&Tag::String("block_support".into())) {
return Err(invalid("wrong cached support data kind"));
}
let shapes = CachedCollisionRegistry::from_nbt(tag, registry)?;
let registry = table.registry();
let shapes = CachedCollisionRegistry::from_table(table)?;
let mut cache = HashMap::new();
let mut states = Vec::with_capacity(registry.len());
for id in 0..registry.len() {
@@ -163,7 +167,11 @@ impl<E: std::fmt::Display> std::fmt::Display for SupportQueryError<E> {
impl<E: std::error::Error + 'static> std::error::Error for SupportQueryError<E> {}
impl<'r> BlockSupportRegistry<'r> {
pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> {
let cached = CachedBlockSupportRegistry::from_nbt(tag, registry)?;
Self::from_table(BlockTable::check(tag, registry, "support")?)
}
pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> {
let registry = table.registry();
let cached = CachedBlockSupportRegistry::from_table(table)?;
let mut dynamic = HashMap::new();
for id in 0..registry.len() {
let id = BlockStateId(id as u32);
+6 -13
View File
@@ -5,10 +5,10 @@
//! This module covers the cached branch only; uncached states remain explicit.
use crate::{
DATA_VERSION,
chunk::{BlockSection, BlockState},
chunk::BlockSection,
compression::invalid,
nbt::Tag,
registry::{BlockRegistry, BlockStateId},
registry::{BlockRegistry, BlockStateId, BlockTable},
};
use horizon_core::geometry::{Axis, VoxelShape};
use std::{collections::HashMap, io};
@@ -35,20 +35,13 @@ impl<'r> CachedCollisionRegistry<'r> {
self.registry
}
pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> {
Self::from_table(BlockTable::check(tag, registry, "collision")?)
}
pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> {
let (tag, registry) = (table.tag(), table.registry());
if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) {
return Err(invalid("collision DataVersion mismatch"));
}
let Some(Tag::List(states)) = tag.get("states") else {
return Err(invalid("missing collision state identities"));
};
if states.len() != registry.len() {
return Err(invalid("collision registry size mismatch"));
}
for (id, state) in states.iter().enumerate() {
if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) {
return Err(invalid("collision state identities differ"));
}
}
let Some(Tag::List(entries)) = tag.get("shapes") else {
return Err(invalid("missing collision shapes"));
};
+3 -10
View File
@@ -8,8 +8,7 @@ use crate::{
collision::CachedCollisionRegistry,
compression::invalid,
fluid::{FluidBlockBindings, FluidKind, FluidState},
nbt::{self, Limits},
registry::{BlockRegistry, BlockStateId},
registry::{BlockRegistry, BlockStateId, BlockTableFile},
worldgen::tags::TagDefinitions,
};
use horizon_core::geometry::{Aabb, Direction, ShapeOp, Vec3, VoxelShape};
@@ -787,14 +786,8 @@ fn build_with_gameplay<G: GameplayStateSource>(
};
// Collision shapes.
let collision_tag = nbt::read_named(
&fs::read(root.join("cached-collision.nbt"))?,
Limits::tables(),
)
.map_err(|e| invalid(e.to_string()))?
.0
.tag;
let cached = CachedCollisionRegistry::from_nbt(&collision_tag, registry)?;
let collision_file = BlockTableFile::open(&root.join("cached-collision.nbt"), registry)?;
let cached = CachedCollisionRegistry::from_table(collision_file.table("collision")?)?;
let mut shapes: Vec<VoxelShape> = Vec::new();
let mut shape_ids = Vec::with_capacity(table.state_count());
for id in 0..table.state_count() {
@@ -1,11 +1,10 @@
//! Registry-bound block movement coefficients, identities and reloadable tags.
use crate::{
DATA_VERSION,
chunk::BlockState,
compression::invalid,
movement_support::MovementBlockWorld,
nbt::Tag,
registry::{BlockRegistry, BlockStateId},
registry::{BlockRegistry, BlockStateId, BlockTable},
};
use horizon_core::position::BlockPos;
use std::{
@@ -55,20 +54,13 @@ impl<'r> MovementProperties<'r> {
}
pub fn from_nbt(tag: &Tag, registry: &'r BlockRegistry) -> io::Result<Self> {
Self::from_table(BlockTable::check(tag, registry, "movement")?)
}
pub fn from_table(table: BlockTable<'_, 'r>) -> io::Result<Self> {
let (tag, registry) = (table.tag(), table.registry());
if tag.get("DataVersion") != Some(&Tag::Int(DATA_VERSION)) {
return Err(invalid("movement properties DataVersion mismatch"));
}
let Some(Tag::List(states)) = tag.get("states") else {
return Err(invalid("missing movement state identities"));
};
if states.len() != registry.len() {
return Err(invalid("movement registry size mismatch"));
}
for (id, state) in states.iter().enumerate() {
if registry.state(BlockStateId(id as u32)) != Some(&BlockState::from_nbt(state)?) {
return Err(invalid("movement state identities differ"));
}
}
let Some(Tag::Compound(blocks)) = tag.get("blocks") else {
return Err(invalid("missing movement definitions"));
};
+164 -3
View File
@@ -124,13 +124,20 @@ impl std::fmt::Display for Error {
}
impl std::error::Error for Error {}
struct Reader<'a> {
/// Hands a streamed list's elements to the caller one at a time (see
/// [`read_named_streaming`]).
type Visit<'s> = &'s mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>;
struct Reader<'a, 's> {
bytes: &'a [u8],
offset: usize,
limits: Limits,
nodes: usize,
/// Lists with this key in the root compound or in a compound directly
/// under it are streamed to the visitor instead of kept.
stream: Option<(&'s str, Visit<'s>)>,
}
impl<'a> Reader<'a> {
impl<'a> Reader<'a, '_> {
fn error(&self, reason: &'static str) -> Error {
Error {
offset: self.offset,
@@ -266,7 +273,11 @@ impl<'a> Reader<'a> {
break;
}
let name = self.string()?;
let value = self.payload(id, depth + 1)?;
let value = if id == 9 && depth <= 1 && self.streams(&name) {
self.stream_list(depth + 1)?
} else {
self.payload(id, depth + 1)?
};
map.insert(name, value); // Java maps retain the last duplicate key.
}
Tag::Compound(map)
@@ -290,6 +301,82 @@ impl<'a> Reader<'a> {
_ => return Err(self.error("unknown tag type")),
})
}
fn streams(&self, name: &JavaString) -> bool {
self.stream
.as_ref()
.is_some_and(|(key, _)| name.0.iter().copied().eq(key.encode_utf16()))
}
/// A list's payload handed to the visitor element by element; the tree
/// keeps it empty. Elements the visitor leaves are read and dropped.
fn stream_list(&mut self, depth: usize) -> Result<Tag, Error> {
if self.nodes >= self.limits.max_nodes {
return Err(self.error("node limit exceeded"));
}
self.nodes += 1;
if depth >= self.limits.max_depth {
return Err(self.error("depth limit exceeded"));
}
let element_id = self.byte()?;
let len = self.count(0)?;
if len > 0 && (element_id == 0 || element_id > 12) {
return Err(self.error("invalid list element type"));
}
if len > self.bytes.len().saturating_sub(self.offset) {
return Err(self.error("truncated list"));
}
let (key, visit) = self.stream.take().expect("streams() checked the visitor");
let mut elements = ListElements {
reader: self,
element_id,
depth: depth + 1,
len,
left: len,
};
let rest = visit(&mut elements).and_then(|()| {
while elements.next_element()?.is_some() {}
Ok(())
});
self.stream = Some((key, visit));
rest.map(|()| Tag::List(Vec::new()))
}
}
/// The elements of a list [`read_named_streaming`] streams, read one at a
/// time.
pub struct ListElements<'r, 'a, 's> {
reader: &'r mut Reader<'a, 's>,
element_id: u8,
depth: usize,
len: usize,
left: usize,
}
impl ListElements<'_, '_, '_> {
/// The list's length, as its header gives it.
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// The next element, unwrapped as [`read_named`] unwraps mixed-list
/// elements; `None` past the last.
pub fn next_element(&mut self) -> Result<Option<Tag>, Error> {
if self.left == 0 {
return Ok(None);
}
self.left -= 1;
let mut value = self.reader.payload(self.element_id, self.depth)?;
if let Tag::Compound(map) = &mut value {
if map.len() == 1 && map.contains_key(&JavaString::default()) {
value = map.remove(&JavaString::default()).unwrap();
}
}
Ok(Some(value))
}
/// An error at the current position in the input.
pub fn error(&self, reason: &'static str) -> Error {
self.reader.error(reason)
}
}
/// Read a file root (type, modified-UTF name, payload), returning bytes consumed.
@@ -299,6 +386,35 @@ pub fn read_named(bytes: &[u8], limits: Limits) -> Result<(NamedTag, usize), Err
offset: 0,
limits,
nodes: 0,
stream: None,
};
let id = reader.byte()?;
let name = if id == 0 {
JavaString::default()
} else {
reader.string()?
};
let tag = reader.payload(id, 0)?;
Ok((NamedTag { name, tag }, reader.offset))
}
/// [`read_named`], except that lists keyed `key` in the root compound or in
/// a compound directly under it are not kept: `visit` reads each one's
/// elements as they come, and the tree holds it as an empty list. Per-state
/// tables check hundreds of thousands of state identities this way without
/// holding them as a tree (crate::registry::BlockTableFile).
pub fn read_named_streaming(
bytes: &[u8],
limits: Limits,
key: &str,
visit: &mut dyn FnMut(&mut ListElements<'_, '_, '_>) -> Result<(), Error>,
) -> Result<(NamedTag, usize), Error> {
let mut reader = Reader {
bytes,
offset: 0,
limits,
nodes: 0,
stream: Some((key, visit)),
};
let id = reader.byte()?;
let name = if id == 0 {
@@ -317,6 +433,7 @@ pub fn read_network(bytes: &[u8], limits: Limits) -> Result<(Tag, usize), Error>
offset: 0,
limits,
nodes: 0,
stream: None,
};
let id = reader.byte()?;
let tag = reader.payload(id, 0)?;
@@ -659,6 +776,50 @@ impl Snbt<'_> {
mod tests {
use super::*;
#[test]
fn streamed_lists_reach_the_visitor_and_stay_empty_in_the_tree() {
let list = |n: i32| Tag::List((0..n).map(Tag::Int).collect());
let root = NamedTag {
name: "".into(),
tag: Tag::compound([
("states", list(3)),
("inner", Tag::compound([("states", list(2))])),
(
"deep",
Tag::compound([("inner", Tag::compound([("states", list(1))]))]),
),
("other", list(4)),
]),
};
let bytes = write_named(&root, Limits::default()).unwrap();
let mut seen = Vec::new();
let (read, used) = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| {
let mut values = vec![l.len() as i32];
// Elements left unread are skipped.
if let Some(Tag::Int(v)) = l.next_element()? {
values.push(v);
}
seen.push(values);
Ok(())
})
.unwrap();
assert_eq!(used, bytes.len());
// Compounds are written in key order: `inner` before `states`.
assert_eq!(seen, [vec![2, 0], vec![3, 0]]);
assert_eq!(read.tag.get("states"), Some(&Tag::List(Vec::new())));
assert_eq!(
read.tag.get("inner").unwrap().get("states"),
Some(&Tag::List(Vec::new()))
);
// Only the root and its direct children stream.
assert_eq!(read.tag.get("deep"), root.tag.get("deep"));
assert_eq!(read.tag.get("other"), root.tag.get("other"));
// A visitor's error stops the read.
let failed = read_named_streaming(&bytes, Limits::default(), "states", &mut |l| {
Err(l.error("refused"))
});
assert_eq!(failed.unwrap_err().reason, "refused");
}
#[test]
fn wire_node_budget_includes_mixed_list_wrappers() {
let tag = Tag::List(vec![Tag::Int(1), Tag::Byte(2)]);
let limits = Limits {
+117
View File
@@ -132,3 +132,120 @@ impl BlockRegistry {
.ok_or_else(|| invalid("invalid block property value"))
}
}
/// A per-state table (`occlusion.nbt`, `cached-collision.nbt`, ...) whose
/// `states` list, the identity of every state it describes in id order, has
/// been checked against `registry`.
#[derive(Clone, Copy)]
pub struct BlockTable<'t, 'r> {
tag: &'t Tag,
registry: &'r BlockRegistry,
/// How many levels, from this one down, [`BlockTableFile::read`] checked
/// while reading: their `states` lists are kept empty in the tree.
read_checked: u8,
}
impl<'t, 'r> BlockTable<'t, 'r> {
/// Checks that `tag`'s `states` names the registry's states in id order.
pub fn check(tag: &'t Tag, registry: &'r BlockRegistry, what: &str) -> io::Result<Self> {
Self {
tag,
registry,
read_checked: 0,
}
.checked(what)
}
fn checked(self, what: &str) -> io::Result<Self> {
let Some(Tag::List(states)) = self.tag.get("states") else {
return Err(invalid(format!("missing {what} state identities")));
};
if self.read_checked == 0 {
if states.len() != self.registry.len() {
return Err(invalid(format!("{what} registry size mismatch")));
}
for (id, state) in states.iter().enumerate() {
if self.registry.state(BlockStateId(id as u32))
!= Some(&BlockState::from_nbt(state)?)
{
return Err(invalid(format!("{what} state identities differ")));
}
}
}
Ok(self)
}
/// The table nested under `key` (`block-properties.nbt`'s `occlusion`),
/// with its own `states`, checked the same way.
pub fn nested(&self, key: &str, what: &str) -> io::Result<Self> {
Self {
tag: self
.tag
.get(key)
.ok_or_else(|| invalid(format!("missing {what}")))?,
registry: self.registry,
read_checked: self.read_checked.saturating_sub(1),
}
.checked(what)
}
pub fn tag(&self) -> &'t Tag {
self.tag
}
pub fn get(&self, key: &str) -> Option<&'t Tag> {
self.tag.get(key)
}
pub fn registry(&self) -> &'r BlockRegistry {
self.registry
}
}
/// A per-state table read from its file with its state identities checked
/// while reading, so they never exist as a tree: for 190,000 states (a big
/// modpack) that tree took 300-600 MB per table.
pub struct BlockTableFile<'r> {
tag: Tag,
registry: &'r BlockRegistry,
}
impl<'r> BlockTableFile<'r> {
/// [`Self::read`] of the file at `path`.
pub fn open(path: &std::path::Path, registry: &'r BlockRegistry) -> io::Result<Self> {
let bytes = std::fs::read(path)?;
Self::read(&bytes, registry).map_err(|e| invalid(format!("{}: {e}", path.display())))
}
/// Reads `bytes`, checking every `states` list of the root compound and
/// of the compounds directly under it against `registry`.
pub fn read(bytes: &[u8], registry: &'r BlockRegistry) -> io::Result<Self> {
let mut visit = |states: &mut crate::nbt::ListElements<'_, '_, '_>| {
if states.len() != registry.len() {
return Err(states.error("table registry size mismatch"));
}
let mut id = 0u32;
while let Some(tag) = states.next_element()? {
let state =
BlockState::from_nbt(&tag).map_err(|_| states.error("invalid table state"))?;
if registry.state(BlockStateId(id)) != Some(&state) {
return Err(states.error("table state identities differ"));
}
id += 1;
}
Ok(())
};
let (named, _) = crate::nbt::read_named_streaming(
bytes,
crate::nbt::Limits::tables(),
"states",
&mut visit,
)
.map_err(|e| invalid(e.to_string()))?;
Ok(Self {
tag: named.tag,
registry,
})
}
/// The table, checked (`what` names it in errors).
pub fn table(&self, what: &str) -> io::Result<BlockTable<'_, 'r>> {
BlockTable {
tag: &self.tag,
registry: self.registry,
read_checked: 2,
}
.checked(what)
}
}
@@ -1764,6 +1764,18 @@ impl OverworldGenerator {
) -> Result<Self, GeneratorError> {
Self::build(data, seed, preset, dimension, true)
}
/// A dimension of a world preset, with or without structures (the
/// world's `generate_structures`: without, no starts, terrain adaptation,
/// structure pieces or structure locating).
pub fn for_world(
data: Arc<GeneratorData>,
seed: i64,
preset: &str,
dimension: &str,
structures: bool,
) -> Result<Self, GeneratorError> {
Self::build(data, seed, preset, dimension, structures)
}
fn build(
data: Arc<GeneratorData>,
seed: i64,
@@ -8,8 +8,7 @@ use crate::{
block_support::BlockSupportRegistry,
block_survival::{BlockSurvivalRegistry, SurvivalDefinitions, SurvivalRule},
collision::CachedCollisionRegistry,
nbt::{self, Limits, Tag},
registry::BlockRegistry,
registry::{BlockRegistry, BlockTableFile},
};
use serde_json::Value;
use std::{
@@ -102,24 +101,21 @@ impl BlockBehavior {
if !dir.join("block-properties.nbt").exists() {
return Ok(None);
}
let read = |name: &str| -> Result<Tag, GeneratorError> {
Ok(
nbt::read_named(&fs::read(dir.join(name))?, Limits::tables())
.map_err(|e| resource(format!("{name}: {e:?}")))?
.0
.tag,
)
let read = |name: &str| -> Result<BlockTableFile<'static>, GeneratorError> {
BlockTableFile::open(&dir.join(name), registry)
.map_err(|e| resource(format!("{name}: {e}")))
};
let json = |name: &str| -> Result<Value, GeneratorError> {
Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?)
};
let collisions =
CachedCollisionRegistry::from_nbt(&read("cached-collision.nbt")?, registry)?;
let properties =
BlockPropertiesRegistry::from_nbt(&read("block-properties.nbt")?, &collisions)?;
let support = Arc::new(BlockSupportRegistry::from_nbt(
&read("cached-support.nbt")?,
registry,
CachedCollisionRegistry::from_table(read("cached-collision.nbt")?.table("collision")?)?;
let properties = BlockPropertiesRegistry::from_table(
read("block-properties.nbt")?.table("block property")?,
&collisions,
)?;
let support = Arc::new(BlockSupportRegistry::from_table(
read("cached-support.nbt")?.table("support")?,
)?);
let collision = Arc::new(BlockCollisionRegistry::new(Arc::new(collisions))?);
let rules = json("survival-rules.json")?
@@ -267,3 +267,75 @@ fn registry_scan_binding_requires_complete_canonical_metadata() {
);
}
}
/// A table read from its bytes checks its state identities while reading and
/// keeps none of them, yet loads the same shapes as the tree; a nested table
/// (`block-properties.nbt`'s `occlusion`) is checked the same way.
#[test]
fn streamed_tables_check_states_while_reading() {
use horizon_world::{
nbt::{self, Limits, NamedTag},
registry::{BlockTable, BlockTableFile},
};
let (registry_tag, collision_tag) = fixture();
let registry = BlockRegistry::from_nbt(&registry_tag).unwrap();
let bytes = |tag: &Tag| {
let root = NamedTag {
name: "".into(),
tag: tag.clone(),
};
nbt::write_named(&root, Limits::tables()).unwrap()
};
let file = BlockTableFile::read(&bytes(&collision_tag), &registry).unwrap();
let table = file.table("collision").unwrap();
assert_eq!(table.get("states"), Some(&Tag::List(Vec::new())));
let streamed = CachedCollisionRegistry::from_table(table).unwrap();
let tree = CachedCollisionRegistry::from_nbt(&collision_tag, &registry).unwrap();
for id in 0..3 {
let id = BlockStateId(id);
assert_eq!(
streamed.cached_shape(id).unwrap().map(|s| s.boxes()),
tree.cached_shape(id).unwrap().map(|s| s.boxes())
);
}
let mut outer = Tag::compound([("occlusion", collision_tag.clone())]);
let Tag::Compound(map) = &mut outer else {
unreachable!()
};
map.insert(
"states".into(),
collision_tag.get("states").unwrap().clone(),
);
let file = BlockTableFile::read(&bytes(&outer), &registry).unwrap();
let nested = file
.table("outer")
.unwrap()
.nested("occlusion", "occlusion")
.unwrap();
assert_eq!(nested.get("states"), Some(&Tag::List(Vec::new())));
CachedCollisionRegistry::from_table(nested).unwrap();
// Checked only as a tree, an emptied list is refused.
assert!(BlockTable::check(nested.tag(), &registry, "occlusion").is_err());
// Other states, a different count or no states at all are refused.
let with_states = |edit: &dyn Fn(&mut Vec<Tag>)| {
let mut tag = collision_tag.clone();
let Some(Tag::List(states)) = tag.get_mut("states") else {
unreachable!()
};
edit(states);
tag
};
let swapped = with_states(&|s| s.swap(1, 2));
assert!(BlockTableFile::read(&bytes(&swapped), &registry).is_err());
let short = with_states(&|s| drop(s.pop()));
assert!(BlockTableFile::read(&bytes(&short), &registry).is_err());
let mut swapped = swapped;
let Tag::Compound(map) = &mut swapped else {
unreachable!()
};
map.remove(&"states".into());
let file = BlockTableFile::read(&bytes(&swapped), &registry).unwrap();
assert!(file.table("collision").is_err());
}
+5 -2
View File
@@ -22,8 +22,11 @@ plan are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/doc
(Tailscale, ZeroTier), or share it only with friends you trust.
- **No secure chat.** Chat isn't signed, and `enforce-secure-profile` has no effect. Vanilla clients show
chat from the server as not secure.
- **Nether and End:** the world follows a player through portals when they are alone on the server
(v2026.10.17.09 and newer). With several players online, portals don't move remote players yet.
- **Nether and End:** each player travels on their own through portals, and operators with `execute in
<dimension> run tp`. The players in the level they leave stop seeing them, and the ones in the level they
arrive in start to, as on vanilla (checked against vanilla with two players, by command and through an End
portal: `tools/test_server_dimension_players_parity.py`). The console can't send another player to a
dimension yet (`execute in ... run tp NAME`).
- **Movement checks and chunk pacing** (release v2026.10.17.16 and newer). The server pulls back players who
move too fast ("moved too quickly"), as vanilla does, and sends chunks as fast as each client acknowledges
them.
+94 -14
View File
@@ -236,7 +236,15 @@ How the game loads them, without touching vanilla's path:
so the same mod set gives the same ids. Saves store block names, so ids never reach the disk.
- Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as
vanilla does. With no mods there is only `minecraft`, so nothing changes.
- The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes.
- The overlay is built on the first start with a set of mods and kept for later starts. A stamp
(`horizon-mod-content.stamp`) hashes the game executable, the base tables and every mod file's
size and mtime. Adding, removing or editing a mod, or a new build of Horizon, rebuilds it. For
ATM10's 40 data-tier mods the build takes about a minute of the first start on a busy machine.
- **Worldgen.** Mods' structures, structure sets, template pools, processor lists, configured and
placed features join the `worldgen/*.json` bundles. Their structure templates go under
`worldgen/structure/<ns>/`. Their biome tags are merged into `worldgen/biome_tags.json`, minus
the biomes and tags Horizon does not have. Mod biomes, noise and density functions are not
carried yet.
- **Limits.** Chunk sections size their global palette to the registry: 16 bits for vanilla's
35,723 states (the same chunks, saves and packets as before), one more bit each time content
mods double it, up to 2^20 states (1,048,576). Past that the content root is refused with the
@@ -398,6 +406,40 @@ Mechanical, the same for every mod, and no agent:
carries those over.
5. Write `<id>.hmod`. For a data-tier mod, it is now ported.
Two cases the oracle fills in:
- **Loot registered in code.** Chipped ships 7 loot tables for about 7,000 blocks; the rest
drop themselves through Java code. When a declared block has no table in the jar and the
oracle shows vanilla's drop-self, carry writes that table in 26.3's shape. When only the
states with one property value drop it (a door's lower half), the entry gets vanilla's
`match_block` condition. Chipped: 6,843 tables added.
- **Worldgen carry-over.** Structure libraries give vanilla's types other names. Carry maps
these to vanilla's:
- `lithostitched:jigsaw`, MoogsStructureLib's and Repurposed Structures' generic jigsaws
become `minecraft:jigsaw`;
- their `advanced_random_spread` becomes `minecraft:random_spread`;
- their versioned, legacy and empty pool elements become vanilla's.
The rules these libraries add on top of vanilla's (no start in liquid, a minimum distance,
caps per piece) are lost, so a mapped structure can start where the mod would not. NeoForge's
convention biome tags (`#c:is_forest`, ...) come from the loader jar
(`<work>/loader/neoforge-<version>-universal.jar`, fetched dev-only and SHA-1 checked) and
are written into the carry. Each mod's `carry-report.json` lists what was mapped and the
types left as they are. The game skips the structures it cannot decode.
On ATM10's 40 data-tier mods:
- 1,061 files were mapped (MVS 413, MNS 210, MSS 157, MES 110, Repurposed Structures 104,
ChoiceTheorem's Overhauled Village 66), and 103 convention biome tags written.
- When Dungeons Arise, Structory and Towns and Towers use vanilla's types already.
- Not generated yet, because their types are Java code: YUNG's API (491 elements and
jigsaws: every "YUNG's Better" mod), Formations (179), the nether jigsaw variants of Moog's
and Repurposed (69), MO Structures (38), and Repurposed's mineshafts, mansions, monuments
and cities.
In a world with the 40 mods, seed 1234, `/locate structure mvs:azelea_house` finds one 27
blocks from spawn and it stands there as the template builds it; When Dungeons Arise's
bandit towers and Structory's chapel locate too.
### 5. Code: one agent per mod
For tiers logic, machines and deep, in dependency order, as many agents at once as the AI
@@ -473,12 +515,15 @@ user's jars are, and its facts and results stay in the work folder. The first re
| Glassential-renewed 3.4.7 | 105/114 | 5177/5177 | 5177/5177 | 4727/5177 | 6447/6447 | 210/210 | 105/105 | 143/143 | 106/115 |
| Factory Blocks 1.4.0 | 42/49 | 42/42 | 0/42 | 42/42 | 294/294 | 84/84 | 42/42 | 1/1 | 42/49 |
| Cloud Glass 1.1.0 | 16/16 | 16/16 | 16/16 | 0/16 | 112/112 | 32/32 | – | 31/31 | 16/16 |
| Chipped 4.0.2 | 6857/6981 | 46212/46443 | 46443/46443 | 46396/46443 | 149093/149093 | 13548/13714 | 11984/12378 | 6/13 | 6849/6973 |
What fails is what API 0.2 cannot express yet: shapes of their own (Japanese doors, hedges,
paths a sixteenth high, glass without collision), sound events of the mod's own
(`factory_blocks:metal_sound`), and blocks whose vanilla class has no vanilla block with the
same states (garage doors, double gates, engraved and facing paths, fans). Every drop, recipe
and tag of the declared blocks matches.
and tag of the declared blocks matches. Chipped's 124 missing blocks are its own classes (workbenches and
`DirectionBlock`), and its workbench recipes need those blocks. Its drops match since carry writes
the self-drop tables it registers in code (step 4).
### 7. Coverage report
@@ -520,9 +565,9 @@ files; `inventory`; `carry` and the harness on the eight mods we downloaded; `re
By tier, the 367 not yet: data 57 (carry-over and the harness, no agent: their jars were not
downloaded), logic 66 (API 0.3), machines 97 (0.4), deep 92 (0.5), client 35, unclassified 20.
Ported: Macaw's Trapdoors. Partial: Macaw's Doors, Fences and Walls, Paths and Pavings,
Glassential, Factory Blocks, Cloud Glass (see the harness table) and MVS, whose 784 worldgen
files are carried but not yet read by the game. Of the 8, 7 are MIT and one LGPL; nothing is
published.
Glassential, Factory Blocks, Cloud Glass, Chipped (see the harness table) and MVS, whose
structures now generate (see step 4). Of the 9, 7 are MIT, one LGPL and Chipped is under the
Terrarium License; nothing is published.
## Measuring "10x"
@@ -582,8 +627,9 @@ vanilla's 35,723; the per-state tables are the next thing to measure), while the
inside its 2 GB heap. Caveats, so none of this is a "10x" claim yet: other sessions and CI
runners kept the machine's load average between 30 and 56 on 14 threads, so the confidence
intervals are as wide as the means (`perf_bench.py --table`); Horizon's frames are paced near
the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay on
a first start (about 5 s, cached afterwards); and the pack runs 1.21.1 against Horizon's 26.3.
the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay
(about 5 s for six mods; it was rebuilt on every start then, and is kept for later starts now);
and the pack runs 1.21.1 against Horizon's 26.3.
A quiet rerun with more games is the next measurement.
**Where the RAM goes (2026-10-17).** RSS overstates it on macOS. `ps` counts pages the allocator
@@ -620,8 +666,44 @@ indexes ids by a hash checked against the states: -22 MB live for vanilla, -124
ATM10's, -30 MB and -140 MB of footprint. The startup peaks are what is left. Each step parses
its per-state tables whole: as JSON trees, or as NBT trees of 300-600 MB each for ATM10
(`block-properties.nbt`, the collision, support, occlusion and movement tables). The peak
reaches 3.9 GB during worldgen loading, and the footprint stays near the peak. Typed, streaming
readers for those tables are the next cut.
reaches 3.9 GB during worldgen loading, and the footprint stays near the peak.
The NBT tables now stream: the readers check each state's identity as it is read, and no
`states` tree is built (#753). That tree was about 240 MB for each table. With the carried
ATM10 mods (153,511 states), the peak of each step falls by about 210 MB:
| step | before MB | after MB |
|---|---:|---:|
| light | 911 | 699 |
| collision | 945 | 733 |
| motion | 1,427 | 1,215 |
| worldgen, uncached | 3,000 | 2,788 |
The fluid tables no longer decode the 211 MB `gameplay-facts.json` a second time (#754). The
content root is kept between starts (#756). What is left at startup:
- `facts.json` goes through a JSON tree, then an NBT tree, in both the registry and the assets
step;
- on a first start, the content-root build and `Behaviours`' decode of `gameplay-facts.json`
(cached afterwards in the behaviour snapshot).
**Against NeoForge, same 40 mods (2026-10-17).** The 40 data-tier mods carried from ATM10 (the
42 without Regions Unexplored and MO Structures, whose libraries are only on CurseForge),
against their jars on NeoForge 21.1.259 with `-Xmx2G`. `perf_bench.py --port-work`, RD 8, one
game each, load average 80-125 on 14 threads; `--warm` measures a later start:
| game | title / world s | RSS steady / peak MB | footprint steady / peak MB |
|---|---|---|---|
| Horizon before #756 (every start) | 81.1 / 102.8 | 4,008 / 4,164 | 2,368 / 2,554 |
| Horizon, first start | 105.2 / 128.8 | 3,795 / 3,993 | 2,306 / 2,560 |
| **Horizon, later starts** | **21.6 / 33.7** | **2,629 / 2,935** | **2,445 / 2,731** |
| NeoForge + 40 mods | 53.2 / 119.9 | 3,099 / 3,203 | 3,304 / 3,381 |
On later starts, Horizon uses 15 % less RSS and 26 % less footprint than NeoForge with the
same mods. It reaches the title 2.5x sooner and the world 3.6x sooner, and its flights ran at
93-233 FPS against NeoForge's 27-62. The first start is slower than NeoForge's to the title,
because it builds the content root once. One game per row on a loaded machine is not a
benchmark; a quiet rerun with repeats is still to do.
## The working version for 2026-10-23
@@ -648,11 +730,9 @@ In scope (each is a PR or a small stack):
6. **Measured:** load time, RAM and FPS of that pack on NeoForge 1.21.1 against Horizon, with
the full ATM10 on NeoForge as the reference row.
Out of scope for that date, and next in order: worldgen carry-over (the 30 or so structure and biome mods;
the first one looked at, Moog's Voyager Structures, declares all 130 structures and 116 structure
sets with MoogsStructureLib's own `generic_jigsaw_structure` and `advanced_random_spread` types and
NeoForge's `c:` biome tags, so its carry needs a mapping to vanilla's jigsaw and random spread
types plus the oracle's biome tags, not only bundle merging),
Out of scope for that date, and next in order: the rest of worldgen carry-over (mods'
structures, sets, pools and features generate now, see step 4; mod biomes, noise and density
functions, and the structure types written in Java such as YUNG's and Formations', do not yet),
behaviour callbacks and server events (0.3), block entities, menus, capabilities and net
(0.4), the first agent-ported logic-tier mod as the pilot of step 5, then machines. Porting all
of ATM10 is a campaign. Each step publishes its coverage number; we make no claim beyond what
@@ -0,0 +1,164 @@
package dev.horizonlab.parity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
import net.minecraft.network.protocol.game.ClientboundMoveEntityPacket;
import net.minecraft.network.protocol.game.ClientboundProjectilePowerPacket;
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSetPassengersPacket;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
import net.minecraft.network.protocol.game.VecDeltaCodec;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.phys.Vec3;
/**
* What a vanilla client knows about the entities a server tracks to it, as
* ClientPacketListener would apply the packets: each entity's spawn packet,
* the entity data it was paired with and all it was sent since, its
* equipment, attributes and passengers, and where the position syncs leave
* it (VecDeltaCodec, as handleMoveEntity and handleEntityPositionSync).
* Entity ids differ between servers: an entity is named by its type and the
* block it was added in, passengers by those names. Players are left out.
*/
final class EntityTranscript {
private static final class Tracked {
final String type;
final String add;
final VecDeltaCodec codec = new VecDeltaCodec();
String name;
String pairing;
final Map<Integer, String> data = new TreeMap<>();
final Map<String, String> equipment = new TreeMap<>();
final Map<String, String> attributes = new TreeMap<>();
int[] passengers = new int[0];
float yRot;
float xRot;
float headRot;
boolean onGround;
String motion;
String power = "";
boolean removed;
Tracked(ClientboundAddEntityPacket p) {
type = BuiltInRegistries.ENTITY_TYPE.getKey(p.getType()).toString();
Vec3 at = new Vec3(p.getX(), p.getY(), p.getZ());
codec.setBase(at);
name = type + " " + (int) Math.floor(at.x) + "," + (int) Math.floor(at.y) + "," + (int) Math.floor(at.z);
add = "at " + at + " rot " + p.getXRot() + "," + p.getYRot() + " head " + p.getYHeadRot() + " data " + p.getData()
+ " motion " + p.getMovement();
yRot = p.getYRot();
xRot = p.getXRot();
headRot = p.getYHeadRot();
motion = String.valueOf(p.getMovement());
}
}
private final Map<Integer, Tracked> byId = new HashMap<>();
private final List<Tracked> all = new ArrayList<>();
private int unknown;
/** Applies one packet; false when it isn't about a tracked entity. */
boolean accept(Packet<?> packet) {
if (packet instanceof ClientboundAddEntityPacket p) {
if (p.getType() == EntityTypes.PLAYER) return true;
Tracked t = new Tracked(p);
byId.put(p.getId(), t);
all.add(t);
return true;
}
if (packet instanceof ClientboundRemoveEntitiesPacket p) {
p.entityIds().forEach(id -> {
Tracked t = byId.remove(id);
if (t != null) t.removed = true;
});
return true;
}
Tracked t = switch (packet) {
case ClientboundSetEntityDataPacket p -> byId.get(p.id());
case ClientboundSetEquipmentPacket p -> byId.get(p.getEntity());
case ClientboundUpdateAttributesPacket p -> byId.get(p.getEntityId());
case ClientboundSetPassengersPacket p -> byId.get(p.getVehicle());
case ClientboundEntityPositionSyncPacket p -> byId.get(p.id());
case ClientboundSetEntityMotionPacket p -> byId.get(p.id());
case ClientboundTeleportEntityPacket p -> byId.get(p.id());
case ClientboundProjectilePowerPacket p -> byId.get(p.getId());
case ClientboundMoveEntityPacket p -> byId.get(VanillaProtocolClient.moveEntityId(p));
case ClientboundRotateHeadPacket p -> byId.get(VanillaProtocolClient.rotateHeadId(p));
default -> null;
};
if (t == null) {
return false;
}
switch (packet) {
case ClientboundSetEntityDataPacket p -> {
for (var v : p.packedItems()) t.data.put(v.id(), net.minecraft.network.syncher.EntityDataSerializers.getSerializedId(v.serializer()) + " " + v.value());
if (t.pairing == null) {
StringBuilder b = new StringBuilder();
for (var v : p.packedItems()) b.append(v.id()).append('=').append(v.value()).append(' ');
t.pairing = b.toString().trim();
}
}
case ClientboundSetEquipmentPacket p -> p.getSlots().forEach(s -> t.equipment.put(s.getFirst().getName(), String.valueOf(s.getSecond())));
case ClientboundUpdateAttributesPacket p -> p.getValues().forEach(a -> t.attributes.put(a.attribute().getRegisteredName(),
a.base() + " " + a.modifiers()));
case ClientboundSetPassengersPacket p -> t.passengers = p.getPassengers();
case ClientboundEntityPositionSyncPacket p -> {
t.codec.setBase(p.position().endPosition());
t.yRot = p.yRot();
t.xRot = p.xRot();
t.onGround = p.onGround();
}
case ClientboundMoveEntityPacket p -> {
if (p.hasPosition()) t.codec.setBase(p.getPositionDelta().decode(t.codec).endPosition());
if (p.hasRotation()) {
t.yRot = p.getYRot();
t.xRot = p.getXRot();
}
t.onGround = p.isOnGround();
}
case ClientboundSetEntityMotionPacket p -> t.motion = String.valueOf(p.movement());
case ClientboundTeleportEntityPacket p -> {
t.codec.setBase(p.change().position());
t.onGround = p.onGround();
}
case ClientboundRotateHeadPacket p -> t.headRot = p.getYHeadRot();
case ClientboundProjectilePowerPacket p -> t.power = " power " + p.getAccelerationPower();
default -> {
}
}
return true;
}
/** One line per entity, sorted, each field named. */
List<String> lines() {
Map<String, Integer> seen = new HashMap<>();
for (Tracked t : all) {
int n = seen.merge(t.name, 1, Integer::sum);
if (n > 1) t.name += " #" + n;
}
Map<Integer, String> names = new HashMap<>();
byId.forEach((id, t) -> names.put(id, t.name));
List<String> out = new ArrayList<>();
for (Tracked t : all) {
List<String> riders = new ArrayList<>();
for (int id : t.passengers) riders.add(names.getOrDefault(id, "player or unknown"));
out.add("entity " + t.name + " | add " + t.add + " | pairing " + t.pairing + " | data " + t.data + " | equipment " + t.equipment
+ " | attributes " + t.attributes + " | passengers " + riders + " | now " + t.codec.getBase() + " rot " + t.xRot + ","
+ t.yRot + " head " + t.headRot + " ground " + t.onGround + " motion " + t.motion + t.power + (t.removed ? " | removed" : ""));
}
out.sort(null);
return out;
}
}
@@ -76,11 +76,19 @@ import net.minecraft.world.phys.Vec3;
* line at a time: {@code step LABEL} (the label the next packets are recorded
* under), {@code join NAME}, {@code cmd NAME COMMAND...}, {@code shift NAME
* on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME
* on|off} (ServerboundPlayerAbilitiesPacket), {@code quit NAME} and {@code
* end}, which writes each player's lines to OUT.
* on|off} (ServerboundPlayerAbilitiesPacket), {@code still NAME} (the
* position reminder LocalPlayer.sendPosition sends every 20 ticks standing
* still), {@code quit NAME}, {@code
* players on|off} (whether each player also records the other players added
* to and removed from its level, and its own respawns into a dimension),
* {@code chat on|off} (whether the system chat each player receives is
* recorded too, as component JSON) and {@code end}, which writes each
* player's lines to OUT.
*/
public final class LocatorBarClient {
private static volatile String step = "start";
private static volatile boolean players;
private static volatile boolean chat;
private static final Map<UUID, String> NAMES = new LinkedHashMap<>();
public static void main(String[] args) throws Exception {
@@ -110,7 +118,10 @@ public final class LocatorBarClient {
abilities.flying = w[2].equals("on");
players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities));
}
case "still" -> players.get(w[1]).still();
case "quit" -> players.get(w[1]).close();
case "players" -> LocatorBarClient.players = w[1].equals("on");
case "chat" -> chat = w[1].equals("on");
case "end" -> {
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) {
for (Player p : players.values()) {
@@ -166,8 +177,10 @@ public final class LocatorBarClient {
private int threshold = -1;
private ProtocolInfo<?> inbound;
private ProtocolInfo<?> outbound;
private PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0);
private volatile PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0);
private boolean loaded;
/** The other players this one was sent, by entity id. */
private final Map<Integer, String> seen = new java.util.HashMap<>();
private final Object replyLock = new Object();
private int replies;
@@ -198,6 +211,14 @@ public final class LocatorBarClient {
}
}
void still() throws Exception {
PositionMoveRotation at = position;
// A tick of its own: the server takes one position per client tick (receivedPositionThisTick).
send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE);
send(new ServerboundMovePlayerPacket.PosRot(at.position(), at.yRot(), at.xRot(), false, false));
send(net.minecraft.network.protocol.game.ServerboundClientTickEndPacket.INSTANCE);
}
void close() throws Exception {
if (socket != null) socket.close();
}
@@ -266,9 +287,27 @@ public final class LocatorBarClient {
while (true) {
Received r = read();
Packet<?> p = r.packet;
if (p instanceof ClientboundTrackedWaypointPacket) {
if (p instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket a
&& a.getType() == net.minecraft.world.entity.EntityTypes.PLAYER) {
String who = NAMES.getOrDefault(a.getUUID(), "uuid?");
seen.put(a.getId(), who);
if (players) record("add " + who + " at " + a.getX() + "," + a.getY() + "," + a.getZ());
} else if (p instanceof net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket rm) {
rm.entityIds().forEach(id -> {
String who = seen.remove(id);
if (who != null && players) record("remove " + who);
});
} else if (p instanceof net.minecraft.network.protocol.game.ClientboundRespawnPacket rs) {
if (players) record("respawn " + rs.commonPlayerSpawnInfo().dimension().identifier());
seen.clear();
} else if (p instanceof ClientboundTrackedWaypointPacket) {
record(waypoint(r.payload));
} else if (p instanceof ClientboundSystemChatPacket) {
} else if (p instanceof ClientboundSystemChatPacket c) {
if (chat) {
record("chat " + (c.overlay() ? "overlay " : "") + net.minecraft.network.chat.ComponentSerialization.CODEC
.encodeStart(ParityData.access.createSerializationContext(com.mojang.serialization.JsonOps.INSTANCE), c.content())
.getOrThrow());
}
synchronized (replyLock) {
replies++;
replyLock.notifyAll();
@@ -113,6 +113,8 @@ public final class VanillaProtocolClient {
private ProtocolInfo<?> inbound;
private ProtocolInfo<?> outbound;
private String state;
/** With a fifth argument {@code entities}: the entity transcript mode (EntityTranscript). */
private EntityTranscript entities;
public static void main(String[] args) throws Exception {
ParityData.init();
@@ -121,6 +123,7 @@ public final class VanillaProtocolClient {
String name = args[2];
Path output = Path.of(args[3]);
VanillaProtocolClient client = new VanillaProtocolClient();
if (args.length > 4 && args[4].equals("entities")) client.entities = new EntityTranscript();
try {
client.status(host, port);
client.join(host, port, name);
@@ -405,6 +408,18 @@ public final class VanillaProtocolClient {
play();
}
/** The entity mode's step, and how long it records the entities for (ms). */
private static final int ENTITIES = 1000;
private static final long ENTITY_WAIT = 20_000;
static int moveEntityId(net.minecraft.network.protocol.game.ClientboundMoveEntityPacket p) {
return (int) field(p, net.minecraft.network.protocol.game.ClientboundMoveEntityPacket.class, "entityId");
}
static int rotateHeadId(net.minecraft.network.protocol.game.ClientboundRotateHeadPacket p) {
return (int) field(p, p.getClass(), "entityId");
}
private static final String[] REPLIES = {"random value 7..7", "random value x", "trigger nope"};
private void play() throws Exception {
@@ -461,6 +476,7 @@ public final class VanillaProtocolClient {
if (r != null) {
Packet<?> packet = r.packet;
counts.merge(r.type, 1, Integer::sum);
if (entities != null && step >= ENTITIES) entities.accept(packet);
if (r.type.equals("minecraft:chunk_batch_start")) ordered = false;
if (respawning && !respawned) {
String line = respawnLine(r);
@@ -581,8 +597,20 @@ public final class VanillaProtocolClient {
commandTree(p);
}
}
if (entities != null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
// Entity mode: the harness's console summons the scene once it reads this
// line; everything tracked from then on is recorded until the client leaves.
record("play-chunks", String.valueOf(chunks.size()));
send(new ServerboundChatPacket("summon the entities", Instant.now(), 0L, Optional.empty(),
new LastSeenMessages.Update(0, new BitSet(), (byte) 0)));
step = ENTITIES;
stepAt = now;
} else if (step == ENTITIES && now - stepAt > ENTITY_WAIT) {
for (String line : entities.lines()) record("play-entities", line);
return;
}
// Once the chunks stop coming: chat, then /list, then leave.
if (loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
if (entities == null && loaded && lastChunk > 0 && now - lastChunk > 3000 && step == 0) {
record("play-chunks", chunks.size() + " " + chunks);
BLOCK_ENTITIES.forEach((k, v) -> record("play-block-entities", k + " " + v));
// Which light layers exist depends on the neighbouring
@@ -910,8 +938,12 @@ public final class VanillaProtocolClient {
}
private static Object field(Object owner, String name) {
return field(owner, owner.getClass(), name);
}
private static Object field(Object owner, Class<?> declaring, String name) {
try {
var f = owner.getClass().getDeclaredField(name);
var f = declaring.getDeclaredField(name);
f.setAccessible(true);
return f.get(owner);
} catch (ReflectiveOperationException e) {
+95 -4
View File
@@ -30,7 +30,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
tree (Tab), and runs what it completed;
- recipes: the guest's recipe book lists the planks recipe the logs unlock, and clicking it
places a log in the inventory's crafting grid (the server places it).
- feedback: the guest's command feedback names the guest and shows the numbers.
- feedback: the guest's command feedback names the guest and shows the numbers, and the host (an
operator) is told of it as `[Guest: ...]`.
- clicks: with real mouse presses (not click-slot, which skips the screen's press and drag
handling), the guest puts a stack into a chest and a furnace, drags planks across
its crafting grid and gathers a stack with a double click; the server has each.
@@ -42,6 +43,10 @@ address. The script then checks, from both sides, what a guest's game used to ge
counts up) and the server eats one loaf and feeds the guest.
- xp: experience orbs 4 blocks away fly to the guest, show on the guest's screen and give the
guest their points (not the host).
- objects: boats, minecarts, armor stands, item frames and paintings the host summons reach the
guest as the host has them (kind, place, rotation, what they hold or wear, the variant).
- projectiles: primed TNT (one falling), an end crystal and an arrow that lands reach the guest where
the host has them; a falling block lands on both sides.
Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of
both sides go to --out (host-*.png, guest-*.png).
@@ -74,7 +79,7 @@ ROOT = Path(__file__).resolve().parents[1]
GUEST_NAME = 'Guest'
# The host plays single player on its server thread (--server-thread).
SERVER_THREAD = False
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'effects', 'portals']
CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'containers', 'feedback', 'titles', 'commands', 'recipes', 'trials', 'clicks', 'sleep', 'push', 'eat', 'xp', 'objects', 'projectiles', 'effects', 'portals']
class Game:
@@ -691,6 +696,8 @@ def check_feedback(host, guest, out):
guest('command', command=f'tp @s {x:.1f} {y:.1f} {z:.1f}')
guest.wait(10)
chat = guest.raw('messages', 5)['chat']
# The host is an operator: broadcastToAdmins tells it, as `[Guest: ...]`.
host_chat = host.raw('messages', 5)['chat']
guest.raw('key', 't', 'tap')
guest.wait(5)
guest.shot(out, 'feedback')
@@ -701,7 +708,9 @@ def check_feedback(host, guest, out):
assert any(replaced in line for line in chat), f'no "{replaced}" in {chat}'
assert any(line.startswith(f'Teleported {GUEST_NAME} to {x:.6f}') for line in chat), f'teleport feedback: {chat}'
assert screen in (None, 'playing'), f'the chat did not close: {screen}'
return chat
told = f'[{GUEST_NAME}: Teleported {GUEST_NAME} to {x:.6f}'
assert any(line.startswith(told) for line in host_chat), f'no "{told}" for the host: {host_chat}'
return chat + host_chat
def chat_input(game):
@@ -1081,6 +1090,88 @@ def check_xp(host, guest, out):
return {'seen': seen, 'guest': xp, 'host': host_xp}
def objects_near(game, centre, key='objects'):
"""`status`'s objects within 8 blocks of centre, without their ids (each side numbers its own)."""
out = []
for o in game('status')[key]:
if all(abs(o['position'][i] - centre[i]) <= 8 for i in (0, 2)):
o = dict(o)
o.pop('id')
o['position'] = [round(v, 2) for v in o['position']]
if 'yaw' in o: # as the packets carry it, in 256ths of a turn
o['yaw'] = round(o['yaw'] * 256 / 360) % 256
out.append(o)
return sorted(out, key=json.dumps)
def check_objects(host, guest, out):
x, y, z = (int(v // 1) for v in pos_of(host))
commands = [
f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:stone',
f'summon minecraft:minecart {x - 4.5} {y} {z + 3.5}',
f'summon minecraft:chest_minecart {x - 2.5} {y} {z + 3.5}',
f'summon minecraft:furnace_minecart {x + 1.5} {y} {z + 3.5}',
f'summon minecraft:tnt_minecart {x + 4.5} {y} {z + 3.5}',
f'summon minecraft:oak_boat {x - 4.5} {y} {z + 6.5} {{Rotation:[45f,0f]}}',
f'summon minecraft:birch_chest_boat {x - 1.5} {y} {z + 6.5}',
f'summon minecraft:bamboo_raft {x + 2.5} {y} {z + 6.5}',
f'summon minecraft:armor_stand {x - 2.5} {y} {z - 2.5} {{Rotation:[90f,0f]}}',
f'summon minecraft:armor_stand {x - 4.5} {y} {z - 2.5} {{ShowArms:1b,Small:1b,NoBasePlate:1b,'
'equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}',
f'summon minecraft:item_frame {x - 3.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 4},{y + 1},{z - 5}],Facing:3b,'
'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}',
f'summon minecraft:glow_item_frame {x - 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x - 2},{y + 1},{z - 5}],Facing:3b}}',
f'summon minecraft:painting {x + 1.5} {y + 1} {z - 4.97} {{block_pos:[I;{x + 1},{y + 1},{z - 5}],facing:0b,'
'variant:"minecraft:kebab"}',
f'summon minecraft:painting {x + 4} {y + 1} {z - 4.97} {{block_pos:[I;{x + 4},{y + 1},{z - 5}],facing:0b,'
'variant:"minecraft:courbet"}',
]
try:
for command in commands:
host('command', command=command)
host.wait(60)
guest.raw('look-at', x, y, z - 5)
guest.wait(10)
guest.shot(out, 'objects')
host.shot(out, 'objects')
mine, theirs = objects_near(host, (x, y, z)), objects_near(guest, (x, y, z))
finally:
host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=8,dz=16]')
host('command', command=f'fill {x - 6} {y} {z - 6} {x + 6} {y + 3} {z - 6} minecraft:air')
assert len(mine) == len(commands) - 1, f'the host has {len(mine)} objects: {mine}'
assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}'
return f'{len(theirs)} objects identical'
def check_projectiles(host, guest, out):
x, y, z = (int(v // 1) for v in pos_of(host))
commands = [
f'summon minecraft:tnt {x + 4.5} {y} {z + 4.5} {{fuse:2000s}}',
f'summon minecraft:end_crystal {x - 4.5} {y} {z + 4.5} {{ShowBottom:0b}}',
f'summon minecraft:tnt {x + 2.5} {y + 5} {z - 3.5} {{fuse:2000s}}',
f'summon minecraft:arrow {x - 2.5} {y + 4} {z - 2.5} {{Motion:[0.2d,0d,0d]}}',
f'summon minecraft:falling_block {x - 0.5} {y + 6} {z - 4.5} {{BlockState:{{Name:"minecraft:sand"}}}}',
]
try:
for command in commands:
host('command', command=command)
host.wait(80)
guest.raw('look-at', x, y, z)
guest.wait(5)
guest.shot(out, 'projectiles')
mine = objects_near(host, (x, y, z), 'projectiles')
theirs = objects_near(guest, (x, y, z), 'projectiles')
finally:
host('command', command=f'kill @e[type=!minecraft:player,x={x - 8},y={y - 2},z={z - 8},dx=16,dy=10,dz=16]')
host('command', command=f'setblock {x - 1} {y} {z - 5} minecraft:air')
assert len(mine) == len(commands) - 1, f'the host has {len(mine)}: {mine}'
# The fuse counts down every tick: the guest's, read a moment later, may be a tick or two behind.
fuses = [(m.pop('fuse', 0), t.pop('fuse', 0)) for m, t in zip(mine, theirs)]
assert theirs == mine, f'the guest sees {theirs}\nthe host has {mine}'
assert all(abs(a - b) <= 2 for a, b in fuses), f'fuses (host, guest): {fuses}'
return f'{len(theirs)} identical, fuses (host, guest) {fuses}'
CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'nametags': check_nametags,
'chest': check_chest, 'crafting': check_crafting, 'sign': check_sign, 'damage': check_damage,
'mobs': check_mobs, 'hazards': check_hazards, 'pvp': check_pvp,
@@ -1089,7 +1180,7 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name
'recipes': check_recipes, 'trials': check_trials, 'walls': check_walls,
'effects': check_effects, 'portals': check_portals, 'clicks': check_clicks,
'sleep': check_sleep, 'push': check_push, 'eat': check_eat,
'xp': check_xp}
'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles}
def run(ctl, out, resources, checks):
+16
View File
@@ -364,6 +364,20 @@ def run_horizon(args, distance):
for carry in dev_mods(args):
command += ["--dev-mod", str(carry)]
env = dict(os.environ, HORIZON_NO_AUDIO="1")
if args.warm:
# A first start to the title screen builds what later starts reuse (the content-mod
# root, the block behaviour snapshot). It is not measured: players see it once.
with (WORK / f"horizon-rd{distance}-warm.log").open("w") as log:
proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT)
try:
poll(Control(sock), lambda s: s.get("screen") == "title", 600)
try:
Control(sock)("quit", timeout=60)
except (OSError, ValueError, RuntimeError):
pass
finally:
stop(proc)
sock.unlink(missing_ok=True)
log = (WORK / f"horizon-rd{distance}.log").open("w")
started = time.time()
proc = subprocess.Popen(command, cwd=game_dir, env=env, stdout=log, stderr=subprocess.STDOUT)
@@ -774,6 +788,8 @@ def main():
p.add_argument("--build", default=str(ROOT / "target/release"))
p.add_argument("--repeats", type=int, default=3, help="games per render distance; the report gives means and 95%% CIs")
p.add_argument("--port-work", help="horizon: also load the mods carried over in this horizon-port-pack work folder")
p.add_argument("--warm", action="store_true",
help="horizon: measure a later start, after one start to the title screen built the caches")
p.add_argument("--mod-jars", help="neoforge: the mod jars to load (the user's own; never committed)")
p.add_argument("--deps", help="neoforge: their required libraries")
p.add_argument("--table", nargs="+", metavar="LABEL", help="print markdown tables of saved runs, and exit")
@@ -0,0 +1,150 @@
#!/usr/bin/env python3
"""Players in different dimensions on a dedicated server, against vanilla's, seen by vanilla clients.
A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat
world vanilla generated first, with Alice and Bob as operators in creative. LocatorBarClient joins them both
(players made of vanilla's protocol code) and records, step by step, the other players each is sent and loses
(ClientboundAddEntityPacket, ClientboundRemoveEntitiesPacket) and the dimensions it respawns into
(ClientboundRespawnPacket), with the locator bar's waypoints, while they change dimensions (ServerPlayer.teleport
through `execute in ... run tp`, and an End portal): Bob goes to the Nether and back, Alice to the End, Bob
follows her there, both come back to the Overworld, then Bob steps into an End portal and comes back. Between
the steps each client sends its position (the reminder a still client sends every second). Each player's lines in each step are compared exactly.
"""
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
import time
try:
from . import lab, test_rust_parity as base
from .test_dedicated_server_parity import Server
from .test_locator_bar_parity import generate_world, server_dir
except ImportError:
import lab
import test_rust_parity as base
from test_dedicated_server_parity import Server
from test_locator_bar_parity import generate_world, server_dir
NAME = 'server-dimension-players-parity'
NETHER, END, OVERWORLD = 'minecraft:the_nether', 'minecraft:the_end', 'minecraft:overworld'
# (label, what the players do).
SCRIPT = [
('bob-nether', [f'cmd Bob execute in {NETHER} run tp @s 0.5 100 0.5']),
('bob-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']),
('alice-end', [f'cmd Alice execute in {END} run tp @s 0.5 100 0.5']),
('bob-end', [f'cmd Bob execute in {END} run tp @s 2.5 100 0.5']),
('alice-back', [f'cmd Alice execute in {OVERWORLD} run tp @s 0.5 100 0.5']),
('bob-back-again', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']),
# Through an End portal (Entity.handlePortal), and back by command.
('bob-portal', ['cmd Bob setblock 2 101 3 minecraft:end_portal', 'cmd Bob tp @s 2.5 101 3.5']),
('bob-portal-back', [f'cmd Bob execute in {OVERWORLD} run tp @s 2.5 100 0.5']),
]
STEP_SECONDS = 5.0
def run_side(label, server_command, client_command, build, world):
directory = build / label
port = server_dir(directory, world)
out = build / f'{label}.txt'
out.unlink(missing_ok=True)
server = Server(server_command, directory, label)
try:
server.wait_for(r'Done \(')
with (build / f'{label}-client.log').open('w') as log:
client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)],
cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT,
text=True)
def tell(line):
client.stdin.write(line + '\n')
client.stdin.flush()
try:
tell('players on')
tell('step joins')
for name, x in (('Alice', 0.5), ('Bob', 2.5)):
tell(f'join {name}')
server.wait_for(rf'{name} joined the game', timeout=120)
tell(f'fly {name} on')
tell(f'cmd {name} tp @s {x} 100 0.5')
time.sleep(STEP_SECONDS)
for step, actions in SCRIPT:
tell(f'step {step}')
for action in actions:
tell(action)
# Standing still, a client still sends its position every 20 ticks
# (LocalPlayer.sendPosition): the server then tracks to it what it has sent the
# chunks of since (ChunkMap.move).
time.sleep(STEP_SECONDS / 2)
tell('still Alice')
tell('still Bob')
time.sleep(STEP_SECONDS / 2)
tell('end')
client.wait(timeout=60)
finally:
if client.poll() is None:
client.kill()
finally:
server.stop()
if not out.exists():
raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)')
return out.read_text(encoding='utf-8').splitlines()
def shown(lines):
"""Each player's adds, removes and respawns, without the waypoints (the locator bar harness has those)."""
return [l for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].split(' ')[0] in ('add', 'remove', 'respawn')]
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java'
classes = build / 'classes'
shutil.rmtree(classes, ignore_errors=True)
classes.mkdir()
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath',
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
cwd=lab.ROOT, check=True, capture_output=True)
subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT,
check=True, capture_output=True)
import json
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui']
client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient']
world = generate_world(vanilla_server, build)
vanilla_all = run_side('vanilla', vanilla_server, client, build, world)
horizon_all = run_side('horizon', horizon_server, client, build, world)
errors = [l for l in vanilla_all + horizon_all if ' error ' in f' {l} ' or 'disconnected' in l]
vanilla, horizon = shown(vanilla_all), shown(horizon_all)
if errors or vanilla != horizon:
import difflib
diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm=''))
raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n'
+ '\n'.join(errors[:10]) + '\n' + diff[:6000])
if sum(' respawn ' in l for l in vanilla) < 6:
raise AssertionError(f'vanilla sent too few respawns: the script did not run (see {build})')
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
scope='Two players on a dedicated server changing dimensions (Nether, End, back): the other players each '
'is sent and loses, and the dimensions it respawns into.'))
print(f'server dimension players: {len(vanilla)} lines identical')
if __name__ == '__main__':
try:
main()
except AssertionError as error:
sys.exit(f'{NAME}: {error}')
+185
View File
@@ -0,0 +1,185 @@
#!/usr/bin/env python3
"""Entities other than mobs and items, tracked to a vanilla client, against vanilla's dedicated server.
A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat
world vanilla generated first, with the same server.properties (offline, view distance 4). A vanilla-codec
client (VanillaProtocolClient in its entity mode) joins, and once its chunks are in it asks for the scene: the
console freezes the ticks, builds a wall and a rail and summons, around the spawn, every kind of minecart (one
set rolling along the rail), boats and a raft (one falling onto the ground), armor stands (posed, small, with
arms, wearing a helmet, one falling), a chicken jockey, item frames (one holding a turned diamond), paintings,
primed TNT (one falling), a floating falling block, an arrow that lands and an end crystal. The client records
for 20 seconds, the ticks running for 200 more after 3, what ClientPacketListener would make of the packets (EntityTranscript): each entity's spawn
packet (position, rotations, data, motion), the entity data it was paired with and all it was sent since, its
equipment and passengers, and where its position syncs leave it. Both transcripts must match exactly,
entity by entity (named by type and the block it was added in). Attributes are recorded but not compared yet:
Horizon sends no ClientboundUpdateAttributesPacket, for mobs either.
"""
import os
from pathlib import Path
import platform
import re
import shutil
import subprocess
import sys
import time
try:
from . import lab, test_rust_parity as base
from .test_dedicated_server_parity import Server
from .test_locator_bar_parity import generate_world, server_dir
except ImportError:
import lab
import test_rust_parity as base
from test_dedicated_server_parity import Server
from test_locator_bar_parity import generate_world, server_dir
NAME = 'server-entities-parity'
# Ticks the scene runs for once it is summoned: 10 seconds, within the client's 20.
STEPS = 200
PLAYER = 'Tester'
# Around the spawn at 0 -60 0 on the flat world's grass: a wall to the north for the frames and paintings,
# a rail to the south.
SCENE = [
'fill -7 -60 -6 7 -57 -6 minecraft:stone',
'fill -7 -60 9 7 -60 9 minecraft:rail',
# Minecarts.
'summon minecraft:minecart -5.5 -60 3.5',
'summon minecraft:chest_minecart -3.5 -60 3.5',
'summon minecraft:furnace_minecart -1.5 -60 3.5',
'summon minecraft:hopper_minecart 1.5 -60 3.5',
'summon minecraft:tnt_minecart 3.5 -60 3.5',
'summon minecraft:command_block_minecart 5.5 -60 1.5 {Command:"say hi"}',
'summon minecraft:minecart -6.5 -60 9.5 {Motion:[0.5d,0d,0d]}',
# Boats.
'summon minecraft:oak_boat -5.5 -60 6.5 {Rotation:[45f,0f]}',
'summon minecraft:birch_chest_boat -2.5 -60 6.5',
'summon minecraft:bamboo_raft 0.5 -60 6.5',
'summon minecraft:oak_boat 4.5 -56 6.5',
# Armor stands.
'summon minecraft:armor_stand -2.5 -60 -2.5 {Rotation:[90f,0f]}',
'summon minecraft:armor_stand 6.5 -57 -2.5',
'summon minecraft:armor_stand -4.5 -60 -2.5 {ShowArms:1b,Small:1b,NoBasePlate:1b,'
'Pose:{Head:[10f,20f,0f],RightArm:[-30f,0f,15f]},equipment:{head:{id:"minecraft:diamond_helmet",count:1}}}',
# A chicken jockey (passengers).
'summon minecraft:chicken -6.5 -60 -2.5 {NoAI:1b,Passengers:[{id:"minecraft:husk",IsBaby:1b,NoAI:1b}]}',
# Frames and paintings on the wall's south face.
'summon minecraft:item_frame -3.5 -59 -4.97 {block_pos:[I;-4,-59,-5],Facing:3b,'
'Item:{id:"minecraft:diamond",count:1},ItemRotation:3b}',
'summon minecraft:glow_item_frame -1.5 -59 -4.97 {block_pos:[I;-2,-59,-5],Facing:3b}',
'summon minecraft:painting 1.5 -59 -4.97 {block_pos:[I;1,-59,-5],facing:0b,variant:"minecraft:kebab"}',
'summon minecraft:painting 4 -59 -4.97 {block_pos:[I;4,-59,-5],facing:0b,variant:"minecraft:courbet"}',
# Primed TNT that won't go off (one falling), a floating falling block, an arrow that lands, an end crystal.
'summon minecraft:tnt 6.5 -60 6.5 {fuse:2000s}',
'summon minecraft:tnt 6.5 -56 4.5 {fuse:2000s}',
'summon minecraft:falling_block 2.5 -58 -0.5 {BlockState:{Name:"minecraft:sand"},NoGravity:1b}',
'summon minecraft:arrow 0.5 -55 -1.5 {Motion:[0.3d,0d,0d]}',
'summon minecraft:end_crystal -6.5 -60 6.5 {ShowBottom:0b}',
]
def scene_lines(lines):
"""The transcript's entities in the scene's area (the world's own animals and slimes wander elsewhere),
without their attributes: ClientboundUpdateAttributesPacket isn't sent by Horizon yet, for mobs either."""
out = []
for line in lines:
m = re.match(r'play-entities entity \S+ (-?\d+),-?\d+,(-?\d+)', line)
if m and -8 <= int(m.group(1)) <= 8 and -7 <= int(m.group(2)) <= 10:
out.append(re.sub(r' \| attributes \{[^|]*\}', '', line))
return out
def run_side(label, server_command, client_command, build, world):
directory = build / label
port = server_dir(directory, world)
out = build / f'{label}-client.txt'
out.unlink(missing_ok=True)
server = Server(server_command, directory, label)
try:
server.wait_for(r'Done \(')
with (build / f'{label}-client.log').open('w') as log:
client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), PLAYER, str(out), 'entities'],
cwd=lab.ROOT, stdout=log, stderr=subprocess.STDOUT, text=True)
try:
server.wait_for(rf'<{PLAYER}> summon the entities', timeout=180)
# Frozen, so that every entity is paired as it was summoned whenever each server tracks it in
# its tick; then they fall and roll.
server.console('tick freeze')
for command in SCENE:
server.console(command)
time.sleep(3)
# A fixed number of ticks, so that what still changes (a TNT fuse) ends the same.
server.console(f'tick step {STEPS}')
client.wait(timeout=120)
finally:
if client.poll() is None:
client.kill()
finally:
server.stop()
if not out.exists():
raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-client.log)')
lines = out.read_text(encoding='utf-8').splitlines()
(build / f'{label}.txt').write_text('\n'.join(lines) + '\n', encoding='utf-8')
return lines
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java'
classes = build / 'classes'
shutil.rmtree(classes, ignore_errors=True)
classes.mkdir()
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath',
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
cwd=lab.ROOT, check=True, capture_output=True)
# SERVER_ENTITIES_SIDES=vanilla (or horizon) runs one side and reads the other's last transcript.
sides = [s for s in os.environ.get('SERVER_ENTITIES_SIDES', 'vanilla,horizon').split(',') if s]
if 'horizon' in sides:
subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT,
check=True, capture_output=True)
import json
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui']
client = ['java', '-Xmx1g', '-Duser.timezone=UTC', '-cp', str(classes) + os.pathsep + dependencies,
'dev.horizonlab.parity.VanillaProtocolClient']
world = generate_world(vanilla_server, build)
results = {}
for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]:
if label in sides:
results[label] = run_side(label, command, client, build, world)
else:
results[label] = (build / f'{label}.txt').read_text(encoding='utf-8').splitlines()
vanilla, horizon = scene_lines(results['vanilla']), scene_lines(results['horizon'])
errors = [l for l in results['vanilla'] + results['horizon'] if l.startswith('error')]
if errors or vanilla != horizon:
import difflib
diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=0, lineterm=''))
raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n'
+ '\n'.join(errors[:10]) + '\n' + diff[:8000])
if len(vanilla) < len(SCENE) - 2:
raise AssertionError(f'only {len(vanilla)} entities tracked: the scene did not run (see {build})')
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
entities=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
scope='Minecarts (one rolling), boats (one falling), armor stands, a chicken jockey, item frames, paintings, primed '
'TNT, a falling block, an arrow and an end crystal tracked to a '
'vanilla-codec client by a dedicated server: spawn packets, entity data, equipment, passengers and the '
'positions the syncs leave them at.'))
print(f'server entities: {len(vanilla)} entities identical')
if __name__ == '__main__':
try:
main()
except AssertionError as error:
sys.exit(f'{NAME}: {error}')
+162
View File
@@ -0,0 +1,162 @@
#!/usr/bin/env python3
"""Command feedback the operators get on a dedicated server (`[Server: ...]`), against vanilla's.
CommandSourceStack.broadcastToAdmins: a command's success that vanilla sends with `broadcast` goes, gray and
italic as `[name: message]`, to every other operator while `send_command_feedback` is on, including the
console's own commands when `broadcast-console-to-ops` is (DedicatedServer.shouldInformAdmins).
A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat
world vanilla generated first, with the same server.properties and Alice and Bob as operators. LocatorBarClient
joins them both (players made of vanilla's protocol code) and records the system chat each receives while, step
by step, the console sets the time, asks for the difficulty (a query, told to nobody), whitelists a player,
deops Bob (who stops hearing of it) and ops him again, turns `send_command_feedback` off and on and saves, and
Alice runs a command herself (Bob hears of it as an operator). Then again with `broadcast-console-to-ops=false`.
Each player's chat in each step is compared exactly.
"""
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
import time
try:
from . import lab, test_rust_parity as base
from .test_dedicated_server_parity import Server
from .test_locator_bar_parity import generate_world, server_dir
except ImportError:
import lab
import test_rust_parity as base
from test_dedicated_server_parity import Server
from test_locator_bar_parity import generate_world, server_dir
NAME = 'server-ops-feedback-parity'
# (label, what the console does, what the players do).
BROADCAST = [
('console-time', ['time set 1000'], []),
('console-query', ['difficulty'], []),
('console-whitelist', ['whitelist add Alice'], []),
('console-deop', ['deop Bob'], []),
('bob-not-op', ['time set 2000'], []),
('alice-bob-not-op', [], ['cmd Alice time set 3000']),
('console-op', ['op Bob'], []),
('alice-bob-op', [], ['cmd Alice time set 4000']),
('feedback-off', ['gamerule send_command_feedback false'], []),
('console-feedback-off', ['time set 5000'], []),
('feedback-on', ['gamerule send_command_feedback true'], []),
('console-save', ['save-all'], []),
]
QUIET = [
('console-time', ['time set 1000'], []),
('console-whitelist', ['whitelist add Alice'], []),
('alice', [], ['cmd Alice time set 3000']),
]
CASES = {'broadcast': ({}, BROADCAST), 'quiet': ({'broadcast-console-to-ops': 'false'}, QUIET)}
STEP_SECONDS = 2.0
def run_side(label, case, server_command, client_command, build, world):
properties, script = CASES[case]
directory = build / label / case
directory.parent.mkdir(parents=True, exist_ok=True)
port = server_dir(directory, world)
with (directory / 'server.properties').open('a', encoding='iso-8859-1') as f:
f.writelines(f'{k}={v}\n' for k, v in properties.items())
out = build / f'{label}-{case}.txt'
out.unlink(missing_ok=True)
server = Server(server_command, directory, f'{label}-{case}')
try:
server.wait_for(r'Done \(')
with (build / f'{label}-{case}-client.log').open('w') as log:
client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)],
cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT,
text=True)
def tell(line):
client.stdin.write(line + '\n')
client.stdin.flush()
try:
for name in ('Alice', 'Bob'):
tell(f'join {name}')
server.wait_for(rf'{name} joined the game', timeout=120)
time.sleep(STEP_SECONDS)
tell('chat on')
for step, console, actions in script:
tell(f'step {step}')
for command in console:
server.console(command)
for action in actions:
tell(action)
time.sleep(STEP_SECONDS)
tell('end')
client.wait(timeout=60)
finally:
if client.poll() is None:
client.kill()
finally:
server.stop()
if not out.exists():
raise AssertionError(f'{label} {case}: the client wrote no transcript (see {build}/{label}-{case}-client.log)')
lines = out.read_text(encoding='utf-8').splitlines()
errors = [l for l in lines if ' error ' in f' {l} ' or 'disconnected' in l]
return [f'{case} {l}' for l in lines if l.split(' ', 2)[2:3] and l.split(' ', 2)[2].startswith('chat ')], errors
def main():
lock, jars, libraries = base.verify_artifacts()
build = lab.LOCAL / NAME
build.mkdir(parents=True, exist_ok=True)
report = build / 'report.json'
report.unlink(missing_ok=True)
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/LocatorBarClient.java'
classes = build / 'classes'
shutil.rmtree(classes, ignore_errors=True)
classes.mkdir()
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath',
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
cwd=lab.ROOT, check=True, capture_output=True)
subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT,
check=True, capture_output=True)
import json
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=lab.ROOT, text=True))
binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui']
client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.LocatorBarClient']
world = generate_world(vanilla_server, build)
sides = {'vanilla': ([], []), 'horizon': ([], [])}
for case in CASES:
for label, command in [('vanilla', vanilla_server), ('horizon', horizon_server)]:
lines, errors = run_side(label, case, command, client, build, world)
sides[label][0].extend(lines)
sides[label][1].extend(errors)
(vanilla, vanilla_errors), (horizon, horizon_errors) = sides['vanilla'], sides['horizon']
(build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8')
(build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8')
errors = vanilla_errors + horizon_errors
if errors or vanilla != horizon:
import difflib
diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm=''))
raise AssertionError(f'{len(errors)} errors, transcripts differ (see {build}/vanilla.txt and horizon.txt):\n'
+ '\n'.join(errors[:10]) + '\n' + diff[:6000])
if sum('[Server' in l or 'chat.type.admin' in l for l in vanilla) < 5:
raise AssertionError(f'vanilla sent the operators too little: the script did not run (see {build})')
lab.write_json(report, dict(
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
scope='The system chat two operators get on a dedicated server while the console and one of them run '
'commands: broadcastToAdmins for the console (broadcast-console-to-ops on and off) and a player, '
'queries, deop and op, send_command_feedback off and on, and save-all.'))
print(f'server ops feedback: {len(vanilla)} chat lines identical')
if __name__ == '__main__':
try:
main()
except AssertionError as error:
sys.exit(f'{NAME}: {error}')
+39 -3
View File
@@ -10,7 +10,10 @@ to the preset's classic ones) and `generate-structures`.
Each case starts vanilla's server and Horizon's (`horizon-client --dedicated --nogui`) in a new folder, waits
for `Done` and stops them. Compared exactly: the world's `data/minecraft/world_gen_settings.dat`, tag types
included, and the warnings and errors logged while the world's settings are read (the `Worker-Main` lines;
the worker's number is left out).
the worker's number is left out). In a world without structures (`generate-structures=false`), also what
`locate structure` answers on the console for a village and a mineshaft (ChunkGenerator.findNearestMapStructure
finds none) and how many of the saved chunks hold structure starts or references (none:
ChunkStatusTasks.generateStructureStarts does nothing).
Horizon generates the normal and flat presets only: `large_biomes`, `amplified`, `single_biome_surface` and
`debug_all_block_states` aren't tested.
@@ -23,6 +26,7 @@ import re
import shutil
import subprocess
import sys
import time
try:
from . import anvil_dump, lab, test_rust_parity as base
@@ -60,6 +64,32 @@ CASES = {
'unknown': {'level-type': 'Some Type', 'level-seed': '42'},
}
WORKER = re.compile(r'\[Worker-Main-\d+/(WARN|ERROR)\]: (.*)')
LOCATE = ['locate structure #minecraft:village', 'locate structure minecraft:mineshaft']
REPLY = re.compile(r'\]: (?:System chat: )?((?:Could not find|The nearest|Unknown|Incorrect).*)')
def console_reply(server, command, timeout=300):
"""The first command reply the console logs after `command`."""
start = len(server.lines)
server.console(command)
deadline = time.time() + timeout
while time.time() < deadline:
for line in server.lines[start:]:
if m := REPLY.search(line):
return m.group(1)
time.sleep(0.2)
raise AssertionError(f'no reply to {command!r} in {timeout} s:\n' + '\n'.join(server.lines[-20:]))
def structure_chunks(world):
"""How many saved overworld chunks hold structure starts, and how many references."""
starts = references = 0
for region in sorted((world / 'region').glob('*.mca')):
for chunk in anvil_dump.region_chunks(region).values():
structures = anvil_dump.plain(chunk, True).get('structures', {})
starts += bool(structures.get('starts'))
references += bool(structures.get('References'))
return f'chunks with structure starts: {starts}, with references: {references}'
def escape(value):
@@ -77,11 +107,16 @@ def run_case(label, command, build, case):
encoding='iso-8859-1')
(directory / 'eula.txt').write_text('eula=true\n')
server = Server(command, directory, f'{label}-{case}')
replies = []
try:
server.wait_for(r'Done \(')
if CASES[case].get('generate-structures') == 'false':
replies = [f'{c}: {console_reply(server, c)}' for c in LOCATE]
finally:
server.stop()
out = [f'# {case}']
out = [f'# {case}', *replies]
if replies:
out.append(structure_chunks(directory / 'world'))
out += [f'{m.group(1)}: {m.group(2)}' for l in server.lines if (m := WORKER.search(l))]
settings = anvil_dump.plain(anvil_dump.read_dat(directory / 'world/data/minecraft/world_gen_settings.dat'),
True, typed=True)
@@ -121,7 +156,8 @@ def main():
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
cases=list(CASES), lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'),
scope='a new dedicated-server world: level-type (flat, normal, unknown), a flat world\'s '
'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged.'))
'generator-settings, generate-structures, as world_gen_settings.dat and the lines logged; without '
'structures, locate structure\'s replies and the saved chunks\' structure starts and references.'))
print(f'server world gen: {len(CASES)} cases, {len(vanilla)} lines identical')