mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-30 20:14:23 +00:00
feat(block): implement loom block
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
pub mod anvil;
|
||||
pub mod beacon_screen_handler;
|
||||
pub mod brewing;
|
||||
pub mod cartography_table_screen_handler;
|
||||
pub mod container_click;
|
||||
pub mod crafting;
|
||||
pub mod double;
|
||||
@@ -47,10 +48,12 @@ pub mod furnace_like;
|
||||
pub mod generic_container_screen_handler;
|
||||
pub mod gui_builder;
|
||||
pub mod lectern_screen_handler;
|
||||
pub mod loom_screen_handler;
|
||||
pub mod merchant;
|
||||
pub mod player;
|
||||
pub mod screen_handler;
|
||||
pub mod slot;
|
||||
pub mod smithing_table_screen_handler;
|
||||
pub mod stonecutter_screen_handler;
|
||||
pub mod sync_handler;
|
||||
pub mod window_property;
|
||||
|
||||
113
crates/pumpkin-inventory/src/loom_screen_handler.rs
Normal file
113
crates/pumpkin-inventory/src/loom_screen_handler.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use std::any::Any;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::player::player_inventory::PlayerInventory;
|
||||
use crate::screen_handler::{
|
||||
InventoryPlayer, ScreenHandler, ScreenHandlerBehaviour, ScreenHandlerFuture,
|
||||
};
|
||||
use crate::slot::NormalSlot;
|
||||
|
||||
use pumpkin_data::item_stack::ItemStack;
|
||||
use pumpkin_data::screen::WindowType;
|
||||
use pumpkin_protocol::java::server::play::SlotActionType;
|
||||
use pumpkin_world::inventory::Inventory;
|
||||
use pumpkin_world::inventory::SimpleInventory;
|
||||
|
||||
pub struct LoomScreenHandler {
|
||||
behaviour: ScreenHandlerBehaviour,
|
||||
pub input_inventory: Arc<SimpleInventory>,
|
||||
pub output_inventory: Arc<SimpleInventory>,
|
||||
}
|
||||
|
||||
impl LoomScreenHandler {
|
||||
pub fn new(sync_id: u8, player_inventory: &Arc<PlayerInventory>) -> Self {
|
||||
let behaviour = ScreenHandlerBehaviour::new(sync_id, Some(WindowType::Loom));
|
||||
let input_inventory = Arc::new(SimpleInventory::new(3));
|
||||
let output_inventory = Arc::new(SimpleInventory::new(1));
|
||||
|
||||
let mut handler = Self {
|
||||
behaviour,
|
||||
input_inventory: input_inventory.clone(),
|
||||
output_inventory: output_inventory.clone(),
|
||||
};
|
||||
|
||||
for i in 0..3 {
|
||||
handler.add_slot(Arc::new(NormalSlot::new(
|
||||
input_inventory.clone() as Arc<dyn Inventory>,
|
||||
i,
|
||||
)));
|
||||
}
|
||||
handler.add_slot(Arc::new(NormalSlot::new(
|
||||
output_inventory as Arc<dyn Inventory>,
|
||||
0,
|
||||
)));
|
||||
|
||||
let player_inventory: Arc<dyn Inventory> = player_inventory.clone();
|
||||
handler.add_player_slots(&player_inventory);
|
||||
|
||||
handler
|
||||
}
|
||||
}
|
||||
|
||||
impl ScreenHandler for LoomScreenHandler {
|
||||
fn get_behaviour(&self) -> &ScreenHandlerBehaviour {
|
||||
&self.behaviour
|
||||
}
|
||||
|
||||
fn get_behaviour_mut(&mut self) -> &mut ScreenHandlerBehaviour {
|
||||
&mut self.behaviour
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn on_slot_click<'a>(
|
||||
&'a mut self,
|
||||
slot_index: i32,
|
||||
button: i32,
|
||||
action_type: SlotActionType,
|
||||
player: &'a dyn InventoryPlayer,
|
||||
) -> ScreenHandlerFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
self.internal_on_slot_click(slot_index, button, action_type, player)
|
||||
.await;
|
||||
})
|
||||
}
|
||||
|
||||
fn quick_move<'a>(
|
||||
&'a mut self,
|
||||
_player: &'a dyn InventoryPlayer,
|
||||
slot_index: i32,
|
||||
) -> ScreenHandlerFuture<'a, ItemStack> {
|
||||
Box::pin(async move {
|
||||
let mut stack = ItemStack::EMPTY.clone();
|
||||
let slot = self.get_behaviour().slots.get(slot_index as usize).cloned();
|
||||
|
||||
if let Some(slot) = slot {
|
||||
let mut slot_stack = slot.get_cloned_stack().await;
|
||||
if !slot_stack.is_empty() {
|
||||
stack = slot_stack.clone();
|
||||
if slot_index < 4 {
|
||||
if !self.insert_item(&mut slot_stack, 4, 40, true).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
} else if !self.insert_item(&mut slot_stack, 0, 3, false).await {
|
||||
return ItemStack::EMPTY.clone();
|
||||
}
|
||||
|
||||
if slot_stack.is_empty() {
|
||||
slot.set_stack(ItemStack::EMPTY.clone()).await;
|
||||
} else {
|
||||
slot.set_stack(slot_stack).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
stack
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user