-
Notifications
You must be signed in to change notification settings - Fork 144
Add an ability to change gamemode from plugins #521
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1fed01d
Add GamemodeEvent
Iaiao 2b00410
Ignore updates to the same gamemode
Iaiao d31fa68
Dispatch GamemodeEvent on join
Iaiao 43c44ae
Simplify abilities change logic
Iaiao 5ea86eb
format
Iaiao 46bdd89
Rewrite ChangeGameState packet
Iaiao 26d63b4
Merge main
Iaiao 8d1d758
Rename ability change events
Iaiao 3ccbecc
Remove if statement
Iaiao 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use base::Gamemode; | ||
| use common::Game; | ||
| use ecs::{SysResult, SystemExecutor}; | ||
| use quill_common::components::{ | ||
| CanBuild, CanCreativeFly, CreativeFlying, CreativeFlyingSpeed, Instabreak, Invulnerable, | ||
| PreviousGamemode, WalkSpeed, | ||
| }; | ||
| use quill_common::events::{ | ||
| BuildingAbilityChangeEvent, CreativeFlyingEvent, FlyingAbilityChangeEvent, GamemodeEvent, | ||
| InstabreakChangeEvent, InvulnerabilityChangeEvent, | ||
| }; | ||
|
|
||
| use crate::{ClientId, Server}; | ||
| use base::anvil::player::PlayerAbilities; | ||
|
|
||
| pub fn register(systems: &mut SystemExecutor<Game>) { | ||
| systems.group::<Server>().add_system(gamemode_change); | ||
| } | ||
|
|
||
| fn gamemode_change(game: &mut Game, server: &mut Server) -> SysResult { | ||
| let mut may_fly_changes = HashMap::new(); | ||
|
Iaiao marked this conversation as resolved.
Outdated
|
||
| let mut fly_changes = HashMap::new(); | ||
| let mut instabreak_changes = HashMap::new(); | ||
| let mut build_changes = HashMap::new(); | ||
| let mut invulnerability_changes = HashMap::new(); | ||
| for ( | ||
| entity, | ||
| ( | ||
| event, | ||
| &client_id, | ||
| &walk_speed, | ||
| &fly_speed, | ||
| mut may_fly, | ||
| mut is_flying, | ||
| mut instabreak, | ||
| mut may_build, | ||
| mut invulnerable, | ||
| gamemode, | ||
| prev_gamemode, | ||
| ), | ||
| ) in game | ||
| .ecs | ||
| .query::<( | ||
| &GamemodeEvent, | ||
|
Defman marked this conversation as resolved.
|
||
| &ClientId, | ||
| &WalkSpeed, | ||
| &CreativeFlyingSpeed, | ||
| &mut CanCreativeFly, | ||
| &mut CreativeFlying, | ||
| &mut Instabreak, | ||
| &mut CanBuild, | ||
| &mut Invulnerable, | ||
| &mut Gamemode, | ||
| &mut PreviousGamemode, | ||
| )>() | ||
| .iter() | ||
| { | ||
| if **event == *gamemode { | ||
| continue; | ||
| } | ||
| *prev_gamemode = PreviousGamemode(Some(*gamemode)); | ||
| *gamemode = **event; | ||
| match gamemode { | ||
| Gamemode::Creative => { | ||
| if !**instabreak { | ||
| instabreak_changes.insert(entity, true); | ||
| instabreak.0 = true; | ||
| } | ||
| if !**may_fly { | ||
| may_fly_changes.insert(entity, true); | ||
| may_fly.0 = true; | ||
| } | ||
| if !**may_build { | ||
| build_changes.insert(entity, true); | ||
| may_build.0 = true; | ||
| } | ||
| if !**invulnerable { | ||
| invulnerability_changes.insert(entity, true); | ||
| invulnerable.0 = true; | ||
| } | ||
| } | ||
| Gamemode::Spectator => { | ||
| if !**is_flying { | ||
| fly_changes.insert(entity, true); | ||
| is_flying.0 = true; | ||
| } | ||
| if **instabreak { | ||
| instabreak_changes.insert(entity, false); | ||
| instabreak.0 = false; | ||
| } | ||
| if !**may_fly { | ||
| may_fly_changes.insert(entity, true); | ||
| may_fly.0 = true; | ||
| } | ||
| if **may_build { | ||
| build_changes.insert(entity, false); | ||
| may_build.0 = false; | ||
| } | ||
| if !**invulnerable { | ||
| invulnerability_changes.insert(entity, true); | ||
| invulnerable.0 = true; | ||
| } | ||
| } | ||
| Gamemode::Survival => { | ||
| if **is_flying { | ||
| fly_changes.insert(entity, false); | ||
| is_flying.0 = false; | ||
| } | ||
| if **instabreak { | ||
| instabreak_changes.insert(entity, false); | ||
| instabreak.0 = false; | ||
| } | ||
| if **may_fly { | ||
| may_fly_changes.insert(entity, false); | ||
| may_fly.0 = false; | ||
| } | ||
| if !**may_build { | ||
| build_changes.insert(entity, true); | ||
| may_build.0 = true; | ||
| } | ||
| if **invulnerable { | ||
| invulnerability_changes.insert(entity, false); | ||
| invulnerable.0 = false; | ||
| } | ||
| } | ||
| Gamemode::Adventure => { | ||
| if **is_flying { | ||
| fly_changes.insert(entity, false); | ||
| is_flying.0 = false; | ||
| } | ||
| if **instabreak { | ||
| instabreak_changes.insert(entity, false); | ||
| instabreak.0 = false; | ||
| } | ||
| if **may_fly { | ||
| may_fly_changes.insert(entity, false); | ||
| may_fly.0 = false; | ||
| } | ||
| if **may_build { | ||
| build_changes.insert(entity, false); | ||
| may_build.0 = false; | ||
| } | ||
| if **invulnerable { | ||
| invulnerability_changes.insert(entity, false); | ||
| invulnerable.0 = false; | ||
| } | ||
| } | ||
| } | ||
| server | ||
| .clients | ||
| .get(client_id) | ||
| .unwrap() | ||
| .change_gamemode(**event); | ||
| server | ||
| .clients | ||
| .get(client_id) | ||
| .unwrap() | ||
| .send_abilities(&PlayerAbilities { | ||
| walk_speed, | ||
| fly_speed, | ||
| may_fly: *may_fly, | ||
| is_flying: *is_flying, | ||
| may_build: *may_build, | ||
| instabreak: *instabreak, | ||
| invulnerable: *invulnerable, | ||
| }); | ||
| } | ||
| for (entity, flying) in fly_changes { | ||
|
Defman marked this conversation as resolved.
|
||
| if flying { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This if statement is redundant, just create |
||
| game.ecs | ||
| .insert_entity_event(entity, CreativeFlyingEvent::new(true)) | ||
| .unwrap(); | ||
| } else { | ||
| game.ecs | ||
| .insert_entity_event(entity, CreativeFlyingEvent::new(false)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| for (entity, instabreak) in instabreak_changes { | ||
| if instabreak { | ||
| game.ecs | ||
| .insert_entity_event(entity, InstabreakChangeEvent(true)) | ||
| .unwrap(); | ||
| } else { | ||
| game.ecs | ||
| .insert_entity_event(entity, InstabreakChangeEvent(false)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| for (entity, may_fly) in may_fly_changes { | ||
| if may_fly { | ||
| game.ecs | ||
| .insert_entity_event(entity, FlyingAbilityChangeEvent(true)) | ||
| .unwrap(); | ||
| } else { | ||
| game.ecs | ||
| .insert_entity_event(entity, FlyingAbilityChangeEvent(false)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| for (entity, build) in build_changes { | ||
| if build { | ||
| game.ecs | ||
| .insert_entity_event(entity, BuildingAbilityChangeEvent(true)) | ||
| .unwrap(); | ||
| } else { | ||
| game.ecs | ||
| .insert_entity_event(entity, BuildingAbilityChangeEvent(false)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| for (entity, invulnerable) in invulnerability_changes { | ||
| if invulnerable { | ||
| game.ecs | ||
| .insert_entity_event(entity, InvulnerabilityChangeEvent(true)) | ||
| .unwrap(); | ||
| } else { | ||
| game.ecs | ||
| .insert_entity_event(entity, InvulnerabilityChangeEvent(false)) | ||
| .unwrap(); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; | ||
| pub use change::{ | ||
| BuildingAbilityChangeEvent, CreativeFlyingEvent, FlyingAbilityChangeEvent, GamemodeEvent, | ||
| InstabreakChangeEvent, InvulnerabilityChangeEvent, SneakEvent, SprintEvent, | ||
| }; | ||
| pub use interact_entity::InteractEntityEvent; | ||
|
|
||
| mod block_interact; | ||
| mod change; | ||
| mod interact_entity; | ||
|
|
||
| pub use block_interact::{BlockInteractEvent, BlockPlacementEvent}; | ||
| pub use change::{CreativeFlyingEvent, SneakEvent, SprintEvent}; | ||
| pub use interact_entity::InteractEntityEvent; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the protocol to take Gamemode and implement write on game mode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ChangeGameState requires it to be f64, so I did it in a different way.