|
| 1 | +use std::collections::BTreeSet; |
| 2 | + |
| 3 | +use pumpkin_data::translation; |
| 4 | +use pumpkin_util::PermissionLvl; |
| 5 | +use pumpkin_util::permission::{Permission, PermissionDefault, PermissionRegistry}; |
| 6 | +use pumpkin_util::text::TextComponent; |
| 7 | + |
| 8 | +use crate::command::argument_builder::{ArgumentBuilder, argument, command, literal}; |
| 9 | +use crate::command::argument_types::core::string::StringArgumentType; |
| 10 | +use crate::command::argument_types::entity::EntityArgumentType; |
| 11 | +use crate::command::context::command_context::CommandContext; |
| 12 | +use crate::command::errors::error_types::CommandErrorType; |
| 13 | +use crate::command::node::dispatcher::CommandDispatcher; |
| 14 | +use crate::command::node::{CommandExecutor, CommandExecutorResult}; |
| 15 | + |
| 16 | +const DESCRIPTION: &str = "Manages the scoreboard tags of entities."; |
| 17 | + |
| 18 | +const PERMISSION: &str = "minecraft:command.tag"; |
| 19 | + |
| 20 | +const ARG_TARGETS: &str = "targets"; |
| 21 | +const ARG_NAME: &str = "name"; |
| 22 | + |
| 23 | +const ADD_FAILED_ERROR_TYPE: CommandErrorType<0> = CommandErrorType::new( |
| 24 | + translation::java::COMMANDS_TAG_ADD_FAILED, |
| 25 | + translation::bedrock::COMMANDS_TAG_ADD_FAILED, |
| 26 | +); |
| 27 | + |
| 28 | +const REMOVE_FAILED_ERROR_TYPE: CommandErrorType<0> = CommandErrorType::new( |
| 29 | + translation::java::COMMANDS_TAG_REMOVE_FAILED, |
| 30 | + translation::bedrock::COMMANDS_TAG_REMOVE_FAILED, |
| 31 | +); |
| 32 | + |
| 33 | +#[derive(Clone, Copy)] |
| 34 | +enum Action { |
| 35 | + Add, |
| 36 | + Remove, |
| 37 | +} |
| 38 | + |
| 39 | +struct ChangeExecutor(Action); |
| 40 | + |
| 41 | +impl CommandExecutor for ChangeExecutor { |
| 42 | + fn execute<'a>(&'a self, context: &'a CommandContext) -> CommandExecutorResult<'a> { |
| 43 | + Box::pin(async move { |
| 44 | + let targets = EntityArgumentType::get_entities(context, ARG_TARGETS).await?; |
| 45 | + let tag = StringArgumentType::get(context, ARG_NAME)?.to_owned(); |
| 46 | + |
| 47 | + let mut changed = 0; |
| 48 | + for target in &targets { |
| 49 | + let entity = target.get_entity(); |
| 50 | + let success = match self.0 { |
| 51 | + Action::Add => entity.add_scoreboard_tag(&tag).await, |
| 52 | + Action::Remove => entity.remove_scoreboard_tag(&tag).await, |
| 53 | + }; |
| 54 | + if success { |
| 55 | + changed += 1; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if changed == 0 { |
| 60 | + return Err(match self.0 { |
| 61 | + Action::Add => ADD_FAILED_ERROR_TYPE.create_without_context(), |
| 62 | + Action::Remove => REMOVE_FAILED_ERROR_TYPE.create_without_context(), |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + let (single_key, multiple_key) = match self.0 { |
| 67 | + Action::Add => ( |
| 68 | + ( |
| 69 | + translation::java::COMMANDS_TAG_ADD_SUCCESS_SINGLE, |
| 70 | + translation::bedrock::COMMANDS_TAG_ADD_SUCCESS_SINGLE, |
| 71 | + ), |
| 72 | + ( |
| 73 | + translation::java::COMMANDS_TAG_ADD_SUCCESS_MULTIPLE, |
| 74 | + translation::bedrock::COMMANDS_TAG_ADD_SUCCESS_MULTIPLE, |
| 75 | + ), |
| 76 | + ), |
| 77 | + Action::Remove => ( |
| 78 | + ( |
| 79 | + translation::java::COMMANDS_TAG_REMOVE_SUCCESS_SINGLE, |
| 80 | + translation::bedrock::COMMANDS_TAG_REMOVE_SUCCESS_SINGLE, |
| 81 | + ), |
| 82 | + ( |
| 83 | + translation::java::COMMANDS_TAG_REMOVE_SUCCESS_MULTIPLE, |
| 84 | + translation::bedrock::COMMANDS_TAG_REMOVE_SUCCESS_MULTIPLE, |
| 85 | + ), |
| 86 | + ), |
| 87 | + }; |
| 88 | + |
| 89 | + let msg = if targets.len() == 1 { |
| 90 | + TextComponent::translate_cross( |
| 91 | + single_key.0, |
| 92 | + single_key.1, |
| 93 | + [ |
| 94 | + TextComponent::text(tag), |
| 95 | + targets[0].get_display_name().await, |
| 96 | + ], |
| 97 | + ) |
| 98 | + } else { |
| 99 | + TextComponent::translate_cross( |
| 100 | + multiple_key.0, |
| 101 | + multiple_key.1, |
| 102 | + [ |
| 103 | + TextComponent::text(tag), |
| 104 | + TextComponent::text(targets.len().to_string()), |
| 105 | + ], |
| 106 | + ) |
| 107 | + }; |
| 108 | + |
| 109 | + context.source.send_feedback(msg, true).await; |
| 110 | + |
| 111 | + Ok(changed) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct ListExecutor; |
| 117 | + |
| 118 | +impl CommandExecutor for ListExecutor { |
| 119 | + fn execute<'a>(&'a self, context: &'a CommandContext) -> CommandExecutorResult<'a> { |
| 120 | + Box::pin(async move { |
| 121 | + let targets = EntityArgumentType::get_entities(context, ARG_TARGETS).await?; |
| 122 | + |
| 123 | + // BTreeSet keeps the output deterministic. |
| 124 | + let mut all_tags = BTreeSet::new(); |
| 125 | + for target in &targets { |
| 126 | + let tags = target.get_entity().scoreboard_tags.lock().await; |
| 127 | + all_tags.extend(tags.iter().cloned()); |
| 128 | + } |
| 129 | + |
| 130 | + let tag_list = |
| 131 | + TextComponent::text(all_tags.iter().cloned().collect::<Vec<String>>().join(", ")); |
| 132 | + |
| 133 | + let msg = if targets.len() == 1 { |
| 134 | + let name = targets[0].get_display_name().await; |
| 135 | + if all_tags.is_empty() { |
| 136 | + TextComponent::translate_cross( |
| 137 | + translation::java::COMMANDS_TAG_LIST_SINGLE_EMPTY, |
| 138 | + translation::bedrock::COMMANDS_TAG_LIST_SINGLE_EMPTY, |
| 139 | + [name], |
| 140 | + ) |
| 141 | + } else { |
| 142 | + TextComponent::translate_cross( |
| 143 | + translation::java::COMMANDS_TAG_LIST_SINGLE_SUCCESS, |
| 144 | + translation::bedrock::COMMANDS_TAG_LIST_SINGLE_SUCCESS, |
| 145 | + [ |
| 146 | + name, |
| 147 | + TextComponent::text(all_tags.len().to_string()), |
| 148 | + tag_list, |
| 149 | + ], |
| 150 | + ) |
| 151 | + } |
| 152 | + } else { |
| 153 | + let count = TextComponent::text(targets.len().to_string()); |
| 154 | + if all_tags.is_empty() { |
| 155 | + TextComponent::translate_cross( |
| 156 | + translation::java::COMMANDS_TAG_LIST_MULTIPLE_EMPTY, |
| 157 | + translation::bedrock::COMMANDS_TAG_LIST_MULTIPLE_EMPTY, |
| 158 | + [count], |
| 159 | + ) |
| 160 | + } else { |
| 161 | + TextComponent::translate_cross( |
| 162 | + translation::java::COMMANDS_TAG_LIST_MULTIPLE_SUCCESS, |
| 163 | + translation::bedrock::COMMANDS_TAG_LIST_MULTIPLE_SUCCESS, |
| 164 | + [ |
| 165 | + count, |
| 166 | + TextComponent::text(all_tags.len().to_string()), |
| 167 | + tag_list, |
| 168 | + ], |
| 169 | + ) |
| 170 | + } |
| 171 | + }; |
| 172 | + |
| 173 | + context.source.send_feedback(msg, false).await; |
| 174 | + |
| 175 | + Ok(all_tags.len() as i32) |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +pub fn register(dispatcher: &mut CommandDispatcher, registry: &mut PermissionRegistry) { |
| 181 | + registry.register_permission_or_panic(Permission::new( |
| 182 | + PERMISSION, |
| 183 | + DESCRIPTION, |
| 184 | + PermissionDefault::Op(PermissionLvl::Two), |
| 185 | + )); |
| 186 | + |
| 187 | + dispatcher.register( |
| 188 | + command("tag", DESCRIPTION).requires(PERMISSION).then( |
| 189 | + argument(ARG_TARGETS, EntityArgumentType::Entities) |
| 190 | + .then( |
| 191 | + literal("add").then( |
| 192 | + argument(ARG_NAME, StringArgumentType::SingleWord) |
| 193 | + .executes(ChangeExecutor(Action::Add)), |
| 194 | + ), |
| 195 | + ) |
| 196 | + .then(literal("list").executes(ListExecutor)) |
| 197 | + .then( |
| 198 | + literal("remove").then( |
| 199 | + argument(ARG_NAME, StringArgumentType::SingleWord) |
| 200 | + .executes(ChangeExecutor(Action::Remove)), |
| 201 | + ), |
| 202 | + ), |
| 203 | + ), |
| 204 | + ); |
| 205 | +} |
0 commit comments