Skip to content

Commit b2e1a48

Browse files
committed
implemented Gamemode
1 parent e8029d2 commit b2e1a48

File tree

9 files changed

+71
-22
lines changed

9 files changed

+71
-22
lines changed

server/src/constants/gamemode.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::network::packets::packet_serialize::PacketSerializable;
2+
use bytes::BytesMut;
3+
4+
#[repr(i8)]
5+
#[derive(Copy, Clone)]
6+
pub enum Gamemode {
7+
Survival,
8+
Creative,
9+
}
10+
11+
impl PacketSerializable for Gamemode {
12+
fn write_size(&self) -> usize {
13+
(*self as i8).write_size()
14+
}
15+
fn write(&self, buf: &mut BytesMut) {
16+
(*self as i8).write(buf)
17+
}
18+
}

server/src/constants/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ pub mod particle;
22
pub mod potions;
33
pub mod sounds;
44
pub mod entity_variants;
5+
pub mod gamemode;
56

67
pub use entity_variants::EntityVariant;
78
pub use entity_variants::ObjectVariant;
9+
pub use gamemode::Gamemode;
810
pub use particle::Particle;
911
pub use potions::PotionEffect;
1012
pub use sounds::Sound;

server/src/network/protocol/play/clientbound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::constants::particle::Particle;
22
use crate::constants::potions::PotionEffect;
3-
use crate::constants::{EntityVariant, ObjectVariant, Sound};
3+
use crate::constants::{EntityVariant, Gamemode, ObjectVariant, Sound};
44
use crate::entity::entity_metadata::{EntityMetadata, PlayerMetadata};
55
use crate::inventory::item_stack::ItemStack;
66
use crate::network::binary::var_int::{var_int_size, write_var_int, VarInt};
@@ -100,7 +100,7 @@ register_packets! {
100100
packet_serializable! {
101101
pub struct JoinGame<'a> {
102102
pub entity_id: i32, // not VarInt,
103-
pub gamemode: u8,
103+
pub gamemode: Gamemode,
104104
pub dimension: u8,
105105
pub difficulty: u8,
106106
pub max_players: u8,

server/src/network/protocol/play/serverbound.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ register_serverbound_packets! {
2929
CloseWindow = 0x0d;
3030
ClickWindow = 0x0e;
3131
ConfirmTransaction = 0x0f;
32-
// CreativeInventoryAction = 0x10;
32+
CreativeInventoryAction = 0x10;
3333
// EnchantItem = 0x11;
3434
// SetSign = 0x12;
3535
// ClientAbilities = 0x13;
@@ -239,6 +239,14 @@ packet_deserializable! {
239239
}
240240
}
241241

242+
packet_deserializable! {
243+
#[derive(Debug)]
244+
pub struct CreativeInventoryAction {
245+
pub slot_id: i16,
246+
pub item_stack: Option<ItemStack>,
247+
}
248+
}
249+
242250
pub struct TabComplete {
243251
pub message: String,
244252
pub target_block: Option<IVec3>

server/src/player/packet_handling.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::inventory::menu::OpenContainer;
22
use crate::network::packets::packet::ProcessPacket;
33
use crate::network::protocol::play::clientbound::Chat;
44
use crate::network::protocol::play::serverbound;
5-
use crate::network::protocol::play::serverbound::{ArmSwing, ChatMessage, ClickWindow, ClientSettings, ClientStatus, HeldItemChange, PlayerAction, PlayerActionType, PlayerBlockPlacement, PlayerDigging, PlayerLook, PlayerPosition, PlayerPositionLook, PlayerUpdate, TabComplete, UseEntity};
5+
use crate::network::protocol::play::serverbound::{ArmSwing, ChatMessage, ClickWindow, ClientSettings, ClientStatus, CreativeInventoryAction, HeldItemChange, PlayerAction, PlayerActionType, PlayerBlockPlacement, PlayerDigging, PlayerLook, PlayerPosition, PlayerPositionLook, PlayerUpdate, TabComplete, UseEntity};
66
use crate::player::player::{Player, PlayerExtension};
77
use crate::types::chat_component::ChatComponent;
88
use crate::types::direction::Direction;
@@ -156,6 +156,12 @@ impl ProcessPacket for serverbound::ConfirmTransaction {
156156
}
157157
}
158158

159+
impl ProcessPacket for CreativeInventoryAction {
160+
fn process<P: PlayerExtension>(&self, player: &mut Player<P>) {
161+
// println!("{self:?}")
162+
}
163+
}
164+
159165
impl ProcessPacket for TabComplete {
160166
fn process<P: PlayerExtension>(&self, _: &mut Player<P>) {
161167
// for commands, we should have some tree system instead of recreating the mess that is mc 1.8.9 CommandBase

server/src/player/player.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::constants::Sound;
1+
use crate::constants::{Gamemode, Sound};
22
use crate::entity::entity::EntityId;
33
use crate::entity::entity_metadata::PlayerMetadata;
44
use crate::inventory::item::{get_item_stack, Item};
@@ -69,6 +69,7 @@ pub struct Player<E : PlayerExtension> {
6969
pub client_id: ClientId,
7070
pub entity_id: EntityId,
7171

72+
pub gamemode: Gamemode,
7273
pub metadata: PlayerMetadata,
7374
pub dirty_metadata: bool,
7475

@@ -108,6 +109,7 @@ impl<E : PlayerExtension> Player<E> {
108109
position: DVec3,
109110
yaw: f32,
110111
pitch: f32,
112+
gamemode: Gamemode,
111113
extension: E,
112114
) -> Self {
113115
Self {
@@ -116,6 +118,7 @@ impl<E : PlayerExtension> Player<E> {
116118
profile: game_profile,
117119
client_id,
118120
entity_id,
121+
gamemode,
119122
metadata: PlayerMetadata { layers: Default::default() },
120123
dirty_metadata: false,
121124
position,

server/src/world/world.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::constants::Particle;
1+
use crate::constants::{Gamemode, Particle};
22
use crate::entity::entity::{Entity, EntityExtension, EntityId};
33
use crate::entity::entity_appearance::EntityAppearance;
44
use crate::network::binary::var_int::VarInt;
@@ -70,16 +70,26 @@ impl<W: WorldExtension> World<W> {
7070
pitch: f32,
7171
profile: GameProfile,
7272
client_id: ClientId,
73+
gamemode: Gamemode,
7374
extension: W::Player,
7475
) -> &mut Player<W::Player> {
7576
let entity_id = self.new_entity_id();
77+
7678
let mut player = Player::new(
77-
self, profile, client_id, entity_id, position, yaw, pitch, extension,
79+
self,
80+
profile,
81+
client_id,
82+
entity_id,
83+
position,
84+
yaw,
85+
pitch,
86+
gamemode,
87+
extension
7888
);
7989

8090
player.write_packet(&JoinGame {
8191
entity_id: player.entity_id,
82-
gamemode: 0,
92+
gamemode,
8393
dimension: 0,
8494
difficulty: 0,
8595
max_players: 0,

src/dungeon/dungeon.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use anyhow::bail;
99
use glam::{ivec3, DVec3, IVec2};
1010
use server::block::block_parameter::Axis;
1111
use server::block::rotatable::Rotatable;
12+
use server::constants::Gamemode;
1213
use server::inventory::menu::OpenContainer;
1314
use server::network::binary::var_int::VarInt;
1415
use server::network::protocol::play::clientbound::{Chat, EntityProperties, PlayerAbilities};
@@ -142,6 +143,7 @@ impl WorldExtension for Dungeon {
142143
0.0,
143144
profile,
144145
client_id,
146+
Gamemode::Survival,
145147
DungeonPlayer {
146148
is_ready: false,
147149
sidebar: Sidebar::new(),

src/dungeon/dungeon_player.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,22 @@ impl DungeonPlayer {
222222
if world.has_started() {
223223
if let Some(room_rc) = player.extension.get_current_room() {
224224
for neighbour in room_rc.borrow().neighbours() {
225-
{
226-
let mut door = neighbour.door.borrow_mut();
225+
let mut door = neighbour.door.borrow_mut();
227226

228-
if !door.contains(position) || door.is_open {
229-
continue;
230-
}
231-
if !door.can_open(world) {
232-
// todo: proper chat message and sound
233-
player.write_packet(&Chat {
234-
component: ChatComponent::new("no key"),
235-
chat_type: 0,
236-
});
237-
continue;
238-
}
239-
door.open(world);
227+
if !door.contains(position) || door.is_open {
228+
continue;
240229
}
230+
if !door.can_open(world) {
231+
// todo: proper chat message and sound
232+
player.write_packet(&Chat {
233+
component: ChatComponent::new("no key"),
234+
chat_type: 0,
235+
});
236+
continue;
237+
}
238+
door.open(world);
239+
drop(door);
240+
241241
neighbour.room.borrow_mut().discovered = true;
242242
world.map.draw_room(&neighbour.room.borrow());
243243
return true;

0 commit comments

Comments
 (0)