Pumpkin/crates/pumpkin-inventory/src/smithing_table_screen_handler.rs
2026-08-26 20:33:08 +02:00

103 lines
3.1 KiB
Rust

use std::any::Any;
use std::sync::Arc;
use crate::player::player_inventory::PlayerInventory;
use crate::screen_handler::{InventoryPlayer, ScreenHandler, ScreenHandlerBehaviour};
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 SmithingTableScreenHandler {
behaviour: ScreenHandlerBehaviour,
pub input_inventory: Arc<SimpleInventory>,
pub output_inventory: Arc<SimpleInventory>,
}
impl SmithingTableScreenHandler {
pub fn new(sync_id: u8, player_inventory: &Arc<PlayerInventory>) -> Self {
let behaviour = ScreenHandlerBehaviour::new(sync_id, Some(WindowType::Smithing));
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 SmithingTableScreenHandler {
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(
&mut self,
slot_index: i32,
button: i32,
action_type: SlotActionType,
player: &dyn InventoryPlayer,
) {
self.internal_on_slot_click(slot_index, button, action_type, player);
}
fn quick_move(&mut self, _player: &dyn InventoryPlayer, slot_index: i32) -> ItemStack {
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();
if !slot_stack.is_empty() {
stack = slot_stack.clone();
if slot_index < 4 {
if !self.insert_item(&mut slot_stack, 4, 40, true) {
return ItemStack::EMPTY.clone();
}
} else if !self.insert_item(&mut slot_stack, 0, 3, false) {
return ItemStack::EMPTY.clone();
}
if slot_stack.is_empty() {
slot.set_stack(ItemStack::EMPTY.clone());
} else {
slot.set_stack(slot_stack);
}
}
}
stack
}
}