Make TextComponent::translate simpler

This commit is contained in:
Alexander Medvedev 2025-02-16 18:47:30 +01:00
parent 4f3c146500
commit 148ac38fe3
38 changed files with 154 additions and 228 deletions

View File

@ -18,4 +18,4 @@ jobs:
- name: Checkout Actions Repository
uses: actions/checkout@v4
- name: Spell Check Repo
uses: crate-ci/typos@v1.29.5
uses: crate-ci/typos@v1.29.7

View File

@ -91,14 +91,15 @@ impl TextComponent {
})
}
pub fn translate<K>(key: K, with: Vec<TextComponent>) -> Self
pub fn translate<K, W>(key: K, with: W) -> Self
where
K: Into<Cow<'static, str>>,
W: Into<Vec<TextComponent>>,
{
Self(TextComponentBase {
content: TextContent::Translate {
translate: key.into(),
with: with.into_iter().map(|x| x.0).collect(),
with: with.into().into_iter().map(|x| x.0).collect(),
},
style: Style::default(),
extra: vec![],
@ -271,7 +272,7 @@ mod test {
fn test_serialize_text_component() {
let msg_comp = TextComponent::translate(
"multiplayer.player.joined",
[TextComponent::text("NAME".to_string())].into(),
[TextComponent::text("NAME".to_string())],
)
.color_named(NamedColor::Yellow);

View File

@ -70,7 +70,7 @@ async fn ban_player(sender: &CommandSender<'_>, player: &Player, reason: Option<
if banned_players.get_entry(&player.gameprofile).is_some() {
sender
.send_message(TextComponent::translate("commands.ban.failed", [].into()))
.send_message(TextComponent::translate("commands.ban.failed", []))
.await;
return;
}
@ -92,15 +92,14 @@ async fn ban_player(sender: &CommandSender<'_>, player: &Player, reason: Option<
[
TextComponent::text(player.gameprofile.name.clone()),
TextComponent::text(reason),
]
.into(),
],
))
.await;
player
.kick(TextComponent::translate(
"multiplayer.disconnect.banned",
[].into(),
[],
))
.await;
}

View File

@ -83,10 +83,7 @@ async fn ban_ip(sender: &CommandSender<'_>, server: &Server, target: &str, reaso
let Some(target_ip) = parse_ip(target, server).await else {
sender
.send_message(TextComponent::translate(
"commands.banip.invalid",
[].into(),
))
.send_message(TextComponent::translate("commands.banip.invalid", []))
.await;
return;
};
@ -95,7 +92,7 @@ async fn ban_ip(sender: &CommandSender<'_>, server: &Server, target: &str, reaso
if banned_ips.get_entry(&target_ip).is_some() {
sender
.send_message(TextComponent::translate("commands.banip.failed", [].into()))
.send_message(TextComponent::translate("commands.banip.failed", []))
.await;
return;
}
@ -124,8 +121,7 @@ async fn ban_ip(sender: &CommandSender<'_>, server: &Server, target: &str, reaso
[
TextComponent::text(target_ip.to_string()),
TextComponent::text(reason),
]
.into(),
],
))
.await;
@ -135,8 +131,7 @@ async fn ban_ip(sender: &CommandSender<'_>, server: &Server, target: &str, reaso
[
TextComponent::text(affected.len().to_string()),
TextComponent::text(names),
]
.into(),
],
))
.await;
@ -144,7 +139,7 @@ async fn ban_ip(sender: &CommandSender<'_>, server: &Server, target: &str, reaso
target
.kick(TextComponent::translate(
"multiplayer.disconnect.ip_banned",
[].into(),
[],
))
.await;
}

View File

@ -110,7 +110,7 @@ impl CommandExecutor for BanListAllExecutor {
async fn handle_banlist(list: Vec<(String, String, String)>, sender: &CommandSender<'_>) {
if list.is_empty() {
sender
.send_message(TextComponent::translate("commands.banlist.none", [].into()))
.send_message(TextComponent::translate("commands.banlist.none", []))
.await;
return;
}
@ -118,7 +118,7 @@ async fn handle_banlist(list: Vec<(String, String, String)>, sender: &CommandSen
sender
.send_message(TextComponent::translate(
"commands.banlist.list",
[TextComponent::text(list.len().to_string())].into(),
[TextComponent::text(list.len().to_string())],
))
.await;
@ -130,8 +130,7 @@ async fn handle_banlist(list: Vec<(String, String, String)>, sender: &CommandSen
TextComponent::text(name),
TextComponent::text(source),
TextComponent::text(reason),
]
.into(),
],
))
.await;
}

View File

@ -76,7 +76,7 @@ impl CommandExecutor for BossbarAddExecuter {
sender,
TextComponent::translate(
"commands.bossbar.create.failed",
vec![TextComponent::text(namespace.to_string())],
[TextComponent::text(namespace.to_string())],
),
)
.await;
@ -93,7 +93,7 @@ impl CommandExecutor for BossbarAddExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.create.success",
vec![bossbar_prefix(bossbar.title.clone(), namespace.to_string())],
[bossbar_prefix(bossbar.title.clone(), namespace.to_string())],
))
.await;
@ -129,7 +129,7 @@ impl CommandExecutor for BossbarGetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.get.max",
vec![
[
bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
@ -145,7 +145,7 @@ impl CommandExecutor for BossbarGetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.get.value",
vec![
[
bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
@ -165,7 +165,7 @@ impl CommandExecutor for BossbarGetExecuter {
sender
.send_message(TextComponent::translate(
state,
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -194,7 +194,7 @@ impl CommandExecutor for BossbarListExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.list.bars.none",
vec![],
[],
))
.await;
return Ok(());
@ -203,7 +203,7 @@ impl CommandExecutor for BossbarListExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.list.bars.none",
vec![],
[],
))
.await;
return Ok(());
@ -228,7 +228,7 @@ impl CommandExecutor for BossbarListExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.list.bars.some",
vec![
[
TextComponent::text(bossbars.len().to_string()),
bossbars_text,
],
@ -264,7 +264,7 @@ impl CommandExecutor for BossbarRemoveExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.remove.success",
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -330,7 +330,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.color.success",
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -344,7 +344,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender,
TextComponent::translate(
"parsing.int.invalid",
vec![TextComponent::text(i32::MAX.to_string())],
[TextComponent::text(i32::MAX.to_string())],
),
)
.await;
@ -373,7 +373,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.max.success",
vec![
[
bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
@ -403,7 +403,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.name.success",
vec![bossbar_prefix(text_component, namespace.to_string())],
[bossbar_prefix(text_component, namespace.to_string())],
))
.await;
Ok(())
@ -426,7 +426,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.players.success.none",
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -463,7 +463,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.players.success.some",
vec![
[
bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
@ -493,7 +493,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.style.success",
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -507,7 +507,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender,
TextComponent::translate(
"parsing.int.invalid",
vec![TextComponent::text(i32::MAX.to_string())],
[TextComponent::text(i32::MAX.to_string())],
),
)
.await;
@ -531,7 +531,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
"commands.bossbar.set.value.success",
vec![
[
bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
@ -568,7 +568,7 @@ impl CommandExecutor for BossbarSetExecuter {
sender
.send_message(TextComponent::translate(
state,
vec![bossbar_prefix(
[bossbar_prefix(
bossbar.bossbar_data.title.clone(),
namespace.to_string(),
)],
@ -682,7 +682,7 @@ async fn handle_bossbar_error(sender: &CommandSender<'_>, error: BossbarUpdateEr
sender,
TextComponent::translate(
"commands.bossbar.unknown",
vec![TextComponent::text(location)],
[TextComponent::text(location)],
),
)
.await;
@ -695,7 +695,7 @@ async fn handle_bossbar_error(sender: &CommandSender<'_>, error: BossbarUpdateEr
key.push_str(&format!(".{variation}"));
}
send_error_message(sender, TextComponent::translate(key, vec![])).await;
send_error_message(sender, TextComponent::translate(key, [])).await;
}
}
}

View File

@ -40,12 +40,12 @@ fn clear_command_text_output(item_count: usize, targets: &[Arc<Player>]) -> Text
match targets {
[target] if item_count == 0 => TextComponent::translate(
"clear.failed.single",
vec![TextComponent::text(target.gameprofile.name.clone())],
[TextComponent::text(target.gameprofile.name.clone())],
)
.color_named(NamedColor::Red),
[target] => TextComponent::translate(
"commands.clear.success.single",
vec![
[
TextComponent::text(item_count.to_string()),
TextComponent::text(target.gameprofile.name.clone())
.click_event(ClickEvent::SuggestCommand(
@ -60,12 +60,12 @@ fn clear_command_text_output(item_count: usize, targets: &[Arc<Player>]) -> Text
),
targets if item_count == 0 => TextComponent::translate(
"clear.failed.multiple",
vec![TextComponent::text(targets.len().to_string())],
[TextComponent::text(targets.len().to_string())],
)
.color_named(NamedColor::Red),
targets => TextComponent::translate(
"commands.clear.success.multiple",
vec![
[
TextComponent::text(item_count.to_string()),
TextComponent::text(targets.len().to_string()),
],

View File

@ -41,7 +41,7 @@ async fn send_damage_result(
if !success {
sender
.send_message(
TextComponent::translate("commands.damage.invulnerable", [].into())
TextComponent::translate("commands.damage.invulnerable", [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -54,8 +54,7 @@ async fn send_damage_result(
[
TextComponent::text(amount.to_string()),
TextComponent::text(target_name),
]
.into(),
],
))
.await;
}

View File

@ -51,7 +51,7 @@ impl CommandExecutor for DeopExecutor {
let player_name = &player.gameprofile.name;
let msg = TextComponent::translate(
"commands.deop.success",
[TextComponent::text(player_name.clone())].into(),
[TextComponent::text(player_name.clone())],
);
sender.send_message(msg).await;
}

View File

@ -59,8 +59,7 @@ impl ExperienceExecutor {
[
TextComponent::text(target.gameprofile.name.clone()),
TextComponent::text(level.to_string()),
]
.into(),
],
))
.await;
}
@ -72,8 +71,7 @@ impl ExperienceExecutor {
[
TextComponent::text(target.gameprofile.name.clone()),
TextComponent::text(points.to_string()),
]
.into(),
],
))
.await;
}
@ -95,8 +93,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(targets_len.to_string()),
]
.into(),
],
)
} else {
TextComponent::translate(
@ -104,8 +101,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(target_name.unwrap()),
]
.into(),
],
)
}
}
@ -116,8 +112,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(targets_len.to_string()),
]
.into(),
],
)
} else {
TextComponent::translate(
@ -125,8 +120,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(target_name.unwrap()),
]
.into(),
],
)
}
}
@ -137,8 +131,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(targets_len.to_string()),
]
.into(),
],
)
} else {
TextComponent::translate(
@ -146,8 +139,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(target_name.unwrap()),
]
.into(),
],
)
}
}
@ -158,8 +150,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(targets_len.to_string()),
]
.into(),
],
)
} else {
TextComponent::translate(
@ -167,8 +158,7 @@ impl ExperienceExecutor {
[
TextComponent::text(amount.to_string()),
TextComponent::text(target_name.unwrap()),
]
.into(),
],
)
}
}
@ -236,7 +226,7 @@ impl CommandExecutor for ExperienceExecutor {
sender
.send_message(TextComponent::translate(
"commands.experience.set.points.invalid",
[].into(),
[],
))
.await;
return Ok(());
@ -246,7 +236,7 @@ impl CommandExecutor for ExperienceExecutor {
sender
.send_message(TextComponent::translate(
"commands.experience.set.points.invalid",
[].into(),
[],
))
.await;
return Ok(());
@ -270,7 +260,7 @@ impl CommandExecutor for ExperienceExecutor {
Err(error_msg) => {
sender
.send_message(
TextComponent::translate(error_msg, [].into())
TextComponent::translate(error_msg, [])
.color(Color::Named(NamedColor::Red)),
)
.await;

View File

@ -151,7 +151,7 @@ impl CommandExecutor for SetblockExecutor {
sender
.send_message(TextComponent::translate(
"commands.fill.success",
[TextComponent::text(placed_blocks.to_string())].into(),
[TextComponent::text(placed_blocks.to_string())],
))
.await;

View File

@ -45,7 +45,7 @@ impl CommandExecutor for GamemodeTargetSelf {
target
.send_system_message(&TextComponent::translate(
"commands.gamemode.success.self",
[TextComponent::translate(gamemode_string, [].into())].into(),
[TextComponent::translate(gamemode_string, [])],
))
.await;
}
@ -83,7 +83,7 @@ impl CommandExecutor for GamemodeTargetPlayer {
target
.send_system_message(&TextComponent::translate(
"gameMode.changed",
[TextComponent::translate(gamemode_string.clone(), [].into())].into(),
[TextComponent::translate(gamemode_string.clone(), [])],
))
.await;
if target_count == 1 {
@ -92,9 +92,8 @@ impl CommandExecutor for GamemodeTargetPlayer {
"commands.gamemode.success.other",
[
TextComponent::text(target.gameprofile.name.clone()),
TextComponent::translate(gamemode_string, [].into()),
]
.into(),
TextComponent::translate(gamemode_string, []),
],
))
.await;
}

View File

@ -83,8 +83,7 @@ impl CommandExecutor for GiveExecutor {
.click_event(ClickEvent::SuggestCommand(
format!("/tell {} ", targets[0].gameprofile.name.clone()).into(),
)),
]
.into(),
],
)
} else {
TextComponent::translate(
@ -100,8 +99,7 @@ impl CommandExecutor for GiveExecutor {
tag: None,
}),
TextComponent::text(targets.len().to_string()),
]
.into(),
],
)
};
sender.send_message(msg).await;

View File

@ -34,7 +34,7 @@ impl CommandExecutor for KickExecutor {
let reason = match args.get(&ARG_REASON) {
Some(Arg::Msg(r)) => TextComponent::text(r.clone()),
_ => TextComponent::translate("multiplayer.disconnect.kicked", [].into()),
_ => TextComponent::translate("multiplayer.disconnect.kicked", []),
};
for target in targets {

View File

@ -52,11 +52,11 @@ impl CommandExecutor for KillExecutor {
));
}
TextComponent::translate("commands.kill.success.single", [entity_display].into())
TextComponent::translate("commands.kill.success.single", [entity_display])
} else {
TextComponent::translate(
"commands.kill.success.multiple",
[TextComponent::text(target_count.to_string())].into(),
[TextComponent::text(target_count.to_string())],
)
};
@ -93,8 +93,7 @@ impl CommandExecutor for KillSelfExecutor {
))
.click_event(ClickEvent::SuggestCommand(
format!("/tell {} ", name.clone()).into(),
))]
.into(),
))],
))
.await;

View File

@ -34,8 +34,7 @@ impl CommandExecutor for ListExecutor {
TextComponent::text(players.len().to_string()),
TextComponent::text(BASIC_CONFIG.max_players.to_string()),
TextComponent::text(get_player_names(players)),
]
.into(),
],
))
.await;

View File

@ -39,7 +39,7 @@ impl CommandExecutor for OpExecutor {
if player.permission_lvl.load() == new_level {
sender
.send_message(TextComponent::translate("commands.op.failed", [].into()))
.send_message(TextComponent::translate("commands.op.failed", []))
.await;
continue;
}
@ -70,7 +70,7 @@ impl CommandExecutor for OpExecutor {
sender
.send_message(TextComponent::translate(
"commands.op.success",
[TextComponent::text(player_name.clone())].into(),
[TextComponent::text(player_name.clone())],
))
.await;
}

View File

@ -41,10 +41,7 @@ impl CommandExecutor for PardonExecutor {
lock.banned_players.remove(idx);
} else {
sender
.send_message(TextComponent::translate(
"commands.pardon.failed",
[].into(),
))
.send_message(TextComponent::translate("commands.pardon.failed", []))
.await;
return Ok(());
}
@ -54,7 +51,7 @@ impl CommandExecutor for PardonExecutor {
sender
.send_message(TextComponent::translate(
"commands.pardon.success",
[TextComponent::text(target)].into(),
[TextComponent::text(target)],
))
.await;
Ok(())

View File

@ -34,10 +34,7 @@ impl CommandExecutor for PardonIpExecutor {
let Ok(ip) = IpAddr::from_str(target) else {
sender
.send_message(TextComponent::translate(
"commands.pardonip.invalid",
[].into(),
))
.send_message(TextComponent::translate("commands.pardonip.invalid", []))
.await;
return Ok(());
};
@ -48,10 +45,7 @@ impl CommandExecutor for PardonIpExecutor {
lock.banned_ips.remove(idx);
} else {
sender
.send_message(TextComponent::translate(
"commands.pardonip.failed",
[].into(),
))
.send_message(TextComponent::translate("commands.pardonip.failed", []))
.await;
return Ok(());
}
@ -61,7 +55,7 @@ impl CommandExecutor for PardonIpExecutor {
sender
.send_message(TextComponent::translate(
"commands.pardonip.success",
[TextComponent::text(ip.to_string())].into(),
[TextComponent::text(ip.to_string())],
))
.await;
Ok(())

View File

@ -52,7 +52,7 @@ impl CommandExecutor for ParticleExecutor {
sender
.send_message(TextComponent::translate(
"commands.particle.success",
[TextComponent::text(format!("{particle:?}"))].into(),
[TextComponent::text(format!("{particle:?}"))],
))
.await;
}

View File

@ -80,9 +80,9 @@ impl CommandExecutor for SoundExecutor {
// Get target players, defaults to sender if not specified
let targets = if let Ok(players) = PlayersArgumentConsumer::find_arg(args, ARG_TARGETS) {
players.to_vec()
players
} else if let Some(player) = sender.as_player() {
vec![player.clone()]
&[player.clone()]
} else {
return Ok(());
};
@ -115,7 +115,7 @@ impl CommandExecutor for SoundExecutor {
let mut players_who_heard = 0;
// Play sound for each target player
for target in &targets {
for target in targets {
let pos = position.unwrap_or(target.living_entity.entity.pos.load());
// Check if player can hear the sound based on volume and distance
@ -134,10 +134,7 @@ impl CommandExecutor for SoundExecutor {
// Send appropriate message based on results
if players_who_heard == 0 {
sender
.send_message(TextComponent::translate(
"commands.playsound.failed",
[].into(),
))
.send_message(TextComponent::translate("commands.playsound.failed", []))
.await;
} else {
let sound_name = sound.to_name();
@ -148,8 +145,7 @@ impl CommandExecutor for SoundExecutor {
[
TextComponent::text(sound_name),
TextComponent::text(targets[0].gameprofile.name.clone()),
]
.into(),
],
))
.await;
} else {
@ -159,8 +155,7 @@ impl CommandExecutor for SoundExecutor {
[
TextComponent::text(sound_name),
TextComponent::text(players_who_heard.to_string()),
]
.into(),
],
))
.await;
}

View File

@ -43,11 +43,10 @@ impl CommandExecutor for PumpkinExecutor {
[TextComponent::text(seed.clone())
.hover_event(HoverEvent::show_text(TextComponent::translate(
Cow::from("chat.copy.click"),
vec![],
[],
)))
.click_event(ClickEvent::CopyToClipboard(Cow::from(seed)))
.color_named(NamedColor::Green)]
.into(),
.color_named(NamedColor::Green)],
))
.await;
Ok(())

View File

@ -75,11 +75,10 @@ impl CommandExecutor for SetblockExecutor {
TextComponent::text(pos.0.x.to_string()),
TextComponent::text(pos.0.y.to_string()),
TextComponent::text(pos.0.z.to_string()),
]
.into(),
],
)
} else {
TextComponent::translate("commands.setblock.failed", [].into())
TextComponent::translate("commands.setblock.failed", [])
})
.await;

View File

@ -22,8 +22,7 @@ impl CommandExecutor for StopExecutor {
) -> Result<(), CommandError> {
sender
.send_message(
TextComponent::translate("commands.stop.stopping", [].into())
.color_named(NamedColor::Red),
TextComponent::translate("commands.stop.stopping", []).color_named(NamedColor::Red),
)
.await;

View File

@ -42,7 +42,7 @@ impl CommandExecutor for SummonExecutor {
sender
.send_message(TextComponent::translate(
"commands.summon.success",
[TextComponent::text(format!("{entity:?}"))].into(),
[TextComponent::text(format!("{entity:?}"))],
))
.await;
}

View File

@ -195,10 +195,7 @@ impl CommandExecutor for TpSelfToEntityExecutor {
}
_ => {
sender
.send_message(TextComponent::translate(
"permissions.requires.player",
[].into(),
))
.send_message(TextComponent::translate("permissions.requires.player", []))
.await;
}
};
@ -226,10 +223,7 @@ impl CommandExecutor for TpSelfToPosExecutor {
}
_ => {
sender
.send_message(TextComponent::translate(
"permissions.requires.player",
[].into(),
))
.send_message(TextComponent::translate("permissions.requires.player", []))
.await;
}
};

View File

@ -67,21 +67,21 @@ impl CommandExecutor for TimeQueryExecutor {
let curr_time = level_time.query_daytime();
TextComponent::translate(
"commands.time.query",
[TextComponent::text(curr_time.to_string())].into(),
[TextComponent::text(curr_time.to_string())],
)
}
QueryMode::GameTime => {
let curr_time = level_time.query_gametime();
TextComponent::translate(
"commands.time.query",
[TextComponent::text(curr_time.to_string())].into(),
[TextComponent::text(curr_time.to_string())],
)
}
QueryMode::Day => {
let curr_time = level_time.query_day();
TextComponent::translate(
"commands.time.query",
[TextComponent::text(curr_time.to_string())].into(),
[TextComponent::text(curr_time.to_string())],
)
}
};
@ -131,7 +131,7 @@ impl CommandExecutor for TimeChangeExecutor {
let curr_time = level_time.query_daytime();
TextComponent::translate(
"commands.time.add",
[TextComponent::text(curr_time.to_string())].into(),
[TextComponent::text(curr_time.to_string())],
)
}
Mode::Set(_) => {
@ -140,7 +140,7 @@ impl CommandExecutor for TimeChangeExecutor {
level_time.send_time(world).await;
TextComponent::translate(
"commands.time.set",
[TextComponent::text(time_count.to_string())].into(),
[TextComponent::text(time_count.to_string())],
)
}
};

View File

@ -50,7 +50,7 @@ impl CommandExecutor for ClearOrResetExecutor {
};
TextComponent::translate(
text,
[TextComponent::text(targets[0].gameprofile.name.clone())].into(),
[TextComponent::text(targets[0].gameprofile.name.clone())],
)
} else {
let text = if reset {
@ -58,10 +58,7 @@ impl CommandExecutor for ClearOrResetExecutor {
} else {
"commands.title.cleared.multiple"
};
TextComponent::translate(
text,
[TextComponent::text(targets.len().to_string())].into(),
)
TextComponent::translate(text, [TextComponent::text(targets.len().to_string())])
})
.await;
@ -96,12 +93,12 @@ impl CommandExecutor for TitleExecutor {
.send_message(if targets.len() == 1 {
TextComponent::translate(
format!("commands.title.show.{mode_name}.single"),
[TextComponent::text(targets[0].gameprofile.name.clone())].into(),
[TextComponent::text(targets[0].gameprofile.name.clone())],
)
} else {
TextComponent::translate(
format!("commands.title.show.{mode_name}.multiple"),
[TextComponent::text(targets.len().to_string())].into(),
[TextComponent::text(targets.len().to_string())],
)
})
.await;

View File

@ -43,10 +43,7 @@ impl CommandExecutor for WeatherExecutor {
.set_weather_parameters(&world, duration, 0, false, false)
.await;
sender
.send_message(TextComponent::translate(
"commands.weather.set.clear",
[].into(),
))
.send_message(TextComponent::translate("commands.weather.set.clear", []))
.await;
}
WeatherMode::Rain => {
@ -54,10 +51,7 @@ impl CommandExecutor for WeatherExecutor {
.set_weather_parameters(&world, 0, duration, true, false)
.await;
sender
.send_message(TextComponent::translate(
"commands.weather.set.rain",
[].into(),
))
.send_message(TextComponent::translate("commands.weather.set.rain", []))
.await;
}
WeatherMode::Thunder => {
@ -65,10 +59,7 @@ impl CommandExecutor for WeatherExecutor {
.set_weather_parameters(&world, 0, duration, true, true)
.await;
sender
.send_message(TextComponent::translate(
"commands.weather.set.thunder",
[].into(),
))
.send_message(TextComponent::translate("commands.weather.set.thunder", []))
.await;
}
}

View File

@ -69,7 +69,7 @@ impl CommandExecutor for WorldborderGetExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.get",
[TextComponent::text(diameter.to_string())].into(),
[TextComponent::text(diameter.to_string())],
))
.await;
Ok(())
@ -109,7 +109,7 @@ impl CommandExecutor for WorldborderSetExecutor {
if (distance - border.new_diameter).abs() < f64::EPSILON {
sender
.send_message(
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [].into())
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -120,7 +120,7 @@ impl CommandExecutor for WorldborderSetExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.set.immediate",
[TextComponent::text(dist)].into(),
[TextComponent::text(dist)],
))
.await;
border.set_diameter(world, distance, None).await;
@ -174,7 +174,7 @@ impl CommandExecutor for WorldborderSetTimeExecutor {
std::cmp::Ordering::Equal => {
sender
.send_message(
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [].into())
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -188,8 +188,7 @@ impl CommandExecutor for WorldborderSetTimeExecutor {
[
TextComponent::text(dist),
TextComponent::text(time.to_string()),
]
.into(),
],
))
.await;
}
@ -201,8 +200,7 @@ impl CommandExecutor for WorldborderSetTimeExecutor {
[
TextComponent::text(dist),
TextComponent::text(time.to_string()),
]
.into(),
],
))
.await;
}
@ -248,7 +246,7 @@ impl CommandExecutor for WorldborderAddExecutor {
if distance == 0.0 {
sender
.send_message(
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [].into())
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -261,7 +259,7 @@ impl CommandExecutor for WorldborderAddExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.set.immediate",
[TextComponent::text(dist)].into(),
[TextComponent::text(dist)],
))
.await;
border.set_diameter(world, distance, None).await;
@ -317,7 +315,7 @@ impl CommandExecutor for WorldborderAddTimeExecutor {
std::cmp::Ordering::Equal => {
sender
.send_message(
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [].into())
TextComponent::translate(NOTHING_CHANGED_EXCEPTION, [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -331,8 +329,7 @@ impl CommandExecutor for WorldborderAddTimeExecutor {
[
TextComponent::text(dist),
TextComponent::text(time.to_string()),
]
.into(),
],
))
.await;
}
@ -344,8 +341,7 @@ impl CommandExecutor for WorldborderAddTimeExecutor {
[
TextComponent::text(dist),
TextComponent::text(time.to_string()),
]
.into(),
],
))
.await;
}
@ -383,8 +379,7 @@ impl CommandExecutor for WorldborderCenterExecutor {
[
TextComponent::text(format!("{x:.2}")),
TextComponent::text(format!("{z:.2}")),
]
.into(),
],
))
.await;
border.set_center(world, x, z).await;
@ -425,11 +420,8 @@ impl CommandExecutor for WorldborderDamageAmountExecutor {
if (damage_per_block - border.damage_per_block).abs() < f32::EPSILON {
sender
.send_message(
TextComponent::translate(
"commands.worldborder.damage.amount.failed",
[].into(),
)
.color(Color::Named(NamedColor::Red)),
TextComponent::translate("commands.worldborder.damage.amount.failed", [])
.color(Color::Named(NamedColor::Red)),
)
.await;
return Ok(());
@ -439,7 +431,7 @@ impl CommandExecutor for WorldborderDamageAmountExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.damage.amount.success",
[TextComponent::text(damage)].into(),
[TextComponent::text(damage)],
))
.await;
border.damage_per_block = damage_per_block;
@ -480,11 +472,8 @@ impl CommandExecutor for WorldborderDamageBufferExecutor {
if (buffer - border.buffer).abs() < f32::EPSILON {
sender
.send_message(
TextComponent::translate(
"commands.worldborder.damage.buffer.failed",
[].into(),
)
.color(Color::Named(NamedColor::Red)),
TextComponent::translate("commands.worldborder.damage.buffer.failed", [])
.color(Color::Named(NamedColor::Red)),
)
.await;
return Ok(());
@ -494,7 +483,7 @@ impl CommandExecutor for WorldborderDamageBufferExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.damage.buffer.success",
[TextComponent::text(buf)].into(),
[TextComponent::text(buf)],
))
.await;
border.buffer = buffer;
@ -535,11 +524,8 @@ impl CommandExecutor for WorldborderWarningDistanceExecutor {
if distance == border.warning_blocks {
sender
.send_message(
TextComponent::translate(
"commands.worldborder.warning.distance.failed",
[].into(),
)
.color(Color::Named(NamedColor::Red)),
TextComponent::translate("commands.worldborder.warning.distance.failed", [])
.color(Color::Named(NamedColor::Red)),
)
.await;
return Ok(());
@ -548,7 +534,7 @@ impl CommandExecutor for WorldborderWarningDistanceExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.warning.distance.success",
[TextComponent::text(distance.to_string())].into(),
[TextComponent::text(distance.to_string())],
))
.await;
border.set_warning_distance(world, distance).await;
@ -589,7 +575,7 @@ impl CommandExecutor for WorldborderWarningTimeExecutor {
if time == border.warning_time {
sender
.send_message(
TextComponent::translate("commands.worldborder.warning.time.failed", [].into())
TextComponent::translate("commands.worldborder.warning.time.failed", [])
.color(Color::Named(NamedColor::Red)),
)
.await;
@ -599,7 +585,7 @@ impl CommandExecutor for WorldborderWarningTimeExecutor {
sender
.send_message(TextComponent::translate(
"commands.worldborder.warning.time.success",
[TextComponent::text(time.to_string())].into(),
[TextComponent::text(time.to_string())],
))
.await;
border.set_warning_delay(world, time).await;

View File

@ -55,7 +55,7 @@ impl LivingEntity {
}
}
pub async fn send_equipment_changes(&self, equipment: Vec<(EquipmentSlot, ItemStack)>) {
pub async fn send_equipment_changes(&self, equipment: &[(EquipmentSlot, ItemStack)]) {
let equipment: Vec<(EquipmentSlot, Slot)> = equipment
.iter()
.map(|(slot, stack)| (*slot, Slot::from(stack)))

View File

@ -504,7 +504,7 @@ impl Player {
.wait_for_keep_alive
.load(std::sync::atomic::Ordering::Relaxed)
{
self.kick(TextComponent::translate("disconnect.timeout", [].into()))
self.kick(TextComponent::translate("disconnect.timeout", []))
.await;
return;
}

View File

@ -552,15 +552,14 @@ impl Client {
if let Some(entry) = banned_players.get_entry(profile) {
let text = TextComponent::translate(
"multiplayer.disconnect.banned.reason",
vec![TextComponent::text(entry.reason.clone())],
[TextComponent::text(entry.reason.clone())],
);
return Some(match entry.expires {
Some(expires) => text.add_child(TextComponent::translate(
"multiplayer.disconnect.banned.expiration",
[TextComponent::text(
expires.format("%F at %T %Z").to_string(),
)]
.into(),
)],
)),
None => text,
});
@ -572,15 +571,14 @@ impl Client {
if let Some(entry) = banned_ips.get_entry(&address.ip()) {
let text = TextComponent::translate(
"multiplayer.disconnect.banned_ip.reason",
vec![TextComponent::text(entry.reason.clone())],
[TextComponent::text(entry.reason.clone())],
);
return Some(match entry.expires {
Some(expires) => text.add_child(TextComponent::translate(
"multiplayer.disconnect.banned_ip.expiration",
[TextComponent::text(
expires.format("%F at %T %Z").to_string(),
)]
.into(),
)],
)),
None => text,
});

View File

@ -20,7 +20,7 @@ impl Client {
std::cmp::Ordering::Less => {
self.kick(&TextComponent::translate(
"multiplayer.disconnect.outdated_client",
[TextComponent::text(CURRENT_MC_VERSION.to_string())].into(),
[TextComponent::text(CURRENT_MC_VERSION.to_string())],
))
.await;
}
@ -28,7 +28,7 @@ impl Client {
std::cmp::Ordering::Greater => {
self.kick(&TextComponent::translate(
"multiplayer.disconnect.incompatible",
[TextComponent::text(CURRENT_MC_VERSION.to_string())].into(),
[TextComponent::text(CURRENT_MC_VERSION.to_string())],
))
.await;
}

View File

@ -95,7 +95,7 @@ impl Client {
if max_players > 0 && server.get_player_count().await >= max_players as usize {
self.kick(&TextComponent::translate(
"multiplayer.disconnect.server_full",
[].into(),
[],
))
.await;
return;
@ -189,13 +189,12 @@ impl Client {
Ok(new_profile) => *profile = new_profile,
Err(error) => {
self.kick(&match error {
AuthError::FailedResponse => TextComponent::translate(
"multiplayer.disconnect.authservers_down",
[].into(),
),
AuthError::FailedResponse => {
TextComponent::translate("multiplayer.disconnect.authservers_down", [])
}
AuthError::UnverifiedUsername => TextComponent::translate(
"multiplayer.disconnect.unverified_username",
[].into(),
[],
),
e => TextComponent::text(e.to_string()),
})
@ -209,7 +208,7 @@ impl Client {
log::debug!("Player (IP '{}', username '{}') tried to log in with the same UUID ('{}') as an online player (IP '{}', username '{}')", &self.address.lock().await, &profile.name, &profile.id, &online_player.client.address.lock().await, &online_player.gameprofile.name);
self.kick(&TextComponent::translate(
"multiplayer.disconnect.duplicate_login",
[].into(),
[],
))
.await;
return;
@ -220,7 +219,7 @@ impl Client {
log::debug!("A player (IP '{}', attempted username '{}') tried to log in with the same username as an online player (UUID '{}', IP '{}', username '{}')", &self.address.lock().await, &profile.name, &profile.id, &online_player.client.address.lock().await, &online_player.gameprofile.name);
self.kick(&TextComponent::translate(
"multiplayer.disconnect.duplicate_login",
[].into(),
[],
))
.await;
return;

View File

@ -143,7 +143,7 @@ impl Player {
if position.x.is_nan() || position.y.is_nan() || position.z.is_nan() {
self.kick(TextComponent::translate(
"multiplayer.disconnect.invalid_player_movement",
[].into(),
[],
))
.await;
return;
@ -232,7 +232,7 @@ impl Player {
{
self.kick(TextComponent::translate(
"multiplayer.disconnect.invalid_player_movement",
[].into(),
[],
))
.await;
return;
@ -329,7 +329,7 @@ impl Player {
if !rotation.yaw.is_finite() || !rotation.pitch.is_finite() {
self.kick(TextComponent::translate(
"multiplayer.disconnect.invalid_player_movement",
[].into(),
[],
))
.await;
return;
@ -483,7 +483,7 @@ impl Player {
inventory.set_selected(dest_slot as u32);
let empty = &ItemStack::new(0, Item::AIR);
let stack = inventory.held_item().unwrap_or(empty);
let equipment = vec![(EquipmentSlot::MainHand, *stack)];
let equipment = &[(EquipmentSlot::MainHand, *stack)];
self.living_entity.send_equipment_changes(equipment).await;
self.client
.send_packet(&CSetHeldItem::new(dest_slot as i8))
@ -587,7 +587,7 @@ impl Player {
if message.chars().any(|c| c == '§' || c < ' ' || c == '\x7F') {
self.kick(TextComponent::translate(
"multiplayer.disconnect.illegal_characters",
[].into(),
[],
))
.await;
return;
@ -759,7 +759,7 @@ impl Player {
// this can't be triggered from a non-modded client.
self.kick(TextComponent::translate(
"multiplayer.disconnect.invalid_entity_attacked",
[].into(),
[],
))
.await;
return;
@ -793,7 +793,7 @@ impl Player {
);
self.kick(TextComponent::translate(
"multiplayer.disconnect.invalid_entity_attacked",
[].into(),
[],
))
.await;
return;
@ -1141,7 +1141,7 @@ impl Player {
inv.set_selected(slot as u32);
let empty = &ItemStack::new(0, Item::AIR);
let stack = inv.held_item().unwrap_or(empty);
let equipment = vec![(EquipmentSlot::MainHand, *stack)];
let equipment = &[(EquipmentSlot::MainHand, *stack)];
self.living_entity.send_equipment_changes(equipment).await;
}

View File

@ -48,7 +48,7 @@ pub struct CachedStatus {
pub struct CachedBranding {
/// Cached Server brand buffer so we don't have to rebuild them every time a player joins
cached_server_brand: Vec<u8>,
cached_server_brand: Box<[u8]>,
}
impl CachedBranding {
@ -63,11 +63,11 @@ impl CachedBranding {
}
const BRAND: &str = "Pumpkin";
const BRAND_BYTES: &[u8] = Self::BRAND.as_bytes();
fn build_brand() -> Vec<u8> {
fn build_brand() -> Box<[u8]> {
let mut buf = Vec::new();
VarInt(Self::BRAND.len() as i32).encode(&mut buf);
buf.extend_from_slice(Self::BRAND_BYTES);
buf
buf.into_boxed_slice()
}
}

View File

@ -884,7 +884,7 @@ impl World {
tokio::spawn(async move {
let msg_comp = TextComponent::translate(
"multiplayer.player.joined",
[TextComponent::text(player.gameprofile.name.clone())].into(),
[TextComponent::text(player.gameprofile.name.clone())],
)
.color_named(NamedColor::Yellow);
let event = PlayerJoinEvent::new(player.clone(), msg_comp);
@ -943,7 +943,7 @@ impl World {
if fire_event {
let msg_comp = TextComponent::translate(
"multiplayer.player.left",
[TextComponent::text(player.gameprofile.name.clone())].into(),
[TextComponent::text(player.gameprofile.name.clone())],
)
.color_named(NamedColor::Yellow);
let event = PlayerLeaveEvent::new(player.clone(), msg_comp);