Skip to content

Commit 8c9e03a

Browse files
refactor(hyperion): NetworkStreamRef to ConnectionId (#648)
1 parent 7311885 commit 8c9e03a

File tree

20 files changed

+158
-128
lines changed

20 files changed

+158
-128
lines changed

crates/hyperion-clap/src/lib.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use flecs_ecs::{
66
prelude::{Component, Module},
77
};
88
use hyperion::{
9-
net::{Compose, DataBundle, NetworkStreamRef, agnostic},
9+
net::{Compose, ConnectionId, DataBundle, agnostic},
1010
simulation::{IgnMap, command::get_root_command_entity, handlers::PacketSwitchQuery},
1111
storage::{CommandCompletionRequest, EventFn},
1212
system_registry::SystemId,
@@ -66,9 +66,8 @@ pub trait MinecraftCommand: Parser + CommandPermission {
6666
match Self::try_parse_from(input) {
6767
Ok(elem) => {
6868
if world.get::<&Compose>(|compose| {
69-
caller
70-
.entity_view(world)
71-
.get::<(&NetworkStreamRef, &Group)>(|(stream, group)| {
69+
caller.entity_view(world).get::<(&ConnectionId, &Group)>(
70+
|(stream, group)| {
7271
if Self::has_required_permission(*group) {
7372
true
7473
} else {
@@ -82,7 +81,8 @@ pub trait MinecraftCommand: Parser + CommandPermission {
8281

8382
false
8483
}
85-
})
84+
},
85+
)
8686
}) {
8787
elem.execute(world, caller);
8888
}
@@ -100,7 +100,7 @@ pub trait MinecraftCommand: Parser + CommandPermission {
100100
world.get::<&Compose>(|compose| {
101101
caller
102102
.entity_view(world)
103-
.get::<&hyperion::net::NetworkStreamRef>(|stream| {
103+
.get::<&hyperion::net::ConnectionId>(|stream| {
104104
let msg = agnostic::chat(msg);
105105
compose.unicast(&msg, *stream, SystemId(8), world).unwrap();
106106
});
@@ -303,15 +303,13 @@ impl MinecraftCommand for PermissionCommand {
303303
Self::Set(cmd) => {
304304
// Handle setting permissions
305305
let Some(entity) = ign_map.get(cmd.player.as_str()) else {
306-
caller
307-
.entity_view(world)
308-
.get::<&NetworkStreamRef>(|stream| {
309-
let msg = format!("§c{} not found", cmd.player);
310-
let chat = hyperion::net::agnostic::chat(msg);
311-
world.get::<&Compose>(|compose| {
312-
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
313-
});
306+
caller.entity_view(world).get::<&ConnectionId>(|stream| {
307+
let msg = format!("§c{} not found", cmd.player);
308+
let chat = hyperion::net::agnostic::chat(msg);
309+
world.get::<&Compose>(|compose| {
310+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
314311
});
312+
});
315313
return;
316314
};
317315

@@ -321,44 +319,38 @@ impl MinecraftCommand for PermissionCommand {
321319
entity.entity_view(world).modified::<Group>();
322320
}
323321

324-
caller
325-
.entity_view(world)
326-
.get::<&NetworkStreamRef>(|stream| {
327-
let msg = format!(
328-
"§b{}§r's group has been set to §e{:?}",
329-
cmd.player, cmd.group
330-
);
331-
let chat = hyperion::net::agnostic::chat(msg);
332-
world.get::<&Compose>(|compose| {
333-
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
334-
});
322+
caller.entity_view(world).get::<&ConnectionId>(|stream| {
323+
let msg = format!(
324+
"§b{}§r's group has been set to §e{:?}",
325+
cmd.player, cmd.group
326+
);
327+
let chat = hyperion::net::agnostic::chat(msg);
328+
world.get::<&Compose>(|compose| {
329+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
335330
});
331+
});
336332
});
337333
}
338334
Self::Get(cmd) => {
339335
let Some(entity) = ign_map.get(cmd.player.as_str()) else {
340-
caller
341-
.entity_view(world)
342-
.get::<&NetworkStreamRef>(|stream| {
343-
let msg = format!("§c{} not found", cmd.player);
344-
let chat = hyperion::net::agnostic::chat(msg);
345-
world.get::<&Compose>(|compose| {
346-
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
347-
});
336+
caller.entity_view(world).get::<&ConnectionId>(|stream| {
337+
let msg = format!("§c{} not found", cmd.player);
338+
let chat = hyperion::net::agnostic::chat(msg);
339+
world.get::<&Compose>(|compose| {
340+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
348341
});
342+
});
349343
return;
350344
};
351345

352346
entity.entity_view(world).get::<&Group>(|group| {
353-
caller
354-
.entity_view(world)
355-
.get::<&NetworkStreamRef>(|stream| {
356-
let msg = format!("§b{}§r's group is §e{:?}", cmd.player, group);
357-
let chat = hyperion::net::agnostic::chat(msg);
358-
world.get::<&Compose>(|compose| {
359-
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
360-
});
347+
caller.entity_view(world).get::<&ConnectionId>(|stream| {
348+
let msg = format!("§b{}§r's group is §e{:?}", cmd.player, group);
349+
let chat = hyperion::net::agnostic::chat(msg);
350+
world.get::<&Compose>(|compose| {
351+
compose.unicast(&chat, *stream, SystemId(8), world).unwrap();
361352
});
353+
});
362354
});
363355
}
364356
}

crates/hyperion-command/src/system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Module for CommandSystemModule {
4949

5050
world.get::<&hyperion::net::Compose>(|compose| {
5151
by.entity_view(world)
52-
.get::<&hyperion::net::NetworkStreamRef>(|stream| {
52+
.get::<&hyperion::net::ConnectionId>(|stream| {
5353
compose
5454
.unicast(&chat, *stream, SystemId(8), &world)
5555
.unwrap();

crates/hyperion-gui/src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{borrow::Cow, cell::Cell, collections::HashMap};
44

55
use flecs_ecs::core::{Entity, EntityViewGet, World, WorldGet};
66
use hyperion::{
7-
net::{Compose, NetworkStreamRef},
7+
net::{Compose, ConnectionId},
88
storage::GlobalEventHandlers,
99
system_registry::SystemId,
1010
valence_protocol::{
@@ -124,13 +124,11 @@ impl Gui {
124124
};
125125

126126
world.get::<&Compose>(|compose| {
127-
player
128-
.entity_view(world)
129-
.get::<&NetworkStreamRef>(|stream| {
130-
compose
131-
.unicast(&set_content_packet, *stream, SystemId(8), world)
132-
.unwrap();
133-
});
127+
player.entity_view(world).get::<&ConnectionId>(|stream| {
128+
compose
129+
.unicast(&set_content_packet, *stream, SystemId(8), world)
130+
.unwrap();
131+
});
134132
});
135133
}
136134

@@ -142,13 +140,11 @@ impl Gui {
142140
};
143141

144142
world.get::<&Compose>(|compose| {
145-
player
146-
.entity_view(world)
147-
.get::<&NetworkStreamRef>(|stream| {
148-
compose
149-
.unicast(&open_screen_packet, *stream, SystemId(8), world)
150-
.unwrap();
151-
});
143+
player.entity_view(world).get::<&ConnectionId>(|stream| {
144+
compose
145+
.unicast(&open_screen_packet, *stream, SystemId(8), world)
146+
.unwrap();
147+
});
152148
});
153149

154150
self.draw(world, player);

crates/hyperion-permission/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use flecs_ecs::{
77
prelude::{Module, flecs},
88
};
99
use hyperion::{
10-
net::{Compose, NetworkStreamRef},
10+
net::{Compose, ConnectionId},
1111
simulation::{Player, Uuid, command::get_command_packet},
1212
storage::LocalDb,
1313
system_registry::SystemId,
@@ -72,7 +72,7 @@ impl Module for PermissionModule {
7272

7373
let cmd_pkt = get_command_packet(&world, root_command, Some(*entity));
7474

75-
entity.get::<&NetworkStreamRef>(|stream| {
75+
entity.get::<&ConnectionId>(|stream| {
7676
world.get::<&Compose>(|compose| {
7777
compose
7878
.unicast(&cmd_pkt, *stream, SystemId(999), &world)

crates/hyperion/src/egress/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sync_chunks::SyncChunksModule;
1919
use sync_entity_state::EntityStateSyncModule;
2020

2121
use crate::{
22-
net::NetworkStreamRef,
22+
net::ConnectionId,
2323
simulation::{ChunkPosition, blocks::Blocks},
2424
system_registry::SystemId,
2525
};
@@ -86,15 +86,15 @@ impl Module for EgressModule {
8686
sequence: VarInt(to_confirm.sequence),
8787
};
8888

89-
entity.get::<&NetworkStreamRef>(|stream| {
89+
entity.get::<&ConnectionId>(|stream| {
9090
if let Err(e) = compose.unicast(&pkt, *stream, SystemId(99), &world) {
9191
error!("failed to send player action response: {e}");
9292
}
9393
});
9494
}
9595
});
9696

97-
let player_location_query = world.new_query::<(&NetworkStreamRef, &ChunkPosition)>();
97+
let player_location_query = world.new_query::<(&ConnectionId, &ChunkPosition)>();
9898

9999
system!(
100100
"egress",

crates/hyperion/src/egress/player_join/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
config::Config,
3030
egress::metadata::show_all,
3131
ingress::PendingRemove,
32-
net::{Compose, DataBundle, NetworkStreamRef},
32+
net::{Compose, ConnectionId, DataBundle},
3333
simulation::{
3434
Comms, Name, Position, Uuid, Yaw,
3535
command::{Command, ROOT_COMMAND, get_command_packet},
@@ -51,7 +51,7 @@ pub fn player_join_world(
5151
compose: &Compose,
5252
uuid: uuid::Uuid,
5353
name: &str,
54-
io: NetworkStreamRef,
54+
io: ConnectionId,
5555
position: &Position,
5656
yaw: &Yaw,
5757
pitch: &Pitch,
@@ -597,7 +597,7 @@ impl Module for PlayerJoinModule {
597597

598598
let entity = world.entity_from_id(entity);
599599

600-
entity.get::<(&Uuid, &Name, &Position, &Yaw, &Pitch, &NetworkStreamRef)>(
600+
entity.get::<(&Uuid, &Name, &Position, &Yaw, &Pitch, &ConnectionId)>(
601601
|(uuid, name, position, yaw, pitch, &stream_id)| {
602602
let query = &query;
603603
let query = &query.0;

crates/hyperion/src/egress/sync_chunks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use valence_protocol::{
1111

1212
use crate::{
1313
config::Config,
14-
net::{Compose, DataBundle, NetworkStreamRef},
14+
net::{Compose, ConnectionId, DataBundle},
1515
simulation::{
1616
ChunkPosition, PacketState, Position,
1717
blocks::{Blocks, GetChunk},
@@ -43,7 +43,7 @@ impl Module for SyncChunksModule {
4343
&Compose($),
4444
&mut ChunkPosition,
4545
&Position,
46-
&NetworkStreamRef,
46+
&ConnectionId,
4747
&mut ChunkSendQueue,
4848
)
4949
.with_enum(PacketState::Play)
@@ -164,7 +164,7 @@ impl Module for SyncChunksModule {
164164

165165
let system_id = SEND_FULL_LOADED_CHUNKS;
166166

167-
system!("send_full_loaded_chunks", world, &Blocks($), &Compose($), &NetworkStreamRef, &mut ChunkSendQueue)
167+
system!("send_full_loaded_chunks", world, &Blocks($), &Compose($), &ConnectionId, &mut ChunkSendQueue)
168168
.with_enum(PacketState::Play)
169169
.kind::<flecs::pipeline::OnUpdate>()
170170
.multi_threaded()

crates/hyperion/src/egress/sync_entity_state.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use valence_protocol::{
1313

1414
use crate::{
1515
Prev,
16-
net::{Compose, NetworkStreamRef},
16+
net::{Compose, ConnectionId},
1717
simulation::{
1818
Pitch, Position, Velocity, Xp, Yaw, animation::ActiveAnimation, metadata::MetadataChanges,
1919
},
@@ -30,10 +30,10 @@ impl Module for EntityStateSyncModule {
3030

3131
world
3232
.system_named::<(
33-
&Compose, // (0)
34-
&NetworkStreamRef, // (1)
35-
&mut (Prev, Xp), // (2)
36-
&mut Xp, // (3)
33+
&Compose, // (0)
34+
&ConnectionId, // (1)
35+
&mut (Prev, Xp), // (2)
36+
&mut Xp, // (3)
3737
)>("entity_xp_sync")
3838
.term_at(0u32)
3939
.singleton()
@@ -54,7 +54,7 @@ impl Module for EntityStateSyncModule {
5454
let compose = table.field_unchecked::<Compose>(0);
5555
let compose = compose.first().unwrap();
5656

57-
let net = table.field_unchecked::<NetworkStreamRef>(1);
57+
let net = table.field_unchecked::<ConnectionId>(1);
5858
let net = net.get(..).unwrap();
5959

6060
let mut prev_xp = table.field_unchecked::<Xp>(2);
@@ -129,7 +129,7 @@ impl Module for EntityStateSyncModule {
129129
&mut Position,
130130
&Yaw,
131131
&Pitch,
132-
&NetworkStreamRef,
132+
&ConnectionId,
133133
&mut ActiveAnimation,
134134
&mut PlayerInventory,
135135
&mut Velocity,

crates/hyperion/src/ingress/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
Prev, Shutdown,
2020
egress::sync_chunks::ChunkSendQueue,
2121
net::{
22-
Compose, MINECRAFT_VERSION, NetworkStreamRef, PROTOCOL_VERSION, PacketDecoder,
22+
Compose, ConnectionId, MINECRAFT_VERSION, PROTOCOL_VERSION, PacketDecoder,
2323
decoder::BorrowedPacketFrame, proxy::ReceiveState,
2424
},
2525
runtime::AsyncRuntime,
@@ -86,7 +86,7 @@ fn process_login(
8686
skins_collection: SkinHandler,
8787
mojang: MojangClient,
8888
packet: &BorrowedPacketFrame<'_>,
89-
stream_id: NetworkStreamRef,
89+
stream_id: ConnectionId,
9090
compose: &Compose,
9191
entity: &EntityView<'_>,
9292
system_id: SystemId,
@@ -195,7 +195,7 @@ fn process_status(
195195
login_state: &mut PacketState,
196196
system_id: SystemId,
197197
packet: &BorrowedPacketFrame<'_>,
198-
packets: NetworkStreamRef,
198+
packets: ConnectionId,
199199
compose: &Compose,
200200
world: &World,
201201
) -> anyhow::Result<()> {
@@ -319,7 +319,7 @@ impl Module for IngressModule {
319319
info!("player_connect");
320320
let view = world
321321
.entity()
322-
.set(NetworkStreamRef::new(connect))
322+
.set(ConnectionId::new(connect))
323323
.set(hyperion_inventory::PlayerInventory::default())
324324
.set(ConfirmBlockSequences::default())
325325
.set(PacketState::Handshake)
@@ -409,7 +409,7 @@ impl Module for IngressModule {
409409
world,
410410
&Uuid,
411411
&Compose($),
412-
&NetworkStreamRef,
412+
&ConnectionId,
413413
&PendingRemove,
414414
)
415415
.kind::<flecs::pipeline::PostLoad>()
@@ -473,7 +473,7 @@ impl Module for IngressModule {
473473
&GlobalEventHandlers($),
474474
&mut PacketDecoder,
475475
&mut PacketState,
476-
&NetworkStreamRef,
476+
&ConnectionId,
477477
?&mut Pose,
478478
&Events($),
479479
&EntitySize,

crates/hyperion/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub use valence_ident;
7676

7777
use crate::{
7878
ingress::PendingRemove,
79-
net::{NetworkStreamRef, PacketDecoder, proxy::ReceiveState},
79+
net::{ConnectionId, PacketDecoder, proxy::ReceiveState},
8080
runtime::Tasks,
8181
simulation::{EgressComm, EntitySize, IgnMap, PacketState, Player},
8282
util::mojang::ApiProvider,
@@ -262,7 +262,7 @@ impl Hyperion {
262262

263263
world.component::<PacketState>();
264264

265-
world.component::<NetworkStreamRef>();
265+
world.component::<ConnectionId>();
266266
world.component::<ReceiveState>();
267267
world.component::<Compose>();
268268
world.component::<CraftingRegistry>();

0 commit comments

Comments
 (0)