Fix: Serverbound Interact

This commit is contained in:
Snowiiii 2024-08-18 18:38:49 +02:00
parent 7b9b35e437
commit c7b3ce6391
3 changed files with 86 additions and 53 deletions

View File

@ -10,8 +10,8 @@ pub mod packet_decoder;
pub mod packet_encoder; pub mod packet_encoder;
pub mod position; pub mod position;
pub mod server; pub mod server;
pub mod uuid;
pub mod slot; pub mod slot;
pub mod uuid;
pub const CURRENT_MC_PROTOCOL: u32 = 767; pub const CURRENT_MC_PROTOCOL: u32 = 767;

View File

@ -1,3 +1,7 @@
use std::char::MAX;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use pumpkin_macros::packet; use pumpkin_macros::packet;
use crate::{ServerPacket, VarInt}; use crate::{ServerPacket, VarInt};
@ -6,19 +10,45 @@ use crate::{ServerPacket, VarInt};
pub struct SInteract { pub struct SInteract {
pub entity_id: VarInt, pub entity_id: VarInt,
pub typ: VarInt, pub typ: VarInt,
pub target_x: Option<f32>, pub target_position: Option<(f32, f32, f32)>,
// don't ask me why, adding more values does not work :c pub hand: Option<VarInt>,
pub sneaking: bool,
} }
// TODO // Great job Mojang ;D
impl ServerPacket for SInteract { impl ServerPacket for SInteract {
fn read( fn read(
bytebuf: &mut crate::bytebuf::ByteBuffer, bytebuf: &mut crate::bytebuf::ByteBuffer,
) -> Result<Self, crate::bytebuf::DeserializerError> { ) -> Result<Self, crate::bytebuf::DeserializerError> {
let entity_id = bytebuf.get_var_int();
let typ = bytebuf.get_var_int();
let action = ActionType::from_i32(typ.0 as i32).unwrap();
let target_position: Option<(f32, f32, f32)> = match action {
ActionType::Interact => None,
ActionType::Attack => None,
ActionType::InteractAt => {
Some((bytebuf.get_f32(), bytebuf.get_f32(), bytebuf.get_f32()))
}
};
let hand = match action {
ActionType::Interact => Some(bytebuf.get_var_int()),
ActionType::Attack => None,
ActionType::InteractAt => Some(bytebuf.get_var_int()),
};
Ok(Self { Ok(Self {
entity_id: bytebuf.get_var_int(), entity_id,
typ: bytebuf.get_var_int(), typ,
target_x: bytebuf.get_option(|v| v.get_f32()), target_position,
hand,
sneaking: bytebuf.get_bool(),
}) })
} }
} }
#[derive(FromPrimitive, PartialEq, Eq)]
pub enum ActionType {
Interact,
Attack,
InteractAt,
}

View File

@ -7,9 +7,9 @@ use pumpkin_protocol::{
}, },
position::WorldPosition, position::WorldPosition,
server::play::{ server::play::{
Action, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport, SInteract, Action, ActionType, SChatCommand, SChatMessage, SClientInformationPlay, SConfirmTeleport,
SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation, SInteract, SPlayerAction, SPlayerCommand, SPlayerPosition, SPlayerPositionRotation,
SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn, SPlayerRotation, SSetCreativeSlot, SSetHeldItem, SSwingArm, SUseItemOn,
}, },
}; };
use pumpkin_text::TextComponent; use pumpkin_text::TextComponent;
@ -268,50 +268,53 @@ impl Client {
} }
pub fn handle_interact(&mut self, server: &mut Server, interact: SInteract) { pub fn handle_interact(&mut self, server: &mut Server, interact: SInteract) {
let attacker_player = self.player.as_ref().unwrap(); let action = ActionType::from_i32(interact.typ.0).unwrap();
let entity_id = interact.entity_id; if action == ActionType::Attack {
// TODO: do validation and stuff let attacker_player = self.player.as_ref().unwrap();
let config = &server.advanced_config.pvp; let entity_id = interact.entity_id;
if config.enabled { // TODO: do validation and stuff
let attacked_client = server.get_by_entityid(self, entity_id.0 as EntityId); let config = &server.advanced_config.pvp;
if let Some(mut client) = attacked_client { if config.enabled {
let token = client.token.clone(); let attacked_client = server.get_by_entityid(self, entity_id.0 as EntityId);
let player = client.player.as_mut().unwrap(); if let Some(mut client) = attacked_client {
let velo = player.velocity; let token = client.token.clone();
if config.protect_creative && player.gamemode == GameMode::Creative { let player = client.player.as_mut().unwrap();
return; let velo = player.velocity;
if config.protect_creative && player.gamemode == GameMode::Creative {
return;
}
if config.knockback {
let pitch = attacker_player.entity.pitch;
let strength = 1.0;
player.knockback(
strength * 0.5,
(pitch * 0.017453292).sin() as f64,
-(pitch * 0.017453292).cos() as f64,
);
let packet = &CEntityVelocity::new(
&entity_id,
player.velocity.x as f32,
player.velocity.y as f32,
player.velocity.z as f32,
);
player.velocity = velo;
client.send_packet(packet);
// attacker_player.velocity = attacker_player.velocity.multiply(0.6, 1.0, 0.6);
}
if config.hurt_animation {
// TODO
// thats how we prevent borrow errors :c
let packet = &CHurtAnimation::new(&entity_id, 10.0);
self.send_packet(packet);
client.send_packet(packet);
server.broadcast_packet_expect(
&[self.token.as_ref(), token.as_ref()],
&CHurtAnimation::new(&entity_id, 10.0),
)
}
} else {
self.kick("Interacted with invalid entitiy id")
} }
if config.knockback {
let pitch = attacker_player.entity.pitch;
let strength = 1.0;
player.knockback(
strength * 0.5,
(pitch * 0.017453292).sin() as f64,
-(pitch * 0.017453292).cos() as f64,
);
let packet = &CEntityVelocity::new(
&entity_id,
player.velocity.x as f32,
player.velocity.y as f32,
player.velocity.z as f32,
);
player.velocity = velo;
client.send_packet(packet);
// attacker_player.velocity = attacker_player.velocity.multiply(0.6, 1.0, 0.6);
}
if config.hurt_animation {
// TODO
// thats how we prevent borrow errors :c
let packet = &CHurtAnimation::new(&entity_id, 10.0);
self.send_packet(packet);
client.send_packet(packet);
server.broadcast_packet_expect(
&[self.token.as_ref(), token.as_ref()],
&CHurtAnimation::new(&entity_id, 10.0),
)
}
} else {
self.kick("Interacted with invalid entitiy id")
} }
} }
} }