-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP interactive blocks * Added jukebox and crafting table * Making feature more dynamic allowing every block action to be registered here * Renamed interactive_block_manager to block_manager * Simplifying register functions * Added on placed * Fix master merge * Added pumpkin_block macro * Removed junk * Added the option to block placing in on_use_with_item * Fixed inventory lock * Adding on_broken and implementing it for the jukebox * Fixed merge
- Loading branch information
Showing
17 changed files
with
440 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use pumpkin_core::math::position::WorldPosition; | ||
use pumpkin_macros::client_packet; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
#[client_packet("play:level_event")] | ||
pub struct CLevelEvent { | ||
event: i32, | ||
location: WorldPosition, | ||
data: i32, | ||
disable_relative_volume: bool, | ||
} | ||
|
||
impl CLevelEvent { | ||
pub fn new( | ||
event: i32, | ||
location: WorldPosition, | ||
data: i32, | ||
disable_relative_volume: bool, | ||
) -> Self { | ||
Self { | ||
event, | ||
location, | ||
data, | ||
disable_relative_volume, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use crate::block::pumpkin_block::{BlockMetadata, PumpkinBlock}; | ||
use crate::entity::player::Player; | ||
use crate::server::Server; | ||
use pumpkin_core::math::position::WorldPosition; | ||
use pumpkin_world::block::block_registry::Block; | ||
use pumpkin_world::item::item_registry::Item; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
pub enum BlockActionResult { | ||
/// Allow other actions to be executed | ||
Continue, | ||
/// Block other actions | ||
Consume, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct BlockManager { | ||
blocks: HashMap<String, Arc<dyn PumpkinBlock>>, | ||
} | ||
|
||
impl BlockManager { | ||
pub fn register<T: PumpkinBlock + BlockMetadata + 'static>(&mut self, block: T) { | ||
self.blocks | ||
.insert(block.name().to_string(), Arc::new(block)); | ||
} | ||
|
||
pub async fn on_use( | ||
&self, | ||
block: &Block, | ||
player: &Player, | ||
location: WorldPosition, | ||
server: &Server, | ||
) { | ||
let pumpkin_block = self.get_pumpkin_block(block); | ||
if let Some(pumpkin_block) = pumpkin_block { | ||
pumpkin_block.on_use(player, location, server).await; | ||
} | ||
} | ||
|
||
pub async fn on_use_with_item( | ||
&self, | ||
block: &Block, | ||
player: &Player, | ||
location: WorldPosition, | ||
item: &Item, | ||
server: &Server, | ||
) -> BlockActionResult { | ||
let pumpkin_block = self.get_pumpkin_block(block); | ||
if let Some(pumpkin_block) = pumpkin_block { | ||
return pumpkin_block | ||
.on_use_with_item(player, location, item, server) | ||
.await; | ||
} | ||
BlockActionResult::Continue | ||
} | ||
|
||
pub async fn on_placed( | ||
&self, | ||
block: &Block, | ||
player: &Player, | ||
location: WorldPosition, | ||
server: &Server, | ||
) { | ||
let pumpkin_block = self.get_pumpkin_block(block); | ||
if let Some(pumpkin_block) = pumpkin_block { | ||
pumpkin_block.on_placed(player, location, server).await; | ||
} | ||
} | ||
|
||
pub async fn on_broken( | ||
&self, | ||
block: &Block, | ||
player: &Player, | ||
location: WorldPosition, | ||
server: &Server, | ||
) { | ||
let pumpkin_block = self.get_pumpkin_block(block); | ||
if let Some(pumpkin_block) = pumpkin_block { | ||
pumpkin_block.on_broken(player, location, server).await; | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn get_pumpkin_block(&self, block: &Block) -> Option<&Arc<dyn PumpkinBlock>> { | ||
self.blocks | ||
.get(format!("minecraft:{}", block.name).as_str()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::block::block_manager::BlockActionResult; | ||
use crate::block::pumpkin_block::PumpkinBlock; | ||
use crate::entity::player::Player; | ||
use crate::server::Server; | ||
use async_trait::async_trait; | ||
use pumpkin_core::math::position::WorldPosition; | ||
use pumpkin_inventory::{CraftingTable, OpenContainer, WindowType}; | ||
use pumpkin_macros::pumpkin_block; | ||
use pumpkin_world::item::item_registry::Item; | ||
|
||
#[pumpkin_block("minecraft:crafting_table")] | ||
pub struct CraftingTableBlock; | ||
|
||
#[async_trait] | ||
impl PumpkinBlock for CraftingTableBlock { | ||
async fn on_use<'a>(&self, player: &Player, _location: WorldPosition, server: &Server) { | ||
self.open_crafting_screen(player, server).await; | ||
} | ||
|
||
async fn on_use_with_item<'a>( | ||
&self, | ||
player: &Player, | ||
_location: WorldPosition, | ||
_item: &Item, | ||
server: &Server, | ||
) -> BlockActionResult { | ||
self.open_crafting_screen(player, server).await; | ||
BlockActionResult::Consume | ||
} | ||
} | ||
|
||
impl CraftingTableBlock { | ||
pub async fn open_crafting_screen(&self, player: &Player, server: &Server) { | ||
//TODO: Adjust /craft command to real crafting table | ||
let entity_id = player.entity_id(); | ||
player.open_container.store(Some(1)); | ||
{ | ||
let mut open_containers = server.open_containers.write().await; | ||
if let Some(ender_chest) = open_containers.get_mut(&1) { | ||
ender_chest.add_player(entity_id); | ||
} else { | ||
let open_container = OpenContainer::new_empty_container::<CraftingTable>(entity_id); | ||
open_containers.insert(1, open_container); | ||
} | ||
} | ||
player | ||
.open_container(server, WindowType::CraftingTable) | ||
.await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::block::block_manager::BlockActionResult; | ||
use crate::block::pumpkin_block::PumpkinBlock; | ||
use crate::entity::player::Player; | ||
use crate::server::Server; | ||
use async_trait::async_trait; | ||
use pumpkin_core::math::position::WorldPosition; | ||
use pumpkin_macros::pumpkin_block; | ||
use pumpkin_registry::SYNCED_REGISTRIES; | ||
use pumpkin_world::item::item_registry::Item; | ||
|
||
#[pumpkin_block("minecraft:jukebox")] | ||
pub struct JukeboxBlock; | ||
|
||
#[async_trait] | ||
impl PumpkinBlock for JukeboxBlock { | ||
async fn on_use<'a>(&self, player: &Player, location: WorldPosition, _server: &Server) { | ||
// For now just stop the music at this position | ||
let world = &player.living_entity.entity.world; | ||
|
||
world.stop_record(location).await; | ||
} | ||
|
||
async fn on_use_with_item<'a>( | ||
&self, | ||
player: &Player, | ||
location: WorldPosition, | ||
item: &Item, | ||
_server: &Server, | ||
) -> BlockActionResult { | ||
let world = &player.living_entity.entity.world; | ||
|
||
let Some(jukebox_playable) = &item.components.jukebox_playable else { | ||
return BlockActionResult::Continue; | ||
}; | ||
|
||
let Some(song) = jukebox_playable.song.split(':').nth(1) else { | ||
return BlockActionResult::Continue; | ||
}; | ||
|
||
let Some(jukebox_song) = SYNCED_REGISTRIES.jukebox_song.get_index_of(song) else { | ||
log::error!("Jukebox playable song not registered!"); | ||
return BlockActionResult::Continue; | ||
}; | ||
|
||
//TODO: Update block state and block nbt | ||
|
||
world.play_record(jukebox_song as i32, location).await; | ||
|
||
BlockActionResult::Consume | ||
} | ||
|
||
async fn on_broken<'a>(&self, player: &Player, location: WorldPosition, _server: &Server) { | ||
// For now just stop the music at this position | ||
let world = &player.living_entity.entity.world; | ||
|
||
world.stop_record(location).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub(crate) mod crafting_table; | ||
pub(crate) mod jukebox; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::block::block_manager::BlockManager; | ||
use crate::block::blocks::crafting_table::CraftingTableBlock; | ||
use crate::block::blocks::jukebox::JukeboxBlock; | ||
use std::sync::Arc; | ||
|
||
pub mod block_manager; | ||
mod blocks; | ||
pub mod pumpkin_block; | ||
|
||
#[must_use] | ||
pub fn default_block_manager() -> Arc<BlockManager> { | ||
let mut manager = BlockManager::default(); | ||
|
||
manager.register(JukeboxBlock); | ||
manager.register(CraftingTableBlock); | ||
|
||
Arc::new(manager) | ||
} |
Oops, something went wrong.