Skip to content

Commit

Permalink
fix(player): move gamemode ability logic to Player::set_gamemode to f…
Browse files Browse the repository at this point in the history
…ix creative flight after death
  • Loading branch information
guffelman committed Jan 26, 2025
1 parent 69cb3bb commit 003e579
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
45 changes: 26 additions & 19 deletions pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,25 +657,7 @@ impl Player {
{
// use another scope so we instantly unlock abilities
let mut abilities = self.abilities.lock().await;
match gamemode {
GameMode::Undefined | GameMode::Survival | GameMode::Adventure => {
abilities.flying = false;
abilities.allow_flying = false;
abilities.creative = false;
abilities.invulnerable = false;
}
GameMode::Creative => {
abilities.allow_flying = true;
abilities.creative = true;
abilities.invulnerable = true;
}
GameMode::Spectator => {
abilities.flying = true;
abilities.allow_flying = true;
abilities.creative = false;
abilities.invulnerable = true;
}
}
abilities.set_for_gamemode(gamemode);
}
self.send_abilities_update().await;
self.living_entity
Expand Down Expand Up @@ -957,6 +939,31 @@ impl Default for Abilities {
}
}

impl Abilities {
pub fn set_for_gamemode(&mut self, gamemode: GameMode) {
match gamemode {
GameMode::Creative => {
self.flying = false; // Start not flying
self.allow_flying = true;
self.creative = true;
self.invulnerable = true;
}
GameMode::Spectator => {
self.flying = true;
self.allow_flying = true;
self.creative = false;
self.invulnerable = true;
}
GameMode::Survival | GameMode::Adventure | GameMode::Undefined => {
self.flying = false;
self.allow_flying = false;
self.creative = false;
self.invulnerable = false;
}
}
}
}

/// Represents the player's dominant hand.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
Expand Down
20 changes: 11 additions & 9 deletions pumpkin/src/net/packet/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@ use pumpkin_protocol::client::play::{CSetContainerSlot, CSetHeldItem};
use pumpkin_protocol::codec::slot::Slot;
use pumpkin_protocol::codec::var_int::VarInt;
use pumpkin_protocol::server::play::SCookieResponse as SPCookieResponse;
use pumpkin_protocol::{
client::play::CCommandSuggestions,
server::play::{SCloseContainer, SCommandSuggestion, SKeepAlive, SSetPlayerGround, SUseItem},
};
use pumpkin_protocol::{
client::play::{
Animation, CAcknowledgeBlockChange, CEntityAnimation, CHeadRot, CPingResponse,
Animation, CAcknowledgeBlockChange, CCommandSuggestions, CEntityAnimation, CHeadRot, CPingResponse,
CPlayerChatMessage, CUpdateEntityPos, CUpdateEntityPosRot, CUpdateEntityRot, FilterType,
},
server::play::{
Action, ActionType, SChatCommand, SChatMessage, SClientCommand, SClientInformationPlay,
SConfirmTeleport, SInteract, SPickItemFromBlock, SPickItemFromEntity, SPlayPingRequest,
SPlayerAbilities, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn, Status,
SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItem, SUseItemOn, Status,
SSetPlayerGround, SKeepAlive, SCloseContainer, SCommandSuggestion,
},
};
use pumpkin_util::math::position::BlockPos;
Expand Down Expand Up @@ -652,12 +649,17 @@ impl Player {

pub async fn handle_client_status(self: &Arc<Self>, client_status: SClientCommand) {
match client_status.action_id.0 {
0 => {
0 => { // Perform Respawn
if self.living_entity.health.load() > 0.0 {
return;
}
self.world().respawn_player(self, false).await;
// TODO: hardcore set spectator
self.world().respawn_player(&self.clone(), false).await;

// Restore abilities based on gamemode after respawn
let mut abilities = self.abilities.lock().await;
abilities.set_for_gamemode(self.gamemode.load());
drop(abilities);
self.send_abilities_update().await;
}
1 => {
// request stats
Expand Down

0 comments on commit 003e579

Please sign in to comment.