mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-30 20:14:23 +00:00
73 lines
1.8 KiB
Rust
73 lines
1.8 KiB
Rust
use super::BlockEntity;
|
|
use pumpkin_nbt::compound::NbtCompound;
|
|
use pumpkin_nbt::tag::NbtTag;
|
|
use pumpkin_util::math::position::BlockPos;
|
|
use std::sync::Mutex;
|
|
|
|
pub struct ConduitBlockEntity {
|
|
pub position: BlockPos,
|
|
pub active: Mutex<bool>,
|
|
pub target: Mutex<Option<NbtTag>>,
|
|
}
|
|
|
|
impl BlockEntity for ConduitBlockEntity {
|
|
fn resource_location(&self) -> &'static str {
|
|
Self::ID
|
|
}
|
|
|
|
fn get_position(&self) -> BlockPos {
|
|
self.position
|
|
}
|
|
|
|
fn from_nbt(nbt: &pumpkin_nbt::compound::NbtCompound, position: BlockPos) -> Self
|
|
where
|
|
Self: Sized,
|
|
{
|
|
let active = nbt.get_bool("Active").unwrap_or(false);
|
|
let target = nbt.get("Target").cloned();
|
|
Self {
|
|
position,
|
|
active: Mutex::new(active),
|
|
target: Mutex::new(target),
|
|
}
|
|
}
|
|
|
|
fn write_nbt(&self, nbt: &mut NbtCompound) {
|
|
if let Ok(active) = self.active.lock() {
|
|
nbt.put_bool("Active", *active);
|
|
}
|
|
if let Ok(target) = self.target.lock()
|
|
&& let Some(tgt) = target.as_ref()
|
|
{
|
|
nbt.put("Target", tgt.clone());
|
|
}
|
|
}
|
|
|
|
fn chunk_data_nbt(&self) -> Option<NbtCompound> {
|
|
let mut nbt = NbtCompound::new();
|
|
nbt.put_bool("Active", *self.active.try_lock().ok()?);
|
|
if let Ok(target) = self.target.try_lock()
|
|
&& let Some(ref tgt) = *target
|
|
{
|
|
nbt.put("Target", tgt.clone());
|
|
}
|
|
Some(nbt)
|
|
}
|
|
|
|
fn as_any(&self) -> &dyn std::any::Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl ConduitBlockEntity {
|
|
pub const ID: &'static str = "minecraft:conduit";
|
|
#[must_use]
|
|
pub const fn new(position: BlockPos) -> Self {
|
|
Self {
|
|
position,
|
|
active: Mutex::new(false),
|
|
target: Mutex::new(None),
|
|
}
|
|
}
|
|
}
|