broadcast command through console and player

Discussion in 'Plugin Development' started by Shril, Jun 7, 2021.

  1. Offline

    Shril

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Player p = (Player) sender;
    StringBuilder message = new StringBuilder("");
    if (cmd.getName().equalsIgnoreCase("crypticbroadcast")) {
    if ((args.length == 0) && (p.hasPermission("ccmcessentials.broadcast"))) {
    p.sendMessage(ChatColor.RED + "Usage: /cb <message>");
    } else {
    for (String part : args) {
    if (!message.toString().equals(""));
    message.append(" ");

    message.append(part);
    }
    if ((p.hasPermission("ccmcessentials.broadcast")) || (sender instanceof ConsoleCommandSender)) {
    Bukkit.getServer().broadcastMessage(Utils.chat(message.toString()));
    } else {
    p.sendMessage(Utils.chat(plugin.getConfig().getString("commands.broadcast.no_permission")));
    }
    }
    }
    return false;
    }
    }

    I get an error whenever trying to send the command through console for a broadcast. Any ideas on how to fix it?
     
  2. Online

    timtower Administrator Administrator Moderator

    @Shril You are casting the sender to a player. You don't need that for this.
     
  3. Offline

    Shril

    @timtower what do you mean by that? I just started learning bukkit & java a couple of days ago
     
  4. Online

    timtower Administrator Administrator Moderator

    @Shril Player p = (Player) sender;
    You don't need that.
    And if you do it: check if it is a player first.
     
  5. Offline

    Shril

    You're probably way more knowledgeable about this than me, but n my understanding that line is what allows me to use p instead of the longer version.

    It worked through the console before when I had this same setup, but instead of:

    Code:
                    for (String part : args) {
                        if (!message.toString().equals(""));
                            message.append(" ");
                       
                        message.append(part);
                            }
    I had:

    Code:
    } else if(args.length >= 1){
                          String message = "";
                          for (String part : args) {
                               if (message != "") message += " ";
                               message += part;
                                   }
    This worked through player and console but only the first word in the message was broadcast.
    @timtower
    btw sorry if I'm not supposed to ping
     
  6. Online

    timtower Administrator Administrator Moderator

    @Shril And longer version is bad why?
    And you are not using "p" anywhere there
     
  7. Offline

    Shril

    @timtower
    It's the same as the 1st post to this thread. tbh it's not bad, I just learned the other way. The issue still at hand is why the first one can run through a player executing the command and not through the console.

    I should've added this earlier but here's the error that I receive once I try and execute the command through console:

    Unexpected exception while parsing console command "cb hello world"
    org.bukkit.command.CommandException: Unhandled exception executing command 'cb' in plugin CCMCEssentials v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:763) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:748) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:387) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:356) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1008) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:847) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_16_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.shril.ccmcessentials.commands.BroadcastCommand.onCommand(BroadcastCommand.java:26) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
    ... 9 more
     
    Last edited: Jun 8, 2021
  8. Online

    timtower Administrator Administrator Moderator

    @Shril Because you are still casting a commandsender to console:
    org.bukkit.craftbukkit.v1_16_R3.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
     
  9. Offline

    Shril

  10. Offline

    davidclue

Share This Page