chore: move to crates folder
This commit is contained in:
1 parent
5d10c0d8a8
commit
cb3660bcd2
3393 files changed
+1270
-741
No files matched your search
@@ -0,0 +1,11 @@
|
||||
pub trait PropertyDelegate: Sync + Send {
|
||||
fn get_property(&self, _index: i32) -> i32;
|
||||
fn set_property(&self, _index: i32, _value: i32);
|
||||
fn get_properties_size(&self) -> i32;
|
||||
}
|
||||
|
||||
/// Trait for extracting smelting experience from cooking block entities.
|
||||
pub trait ExperienceContainer: Send + Sync {
|
||||
/// Extract and reset accumulated experience, returning the total as an integer
|
||||
fn extract_experience(&self) -> i32;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
pub mod entities;
|
||||
pub mod viewer;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use pumpkin_data::{Block, BlockState, BlockStateId};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct BlockStateCodec {
|
||||
/// Block name
|
||||
#[serde(
|
||||
deserialize_with = "parse_block_name",
|
||||
serialize_with = "block_to_string"
|
||||
)]
|
||||
pub name: &'static Block,
|
||||
/// Key-value pairs of properties
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub properties: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
fn parse_block_name<'de, D: Deserializer<'de>>(
|
||||
deserializer: D,
|
||||
) -> Result<&'static Block, D::Error> {
|
||||
let s = String::deserialize(deserializer)?;
|
||||
let block =
|
||||
Block::from_name(s.as_str()).ok_or(serde::de::Error::custom("Invalid block name"))?;
|
||||
Ok(block)
|
||||
}
|
||||
|
||||
fn block_to_string<S: Serializer>(block: &'static Block, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(block.name)
|
||||
}
|
||||
|
||||
impl BlockStateCodec {
|
||||
#[must_use]
|
||||
pub fn get_state(&self) -> &'static BlockState {
|
||||
let state_id = self.get_state_id();
|
||||
BlockState::from_id(state_id)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn get_block(&self) -> &'static Block {
|
||||
self.name
|
||||
}
|
||||
|
||||
/// Prefer this over `get_state` when the only the state ID is needed
|
||||
#[must_use]
|
||||
pub fn get_state_id(&self) -> BlockStateId {
|
||||
let block = self.name;
|
||||
|
||||
let Some(properties_map) = &self.properties else {
|
||||
return block.default_state.id;
|
||||
};
|
||||
|
||||
let props_iter = properties_map
|
||||
.iter()
|
||||
.map(|(k, v)| (k.as_str(), v.as_str()))
|
||||
.collect::<Vec<(&str, &str)>>();
|
||||
|
||||
let block_properties = block.from_properties(&props_iter);
|
||||
block_properties.to_state_id(block)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pumpkin_data::BlockStateId;
|
||||
|
||||
use crate::chunk::palette::BLOCK_NETWORK_MAX_BITS;
|
||||
|
||||
#[test]
|
||||
fn proper_network_bits_per_entry() {
|
||||
let id_to_test = 1 << BLOCK_NETWORK_MAX_BITS;
|
||||
assert!(
|
||||
BlockStateId::new_or_air(id_to_test) == BlockStateId::AIR,
|
||||
"We need to update our constants!"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use std::sync::atomic::{AtomicU16, Ordering};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ViewerCountTracker {
|
||||
pub old: AtomicU16,
|
||||
pub current: AtomicU16,
|
||||
}
|
||||
|
||||
impl Default for ViewerCountTracker {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl ViewerCountTracker {
|
||||
#[must_use]
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
old: AtomicU16::new(0),
|
||||
current: AtomicU16::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_container(&self) {
|
||||
self.current.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn close_container(&self) {
|
||||
self.current.fetch_sub(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Returns the current number of players viewing this container
|
||||
pub fn get_viewer_count(&self) -> u16 {
|
||||
self.current.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user