Skip to content

Commit

Permalink
feat: trackable player inventory (#386)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Andrew Gazelka <andrew.gazelka@gmail.com>
  • Loading branch information
Ruben2424 and andrewgazelka authored May 19, 2024
1 parent dc5afba commit ebe4539
Show file tree
Hide file tree
Showing 21 changed files with 1,157 additions and 175 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ members = [

[profile.dev.package."*"]
opt-level = 3
debug = false
strip = true

[profile.test.package."*"]
opt-level = 3
debug = false
strip = true

[profile.release]
#debug = true
Expand Down
3 changes: 2 additions & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# https://doc.rust-lang.org/nightly/clippy/lint_configuration.html
#cognitive-complexity-threshold = 12
cognitive-complexity-threshold = 200
excessive-nesting-threshold = 7
too-many-lines-threshold = 200
2 changes: 2 additions & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ hex = "0.4.3"
#rust-mc-bot.workspace = true
rustc_version = "0.4.0"
tango-bench = "0.5.0"
tracing-subscriber = "0.3.18"

#[[bench]]
#name = "many_zombies"
Expand All @@ -100,6 +101,7 @@ cast_possible_wrap = "allow"
cast_precision_loss = "allow"
missing_errors_doc = "allow"
module_name_repetitions = "allow"
missing_panics_doc = "allow"

perf = "deny"
style = "deny"
Expand Down
134 changes: 116 additions & 18 deletions crates/server/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{alloc::Allocator, borrow::Cow, cell::RefCell, fmt::Debug};
use std::{alloc::Allocator, cell::RefCell, fmt::Debug};

use bumpalo::Bump;
use derive_more::{Deref, DerefMut};
use evenio::{component::Component, entity::EntityId, event::Event};
use glam::Vec3;
use rayon_local::RayonLocal;
use valence_generated::{block::BlockState, status_effects::StatusEffect};
use valence_protocol::{
packets::play::entity_equipment_update_s2c::EquipmentEntry, BlockPos, Hand,
};
use valence_protocol::{packets::play::click_slot_c2s::SlotChange, BlockPos, Hand, ItemStack};
use valence_server::entity::EntityKind;
use valence_text::Text;

Expand All @@ -18,6 +16,111 @@ use crate::{
util::player_skin::PlayerSkin,
};

/// An event that is sent when a player clicks in the inventory.
#[derive(Event, Debug)]
pub struct ClickEvent {
#[event(target)]
pub by: EntityId,
pub click_type: ClickType,
// maybe use smallvec to reduce heap allocations
pub slot_changes: Vec<SlotChange>,
pub carried_item: ItemStack,
}

/// The type of click that the player performed.
#[derive(Copy, Clone, Debug)]
pub enum ClickType {
LeftClick {
slot: i16,
// todo: left click only can result in 1 slot change right?
// slot_change: SlotChange,
},
RightClick {
slot: i16,
// todo: left click only can result in 1 slot change right?
// slot_change: SlotChange,
},
LeftClickOutsideOfWindow,
RightClickOutsideOfWindow,
ShiftLeftClick {
slot: i16,
// todo: should be 2 slot changes right?
// slot_changes: [SlotChange; 2],
},
ShiftRightClick {
slot: i16,
// todo: should be 2 slot changes right?
// slot_changes: [SlotChange; 2],
},
HotbarKeyPress {
button: i8,
slot: i16,
// todo: should be 2 slot changes right?
// slot_changes: [SlotChange; 2],
},
OffHandSwap {
slot: i16,
// todo: should be 2 slot changes right?
// slot_changes: [SlotChange; 2],
},
// todo: support for creative mode
CreativeMiddleClick {
slot: i16,
},
QDrop {
slot: i16,
// todo: left click only can result in 1 slot change right?
// slot_change: SlotChange,
},
QControlDrop {
slot: i16,
// todo: left click only can result in 1 slot change right?
// slot_change: SlotChange,
},
StartLeftMouseDrag,
StartRightMouseDrag,
StartMiddleMouseDrag,
AddSlotLeftDrag {
slot: i16,
},
AddSlotRightDrag {
slot: i16,
},
AddSlotMiddleDrag {
slot: i16,
},
EndLeftMouseDrag {
// slot_changes: Vec<SlotChange>,
},
EndRightMouseDrag {
// slot_changes: Vec<SlotChange>,
},
EndMiddleMouseDrag,
DoubleClick {
slot: i16,
// slot_changes: Vec<SlotChange>,
},
DoubleClickReverseOrder {
slot: i16,
// slot_changes: Vec<SlotChange>,
},
}

#[derive(Event)]
/// An event that is sent when a player is changes his main hand
pub struct UpdateSelectedSlot {
#[event(target)]
pub id: EntityId,
pub slot: usize,
}

/// This event is sent when the payer equipment gets sent to the client.
#[derive(Event)]
pub struct UpdateEquipment {
#[event(target)]
pub id: EntityId,
}

/// Initialize a Minecraft entity (like a zombie) with a given pose.
#[derive(Event)]
pub struct InitEntity {
Expand Down Expand Up @@ -258,6 +361,15 @@ pub struct ChatMessage {
pub message: Text,
}

impl ChatMessage {
pub fn new(target: EntityId, message: impl Into<Text>) -> Self {
Self {
target,
message: message.into(),
}
}
}

#[derive(Event)]
pub struct DisguisePlayer {
#[event(target)]
Expand Down Expand Up @@ -329,17 +441,3 @@ pub struct PointCompass {
pub target: EntityId,
pub point_to: BlockPos,
}

#[derive(Event)]
pub struct SetEquipment<'a> {
#[event(target)]
pub target: EntityId,
pub equipment: Cow<'a, [EquipmentEntry]>,
}

impl<'a> SetEquipment<'a> {
pub fn new(target: EntityId, equipment: impl Into<Cow<'a, [EquipmentEntry]>>) -> Self {
let equipment = equipment.into();
Self { target, equipment }
}
}
Loading

0 comments on commit ebe4539

Please sign in to comment.