Add shulker boxes and ender chests
Shulker boxes are 27-slot containers that refuse other boxes, keep their contents when broken, exploded or dropped (the item's container component) and restore them when placed. Ender chests show the player's ender inventory, saved as EnderItems. Both play their open and close sounds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -79,6 +79,7 @@ impl BlockEntity {
|
||||
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27,
|
||||
"minecraft:hopper" => 5,
|
||||
"minecraft:dispenser" | "minecraft:dropper" => 9,
|
||||
n if n.ends_with("shulker_box") => 27,
|
||||
_ => return None,
|
||||
};
|
||||
Some(Self::Chest(vec![None; size]))
|
||||
@@ -95,6 +96,7 @@ impl BlockEntity {
|
||||
"minecraft:furnace" => "minecraft:furnace",
|
||||
"minecraft:blast_furnace" => "minecraft:blast_furnace",
|
||||
"minecraft:smoker" => "minecraft:smoker",
|
||||
n if n.ends_with("shulker_box") => "minecraft:shulker_box",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ mod shield;
|
||||
mod sign_edit;
|
||||
mod station_control;
|
||||
mod stations;
|
||||
mod storage;
|
||||
mod titles;
|
||||
mod tnt;
|
||||
mod totem;
|
||||
@@ -532,6 +533,11 @@ struct Game<'a> {
|
||||
boat_models: boats::BoatModels,
|
||||
/// The boat the player rides.
|
||||
riding: Option<u32>,
|
||||
/// The ender inventory (27 slots) and the ender chest showing it.
|
||||
ender_items: Vec<Option<ItemStack>>,
|
||||
ender_open: Option<(i32, i32, i32)>,
|
||||
/// Contents of broken shulker boxes waiting for their dropped item.
|
||||
pending_shulker: HashMap<[i32; 3], Vec<(u8, ItemStack)>>,
|
||||
tnts: Vec<tnt::PrimedTnt>,
|
||||
hoppers: HashMap<(i32, i32, i32), machines::HopperState>,
|
||||
creative_screen: Creative,
|
||||
@@ -822,7 +828,8 @@ impl Game<'_> {
|
||||
});
|
||||
}
|
||||
/// Block.popResource
|
||||
fn pop_resource(&mut self, pos: [i32; 3], stack: ItemStack) {
|
||||
fn pop_resource(&mut self, pos: [i32; 3], mut stack: ItemStack) {
|
||||
self.attach_shulker_contents(pos, &mut stack);
|
||||
let r = &mut self.world.random;
|
||||
let mut offset = || f64::from(r.next_float()) * 0.5 - 0.25;
|
||||
let p = [
|
||||
@@ -1101,6 +1108,10 @@ impl Game<'_> {
|
||||
self.audio.play(&name, Some(c), volume, pitch);
|
||||
}
|
||||
LevelEvent::DropResources { pos, state } => {
|
||||
if let Some(entity) = self.world.block_entities.remove(&(pos.x, pos.y, pos.z)) {
|
||||
let name = self.res.facts.get(state).name.clone();
|
||||
self.release_contents([pos.x, pos.y, pos.z], entity, &name);
|
||||
}
|
||||
let drops = self.res.items.block_drops(
|
||||
self.res.assets.registry,
|
||||
state,
|
||||
@@ -1112,11 +1123,6 @@ impl Game<'_> {
|
||||
self.pop_resource([pos.x, pos.y, pos.z], d);
|
||||
}
|
||||
}
|
||||
if let Some(entity) = self.world.block_entities.remove(&(pos.x, pos.y, pos.z)) {
|
||||
for stack in entity.contents() {
|
||||
self.pop_resource([pos.x, pos.y, pos.z], stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
LevelEvent::FallingBlock { pos, state } => {
|
||||
let p = Vec3::new(
|
||||
@@ -1204,10 +1210,22 @@ impl Game<'_> {
|
||||
let key = (pos[0], pos[1], pos[2]);
|
||||
if &*name == "minecraft:crafting_table" {
|
||||
self.craft_size = 3;
|
||||
} else if &*name == "minecraft:ender_chest" {
|
||||
self.open_ender_chest(key);
|
||||
let at = [
|
||||
f64::from(key.0) + 0.5,
|
||||
f64::from(key.1) + 0.5,
|
||||
f64::from(key.2) + 0.5,
|
||||
];
|
||||
let pitch = self.world.random.next_float() * 0.1 + 0.9;
|
||||
self.audio
|
||||
.play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch);
|
||||
} else if let Some(entity) = BlockEntity::for_block(&name) {
|
||||
self.world.block_entities.entry(key).or_insert(entity);
|
||||
self.container = Some(key);
|
||||
let sound = if name.contains("chest") {
|
||||
let sound = if storage::is_shulker_box(&name) {
|
||||
Some("minecraft:block.shulker_box.open")
|
||||
} else if name.contains("chest") {
|
||||
Some("minecraft:block.chest.open")
|
||||
} else if &*name == "minecraft:barrel" {
|
||||
Some("minecraft:block.barrel.open")
|
||||
@@ -1375,10 +1393,9 @@ impl Game<'_> {
|
||||
}
|
||||
.unwrap_or(self.world.air);
|
||||
if let Some(entity) = self.world.block_entities.remove(&(pos[0], pos[1], pos[2])) {
|
||||
// Containers.dropContents
|
||||
for stack in entity.contents() {
|
||||
self.pop_resource(pos, stack);
|
||||
}
|
||||
// Containers.dropContents (shulker boxes keep theirs).
|
||||
let name = facts.name.clone();
|
||||
self.release_contents(pos, entity, &name);
|
||||
if self.container == Some((pos[0], pos[1], pos[2])) {
|
||||
self.container = None;
|
||||
self.screen = Screen::Playing;
|
||||
@@ -1538,6 +1555,7 @@ impl Game<'_> {
|
||||
self.world
|
||||
.block_entities
|
||||
.insert((target[0], target[1], target[2]), entity);
|
||||
self.fill_placed_box(target, &stack);
|
||||
if sign {
|
||||
self.open_sign_editor((target[0], target[1], target[2]), true);
|
||||
}
|
||||
@@ -2972,7 +2990,11 @@ impl Game<'_> {
|
||||
.get(self.world.block(key.0, key.1, key.2))
|
||||
.name
|
||||
.clone();
|
||||
let sound = if name.contains("chest") {
|
||||
let sound = if storage::is_shulker_box(&name) {
|
||||
Some("minecraft:block.shulker_box.close")
|
||||
} else if &*name == "minecraft:ender_chest" {
|
||||
Some("minecraft:block.ender_chest.close")
|
||||
} else if name.contains("chest") {
|
||||
Some("minecraft:block.chest.close")
|
||||
} else if &*name == "minecraft:barrel" {
|
||||
Some("minecraft:block.barrel.close")
|
||||
@@ -2993,6 +3015,7 @@ impl Game<'_> {
|
||||
}
|
||||
self.world.modified.insert((key.0 >> 4, key.2 >> 4));
|
||||
}
|
||||
self.close_ender_chest();
|
||||
}
|
||||
fn click_inventory(&mut self, right: bool) {
|
||||
if self.station_click(right) {
|
||||
@@ -3012,6 +3035,11 @@ impl Game<'_> {
|
||||
let Some(slot) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else {
|
||||
return;
|
||||
};
|
||||
if let Some(c) = &self.carried
|
||||
&& self.refuses(slot, c)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let max = |items: &Items, s: &ItemStack| items.get(&s.item).map_or(64, |i| i.max_stack);
|
||||
let shift =
|
||||
self.keys.contains(&KeyCode::ShiftLeft) || self.keys.contains(&KeyCode::ShiftRight);
|
||||
@@ -4137,6 +4165,11 @@ impl Game<'_> {
|
||||
let mut inventory = vec![None; 36];
|
||||
self.armor = [None, None, None, None];
|
||||
self.offhand = None;
|
||||
self.ender_items = if level.ender_items.len() == 27 {
|
||||
level.ender_items.clone()
|
||||
} else {
|
||||
vec![None; 27]
|
||||
};
|
||||
for (slot, id, count, damage) in &level.inventory {
|
||||
let stack = ItemStack {
|
||||
item: Arc::from(id.as_str()),
|
||||
@@ -6086,6 +6119,13 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
level.dimension = self.dim.id().to_owned();
|
||||
// An open ender chest holds the live copy.
|
||||
if let Some(key) = self.ender_open
|
||||
&& let Some(BlockEntity::Chest(items)) = self.world.block_entities.get(&key)
|
||||
{
|
||||
self.ender_items = items.clone();
|
||||
}
|
||||
level.ender_items = self.ender_items.clone();
|
||||
level.active_effects = self
|
||||
.potions
|
||||
.effects
|
||||
@@ -7076,6 +7116,9 @@ pub fn run() -> Result<()> {
|
||||
boats: Vec::new(),
|
||||
boat_models: boats::BoatModels::load(&root),
|
||||
riding: None,
|
||||
ender_items: vec![None; 27],
|
||||
ender_open: None,
|
||||
pending_shulker: HashMap::new(),
|
||||
tnts: Vec::new(),
|
||||
hoppers: HashMap::new(),
|
||||
creative_screen: Creative::load(&game_root)?,
|
||||
|
||||
@@ -407,11 +407,15 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
let contents = self.world.block_entities.remove(&(pos[0], pos[1], pos[2]));
|
||||
let name = self
|
||||
.res
|
||||
.facts
|
||||
.get(self.world.block(pos[0], pos[1], pos[2]))
|
||||
.name
|
||||
.clone();
|
||||
self.level_set(pos, self.world.air)?;
|
||||
if let Some(entity) = contents {
|
||||
for stack in entity.contents() {
|
||||
self.pop_resource(pos, stack);
|
||||
}
|
||||
self.release_contents(pos, entity, &name);
|
||||
}
|
||||
if is_tnt {
|
||||
self.spawn_primed_tnt(BlockPos::new(pos[0], pos[1], pos[2]), true);
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
//! Shulker boxes (`ShulkerBoxBlockEntity`: 27 slots that refuse other boxes,
|
||||
//! kept in the dropped item's `container` component) and ender chests (the
|
||||
//! player's `PlayerEnderChestContainer`, saved as `EnderItems`).
|
||||
use super::Game;
|
||||
use horizon_client::{containers::BlockEntity, items::ItemStack};
|
||||
|
||||
pub(super) fn is_shulker_box(name: &str) -> bool {
|
||||
name.ends_with("shulker_box")
|
||||
}
|
||||
impl Game<'_> {
|
||||
/// Block-entity contents on removal: shulker boxes keep theirs for the
|
||||
/// dropped box (`copy_components`), other containers spill them.
|
||||
pub(super) fn release_contents(&mut self, pos: [i32; 3], entity: BlockEntity, block: &str) {
|
||||
if is_shulker_box(block) {
|
||||
if let BlockEntity::Chest(items) = entity {
|
||||
let kept = items
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, s)| Some((i as u8, s?)))
|
||||
.collect();
|
||||
self.pending_shulker.insert(pos, kept);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for stack in entity.contents() {
|
||||
self.pop_resource(pos, stack);
|
||||
}
|
||||
}
|
||||
/// The contents a shulker box dropped at `pos` carries.
|
||||
pub(super) fn attach_shulker_contents(&mut self, pos: [i32; 3], stack: &mut ItemStack) {
|
||||
if !is_shulker_box(&stack.item) {
|
||||
return;
|
||||
}
|
||||
if let Some(contents) = self.pending_shulker.remove(&pos)
|
||||
&& !contents.is_empty()
|
||||
{
|
||||
stack.data_mut().container = contents;
|
||||
}
|
||||
}
|
||||
/// A placed shulker box takes the item's contents.
|
||||
pub(super) fn fill_placed_box(&mut self, pos: [i32; 3], stack: &ItemStack) {
|
||||
let Some(data) = stack.data() else { return };
|
||||
if !is_shulker_box(&stack.item) || data.container.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(BlockEntity::Chest(items)) =
|
||||
self.world.block_entities.get_mut(&(pos[0], pos[1], pos[2]))
|
||||
{
|
||||
for (slot, s) in &data.container {
|
||||
if let Some(cell) = items.get_mut(usize::from(*slot)) {
|
||||
*cell = Some(s.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// ShulkerBoxSlot.mayPlace: no shulker box inside a shulker box.
|
||||
pub(super) fn refuses(&self, slot: usize, stack: &ItemStack) -> bool {
|
||||
(300..400).contains(&slot)
|
||||
&& is_shulker_box(&stack.item)
|
||||
&& self.container.is_some_and(|k| {
|
||||
is_shulker_box(&self.res.facts.get(self.world.block(k.0, k.1, k.2)).name)
|
||||
})
|
||||
}
|
||||
/// Opening an ender chest shows the player's ender inventory in its place.
|
||||
pub(super) fn open_ender_chest(&mut self, key: (i32, i32, i32)) {
|
||||
self.world
|
||||
.block_entities
|
||||
.insert(key, BlockEntity::Chest(self.ender_items.clone()));
|
||||
self.container = Some(key);
|
||||
self.ender_open = Some(key);
|
||||
}
|
||||
/// Closing it copies the items back and leaves no block-entity contents.
|
||||
pub(super) fn close_ender_chest(&mut self) {
|
||||
let Some(key) = self.ender_open.take() else {
|
||||
return;
|
||||
};
|
||||
if let Some(BlockEntity::Chest(items)) = self.world.block_entities.remove(&key) {
|
||||
self.ender_items = items;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,8 @@ pub struct StackData {
|
||||
pub potion: Option<Arc<str>>,
|
||||
/// `minecraft:trim`: (material, pattern).
|
||||
pub trim: Option<(Arc<str>, Arc<str>)>,
|
||||
/// `minecraft:container` (shulker boxes): the non-empty slots.
|
||||
pub container: Vec<(u8, ItemStack)>,
|
||||
}
|
||||
impl StackData {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
@@ -236,6 +238,19 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option<Tag> {
|
||||
]),
|
||||
));
|
||||
}
|
||||
if !d.container.is_empty() {
|
||||
let slots = d
|
||||
.container
|
||||
.iter()
|
||||
.map(|(slot, stack)| {
|
||||
Tag::compound([
|
||||
("slot", Tag::Int(i32::from(*slot))),
|
||||
("item", crate::save::item_to_nbt(stack)),
|
||||
])
|
||||
})
|
||||
.collect();
|
||||
entries.push(("minecraft:container", Tag::List(slots)));
|
||||
}
|
||||
}
|
||||
(!entries.is_empty()).then(|| Tag::compound(entries))
|
||||
}
|
||||
@@ -263,6 +278,18 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option<Box<StackData
|
||||
.max(0) as u32,
|
||||
custom_name: c.get("minecraft:custom_name").and_then(read_text),
|
||||
potion,
|
||||
container: match c.get("minecraft:container") {
|
||||
Some(Tag::List(list)) => list
|
||||
.iter()
|
||||
.filter_map(|e| {
|
||||
Some((
|
||||
e.get("slot").and_then(int)?.clamp(0, 255) as u8,
|
||||
crate::save::item_from_nbt(e.get("item")?)?,
|
||||
))
|
||||
})
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
},
|
||||
trim: c.get("minecraft:trim").and_then(|t| {
|
||||
Some((
|
||||
Arc::from(t.get("material").and_then(string)?.as_str()),
|
||||
|
||||
@@ -343,6 +343,9 @@ fn block_entity_id(e: &crate::containers::BlockEntity, block: &str) -> String {
|
||||
crate::containers::BlockEntity::Chest(_),
|
||||
"minecraft:trapped_chest" | "minecraft:barrel" | "minecraft:dropper",
|
||||
) => block.to_owned(),
|
||||
(crate::containers::BlockEntity::Chest(_), b) if b.ends_with("shulker_box") => {
|
||||
"minecraft:shulker_box".to_owned()
|
||||
}
|
||||
_ => e.id().to_owned(),
|
||||
}
|
||||
}
|
||||
@@ -1222,6 +1225,8 @@ pub struct Level {
|
||||
pub xp_seed: i32,
|
||||
/// Player.active_effects: (id, duration, amplifier, ambient, show_icon).
|
||||
pub active_effects: Vec<(String, i32, i32, bool, bool)>,
|
||||
/// Player.EnderItems: the ender chest inventory (27 slots).
|
||||
pub ender_items: Vec<Option<crate::items::ItemStack>>,
|
||||
pub xp_level: i32,
|
||||
pub xp_progress: f32,
|
||||
pub xp_total: i32,
|
||||
@@ -1423,6 +1428,7 @@ impl Level {
|
||||
}
|
||||
}
|
||||
self.inventory = inventory;
|
||||
self.ender_items = decode_items(p.get("EnderItems"), 27);
|
||||
self.inventory_components = inventory_components;
|
||||
self.xp_seed = n("XpSeed").unwrap_or(0) as i32;
|
||||
self.active_effects = match p.get("active_effects") {
|
||||
@@ -1448,7 +1454,7 @@ impl Level {
|
||||
let mut p = compound_or_empty(self.player_extra.clone());
|
||||
// Keys rewritten from scratch.
|
||||
if let Tag::Compound(map) = &mut p {
|
||||
for key in ["Inventory", "equipment", "Pos", "respawn"] {
|
||||
for key in ["Inventory", "EnderItems", "equipment", "Pos", "respawn"] {
|
||||
map.remove(&JavaString::from(key));
|
||||
}
|
||||
}
|
||||
@@ -1537,6 +1543,7 @@ impl Level {
|
||||
),
|
||||
("playerGameType", Tag::Int(i32::from(self.creative))),
|
||||
("Inventory", Tag::List(inventory)),
|
||||
("EnderItems", encode_items(&self.ender_items)),
|
||||
("abilities", abilities),
|
||||
(
|
||||
"Dimension",
|
||||
|
||||
Reference in New Issue
Block a user