use super::{Block, BlockState};
use crate::generated::v26_1 as g;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Item(pub(crate) u16);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ItemData {
pub name: &'static str,
#[cfg(feature = "display-names")]
pub display_name: &'static str,
pub max_stack_size: u8,
pub max_damage: Option<u16>,
pub block: Option<u16>,
pub food: Option<Food>,
pub tool: Option<u16>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Food {
pub nutrition: u8,
pub saturation: f32,
pub can_always_eat: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct BlockSet(pub(crate) u16);
impl BlockSet {
#[inline]
#[must_use]
pub fn contains(self, b: Block) -> bool {
let w = &g::items::BLOCK_SETS[self.0 as usize];
let i = usize::from(b.id());
w[i / 64] & (1 << (i % 64)) != 0
}
#[must_use]
pub fn tag(self) -> Option<&'static str> {
let n = g::items::BLOCK_SET_NAMES[self.0 as usize];
(!n.is_empty()).then_some(n)
}
#[must_use]
pub fn from_tag(name: &str) -> Option<BlockSet> {
g::items::BLOCK_SET_NAMES
.iter()
.position(|n| !n.is_empty() && *n == name)
.map(|i| BlockSet(i as u16))
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ToolRule {
pub blocks: BlockSet,
pub speed: Option<f32>,
pub correct_for_drops: Option<bool>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Tool {
pub rules: &'static [ToolRule],
pub default_mining_speed: f32,
pub damage_per_block: u32,
pub can_destroy_blocks_in_creative: bool,
}
impl Tool {
#[must_use]
pub fn mining_speed(&self, b: Block) -> f32 {
for r in self.rules {
if let Some(s) = r.speed
&& r.blocks.contains(b)
{
return s;
}
}
self.default_mining_speed
}
#[must_use]
pub fn correct_for_drops(&self, b: Block) -> bool {
for r in self.rules {
if let Some(c) = r.correct_for_drops
&& r.blocks.contains(b)
{
return c;
}
}
false
}
}
impl Item {
pub const AIR: Item = Item(0);
#[must_use]
pub fn from_id(id: u16) -> Option<Item> {
(usize::from(id) < g::items::ITEMS.len()).then_some(Item(id))
}
#[must_use]
pub fn from_name(name: &str) -> Option<Item> {
crate::common::lookup_name(&g::items::ITEM_NAMES, name).map(Item)
}
#[must_use]
pub const fn id(self) -> u16 {
self.0
}
#[must_use]
pub fn data(self) -> &'static ItemData {
&g::items::ITEMS[self.0 as usize]
}
#[must_use]
pub fn name(self) -> &'static str {
self.data().name
}
#[must_use]
pub fn max_stack_size(self) -> u8 {
self.data().max_stack_size
}
#[must_use]
pub fn block(self) -> Option<Block> {
self.data().block.map(Block)
}
#[must_use]
pub fn food(self) -> Option<Food> {
self.data().food
}
#[must_use]
pub fn tool(self) -> Option<&'static Tool> {
self.data().tool.map(|i| &g::items::TOOLS[i as usize])
}
#[must_use]
pub fn destroy_speed(self, b: Block) -> f32 {
self.tool().map_or(1.0, |t| t.mining_speed(b))
}
#[must_use]
pub fn correct_for_drops(self, b: Block) -> bool {
self.tool().is_some_and(|t| t.correct_for_drops(b))
}
pub fn all() -> impl ExactSizeIterator<Item = Item> {
(0..g::items::ITEMS.len() as u16).map(Item)
}
}
#[must_use]
pub fn food(item: Item) -> Option<Food> {
item.food()
}
impl Block {
#[must_use]
pub fn destroy_time(self) -> f32 {
g::items::BLOCK_DESTROY_TIME[self.0 as usize]
}
#[must_use]
pub fn requires_correct_tool_for_drops(self) -> bool {
let i = self.0 as usize;
g::items::BLOCK_REQUIRES_TOOL[i / 64] & (1 << (i % 64)) != 0
}
#[must_use]
pub fn item(self) -> Item {
Item(g::items::BLOCK_ITEM[self.0 as usize])
}
}
impl BlockState {
#[must_use]
pub fn destroy_time(self) -> f32 {
self.block().destroy_time()
}
#[must_use]
pub fn requires_correct_tool_for_drops(self) -> bool {
self.block().requires_correct_tool_for_drops()
}
}