Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

14 changes: 1 addition & 13 deletions crates/blocks/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,7 @@ impl Block {
}

pub fn can_place_block_in(self) -> bool {
matches!(self.get_id(),
0 // Air
| 12958..=12959 // Void and Cave air
| 80..=95 // Water
| 96..=111 // Lava
| 2005 // Short Grass
| 2006 // Fern
| 2007 // Dead bush
| 2008 // Seagrass
| 2009..=2010 // Tall Seagrass
| 10755..=10756 // Tall Grass
| 10757..=10758 // Large Fern
)
self == Block::Air
}

pub(crate) fn complex_rotate(&mut self, amt: RotateAmt) {
Expand Down
1 change: 1 addition & 0 deletions crates/blocks/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ impl Block {
Block::OakWallSign { .. } => Item::OakSign,
Block::BirchWallSign { .. } => Item::BirchSign,
Block::WarpedWallSign { .. } => Item::WarpedSign,
Block::WaterCauldron { .. } => Item::Cauldron,
_ => return None,
})
}
Expand Down
8 changes: 7 additions & 1 deletion crates/blocks/src/items.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::block_entities::ContainerType;
pub use crate::generated::Item;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct ItemStack {
pub item_type: Item,
pub count: u8,
Expand All @@ -10,6 +10,12 @@ pub struct ItemStack {
}

impl ItemStack {
pub fn is_same_item(&self, other: &ItemStack) -> bool {
self.item_type == other.item_type
&& self.nbt == other.nbt
&& self.container_slots == other.container_slots
}

/// Create container item with specified signal strength
pub fn container_with_ss(container_ty: ContainerType, ss: u8) -> ItemStack {
let item = match container_ty {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ pub fn use_item_on_block(
}
let can_place = item.item_type.is_block() && world.get_block(block_pos).can_place_block_in();

if !ctx.player.crouching
if !ctx.player.skips_block_use()
&& on_use(
use_block,
world,
Expand Down
47 changes: 46 additions & 1 deletion crates/core/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ impl Player {
}
}

pub fn skips_block_use(&self) -> bool {
let main_hand = self.selected_slot as usize + 36;
self.crouching && (self.inventory[main_hand].is_some() || self.inventory[45].is_some())
}

pub fn teleport(&mut self, pos: PlayerPos) {
// Prevent from teleporting to Infinity or NaN
if !pos.x.is_finite() || !pos.y.is_finite() || !pos.z.is_finite() {
Expand Down Expand Up @@ -492,6 +497,10 @@ impl Player {

pub fn set_gamemode(&mut self, gamemode: Gamemode) {
self.gamemode = gamemode;
// The client starts flying when it becomes a spectator, without telling the server
if matches!(gamemode, Gamemode::Spectator) {
self.flying = true;
}
let change_game_state = CGameEvent {
reason: CGameEventType::ChangeGamemode,
value: self.gamemode.get_id() as f32,
Expand Down Expand Up @@ -556,6 +565,42 @@ impl Player {
pub fn set_held_slot(&mut self, slot: u32) {
let set_held_slot = CSetHeldSlot { slot: slot as i32 }.encode();
self.client.send_packet(&set_held_slot);
self.selected_slot = slot;
}

pub fn pick_item(&mut self, item: ItemStack) {
let matching_slot = (36..45).chain(9..36).find(|&slot| {
self.inventory[slot as usize]
.as_ref()
.is_some_and(|stack| stack.is_same_item(&item))
});
if let Some(slot) = matching_slot
&& slot >= 36
{
self.set_held_slot(slot - 36);
return;
}

let hotbar_slot = (0..9)
.map(|offset| (self.selected_slot + offset) % 9)
.find(|&slot| self.inventory[(36 + slot) as usize].is_none())
.unwrap_or(self.selected_slot);
let replaced = self.inventory[(36 + hotbar_slot) as usize].take();
match matching_slot {
Some(slot) => {
let picked = self.inventory[slot as usize].take();
self.set_inventory_slot(slot, replaced);
self.set_inventory_slot(36 + hotbar_slot, picked);
}
None => {
let free_slot = (9..36).find(|&slot| self.inventory[slot as usize].is_none());
if let (Some(free_slot), Some(replaced)) = (free_slot, replaced) {
self.set_inventory_slot(free_slot, Some(replaced));
}
self.set_inventory_slot(36 + hotbar_slot, Some(item));
}
}
self.set_held_slot(hotbar_slot);
}

pub fn spawn_packet(&self) -> CSpawnEntity {
Expand Down Expand Up @@ -598,7 +643,7 @@ impl Player {
CSetEntityMetadataEntry {
index: 6,
metadata_type: 21,
value: vec![if self.crouching { 5 } else { 0 }],
value: vec![if self.crouching && !self.flying { 5 } else { 0 }],
},
CSetEntityMetadataEntry {
index: 17,
Expand Down
5 changes: 3 additions & 2 deletions crates/core/src/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl Plot {

fn change_player_gamemode(&mut self, player_idx: usize, gamemode: Gamemode) {
self.players[player_idx].set_gamemode(gamemode);
self.send_metadata_to_others(player_idx);
let _ = self.message_sender.send(Message::PlayerUpdateGamemode(
self.players[player_idx].uuid,
gamemode,
Expand Down Expand Up @@ -565,7 +566,7 @@ impl Plot {
if self.redpiler.is_active() {
let block = self.world.get_block(block_pos);
let lever_or_button = matches!(block, Block::Lever { .. } | Block::StoneButton { .. });
if lever_or_button && !self.players[player].crouching {
if lever_or_button && !self.players[player].skips_block_use() {
self.redpiler.on_use_block(block_pos);
self.publish_world();
return;
Expand Down Expand Up @@ -601,7 +602,7 @@ impl Plot {
}

let block = self.world.get_block(block_pos);
if !self.players[player].crouching {
if !self.players[player].skips_block_use() {
interaction::on_use(
block,
&mut self.world,
Expand Down
Loading
Loading