Skip to content
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

Don't break blocks with swords in creative #387

Closed
Closed
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
30 changes: 28 additions & 2 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::collections::HashSet;
use std::sync::{Arc, LazyLock};

use super::PlayerConfig;
use crate::block::block_manager::BlockActionResult;
Expand Down Expand Up @@ -37,7 +38,7 @@ use pumpkin_protocol::{
},
};
use pumpkin_world::block::{block_registry::get_block_by_item, BlockFace};
use pumpkin_world::item::item_registry::get_item_by_id;
use pumpkin_world::item::item_registry::{get_item, get_item_by_id};
use thiserror::Error;

fn modulus(a: f32, b: f32) -> f32 {
Expand Down Expand Up @@ -84,6 +85,22 @@ impl PumpkinError for BlockPlacingError {
}
}

static CREATIVE_DONT_BREAK_WITH: LazyLock<HashSet<u16>> = LazyLock::new(|| {
[
get_item("wooden_sword").unwrap().id,
get_item("stone_sword").unwrap().id,
get_item("iron_sword").unwrap().id,
get_item("golden_sword").unwrap().id,
get_item("diamond_sword").unwrap().id,
get_item("netherite_sword").unwrap().id,
get_item("trident").unwrap().id,
Comment on lines +90 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just check if it's a sword instead of having a hardcoded list of items?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_sword is also hardcoded, and it is missing the trident and mace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_sword is also hardcoded, and it is missing the trident and mace

🤦
something filtering over all items could filter using contains and checking if the item name contains sword

itemName.contains("sword")

Also, doesn't Minecraft have metadata for which category or what type of item this is (e.g. swords)?

get_item("mace").unwrap().id,
]
.iter()
.copied()
.collect()
});

/// Handles all Play Packets send by a real Player
/// NEVER TRUST THE CLIENT. HANDLE EVERY ERROR, UNWRAP/EXPECT ARE FORBIDDEN
impl Player {
Expand Down Expand Up @@ -540,6 +557,15 @@ impl Player {
// TODO: do validation
// TODO: Config
if self.gamemode.load() == GameMode::Creative {
// Don't break block if player is holding an item like a sword
if let Some(inv) = &self.living_entity.inventory {
if let Some(item) = inv.lock().await.held_item() {
if CREATIVE_DONT_BREAK_WITH.contains(&item.item_id) {
return;
}
}
}

let location = player_action.location;
// Block break & block break sound
let entity = &self.living_entity.entity;
Expand Down
Loading