Move items with hoppers, dispensers and droppers; feed comparators from containers
Port HopperBlockEntity (push/pull, item entities, furnace faces, double chests, transfer cooldowns), DispenserBlock/DropperBlock.dispenseFrom with the default, bone meal, TNT, flint and steel, bucket and arrow behaviours, and container comparator signals with setChanged notifications. Hopper and dispenser/dropper block entities save with their own ids; their screens use the vanilla layouts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -63,15 +63,36 @@ impl BlockEntity {
|
||||
speed: 1.0,
|
||||
}));
|
||||
}
|
||||
matches!(
|
||||
name,
|
||||
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel"
|
||||
)
|
||||
.then(|| Self::Chest(vec![None; 27]))
|
||||
let size = match name {
|
||||
"minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27,
|
||||
"minecraft:hopper" => 5,
|
||||
"minecraft:dispenser" | "minecraft:dropper" => 9,
|
||||
_ => return None,
|
||||
};
|
||||
Some(Self::Chest(vec![None; size]))
|
||||
}
|
||||
/// The vanilla block entity type id of a container block.
|
||||
pub fn id_for_block(name: &str) -> Option<&'static str> {
|
||||
Some(match name {
|
||||
"minecraft:chest" => "minecraft:chest",
|
||||
"minecraft:trapped_chest" => "minecraft:trapped_chest",
|
||||
"minecraft:barrel" => "minecraft:barrel",
|
||||
"minecraft:hopper" => "minecraft:hopper",
|
||||
"minecraft:dispenser" => "minecraft:dispenser",
|
||||
"minecraft:dropper" => "minecraft:dropper",
|
||||
"minecraft:furnace" => "minecraft:furnace",
|
||||
"minecraft:blast_furnace" => "minecraft:blast_furnace",
|
||||
"minecraft:smoker" => "minecraft:smoker",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
pub fn id(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Chest(_) => "minecraft:chest",
|
||||
Self::Chest(items) => match items.len() {
|
||||
5 => "minecraft:hopper",
|
||||
9 => "minecraft:dispenser",
|
||||
_ => "minecraft:chest",
|
||||
},
|
||||
Self::Furnace(f) => match f.kind {
|
||||
FurnaceKind::Furnace => "minecraft:furnace",
|
||||
FurnaceKind::BlastFurnace => "minecraft:blast_furnace",
|
||||
@@ -85,6 +106,12 @@ impl BlockEntity {
|
||||
Self::Furnace(f) => f.items.iter().flatten().cloned().collect(),
|
||||
}
|
||||
}
|
||||
pub fn slots(&self) -> &[Option<ItemStack>] {
|
||||
match self {
|
||||
Self::Chest(items) => items,
|
||||
Self::Furnace(f) => &f.items,
|
||||
}
|
||||
}
|
||||
pub fn slots_mut(&mut self) -> &mut [Option<ItemStack>] {
|
||||
match self {
|
||||
Self::Chest(items) => items,
|
||||
|
||||
@@ -4,6 +4,7 @@ mod blockctl;
|
||||
mod control;
|
||||
mod explosion;
|
||||
mod growth;
|
||||
mod machines;
|
||||
mod tnt;
|
||||
use horizon_client::{
|
||||
Result,
|
||||
@@ -202,8 +203,14 @@ struct GameLevel<'w> {
|
||||
player_box: Option<([f64; 3], [f64; 3])>,
|
||||
biomes: &'w Biomes,
|
||||
seed: i64,
|
||||
behaviours: &'w Behaviours,
|
||||
items: &'w Items,
|
||||
registry: &'static horizon_world::registry::BlockRegistry,
|
||||
}
|
||||
impl LevelAccess for GameLevel<'_> {
|
||||
fn block_entity_analog_signal(&self, pos: BlockPos, _state: BlockStateId, _direction: Direction) -> i32 {
|
||||
machines::container_signal(self.world, self.behaviours, self.items, self.registry, pos)
|
||||
}
|
||||
fn biome_name(&self, pos: BlockPos) -> Option<&str> {
|
||||
let chunk = self.world.chunks.get(&(pos.x >> 4, pos.z >> 4))?;
|
||||
Some(&self.biomes.get(chunk.biome(pos.x, pos.y, pos.z)).name)
|
||||
@@ -396,6 +403,7 @@ struct Game<'a> {
|
||||
level_events: Vec<LevelEvent>,
|
||||
falling: Vec<FallingBlock>,
|
||||
tnts: Vec<tnt::PrimedTnt>,
|
||||
hoppers: HashMap<(i32, i32, i32), machines::HopperState>,
|
||||
creative_screen: Creative,
|
||||
control: Option<horizon_client::control::Server>,
|
||||
frame_stats: horizon_client::control::FrameStats,
|
||||
@@ -703,6 +711,9 @@ impl Game<'_> {
|
||||
player_box,
|
||||
biomes: &self.res.biomes,
|
||||
seed: self.level.seed,
|
||||
behaviours: &self.res.behaviours,
|
||||
items: &self.res.items,
|
||||
registry: self.res.assets.registry,
|
||||
};
|
||||
f(&self.res.behaviours, &mut level, &mut self.block_ticks)
|
||||
}
|
||||
@@ -825,6 +836,7 @@ impl Game<'_> {
|
||||
PlayerEffect::InteractLoot(_) | PlayerEffect::RespawnAnchor => {}
|
||||
},
|
||||
LevelEvent::PrimeTnt { pos } => self.spawn_primed_tnt(pos, false),
|
||||
LevelEvent::BlockEvent { pos, state, kind: "dispense" } => self.dispense_from(pos, state)?,
|
||||
LevelEvent::BlockEvent { pos, state, kind: "pop_block_item" } => {
|
||||
let item = self.res.facts.get(state).item.clone();
|
||||
self.pop_resource([pos.x, pos.y, pos.z], ItemStack { item, count: 1, damage: 0 });
|
||||
@@ -1741,7 +1753,7 @@ impl Game<'_> {
|
||||
batch
|
||||
}
|
||||
fn inventory_origin(&self, batch: &HudBatch) -> (f32, f32) {
|
||||
((batch.width - 176.0) / 2.0, (batch.height - 166.0) / 2.0)
|
||||
((batch.width - 176.0) / 2.0, (batch.height - self.screen_height()) / 2.0)
|
||||
}
|
||||
/// Slot ids: 0..36 player inventory, 100..109 crafting grid, 200 result.
|
||||
fn container_kind(&self) -> Option<&BlockEntity> {
|
||||
@@ -1765,6 +1777,9 @@ impl Game<'_> {
|
||||
}
|
||||
/// InventoryMenu / CraftingMenu slot layouts.
|
||||
fn slot_position(&self, i: usize) -> (f32, f32) {
|
||||
if let Some(p) = self.machine_slot_position(i) {
|
||||
return p;
|
||||
}
|
||||
let table = self.craft_size == 3;
|
||||
// ContainerScreen (3 rows) shifts the player inventory down by one pixel.
|
||||
let shift = if matches!(self.container_kind(), Some(BlockEntity::Chest(_))) { 1.0 } else { 0.0 };
|
||||
@@ -1834,6 +1849,7 @@ impl Game<'_> {
|
||||
};
|
||||
let grey = rgba(64, 64, 64, 255);
|
||||
match self.container.and_then(|k| self.world.block_entities.get(&k)) {
|
||||
Some(BlockEntity::Chest(_)) if self.draw_machine_screen(batch, ox, oy) => {}
|
||||
Some(BlockEntity::Chest(_)) => {
|
||||
// ContainerScreen with 3 rows: top 71px, then the player inventory from v=126.
|
||||
part(batch, "gui/container/generic_54", 0.0, 0.0, 176.0, 71.0, ox, oy);
|
||||
@@ -2497,6 +2513,7 @@ impl Game<'_> {
|
||||
self.server_explode(center, radius, explosion::BlockInteraction::DestroyWithDecay, false)
|
||||
}
|
||||
fn tick_block_entities(&mut self) -> Result<()> {
|
||||
self.tick_hoppers()?;
|
||||
// ChestLidController: 0.1 per tick towards open/closed.
|
||||
if let Some(key) = self.container {
|
||||
self.chest_open.entry(key).or_insert((0.0, 0.0));
|
||||
@@ -4424,6 +4441,9 @@ impl ApplicationHandler for Game<'_> {
|
||||
}
|
||||
Screen::Inventory if pressed => {
|
||||
self.click_inventory(button == MouseButton::Right);
|
||||
if let Some(key) = self.container {
|
||||
self.container_changed(key);
|
||||
}
|
||||
}
|
||||
Screen::Creative if pressed => {
|
||||
self.click_creative(button == MouseButton::Right);
|
||||
@@ -4754,6 +4774,7 @@ pub fn run() -> Result<()> {
|
||||
level_events: Vec::new(),
|
||||
falling: Vec::new(),
|
||||
tnts: Vec::new(),
|
||||
hoppers: HashMap::new(),
|
||||
creative_screen: Creative::load(&game_root)?,
|
||||
control: None,
|
||||
frame_stats: Default::default(),
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -125,7 +125,7 @@ fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::containers:
|
||||
let pos = (int("x")?, int("y")?, int("z")?);
|
||||
let mut entity = BlockEntity::for_block(&id)?;
|
||||
match &mut entity {
|
||||
BlockEntity::Chest(items) => *items = decode_items(tag.get("Items"), 27),
|
||||
BlockEntity::Chest(items) => *items = decode_items(tag.get("Items"), items.len()),
|
||||
BlockEntity::Furnace(f) => {
|
||||
let items = decode_items(tag.get("Items"), 3);
|
||||
f.items = [items[0].clone(), items[1].clone(), items[2].clone()];
|
||||
@@ -209,7 +209,15 @@ pub fn encode(chunk: &Chunk, registry: &BlockRegistry, biomes: &Biomes) -> Named
|
||||
("InhabitedTime", Tag::Long(0)),
|
||||
("isLightOn", Tag::Byte(0)),
|
||||
("sections", Tag::List(sections)),
|
||||
("block_entities", Tag::List(chunk.block_entities.iter().map(|(p, e)| encode_block_entity(*p, e)).collect())),
|
||||
("block_entities", Tag::List(chunk.block_entities.iter().map(|(p, e)| {
|
||||
// The block's own entity id (barrels, droppers, trapped chests).
|
||||
let mut tag = encode_block_entity(*p, e);
|
||||
let state = chunk.block(p.0 & 15, p.1, p.2 & 15);
|
||||
if let (Tag::Compound(entries), Some(id)) = (&mut tag, registry.state(state).and_then(|s| crate::containers::BlockEntity::id_for_block(&s.name))) {
|
||||
entries.insert("id".into(), string(id));
|
||||
}
|
||||
tag
|
||||
}).collect())),
|
||||
]),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,7 @@ mod query;
|
||||
mod rails;
|
||||
mod redstone;
|
||||
mod shapes;
|
||||
mod signals;
|
||||
mod speleothem;
|
||||
mod survive;
|
||||
mod tick;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
//! Output-signal notifications for block entities the caller owns.
|
||||
use super::{Behaviours, LevelAccess, Ticks};
|
||||
use horizon_core::position::BlockPos;
|
||||
|
||||
impl Behaviours {
|
||||
/// `BlockEntity.setChanged`: a container at `pos` changed its contents, so
|
||||
/// comparators reading it (`Level.updateNeighbourForOutputSignal`) update.
|
||||
pub fn container_changed<L: LevelAccess + ?Sized>(
|
||||
&self,
|
||||
level: &mut L,
|
||||
ticks: &mut Ticks,
|
||||
pos: BlockPos,
|
||||
) {
|
||||
let state = level.block_state(pos);
|
||||
if !self.is_air(state) {
|
||||
let block = self.block(state);
|
||||
self.sim(level, ticks)
|
||||
.update_neighbour_for_output_signal(pos, block);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user