Implement /bcv ourselves instead of using the Command class

It was broken.
This commit is contained in:
Allink 2023-04-02 00:47:21 +01:00
parent d68dfb5b01
commit d3f55a0451
No known key found for this signature in database

View file

@ -2,14 +2,23 @@ package pw.kaboom.extras.commands;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.ChatColor; import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Collection;
public final class CommandBroadcastVanilla implements CommandExecutor { public final class CommandBroadcastVanilla implements CommandExecutor {
private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER =
LegacyComponentSerializer
.legacyAmpersand();
public boolean onCommand(final @Nonnull CommandSender sender, public boolean onCommand(final @Nonnull CommandSender sender,
final @Nonnull Command command, final @Nonnull Command command,
final @Nonnull String label, final @Nonnull String label,
@ -21,8 +30,29 @@ public final class CommandBroadcastVanilla implements CommandExecutor {
return true; return true;
} }
Command.broadcastCommandMessage(sender, ChatColor.translateAlternateColorCodes( final Component senderName = sender.name();
'&', String.join(" ", args))); final String input = String.join(" ", args);
final Component component = LEGACY_COMPONENT_SERIALIZER.deserialize(input);
final Component broadcastComponent = Component.translatable("chat.type.admin")
.decorate(TextDecoration.ITALIC)
.color(NamedTextColor.GRAY)
.args(senderName, component);
sender.sendMessage(component);
final Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers();
for (final Player onlinePlayer : onlinePlayers) {
if (onlinePlayer.equals(sender)) {
continue;
}
onlinePlayer.sendMessage(broadcastComponent);
}
final ConsoleCommandSender consoleCommandSender = Bukkit.getConsoleSender();
consoleCommandSender.sendMessage(broadcastComponent);
return true; return true;
} }
} }