Skip to content
Open
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
12 changes: 6 additions & 6 deletions pumpkin-inventory/src/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ impl Inventory for DoubleInventory {
self.second.mark_dirty();
}

fn on_open(&self) {
self.first.on_open();
self.second.on_open();
async fn on_open(&self) {
self.first.on_open().await;
self.second.on_open().await;
}

fn on_close(&self) {
self.first.on_close();
self.second.on_close();
async fn on_close(&self) {
self.first.on_close().await;
self.second.on_close().await;
}

fn is_valid_slot_for(&self, slot: usize, stack: &ItemStack) -> bool {
Expand Down
19 changes: 12 additions & 7 deletions pumpkin-inventory/src/generic_container_screen_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
slot::NormalSlot,
};

pub fn create_generic_9x3(
pub async fn create_generic_9x3(
sync_id: u8,
player_inventory: &Arc<PlayerInventory>,
inventory: Arc<dyn Inventory>,
Expand All @@ -23,9 +23,10 @@ pub fn create_generic_9x3(
3,
9,
)
.await
}

pub fn create_generic_9x6(
pub async fn create_generic_9x6(
sync_id: u8,
player_inventory: &Arc<PlayerInventory>,
inventory: Arc<dyn Inventory>,
Expand All @@ -38,9 +39,10 @@ pub fn create_generic_9x6(
6,
9,
)
.await
}

pub fn create_generic_3x3(
pub async fn create_generic_3x3(
sync_id: u8,
player_inventory: &Arc<PlayerInventory>,
inventory: Arc<dyn Inventory>,
Expand All @@ -53,9 +55,10 @@ pub fn create_generic_3x3(
3,
3,
)
.await
}

pub fn create_hopper(
pub async fn create_hopper(
sync_id: u8,
player_inventory: &Arc<PlayerInventory>,
inventory: Arc<dyn Inventory>,
Expand All @@ -68,6 +71,7 @@ pub fn create_hopper(
1,
5,
)
.await
}

pub struct GenericContainerScreenHandler {
Expand All @@ -78,7 +82,7 @@ pub struct GenericContainerScreenHandler {
}

impl GenericContainerScreenHandler {
fn new(
async fn new(
screen_type: WindowType,
sync_id: u8,
player_inventory: &Arc<PlayerInventory>,
Expand All @@ -94,7 +98,8 @@ impl GenericContainerScreenHandler {
};

// TODO: Add player entity as a parameter
inventory.on_open();
inventory.on_open().await;

handler.add_inventory_slots();
let player_inventory: Arc<dyn Inventory> = player_inventory.clone();
handler.add_player_slots(&player_inventory);
Expand All @@ -118,7 +123,7 @@ impl GenericContainerScreenHandler {
impl ScreenHandler for GenericContainerScreenHandler {
async fn on_closed(&mut self, player: &dyn InventoryPlayer) {
self.default_on_closed(player).await;
self.inventory.on_close();
self.inventory.on_close().await;
}

fn as_any(&self) -> &dyn Any {
Expand Down
110 changes: 110 additions & 0 deletions pumpkin-inventory/src/player/ender_chest_inventory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::{any::Any, array::from_fn, sync::Arc};

use async_trait::async_trait;
use pumpkin_world::{
block::viewer::ViewerCountTracker,
inventory::{Clearable, Inventory, split_stack},
item::ItemStack,
};
use tokio::sync::Mutex;

#[derive(Debug)]
pub struct EnderChestInventory {
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
pub tracker: Mutex<Option<Arc<ViewerCountTracker>>>,
}

impl Default for EnderChestInventory {
fn default() -> Self {
Self::new()
}
}

impl EnderChestInventory {
pub const INVENTORY_SIZE: usize = 27;

pub fn new() -> Self {
Self {
items: from_fn(|_| Arc::new(Mutex::new(ItemStack::EMPTY.clone()))),
tracker: Mutex::new(None),
}
}

pub async fn set_tracker(&self, tracker: Arc<ViewerCountTracker>) {
self.tracker.lock().await.replace(tracker);
}

pub async fn has_tracker(&self) -> bool {
self.tracker.lock().await.is_some()
}

pub async fn is_tracker(&self, tracker: &Arc<ViewerCountTracker>) -> bool {
if let Some(value) = self.tracker.lock().await.as_ref() {
return Arc::ptr_eq(value, tracker);
}
false
}
}

#[async_trait]
impl Inventory for EnderChestInventory {
fn size(&self) -> usize {
self.items.len()
}

async fn is_empty(&self) -> bool {
for slot in self.items.iter() {
if !slot.lock().await.is_empty() {
return false;
}
}

true
}

async fn get_stack(&self, slot: usize) -> Arc<Mutex<ItemStack>> {
self.items[slot].clone()
}

async fn remove_stack(&self, slot: usize) -> ItemStack {
let mut removed = ItemStack::EMPTY.clone();
let mut guard = self.items[slot].lock().await;
std::mem::swap(&mut removed, &mut *guard);
removed
}

async fn remove_stack_specific(&self, slot: usize, amount: u8) -> ItemStack {
split_stack(&self.items, slot, amount).await
}

async fn set_stack(&self, slot: usize, stack: ItemStack) {
*self.items[slot].lock().await = stack;
}

async fn on_open(&self) {
if let Some(tracker) = self.tracker.lock().await.as_ref() {
tracker.open_container();
}
}

async fn on_close(&self) {
if let Some(tracker) = self.tracker.lock().await.as_ref() {
tracker.close_container();
}
}

fn mark_dirty(&self) {}

fn as_any(&self) -> &dyn Any {
self
}
}

#[async_trait]
impl Clearable for EnderChestInventory {
async fn clear(&self) {
for slot in self.items.iter() {
*slot.lock().await = ItemStack::EMPTY.clone();
}
}
}
1 change: 1 addition & 0 deletions pumpkin-inventory/src/player/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod ender_chest_inventory;
pub mod player_inventory;
pub mod player_screen_handler;
10 changes: 6 additions & 4 deletions pumpkin-world/src/block/entities/barrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ use super::BlockEntity;
#[derive(Debug)]
pub struct BarrelBlockEntity {
pub position: BlockPos,
pub items: [Arc<Mutex<ItemStack>>; 27],
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
pub dirty: AtomicBool,

// Viewer
pub viewers: ViewerCountTracker,
viewers: ViewerCountTracker,
}

#[async_trait]
Expand Down Expand Up @@ -102,7 +102,9 @@ impl ViewerCountListener for BarrelBlockEntity {
}

impl BarrelBlockEntity {
pub const INVENTORY_SIZE: usize = 27;
pub const ID: &'static str = "minecraft:barrel";

pub fn new(position: BlockPos) -> Self {
Self {
position,
Expand Down Expand Up @@ -186,11 +188,11 @@ impl Inventory for BarrelBlockEntity {
*self.items[slot].lock().await = stack;
}

fn on_open(&self) {
async fn on_open(&self) {
self.viewers.open_container();
}

fn on_close(&self) {
async fn on_close(&self) {
self.viewers.close_container();
}

Expand Down
9 changes: 5 additions & 4 deletions pumpkin-world/src/block/entities/chest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ use super::BlockEntity;
#[derive(Debug)]
pub struct ChestBlockEntity {
pub position: BlockPos,
pub items: [Arc<Mutex<ItemStack>>; 27],
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
pub dirty: AtomicBool,

// Viewer
pub viewers: ViewerCountTracker,
viewers: ViewerCountTracker,
}

#[async_trait]
Expand Down Expand Up @@ -113,6 +113,7 @@ impl ViewerCountListener for ChestBlockEntity {
}

impl ChestBlockEntity {
pub const INVENTORY_SIZE: usize = 27;
pub const LID_ANIMATION_EVENT_TYPE: u8 = 1;
pub const ID: &'static str = "minecraft:chest";

Expand Down Expand Up @@ -194,11 +195,11 @@ impl Inventory for ChestBlockEntity {
*self.items[slot].lock().await = stack;
}

fn on_open(&self) {
async fn on_open(&self) {
self.viewers.open_container();
}

fn on_close(&self) {
async fn on_close(&self) {
self.viewers.close_container();
}

Expand Down
3 changes: 2 additions & 1 deletion pumpkin-world/src/block/entities/chiseled_bookshelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
#[derive(Debug)]
pub struct ChiseledBookshelfBlockEntity {
pub position: BlockPos,
pub items: [Arc<Mutex<ItemStack>>; 6],
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
pub last_interacted_slot: AtomicI8,
pub dirty: AtomicBool,
}
Expand Down Expand Up @@ -81,6 +81,7 @@ impl BlockEntity for ChiseledBookshelfBlockEntity {
}

impl ChiseledBookshelfBlockEntity {
pub const INVENTORY_SIZE: usize = 6;
pub const ID: &'static str = "minecraft:chiseled_bookshelf";

pub fn new(position: BlockPos) -> Self {
Expand Down
4 changes: 3 additions & 1 deletion pumpkin-world/src/block/entities/dropper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::sync::{Mutex, MutexGuard};
#[derive(Debug)]
pub struct DropperBlockEntity {
pub position: BlockPos,
pub items: [Arc<Mutex<ItemStack>>; 9],
pub items: [Arc<Mutex<ItemStack>>; Self::INVENTORY_SIZE],
pub dirty: AtomicBool,
}

Expand Down Expand Up @@ -62,7 +62,9 @@ impl BlockEntity for DropperBlockEntity {
}

impl DropperBlockEntity {
pub const INVENTORY_SIZE: usize = 9;
pub const ID: &'static str = "minecraft:dropper";

pub fn new(position: BlockPos) -> Self {
Self {
position,
Expand Down
Loading
Loading