Skip to content

Commit

Permalink
Improved pvp (#173)
Browse files Browse the repository at this point in the history
* Added CSoundEffect packet

* Added sound macro

added a macro that turns sound namespaces to the sounds id

* added CEntitySoundEffect

* updated id to i32

* Added sound effects to pvp

* Added sweeping and particles

* Added I-Frames to living entity

* Refactored combat

split up the combat handling into multiple functions and a new combat module

* use new packet macro

* updated json files

* fixed attacking

* use builtin clamp
  • Loading branch information
Alvsch authored Oct 24, 2024
1 parent d78c02f commit 7de3e0e
Show file tree
Hide file tree
Showing 17 changed files with 7,566 additions and 75 deletions.
446 changes: 446 additions & 0 deletions assets/particles.json

Large diffs are not rendered by default.

6,546 changes: 6,546 additions & 0 deletions assets/sounds.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ pub fn blocks_enum(_item: TokenStream) -> TokenStream {
pub fn block_categories_enum(_item: TokenStream) -> TokenStream {
block_state::block_type_enum_impl()
}

mod sound;
#[proc_macro]
pub fn sound(item: TokenStream) -> TokenStream {
sound::sound_impl(item)
}

mod particle;
#[proc_macro]
pub fn particle(item: TokenStream) -> TokenStream {
particle::particle_impl(item)
}
27 changes: 27 additions & 0 deletions pumpkin-macros/src/particle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::{collections::HashMap, sync::LazyLock};

use proc_macro::TokenStream;
use quote::quote;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Particle {
name: String,
id: i32,
}

static SOUNDS: LazyLock<HashMap<String, i32>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Particle>>(include_str!("../../assets/particles.json"))
.expect("Could not parse particles.json registry.")
.into_iter()
.map(|val| (val.name, val.id))
.collect()
});

pub(crate) fn particle_impl(item: TokenStream) -> TokenStream {
let input_string = item.to_string();
let sound_name = input_string.trim_matches('"');

let id = SOUNDS.get(sound_name).expect("Invalid particle");
quote! { #id }.into()
}
27 changes: 27 additions & 0 deletions pumpkin-macros/src/sound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::{collections::HashMap, sync::LazyLock};

use proc_macro::TokenStream;
use quote::quote;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct Sound {
name: String,
id: i32,
}

static SOUNDS: LazyLock<HashMap<String, i32>> = LazyLock::new(|| {
serde_json::from_str::<Vec<Sound>>(include_str!("../../assets/sounds.json"))
.expect("Could not parse sounds.json registry.")
.into_iter()
.map(|val| (val.name, val.id))
.collect()
});

pub(crate) fn sound_impl(item: TokenStream) -> TokenStream {
let input_string = item.to_string();
let sound_name = input_string.trim_matches('"');

let id = SOUNDS.get(sound_name).expect("Invalid sound");
quote! { #id }.into()
}
41 changes: 41 additions & 0 deletions pumpkin-protocol/src/client/play/c_entity_sound_effect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use pumpkin_macros::client_packet;
use serde::Serialize;

use crate::{SoundCategory, VarInt};

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::EntitySoundEffect as i32)]
pub struct CEntitySoundEffect {
sound_id: VarInt,
// TODO: add sound from name
// sound_name: Option<&'a str>,
// has_fixed_range: Option<bool>,
// range: Option<f32>,
sound_category: VarInt,
entity_id: VarInt,
volume: f32,
pitch: f32,
seed: f64,
}

impl CEntitySoundEffect {
pub fn new(
sound_id: VarInt,
sound_category: SoundCategory,
entity_id: VarInt,
volume: f32,
pitch: f32,
seed: f64,
) -> Self {
Self {
sound_id: VarInt(sound_id.0 + 1),
sound_category: VarInt(sound_category as i32),
entity_id,
volume,
pitch,
seed,
}
}
}
48 changes: 48 additions & 0 deletions pumpkin-protocol/src/client/play/c_sound_effect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use pumpkin_macros::client_packet;
use serde::Serialize;

use crate::{SoundCategory, VarInt};

use super::ClientboundPlayPackets;

#[derive(Serialize)]
#[client_packet(ClientboundPlayPackets::SoundEffect as i32)]
pub struct CSoundEffect {
sound_id: VarInt,
// TODO: add sound from name
// sound_name: Option<&'a str>,
// has_fixed_range: Option<bool>,
// range: Option<f32>,
sound_category: VarInt,
effect_position_x: i32,
effect_position_y: i32,
effect_position_z: i32,
volume: f32,
pitch: f32,
seed: f64,
}

impl CSoundEffect {
#[allow(clippy::too_many_arguments)]
pub fn new(
sound_id: VarInt,
sound_category: SoundCategory,
effect_position_x: f64,
effect_position_y: f64,
effect_position_z: f64,
volume: f32,
pitch: f32,
seed: f64,
) -> Self {
Self {
sound_id: VarInt(sound_id.0 + 1),
sound_category: VarInt(sound_category as i32),
effect_position_x: (effect_position_x * 8.0) as i32,
effect_position_y: (effect_position_y * 8.0) as i32,
effect_position_z: (effect_position_z * 8.0) as i32,
volume,
pitch,
seed,
}
}
}
4 changes: 4 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod c_disguised_chat_message;
mod c_display_objective;
mod c_entity_animation;
mod c_entity_metadata;
mod c_entity_sound_effect;
mod c_entity_status;
mod c_entity_velocity;
mod c_game_event;
Expand All @@ -33,6 +34,7 @@ mod c_set_container_slot;
mod c_set_health;
mod c_set_held_item;
mod c_set_title;
mod c_sound_effect;
mod c_spawn_entity;
mod c_subtitle;
mod c_sync_player_position;
Expand All @@ -59,6 +61,7 @@ pub use c_disguised_chat_message::*;
pub use c_display_objective::*;
pub use c_entity_animation::*;
pub use c_entity_metadata::*;
pub use c_entity_sound_effect::*;
pub use c_entity_status::*;
pub use c_entity_velocity::*;
pub use c_game_event::*;
Expand All @@ -82,6 +85,7 @@ pub use c_set_container_slot::*;
pub use c_set_health::*;
pub use c_set_held_item::*;
pub use c_set_title::*;
pub use c_sound_effect::*;
pub use c_spawn_entity::*;
pub use c_subtitle::*;
pub use c_sync_player_position::*;
Expand Down
14 changes: 14 additions & 0 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ impl From<VarInt> for ConnectionState {
}
}
}

pub enum SoundCategory {
Master,
Music,
Records,
Weather,
Blocks,
Hostile,
Neutral,
Players,
Ambient,
Voice,
}

pub struct RawPacket {
pub id: VarInt,
pub bytebuf: ByteBuffer,
Expand Down
12 changes: 12 additions & 0 deletions pumpkin-world/src/item/item_categories.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use crate::item::ItemStack;

impl ItemStack {
pub fn is_sword(&self) -> bool {
[
818, // Wooden
823, // Stone
828, // Gold
833, // Iron
838, // Diamond
843, // Netherite
]
.contains(&self.item_id)
}

pub fn is_helmet(&self) -> bool {
[
// Leather
Expand Down
1 change: 1 addition & 0 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pumpkin-world = { path = "../pumpkin-world" }
pumpkin-entity = { path = "../pumpkin-entity" }
pumpkin-protocol = { path = "../pumpkin-protocol" }
pumpkin-registry = { path = "../pumpkin-registry" }
pumpkin-macros = { path = "../pumpkin-macros" }

itertools.workspace = true
log.workspace = true
Expand Down
Loading

0 comments on commit 7de3e0e

Please sign in to comment.