-
-
Notifications
You must be signed in to change notification settings - Fork 164
Add entity equipment support #254
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
Open
mymatsubara
wants to merge
5
commits into
valence-rs:main
Choose a base branch
from
mymatsubara:equipment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dbfcc9c
Add entity equipment support
mymatsubara 753c04e
Fixes from PR comments
mymatsubara 4541279
Update crates/valence/examples/random_equipment.rs
mymatsubara f25a089
Add set equipment packet test
mymatsubara b593c59
Merge branch 'equipment' of https://github.com/mymatsubara/valence in…
mymatsubara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| use bevy_app::CoreStage; | ||
| use rand::Rng; | ||
| use valence::client::despawn_disconnected_clients; | ||
| use valence::client::event::default_event_handler; | ||
| use valence::equipment::{EquipmentSlot, Equipments}; | ||
| use valence::prelude::*; | ||
|
|
||
| const BOARD_MIN_X: i32 = -30; | ||
| const BOARD_MAX_X: i32 = 30; | ||
| const BOARD_MIN_Z: i32 = -30; | ||
| const BOARD_MAX_Z: i32 = 30; | ||
| const BOARD_Y: i32 = 64; | ||
|
|
||
| const SPAWN_POS: DVec3 = DVec3::new( | ||
| (BOARD_MIN_X + BOARD_MAX_X) as f64 / 2.0, | ||
| BOARD_Y as f64 + 1.0, | ||
| (BOARD_MIN_Z + BOARD_MAX_Z) as f64 / 2.0, | ||
| ); | ||
|
|
||
| pub fn main() { | ||
| tracing_subscriber::fmt().init(); | ||
|
|
||
| App::new() | ||
| .add_plugin( | ||
| ServerPlugin::new(()) | ||
| .with_biomes(vec![Biome { | ||
| grass_color: Some(0x00ff00), | ||
| ..Default::default() | ||
| }]) | ||
| .with_connection_mode(ConnectionMode::Offline), | ||
| ) | ||
| .add_system_to_stage(EventLoop, default_event_handler) | ||
| .add_system_set(PlayerList::default_system_set()) | ||
| .add_startup_system(setup) | ||
| .add_system(init_clients) | ||
| .add_system(despawn_disconnected_clients) | ||
| .add_system_to_stage(CoreStage::Update, randomize_equipment) | ||
|
mymatsubara marked this conversation as resolved.
Outdated
|
||
| .run(); | ||
| } | ||
|
|
||
| fn setup(world: &mut World) { | ||
| let mut instance = world | ||
| .resource::<Server>() | ||
| .new_instance(DimensionId::default()); | ||
|
|
||
| for z in -10..10 { | ||
| for x in -10..10 { | ||
| instance.insert_chunk([x, z], Chunk::default()); | ||
| } | ||
| } | ||
|
|
||
| for z in BOARD_MIN_Z..=BOARD_MAX_Z { | ||
| for x in BOARD_MIN_X..=BOARD_MAX_X { | ||
| instance.set_block_state([x, BOARD_Y, z], BlockState::DIRT); | ||
| } | ||
| } | ||
|
|
||
| let instance = world.spawn(instance); | ||
| let instance_entity = instance.id(); | ||
|
|
||
| let mut equipments = Equipments::default(); | ||
| equipments.set( | ||
| ItemStack::new(ItemKind::IronBoots, 1, None), | ||
| EquipmentSlot::Boots, | ||
| ); | ||
|
|
||
| // Spawn armor stand | ||
| let mut armor_stand = world.spawn(( | ||
| McEntity::new(EntityKind::ArmorStand, instance_entity), | ||
| equipments, | ||
| )); | ||
|
|
||
| if let Some(mut armor_stand) = armor_stand.get_mut::<McEntity>() { | ||
| let position = [SPAWN_POS.x, SPAWN_POS.y, SPAWN_POS.z + 3.0]; | ||
| armor_stand.set_position(position); | ||
| armor_stand.set_yaw(180.0); | ||
| } | ||
| } | ||
|
|
||
| fn init_clients( | ||
| mut clients: Query<(&mut Client, Entity), Added<Client>>, | ||
| instances: Query<Entity, With<Instance>>, | ||
| mut commands: Commands, | ||
| ) { | ||
| let instance = instances.single(); | ||
|
|
||
| for (mut client, entity) in &mut clients { | ||
| client.set_position(SPAWN_POS); | ||
| client.set_instance(instances.single()); | ||
| client.set_game_mode(GameMode::Creative); | ||
|
|
||
| let equipments = Equipments::default(); | ||
|
|
||
| commands.entity(entity).insert(( | ||
| equipments, | ||
| McEntity::with_uuid(EntityKind::Player, instance, client.uuid()), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| fn randomize_equipment(mut query: Query<&mut Equipments>, server: Res<Server>) { | ||
| let ticks = server.current_tick(); | ||
| if ticks % server.tps() != 0 { | ||
| return; | ||
| } | ||
|
|
||
| for mut equips in &mut query { | ||
| equips.clear(); | ||
|
|
||
| let (slot, item_kind) = match rand::thread_rng().gen_range(0..=5) { | ||
| 0 => (EquipmentSlot::MainHand, ItemKind::DiamondSword), | ||
| 1 => (EquipmentSlot::OffHand, ItemKind::Torch), | ||
| 2 => (EquipmentSlot::Boots, ItemKind::IronBoots), | ||
| 3 => (EquipmentSlot::Leggings, ItemKind::DiamondLeggings), | ||
| 4 => (EquipmentSlot::Chestplate, ItemKind::ChainmailChestplate), | ||
| 5 => (EquipmentSlot::Helmet, ItemKind::LeatherHelmet), | ||
| _ => (EquipmentSlot::Boots, ItemKind::IronBoots), | ||
| }; | ||
|
|
||
| let item = ItemStack::new(item_kind, 1, None); | ||
|
|
||
| equips.set(item, slot); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.