Skip to content

[WIP] Implement Effects #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"libcraft/particles",
"libcraft/text",
"libcraft/inventory",
"libcraft/effects",

# Quill
"quill/sys-macros",
Expand Down
1 change: 1 addition & 0 deletions feather/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ uuid = "0.8"
slab = "0.4"
libcraft-core = { path = "../../libcraft/core" }
libcraft-items = { path = "../../libcraft/items" }
libcraft-effects = { path = "../../libcraft/effects" }
worldgen = { path = "../worldgen", package = "feather-worldgen" }

[features]
Expand Down
38 changes: 37 additions & 1 deletion feather/server/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use common::{
use libcraft_items::InventorySlot;
use packets::server::{Particle, SetSlot, SpawnLivingEntity, UpdateLight, WindowConfirmation};
use protocol::packets::server::{
EntityPosition, EntityPositionAndRotation, EntityTeleport, HeldItemChange, PlayerAbilities,
EntityEffect, EntityPosition, EntityPositionAndRotation, EntityTeleport, HeldItemChange,
PlayerAbilities, RemoveEntityEffect,
};
use protocol::{
packets::{
Expand All @@ -42,6 +43,7 @@ use crate::{
network_id_registry::NetworkId,
Options,
};
use quill_common::components_effects::EffectFlags;
use slab::Slab;

/// Max number of chunks to send to a client per tick.
Expand Down Expand Up @@ -602,6 +604,40 @@ impl Client {
self.send_packet(HeldItemChange { slot });
}

pub fn send_entity_effect(
&self,
network_id: NetworkId,
effect_id: u8,
amplifier: i8,
duration: i32,
flags: EffectFlags,
) {
let mut flags_bit = 0;
if flags.ambient {
flags_bit |= 1 << 0;
}
if flags.particle {
flags_bit |= 1 << 1;
}
if flags.icon {
flags_bit |= 1 << 2;
}
self.send_packet(EntityEffect {
entity_id: network_id.0,
effect_id,
amplifier,
duration,
flags: flags_bit,
})
}

pub fn send_remove_entity_effect(&self, network_id: NetworkId, effect_id: u8) {
self.send_packet(RemoveEntityEffect {
entity_id: network_id.0,
effect_id,
})
}

fn register_entity(&self, network_id: NetworkId) {
self.sent_entities.borrow_mut().insert(network_id);
}
Expand Down
8 changes: 6 additions & 2 deletions feather/server/src/packet_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod entity_action;
mod interaction;
pub mod inventory;
mod movement;
mod use_item;

/// Handles a packet received from a client.
pub fn handle_packet(
Expand Down Expand Up @@ -77,6 +78,10 @@ pub fn handle_packet(
entity_action::handle_entity_action(game, player_id, packet)
}

ClientPlayPacket::UseItem(packet) => {
use_item::handle_use_item(game, server, packet, player_id)
}

ClientPlayPacket::TeleportConfirm(_)
| ClientPlayPacket::QueryBlockNbt(_)
| ClientPlayPacket::SetDifficulty(_)
Expand Down Expand Up @@ -108,8 +113,7 @@ pub fn handle_packet(
| ClientPlayPacket::UpdateJigsawBlock(_)
| ClientPlayPacket::UpdateStructureBlock(_)
| ClientPlayPacket::UpdateSign(_)
| ClientPlayPacket::Spectate(_)
| ClientPlayPacket::UseItem(_) => Ok(()),
| ClientPlayPacket::Spectate(_) => Ok(()),
}
}

Expand Down
40 changes: 40 additions & 0 deletions feather/server/src/packet_handlers/use_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::Server;
use base::TPS;
use common::Game;
use ecs::{Entity, SysResult};
use protocol::packets::client::UseItem;
use quill_common::components_effects::*;

pub fn handle_use_item(
game: &mut Game,
_server: &mut Server,
_packet: UseItem,
player: Entity,
) -> SysResult {
// example for effect system
let mut speed = SpeedEffect::default();
speed.add_effect(EffectApplication {
amplifier: 0,
duration: 20 * TPS,
flags: EffectFlags {
particle: true,
ambient: false,
icon: true,
},
start_tick: 0,
});

speed.add_effect(EffectApplication {
amplifier: 1,
duration: 10 * TPS,
flags: EffectFlags {
particle: true,
ambient: false,
icon: true,
},
start_tick: 0,
});

game.ecs.insert(player, speed)?;
Ok(())
}
2 changes: 2 additions & 0 deletions feather/server/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod block;
mod chat;
mod effects;
mod entity;
mod particle;
mod player_join;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub fn register(server: Server, game: &mut Game, systems: &mut SystemExecutor<Ga
chat::register(game, systems);
particle::register(systems);
plugin_message::register(systems);
effects::register(game, systems);

systems.group::<Server>().add_system(tick_clients);
}
Expand Down
Loading