Skip to content

feat: create vanilla-behaviors crate #858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
27 changes: 18 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ members = [
'crates/hyperion-proto',
'crates/hyperion-proxy',
'crates/hyperion-rank-tree',
'crates/hyperion-respawn',
'crates/hyperion-scheduled',
'crates/hyperion-stats',
'crates/hyperion-text',
'crates/hyperion-utils',
'crates/simd-utils',
'crates/system-order',
'crates/vanilla-behaviors',
'events/tag',
'tools/packet-inspector',
'tools/rust-mc-bot',
Expand Down Expand Up @@ -203,6 +203,9 @@ path = 'crates/hyperion-text'
[workspace.dependencies.hyperion-utils]
path = 'crates/hyperion-utils'

[workspace.dependencies.vanilla-behaviors]
path = 'crates/vanilla-behaviors'

[workspace.dependencies.indexmap]
features = ['rayon']
version = '2.7.1'
Expand All @@ -219,9 +222,6 @@ version = '0.3.6'
features = ['rustls-tls', 'stream']
version = '0.12.12'

[workspace.dependencies.hyperion-respawn]
path = 'crates/hyperion-respawn'

[workspace.dependencies.roaring]
features = ['simd']
version = '0.10.10'
Expand Down
14 changes: 0 additions & 14 deletions crates/hyperion-respawn/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion crates/hyperion-respawn/README.md

This file was deleted.

1 change: 1 addition & 0 deletions crates/hyperion-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use flecs_ecs::{

mod cached_save;
mod lifetime;
pub mod structures;
pub use cached_save::cached_save;
pub use lifetime::*;

Expand Down
79 changes: 79 additions & 0 deletions crates/hyperion-utils/src/structures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use valence_protocol::math::DVec3;

// /!\ Minecraft version dependent
pub enum DamageType {
Arrow,
BadRespawnPoint,
Cactus,
Cramming,
DragonBreath,
DryOut,
Drown,
Explosion,
Fall,
FallingAnvil,
FallingBlock,
FallingStalactite,
Fireball,
Fireworks,
FlyIntoWall,
Freeze,
Generic,
GenericKill,
HotFloor,
InFire,
InWall,
IndirectMagic,
Lava,
LightningBolt,
Magic,
MobAttack,
MobAttackNoAggro,
MobProjectile,
OnFire,
OutOfWorld,
OutsideBorder,
PlayerAttack,
PlayerExplosion,
SonicBoom,
Stalagmite,
Sting,
Starve,
SweetBerryBush,
Thorns,
Thrown,
Trident,
UnattributedFireball,
Wither,
WitherSkull,
}

pub struct DamageCause {
pub damage_type: DamageType,
pub position: Option<DVec3>,
pub source_entity: i32,
pub direct_source: i32,
}

impl DamageCause {
#[must_use]
pub const fn new(damage_type: DamageType) -> Self {
Self {
damage_type,
position: Option::None,
source_entity: -1,
direct_source: -1,
}
}

pub const fn with_position(&mut self, position: DVec3) -> &mut Self {
self.position = Option::Some(position);
self
}

pub const fn with_entities(&mut self, source: i32, direct_source: i32) -> &mut Self {
self.source_entity = source;
self.direct_source = direct_source;
self
}
}
52 changes: 26 additions & 26 deletions crates/hyperion/src/egress/player_join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ use std::{borrow::Cow, collections::BTreeSet, ops::Index};

use anyhow::Context;
use flecs_ecs::prelude::*;
use glam::DVec3;
use hyperion_crafting::{Action, CraftingRegistry, RecipeBookState};
use hyperion_utils::EntityExt;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tracing::{info, instrument};
use valence_protocol::{
ByteAngle, GameMode, Ident, PacketEncoder, RawBytes, VarInt, Velocity,
game_mode::OptGameMode,
ident,
ByteAngle, Ident, PacketEncoder, RawBytes, VarInt, Velocity, ident,
packets::play::{
self, GameJoinS2c,
player_position_look_s2c::PlayerPositionLookFlags,
Expand All @@ -21,7 +18,7 @@ use valence_registry::{BiomeRegistry, RegistryCodec};
use valence_server::entity::EntityKind;
use valence_text::IntoText;

use crate::simulation::{MovementTracking, PacketState, Pitch};
use crate::simulation::{Gamemode, PacketState, Pitch};

mod list;
pub use list::*;
Expand All @@ -46,6 +43,7 @@ use crate::{
reason = "todo: we should refactor at some point"
)]
#[instrument(skip_all, fields(name = name))]
#[allow(clippy::type_complexity)]
pub fn player_join_world(
entity: &EntityView<'_>,
compose: &Compose,
Expand All @@ -67,26 +65,18 @@ pub fn player_join_world(
&Pitch,
&PlayerSkin,
&EntityFlags,
&Gamemode,
)>,
crafting_registry: &CraftingRegistry,
config: &Config,
gamemode: &Gamemode,
) -> anyhow::Result<()> {
static CACHED_DATA: once_cell::sync::OnceCell<bytes::Bytes> = once_cell::sync::OnceCell::new();

let mut bundle = DataBundle::new(compose, system);

let id = entity.minecraft_id();

entity.set(MovementTracking {
received_movement_packets: 0,
last_tick_flying: false,
last_tick_position: **position,
fall_start_y: position.y,
server_velocity: DVec3::ZERO,
sprinting: false,
was_on_ground: false,
});

let registry_codec = registry_codec_raw();
let codec = RegistryCodec::default();

Expand All @@ -111,11 +101,11 @@ pub fn player_join_world(
enable_respawn_screen: false,
dimension_name: dimension_name.into(),
hashed_seed: 0,
game_mode: GameMode::Survival,
game_mode: gamemode.current,
is_flat: false,
last_death_location: None,
portal_cooldown: 60.into(),
previous_game_mode: OptGameMode(Some(GameMode::Survival)),
previous_game_mode: gamemode.previous,
dimension_type_name: ident!("minecraft:overworld").into(),
is_debug: false,
};
Expand Down Expand Up @@ -194,7 +184,7 @@ pub fn player_join_world(
let _enter = scope.enter();
query
.iter_stage(world)
.each(|(uuid, name, _, _, _, _skin, _)| {
.each(|(uuid, name, _, _, _, _skin, _, gamemode)| {
// todo: in future, do not clone

let entry = PlayerListEntry {
Expand All @@ -205,7 +195,7 @@ pub fn player_join_world(
chat_data: None,
listed: true,
ping: 20,
game_mode: GameMode::Creative,
game_mode: gamemode.current,
display_name: Some(name.to_string().into_cow_text()),
};

Expand Down Expand Up @@ -240,9 +230,8 @@ pub fn player_join_world(

let mut metadata = MetadataChanges::default();

query
.iter_stage(world)
.each_iter(|it, idx, (uuid, _, position, yaw, pitch, _, flags)| {
query.iter_stage(world).each_iter(
|it, idx, (uuid, _, position, yaw, pitch, _, flags, _)| {
let mut result = || {
let query_entity = it.entity(idx);

Expand Down Expand Up @@ -275,7 +264,8 @@ pub fn player_join_world(
if let Err(e) = result() {
query_errors.push(e);
}
});
},
);

if !query_errors.is_empty() {
return Err(anyhow::anyhow!(
Expand Down Expand Up @@ -305,7 +295,7 @@ pub fn player_join_world(
chat_data: None,
listed: true,
ping: 20,
game_mode: GameMode::Survival,
game_mode: gamemode.current,
display_name: Some(name.to_string().into_cow_text()),
}];

Expand Down Expand Up @@ -504,6 +494,7 @@ impl Module for PlayerJoinModule {
&Pitch,
&PlayerSkin,
&EntityFlags,
&Gamemode,
)>();

let query = SendableQuery(query);
Expand Down Expand Up @@ -574,8 +565,16 @@ impl Module for PlayerJoinModule {

let entity = world.entity_from_id(entity);

entity.get::<(&Uuid, &Name, &Position, &Yaw, &Pitch, &ConnectionId)>(
|(uuid, name, position, yaw, pitch, &stream_id)| {
entity.try_get::<(
&Uuid,
&Name,
&Position,
&Yaw,
&Pitch,
&ConnectionId,
&Gamemode,
)>(
|(uuid, name, position, yaw, pitch, &stream_id, gamemode)| {
let query = &query;
let query = &query.0;
entity.set_name(name);
Expand All @@ -597,6 +596,7 @@ impl Module for PlayerJoinModule {
query,
crafting_registry,
config,
gamemode,
) {
entity.set(PendingRemove::new(e.to_string()));
}
Expand Down
Loading
Loading