Add a PumpkinError trait, and implement it for InventoryError and DeserializerError
This commit is contained in:
parent
2ef1a13f4d
commit
b0f1e60e46
@ -17,17 +17,3 @@ pub enum InventoryError {
|
|||||||
#[error("Player does not have enough permissions")]
|
#[error("Player does not have enough permissions")]
|
||||||
PermissionError,
|
PermissionError,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InventoryError {
|
|
||||||
pub fn should_kick(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
InventoryError::InvalidSlot
|
|
||||||
| InventoryError::ClosedContainerInteract(..)
|
|
||||||
| InventoryError::InvalidPacket
|
|
||||||
| InventoryError::PermissionError => true,
|
|
||||||
InventoryError::LockError
|
|
||||||
| InventoryError::OutOfOrderDragging
|
|
||||||
| InventoryError::MultiplePlayersDragging => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ use pumpkin_core::{
|
|||||||
use pumpkin_entity::{entity_type::EntityType, EntityId};
|
use pumpkin_entity::{entity_type::EntityType, EntityId};
|
||||||
use pumpkin_inventory::player::PlayerInventory;
|
use pumpkin_inventory::player::PlayerInventory;
|
||||||
use pumpkin_protocol::{
|
use pumpkin_protocol::{
|
||||||
bytebuf::{packet_id::Packet, DeserializerError},
|
bytebuf::packet_id::Packet,
|
||||||
client::play::{
|
client::play::{
|
||||||
CGameEvent, CPlayDisconnect, CPlayerAbilities, CPlayerInfoUpdate, CSyncPlayerPosition,
|
CGameEvent, CPlayDisconnect, CPlayerAbilities, CPlayerInfoUpdate, CSyncPlayerPosition,
|
||||||
CSystemChatMessage, PlayerAction,
|
CSystemChatMessage, PlayerAction,
|
||||||
@ -27,14 +27,14 @@ use pumpkin_protocol::{
|
|||||||
use pumpkin_protocol::server::play::SCloseContainer;
|
use pumpkin_protocol::server::play::SCloseContainer;
|
||||||
use pumpkin_world::item::ItemStack;
|
use pumpkin_world::item::ItemStack;
|
||||||
|
|
||||||
|
use super::Entity;
|
||||||
|
use crate::error::PumpkinError;
|
||||||
use crate::{
|
use crate::{
|
||||||
client::{authentication::GameProfile, Client, PlayerConfig},
|
client::{authentication::GameProfile, Client, PlayerConfig},
|
||||||
server::Server,
|
server::Server,
|
||||||
world::World,
|
world::World,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::Entity;
|
|
||||||
|
|
||||||
pub struct PlayerAbilities {
|
pub struct PlayerAbilities {
|
||||||
pub invulnerable: bool,
|
pub invulnerable: bool,
|
||||||
pub flying: bool,
|
pub flying: bool,
|
||||||
@ -268,9 +268,17 @@ impl Player {
|
|||||||
match self.handle_play_packet(server, &mut packet).await {
|
match self.handle_play_packet(server, &mut packet).await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let text = format!("Error while reading incoming packet {}", e);
|
if e.is_kick() {
|
||||||
log::error!("{}", text);
|
if let Some(kick_reason) = e.client_kick_reason() {
|
||||||
self.kick(TextComponent::text(&text))
|
self.kick(TextComponent::text(&kick_reason))
|
||||||
|
} else {
|
||||||
|
self.kick(TextComponent::text(&format!(
|
||||||
|
"Error while reading incoming packet {}",
|
||||||
|
e
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.log();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -280,7 +288,7 @@ impl Player {
|
|||||||
&mut self,
|
&mut self,
|
||||||
server: &Arc<Server>,
|
server: &Arc<Server>,
|
||||||
packet: &mut RawPacket,
|
packet: &mut RawPacket,
|
||||||
) -> Result<(), DeserializerError> {
|
) -> Result<(), Box<dyn PumpkinError>> {
|
||||||
let bytebuf = &mut packet.bytebuf;
|
let bytebuf = &mut packet.bytebuf;
|
||||||
match packet.id.0 {
|
match packet.id.0 {
|
||||||
SConfirmTeleport::PACKET_ID => {
|
SConfirmTeleport::PACKET_ID => {
|
||||||
@ -353,8 +361,7 @@ impl Player {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
SSetCreativeSlot::PACKET_ID => {
|
SSetCreativeSlot::PACKET_ID => {
|
||||||
self.handle_set_creative_slot(server, SSetCreativeSlot::read(bytebuf)?)
|
self.handle_set_creative_slot(server, SSetCreativeSlot::read(bytebuf)?)?;
|
||||||
.unwrap();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
SPlayPingRequest::PACKET_ID => {
|
SPlayPingRequest::PACKET_ID => {
|
||||||
@ -363,8 +370,7 @@ impl Player {
|
|||||||
}
|
}
|
||||||
SClickContainer::PACKET_ID => {
|
SClickContainer::PACKET_ID => {
|
||||||
self.handle_click_container(server, SClickContainer::read(bytebuf)?)
|
self.handle_click_container(server, SClickContainer::read(bytebuf)?)
|
||||||
.await
|
.await?;
|
||||||
.unwrap();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
SCloseContainer::PACKET_ID => {
|
SCloseContainer::PACKET_ID => {
|
||||||
|
|||||||
64
pumpkin/src/error.rs
Normal file
64
pumpkin/src/error.rs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
use log::log;
|
||||||
|
use pumpkin_inventory::InventoryError;
|
||||||
|
use pumpkin_protocol::bytebuf::DeserializerError;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
pub trait PumpkinError: std::error::Error + Display {
|
||||||
|
fn is_kick(&self) -> bool;
|
||||||
|
|
||||||
|
fn log(&self) {
|
||||||
|
log!(self.severity(), "{}", self.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn severity(&self) -> log::Level;
|
||||||
|
|
||||||
|
fn client_kick_reason(&self) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<ErrorType: PumpkinError + 'static> From<ErrorType> for Box<dyn PumpkinError> {
|
||||||
|
fn from(error: ErrorType) -> Self {
|
||||||
|
Box::new(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PumpkinError for InventoryError {
|
||||||
|
fn is_kick(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
InventoryError::InvalidSlot
|
||||||
|
| InventoryError::ClosedContainerInteract(..)
|
||||||
|
| InventoryError::InvalidPacket
|
||||||
|
| InventoryError::PermissionError => true,
|
||||||
|
InventoryError::LockError
|
||||||
|
| InventoryError::OutOfOrderDragging
|
||||||
|
| InventoryError::MultiplePlayersDragging => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn severity(&self) -> log::Level {
|
||||||
|
match self {
|
||||||
|
InventoryError::LockError
|
||||||
|
| InventoryError::InvalidSlot
|
||||||
|
| InventoryError::ClosedContainerInteract(..)
|
||||||
|
| InventoryError::InvalidPacket
|
||||||
|
| InventoryError::PermissionError => log::Level::Error,
|
||||||
|
InventoryError::OutOfOrderDragging => log::Level::Info,
|
||||||
|
InventoryError::MultiplePlayersDragging => log::Level::Warn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn client_kick_reason(&self) -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PumpkinError for DeserializerError {
|
||||||
|
fn is_kick(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn severity(&self) -> log::Level {
|
||||||
|
log::Level::Error
|
||||||
|
}
|
||||||
|
|
||||||
|
fn client_kick_reason(&self) -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,6 +17,7 @@ use server::Server;
|
|||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod entity;
|
pub mod entity;
|
||||||
|
mod error;
|
||||||
pub mod proxy;
|
pub mod proxy;
|
||||||
pub mod rcon;
|
pub mod rcon;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user