feat: add posteffect command
This commit is contained in:
1 parent
12f5687629
commit
218a7fe467
7 files changed
+411
No files matched your search
@@ -9,6 +9,7 @@ mod finish_config;
|
||||
mod known_packs;
|
||||
mod ping;
|
||||
mod plugin_message;
|
||||
mod post_effects;
|
||||
mod registry_data;
|
||||
mod remove_resource_pack;
|
||||
mod reset_chat;
|
||||
@@ -29,6 +30,7 @@ pub use finish_config::*;
|
||||
pub use known_packs::*;
|
||||
pub use ping::*;
|
||||
pub use plugin_message::*;
|
||||
pub use post_effects::*;
|
||||
pub use registry_data::*;
|
||||
pub use remove_resource_pack::*;
|
||||
pub use reset_chat::*;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
use pumpkin_data::packet::clientbound::config::POST_EFFECTS;
|
||||
use pumpkin_macros::java_packet;
|
||||
use pumpkin_util::version::JavaMinecraftVersion;
|
||||
|
||||
use crate::ClientPacket;
|
||||
use crate::ser::NetworkWriteExt;
|
||||
|
||||
#[java_packet(POST_EFFECTS)]
|
||||
pub struct CConfigPostEffects<'a> {
|
||||
pub effects: &'a [String],
|
||||
}
|
||||
|
||||
impl<'a> CConfigPostEffects<'a> {
|
||||
#[must_use]
|
||||
pub const fn new(effects: &'a [String]) -> Self {
|
||||
Self { effects }
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientPacket for CConfigPostEffects<'_> {
|
||||
fn write_packet_data(
|
||||
&self,
|
||||
mut write: impl std::io::Write,
|
||||
version: &JavaMinecraftVersion,
|
||||
) -> Result<(), crate::ser::WritingError> {
|
||||
if *version >= JavaMinecraftVersion::V_26_3 {
|
||||
write.write_list(self.effects, |w, effect| w.write_string(effect))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -326,3 +326,6 @@ pub use game_test_highlight_pos::*;
|
||||
|
||||
mod test_instance_block_status;
|
||||
pub use test_instance_block_status::*;
|
||||
|
||||
mod post_effects;
|
||||
pub use post_effects::*;
|
||||
@@ -0,0 +1,31 @@
|
||||
use pumpkin_data::packet::clientbound::play::POST_EFFECTS;
|
||||
use pumpkin_macros::java_packet;
|
||||
use pumpkin_util::version::JavaMinecraftVersion;
|
||||
|
||||
use crate::ClientPacket;
|
||||
use crate::ser::NetworkWriteExt;
|
||||
|
||||
#[java_packet(POST_EFFECTS)]
|
||||
pub struct CPostEffects<'a> {
|
||||
pub effects: &'a [String],
|
||||
}
|
||||
|
||||
impl<'a> CPostEffects<'a> {
|
||||
#[must_use]
|
||||
pub const fn new(effects: &'a [String]) -> Self {
|
||||
Self { effects }
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientPacket for CPostEffects<'_> {
|
||||
fn write_packet_data(
|
||||
&self,
|
||||
mut write: impl std::io::Write,
|
||||
version: &JavaMinecraftVersion,
|
||||
) -> Result<(), crate::ser::WritingError> {
|
||||
if *version >= JavaMinecraftVersion::V_26_3 {
|
||||
write.write_list(self.effects, |w, effect| w.write_string(effect))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@ mod place;
|
||||
mod playsound;
|
||||
mod plugin;
|
||||
mod plugins;
|
||||
mod posteffect;
|
||||
mod pumpkin;
|
||||
mod raid;
|
||||
mod random;
|
||||
@@ -176,6 +177,7 @@ pub fn default_dispatcher(
|
||||
summon::register(&mut dispatcher, registry);
|
||||
swing::register(&mut dispatcher, registry);
|
||||
compute::register(&mut dispatcher, registry);
|
||||
posteffect::register(&mut dispatcher, registry);
|
||||
damage::register(&mut dispatcher, registry);
|
||||
rotate::register(&mut dispatcher, registry);
|
||||
tellraw::register(&mut dispatcher, registry);
|
||||
|
||||
@@ -0,0 +1,268 @@
|
||||
use pumpkin_data::translation;
|
||||
use pumpkin_util::PermissionLvl;
|
||||
use pumpkin_util::permission::{Permission, PermissionDefault, PermissionRegistry};
|
||||
use pumpkin_util::text::TextComponent;
|
||||
|
||||
use crate::command::argument_builder::{ArgumentBuilder, argument, command, literal};
|
||||
use crate::command::argument_types::entity::EntityArgumentType;
|
||||
use crate::command::argument_types::identifier::IdentifierArgumentType;
|
||||
use crate::command::context::command_context::CommandContext;
|
||||
use crate::command::errors::error_types::CommandErrorType;
|
||||
use crate::command::node::dispatcher::CommandDispatcher;
|
||||
use crate::command::node::{CommandExecutor, CommandExecutorResult};
|
||||
use crate::entity::EntityBase;
|
||||
|
||||
const DESCRIPTION: &str = "Adds, removes, or lists screen shaders (post-processing effects).";
|
||||
const PERMISSION: &str = "minecraft:command.posteffect";
|
||||
|
||||
const ARG_PLAYER: &str = "player";
|
||||
const ARG_TARGET: &str = "target";
|
||||
const ARG_POSTEFFECT: &str = "posteffect";
|
||||
|
||||
const ERROR_ADD_FAILED: CommandErrorType<0> = CommandErrorType::new(
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_FAILED,
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_FAILED,
|
||||
);
|
||||
|
||||
const ERROR_CLEAR_FAILED: CommandErrorType<0> = CommandErrorType::new(
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_FAILED,
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_FAILED,
|
||||
);
|
||||
|
||||
const ERROR_REMOVE_FAILED: CommandErrorType<0> = CommandErrorType::new(
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_FAILED,
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_FAILED,
|
||||
);
|
||||
|
||||
struct AddExecutor;
|
||||
|
||||
impl CommandExecutor for AddExecutor {
|
||||
fn execute(&self, context: &CommandContext) -> CommandExecutorResult {
|
||||
let players = EntityArgumentType::get_players(context, ARG_PLAYER)?;
|
||||
let effect_id = IdentifierArgumentType::get(context, ARG_POSTEFFECT)?;
|
||||
let effect_str = effect_id.to_string();
|
||||
|
||||
let mut added_count = 0;
|
||||
let mut first_player = None;
|
||||
|
||||
for player in &players {
|
||||
if player.add_post_effect(effect_str.clone()) {
|
||||
if first_player.is_none() {
|
||||
first_player = Some(player.clone());
|
||||
}
|
||||
added_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if added_count == 0 {
|
||||
return Err(ERROR_ADD_FAILED.create_without_context());
|
||||
}
|
||||
|
||||
let msg = if added_count == 1 {
|
||||
let player = first_player.unwrap_or_else(|| players[0].clone());
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_SUCCESS_SINGLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_SUCCESS_SINGLE,
|
||||
[TextComponent::text(effect_str), player.get_display_name()],
|
||||
)
|
||||
} else {
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_SUCCESS_MULTIPLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_ADD_SUCCESS_MULTIPLE,
|
||||
[
|
||||
TextComponent::text(effect_str),
|
||||
TextComponent::text(added_count.to_string()),
|
||||
],
|
||||
)
|
||||
};
|
||||
|
||||
context.source.send_feedback(msg, true);
|
||||
Ok(added_count)
|
||||
}
|
||||
}
|
||||
|
||||
struct ClearExecutor;
|
||||
|
||||
impl CommandExecutor for ClearExecutor {
|
||||
fn execute(&self, context: &CommandContext) -> CommandExecutorResult {
|
||||
let players = EntityArgumentType::get_players(context, ARG_PLAYER)?;
|
||||
|
||||
let mut cleared_players = 0;
|
||||
let mut first_player = None;
|
||||
|
||||
for player in &players {
|
||||
if player.clear_post_effects() > 0 {
|
||||
if first_player.is_none() {
|
||||
first_player = Some(player.clone());
|
||||
}
|
||||
cleared_players += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if cleared_players == 0 {
|
||||
return Err(ERROR_CLEAR_FAILED.create_without_context());
|
||||
}
|
||||
|
||||
let msg = if cleared_players == 1 {
|
||||
let player = first_player.unwrap_or_else(|| players[0].clone());
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_SUCCESS_SINGLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_SUCCESS_SINGLE,
|
||||
[player.get_display_name()],
|
||||
)
|
||||
} else {
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_SUCCESS_MULTIPLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_CLEAR_SUCCESS_MULTIPLE,
|
||||
[TextComponent::text(cleared_players.to_string())],
|
||||
)
|
||||
};
|
||||
|
||||
context.source.send_feedback(msg, true);
|
||||
Ok(cleared_players)
|
||||
}
|
||||
}
|
||||
|
||||
struct ListExecutor;
|
||||
|
||||
impl CommandExecutor for ListExecutor {
|
||||
fn execute(&self, context: &CommandContext) -> CommandExecutorResult {
|
||||
let player = EntityArgumentType::get_player(context, ARG_TARGET)?;
|
||||
let effects = player.get_post_effects();
|
||||
let name = player.get_display_name();
|
||||
|
||||
if effects.is_empty() {
|
||||
let msg = TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_LIST_EMPTY,
|
||||
translation::java::COMMANDS_POSTEFFECT_LIST_EMPTY,
|
||||
[name],
|
||||
);
|
||||
context.source.send_feedback(msg, false);
|
||||
Ok(0)
|
||||
} else {
|
||||
let count = effects.len();
|
||||
let effects_str = effects.join(", ");
|
||||
let msg = TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_LIST_SUCCESS,
|
||||
translation::java::COMMANDS_POSTEFFECT_LIST_SUCCESS,
|
||||
[
|
||||
name,
|
||||
TextComponent::text(count.to_string()),
|
||||
TextComponent::text(effects_str),
|
||||
],
|
||||
);
|
||||
context.source.send_feedback(msg, false);
|
||||
Ok(count as i32)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RemoveExecutor;
|
||||
|
||||
impl CommandExecutor for RemoveExecutor {
|
||||
fn execute(&self, context: &CommandContext) -> CommandExecutorResult {
|
||||
let players = EntityArgumentType::get_players(context, ARG_PLAYER)?;
|
||||
let effect_id = IdentifierArgumentType::get(context, ARG_POSTEFFECT)?;
|
||||
let effect_str = effect_id.to_string();
|
||||
|
||||
let mut removed_count = 0;
|
||||
let mut first_player = None;
|
||||
|
||||
for player in &players {
|
||||
if player.remove_post_effect(&effect_str) {
|
||||
if first_player.is_none() {
|
||||
first_player = Some(player.clone());
|
||||
}
|
||||
removed_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if removed_count == 0 {
|
||||
return Err(ERROR_REMOVE_FAILED.create_without_context());
|
||||
}
|
||||
|
||||
let msg = if removed_count == 1 {
|
||||
let player = first_player.unwrap_or_else(|| players[0].clone());
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_SUCCESS_SINGLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_SUCCESS_SINGLE,
|
||||
[TextComponent::text(effect_str), player.get_display_name()],
|
||||
)
|
||||
} else {
|
||||
TextComponent::translate_cross(
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_SUCCESS_MULTIPLE,
|
||||
translation::java::COMMANDS_POSTEFFECT_REMOVE_SUCCESS_MULTIPLE,
|
||||
[
|
||||
TextComponent::text(effect_str),
|
||||
TextComponent::text(removed_count.to_string()),
|
||||
],
|
||||
)
|
||||
};
|
||||
|
||||
context.source.send_feedback(msg, true);
|
||||
Ok(removed_count)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(dispatcher: &mut CommandDispatcher, registry: &PermissionRegistry) {
|
||||
registry.register_permission_or_panic(Permission::new(
|
||||
PERMISSION,
|
||||
DESCRIPTION,
|
||||
PermissionDefault::Op(PermissionLvl::Two),
|
||||
));
|
||||
|
||||
dispatcher.register(
|
||||
command("posteffect", DESCRIPTION)
|
||||
.requires(PERMISSION)
|
||||
.then(
|
||||
literal("add").then(
|
||||
argument(ARG_PLAYER, EntityArgumentType::Players).then(
|
||||
argument(ARG_POSTEFFECT, IdentifierArgumentType).executes(AddExecutor),
|
||||
),
|
||||
),
|
||||
)
|
||||
.then(
|
||||
literal("clear").then(
|
||||
argument(ARG_PLAYER, EntityArgumentType::Players).executes(ClearExecutor),
|
||||
),
|
||||
)
|
||||
.then(
|
||||
literal("list")
|
||||
.then(argument(ARG_TARGET, EntityArgumentType::Player).executes(ListExecutor)),
|
||||
)
|
||||
.then(literal("remove").then(
|
||||
argument(ARG_PLAYER, EntityArgumentType::Players).then(
|
||||
argument(ARG_POSTEFFECT, IdentifierArgumentType).executes(RemoveExecutor),
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::command::context::command_source::CommandSource;
|
||||
|
||||
#[test]
|
||||
fn parse_posteffect() {
|
||||
let mut dispatcher = CommandDispatcher::new();
|
||||
let registry = PermissionRegistry::default();
|
||||
register(&mut dispatcher, ®istry);
|
||||
|
||||
let source = Arc::new(CommandSource::dummy());
|
||||
|
||||
let result = dispatcher.parse_input("posteffect add @a minecraft:blur", &source);
|
||||
assert!(result.errors.is_empty(), "Errors: {:?}", result.errors);
|
||||
|
||||
let result = dispatcher.parse_input("posteffect clear @a", &source);
|
||||
assert!(result.errors.is_empty(), "Errors: {:?}", result.errors);
|
||||
|
||||
let result = dispatcher.parse_input("posteffect list @p", &source);
|
||||
assert!(result.errors.is_empty(), "Errors: {:?}", result.errors);
|
||||
|
||||
let result = dispatcher.parse_input("posteffect remove @a minecraft:blur", &source);
|
||||
assert!(result.errors.is_empty(), "Errors: {:?}", result.errors);
|
||||
}
|
||||
}
|
||||
@@ -527,6 +527,7 @@ pub struct Player {
|
||||
pub seen_credits: AtomicBool,
|
||||
pub score: AtomicI32,
|
||||
pub spawn_extra_particles_on_fall: AtomicBool,
|
||||
pub post_effects: std::sync::Mutex<Vec<String>>,
|
||||
/// Inbound packets waiting to be processed during player tick.
|
||||
pub inbound_packets: SegQueue<RawPacket>,
|
||||
}
|
||||
@@ -828,10 +829,83 @@ impl Player {
|
||||
seen_credits: AtomicBool::new(false),
|
||||
score: AtomicI32::new(0),
|
||||
spawn_extra_particles_on_fall: AtomicBool::new(false),
|
||||
post_effects: std::sync::Mutex::new(Vec::new()),
|
||||
inbound_packets: SegQueue::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_post_effects(&self) -> Vec<String> {
|
||||
self.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn has_post_effect(&self, effect: &str) -> bool {
|
||||
self.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.iter()
|
||||
.any(|e| e == effect)
|
||||
}
|
||||
|
||||
pub fn add_post_effect(&self, effect: String) -> bool {
|
||||
let mut effects = self
|
||||
.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if effects.iter().any(|e| e == &effect) {
|
||||
return false;
|
||||
}
|
||||
effects.push(effect);
|
||||
self.try_send_client_packet(&pumpkin_protocol::java::client::play::CPostEffects::new(
|
||||
&effects,
|
||||
));
|
||||
true
|
||||
}
|
||||
|
||||
pub fn remove_post_effect(&self, effect: &str) -> bool {
|
||||
let mut effects = self
|
||||
.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
effects.iter().position(|e| e == effect).is_some_and(|pos| {
|
||||
effects.remove(pos);
|
||||
self.try_send_client_packet(&pumpkin_protocol::java::client::play::CPostEffects::new(
|
||||
&effects,
|
||||
));
|
||||
true
|
||||
})
|
||||
}
|
||||
|
||||
pub fn clear_post_effects(&self) -> usize {
|
||||
let mut effects = self
|
||||
.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if effects.is_empty() {
|
||||
return 0;
|
||||
}
|
||||
let count = effects.len();
|
||||
effects.clear();
|
||||
self.try_send_client_packet(&pumpkin_protocol::java::client::play::CPostEffects::new(
|
||||
&effects,
|
||||
));
|
||||
count
|
||||
}
|
||||
|
||||
pub fn send_post_effects(&self) {
|
||||
let effects = self
|
||||
.post_effects
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
self.try_send_client_packet(&pumpkin_protocol::java::client::play::CPostEffects::new(
|
||||
&effects,
|
||||
));
|
||||
}
|
||||
|
||||
/// Sets the tab list header and footer for Java Edition clients.
|
||||
///
|
||||
/// Note: Tab list header and footer formatting is a Java Edition-specific protocol feature
|
||||
|
||||
Reference in new issue
Block a user