Add Commands

This commit is contained in:
Snowiiii committed 2024-08-05 10:56:57 +02:00
1 parent 5714c1b786
commit c54ab006ae
14 files changed
+209 -18

No files matched your search

+2 -1
View File
@@ -1,6 +1,7 @@
[package]
name = "pumpkin"
version = "0.1.0"
version = "0.1.0-dev"
description = "Empowering everyone to host fast and efficient Minecraft servers."
edition = "2021"
[dependencies]
+15 -4
View File
@@ -17,7 +17,7 @@ use pumpkin_protocol::{
client::{
config::CConfigDisconnect,
login::CLoginDisconnect,
play::{CPlayDisconnect, CSyncPlayerPostion},
play::{CPlayDisconnect, CSyncPlayerPostion, CSystemChatMessge},
},
packet_decoder::PacketDecoder,
packet_encoder::PacketEncoder,
@@ -25,10 +25,13 @@ use pumpkin_protocol::{
config::{SAcknowledgeFinishConfig, SClientInformation, SKnownPacks, SPluginMessage},
handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
play::{SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation},
play::{
SChatCommand, SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation,
SPlayerRotation,
},
status::{SPingRequest, SStatusRequest},
},
text::Text,
text::TextComponent,
ClientPacket, ConnectionState, PacketError, RawPacket, ServerPacket,
};
@@ -224,6 +227,9 @@ impl Client {
SConfirmTeleport::PACKET_ID => {
self.handle_confirm_teleport(server, SConfirmTeleport::read(bytebuf))
}
SChatCommand::PACKET_ID => {
self.handle_chat_command(server, SChatCommand::read(bytebuf))
}
SPlayerPosition::PACKET_ID => {
self.handle_position(server, SPlayerPosition::read(bytebuf))
}
@@ -284,6 +290,11 @@ impl Client {
}
}
pub fn send_message(&mut self, text: TextComponent) {
self.send_packet(CSystemChatMessge::new(text, false))
.unwrap_or_else(|e| self.kick(&e.to_string()));
}
/// Kicks the Client with a reason depending on the connection state
pub fn kick(&mut self, reason: &str) {
dbg!(reason);
@@ -297,7 +308,7 @@ impl Client {
.unwrap_or_else(|_| self.close());
}
ConnectionState::Play => {
self.send_packet(CPlayDisconnect::new(Text {
self.send_packet(CPlayDisconnect::new(TextComponent {
text: reason.to_string(),
}))
.unwrap_or_else(|_| self.close());
+11 -2
View File
@@ -1,8 +1,11 @@
use pumpkin_protocol::server::play::{
SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation,
SChatCommand, SConfirmTeleport, SPlayerPosition, SPlayerPositionRotation, SPlayerRotation,
};
use crate::server::Server;
use crate::{
commands::{handle_command, CommandSender},
server::Server,
};
use super::Client;
@@ -10,6 +13,8 @@ use super::Client;
pub trait PlayerPacketProcessor {
fn handle_confirm_teleport(&mut self, server: &mut Server, confirm_teleport: SConfirmTeleport);
fn handle_chat_command(&mut self, server: &mut Server, command: SChatCommand);
fn handle_position(&mut self, server: &mut Server, position: SPlayerPosition);
fn handle_position_rotation(
@@ -78,4 +83,8 @@ impl PlayerPacketProcessor for Client {
player.yaw = rotation.yaw;
player.pitch = rotation.pitch;
}
fn handle_chat_command(&mut self, server: &mut Server, command: SChatCommand) {
handle_command(&mut CommandSender::Player(self), command.command, server);
}
}
+38
View File
@@ -0,0 +1,38 @@
use pumpkin::PumpkinCommand;
use pumpkin_protocol::text::TextComponent;
use crate::{client::Client, server::Server};
mod pumpkin;
/// I think it would be great to split this up into a seperate crate, But idk how i should do that, Because we have to rely on Client and Server
pub trait Command<'a> {
// Name of the Plugin, Use lower case
const NAME: &'a str;
const DESCRIPTION: &'a str;
fn on_execute(sender: &mut CommandSender<'a>, command: String, server: &mut Server);
}
pub enum CommandSender<'a> {
Console,
Player(&'a mut Client),
}
impl<'a> CommandSender<'a> {
pub fn send_message(&mut self, text: TextComponent) {
match self {
// todo: add color and stuff to console
CommandSender::Console => log::info!("{}", text.text),
CommandSender::Player(c) => c.send_message(text),
}
}
}
pub fn handle_command(sender: &mut CommandSender, command: String, server: &mut Server) {
let command = command.to_lowercase();
dbg!("handling command");
dbg!(&command);
if command.starts_with(PumpkinCommand::NAME) {
PumpkinCommand::on_execute(sender, command, server)
}
}
+29
View File
@@ -0,0 +1,29 @@
use pumpkin_protocol::{text::TextComponent, CURRENT_MC_PROTOCOL};
use crate::server::CURRENT_MC_VERSION;
use super::Command;
pub struct PumpkinCommand {}
impl PumpkinCommand {
pub fn new() -> Self {
Self {}
}
}
impl<'a> Command<'a> for PumpkinCommand {
const NAME: &'a str = "pumpkin";
const DESCRIPTION: &'a str = "Displays information about Pumpkin";
fn on_execute(
sender: &mut super::CommandSender<'a>,
_command: String,
_server: &mut crate::server::Server,
) {
let version = env!("CARGO_PKG_VERSION");
let description = env!("CARGO_PKG_DESCRIPTION");
sender.send_message(TextComponent::from(format!("Pumpkin {version}, {description} (Minecraft {CURRENT_MC_VERSION}, Protocol {CURRENT_MC_PROTOCOL})")))
}
}
+11 -2
View File
@@ -8,14 +8,23 @@ use crate::{entity::player::GameMode, server::Difficulty};
const CURRENT_BASE_VERSION: &str = "1.0.0";
#[derive(Deserialize, Serialize)]
/// The idea is that Pumpkin should very customizable, You can Enable or Disable Features depning on your needs.
/// This also allows you get some Performance or Resource boosts.
/// Important: The Configuration should match Vanilla by default
pub struct AdvancedConfiguration {
pub liquid_physics: bool,
/// Requires Online mode
/// Should player have skins
use_skins: bool,
/// Should chat be enabled
enable_chat: bool,
}
/// Important: The Configuration should match Vanilla by default
impl Default for AdvancedConfiguration {
fn default() -> Self {
Self {
liquid_physics: true,
use_skins: true,
enable_chat: true,
}
}
}
+1
View File
@@ -10,6 +10,7 @@ use server::Server;
const SERVER: Token = Token(0);
pub mod client;
pub mod commands;
pub mod configuration;
pub mod entity;
pub mod server;
+6 -3
View File
@@ -14,6 +14,7 @@ use pumpkin_protocol::{
play::{CChunkDataUpdateLight, CGameEvent, CLogin, CPlayerInfoUpdate, PlayerAction},
},
BitSet, ClientPacket, PacketError, Players, Sample, StatusResponse, VarInt, VarInt32, Version,
CURRENT_MC_PROTOCOL,
};
use pumpkin_world::chunk::TestChunk;
use rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
@@ -28,6 +29,8 @@ use crate::{
},
};
pub const CURRENT_MC_VERSION: &str = "1.21";
pub struct Server {
pub compression_threshold: Option<u8>,
@@ -174,7 +177,7 @@ impl Server {
}
/// Sends a Packet to all Players
fn broadcast_packet<P>(&mut self, packet: P) -> Result<(), PacketError>
fn _broadcast_packet<P>(&mut self, packet: P) -> Result<(), PacketError>
where
P: ClientPacket,
P: Clone,
@@ -234,8 +237,8 @@ impl Server {
StatusResponse {
version: Version {
name: "1.21".into(),
protocol: 767,
name: CURRENT_MC_VERSION.into(),
protocol: CURRENT_MC_PROTOCOL,
},
players: Players {
max: config.max_players,