Solved Make all players chat

Discussion in 'Plugin Development' started by FireRoz, Mar 7, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    FireRoz

    Hi, I'm having an issue with this code.
    Someone said this had worked but yeah it didn't.
    Code:
    String sudomsgall = StringUtils.join(args, " ", 1, args.length);
                            for (Player allp : Bukkit.getServer().getOnlinePlayers())
                                allp.chat(sudomsgall)
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&2&lSudo&8] &a&nYou've sudo'd&2 everyone" + " &a&nto say&f " + sudomsgall + "&a."));
                                Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7&o[" + sender.getName() + "&7&o: &esudo'd everyone" + " to say " + sudomsgall + "&7&o]"), "yoursudo.notify");
    Basically, no errors in Intellij but when I execute the messages are being sent but no one is being sudo'd.

    Ingame error:
    An internal error has occured while trying to perform this command.

    Console error:
    [ERROR] null
     
  2. Offline

    CraftCreeper6

    @FireRoz
    Is that the whole console log?

    Can I see the command code?

    What's supposed to happen?
     
    Last edited: Mar 7, 2021
  3. Offline

    Strahan

    Well, you code has some issues with its logic for operation but it works for me. No null error. When you post an error, you need to post the full stack trace. Just "[ERRROR] null" is practically useless. The stack trace will give us specifics. Also it's best to either post the full class with the stack trace, or if you want to stick to a snippet at least tell us to what line the stacktrace is referring.
     
  4. Offline

    Veyn12

    Where "{}" ?????
     
  5. Offline

    FireRoz

    what?
    this isn't skript :p

    this really is all the error...
    it just said [ERROR] null in console

    1) yes for the enabled part... practically nothing else.
    2) it's what i posted
    3) sudo all players to say the string

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.


    EDIT: I removed the sudo thingy and I still got the internal error. @CraftCreeper6 I'll do what you say and I'll try it


    EDIT VERSION 2: Now it just sends an internal error, nothing else worked ;P

    EDIT VERSION 69: (ps, sorry for the horrible joke I had to lol) here is full codededed
    Code:
    package davinchi.fireroz.yoursudo;
    
    import org.apache.commons.lang.StringUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import java.lang.String;
    
    import org.bukkit.entity.Player;
    
    public class SudoCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!sender.hasPermission("yoursudo.sudo")) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cSorry &6" + sender.getName() + "&c, you do not have permission to use this command."));
            } else {// sender doesn't have permission (bracket)
                if (args.length < 2) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aUsage: &2/sudo &f<player> <message>"));
    
                } else {// args.length
                    if (args[0].equalsIgnoreCase("*")) {
                        if (!sender.hasPermission("yoursudo.sudoall")) {
                            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cSorry &6" + sender.getName() + "&c, you do not have permission to use this command."));
                        } else {
                            Bukkit.getOnlinePlayers().forEach(allp -> {
                                String sudomsgall = StringUtils.join(args, " ", -1, args.length);
                                allp.chat(sudomsgall);
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&2&lSudo&8] &a&nYou've sudo'd&2 everyone" + " &a&nto say&f " + sudomsgall + "&a."));
                                Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7&o[" + sender.getName() + "&7&o: &asudo'd everyone" + " to say " + sudomsgall + "&7&o]"), "yoursudo.notify");
                            });
                        }
                    } // if it's * then do this
                    Player target = Bukkit.getPlayerExact(args[0]);
                    if (target.hasPermission("yoursudo.exempt")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&nYou cannot sudo this user."));
    
                    } else {// if target ghas permissino
    
                        if (target != null) {// if target is offline so
                            sender.sendMessage(ChatColor.RED + "This player is not online.");
    
                        // command
                    } else {
                            if (sender instanceof Player) {
                                Player player = (Player) sender;
                                String sudomsg = StringUtils.join(args, " ", 1, args.length);
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&2&lSudo&8] &a&nYou've sudo'd&2 " + target.getName() + " &a&nto say&f " + sudomsg + "&a."));
                                target.chat(sudomsg);
                                Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&7&o[" + sender.getName() + "&7&o: &asudo'd " + target.getName() + " to say " + sudomsg + "&7&o]"), "yoursudo.notify");
                            }
                            } // if sender instance of player
                        }
                    }
                }
    
                return true;
            }
      }
    
    
    \


    SOLVED: Used return false ;)
    that was
    okay
    thanks alll
     
    Last edited: Mar 9, 2021
  6. Offline

    CraftCreeper6

    @FireRoz
    There's more to the command code that could be a problem, such as, are you sending this command from console? If so then casting to player will throw null.

    In any case, your problem lies with the StringUtils#join() line.

    You are passing args#length as the final parameter to indicate you want the whole array. Length returns the number of elements in the list. Although, Java starts counting from 0 and that's true with indexing an array too. Use args#length - 1 to fix your problem.

    Though I'm hesitant because this would usually throw an IndexOutOfBoundsException and your console says it's a NullPointer. And that could be for two reasons, one is that the hosting service you use is TLDRing the stacktrace, or I am wrong about the args#length - 1. In either case, it's probably best you show some more code surrounding the command so I can help to find where the problem may actually lie.

    Let me know how it goes.
     
  7. Offline

    Strahan

    Yea, there are still a bunch of issues. As to the joining, you want collection, delimiter, starting index, end. So if your command is /sudo username text to have them say, you'd want StringUtils.join(args, " ", 1, args.length).
    Also:
    Consider returning in your negative logic checks to reduce unnecessary indentation
    You need to return in your wildcard logic block, because the way you have it, it will drop to the single user code after execution
    You are both sending a msg to the sender and broadcasting. I'd check if they already have notify and not msg them if so to avoid needless duplication of notification
    You are checking target to see if it's null, but incorrectly doing it after you've already attempted to call methods against it. Also your operator is backwards - you are telling them they are not on if it is NOT null.
    You are requiring sender to be a Player to use the single player sudo. Why? Nothing you do requires Player functionality. All you are doing is handicapping console functionality
    Consider using the config for messaging. It's more flexible in case you want to reword things, plus it will declutter the code.
    If I were going to refactor this, I'd probably end up with it looking like this:
    Show Spoiler
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!sender.hasPermission("yoursudo.sudo")) {
            msg(sender, "access-denied");
            return true;
        }
    
        if (args.length < 2) {
            msg(sender, "help");
            return true;
        }
    
        String sudomsg = StringUtils.join(args, " ", 1, args.length);
        Map<String, String> data = new HashMap<>();
        data.put("%sender%", (sender instanceof Player?((Player)sender).getName():"Console"));
        data.put("%target%", (args[0].equals("*")?"everyone":args[0]));
        data.put("%message%", sudomsg);
    
        if (args[0].equalsIgnoreCase("*")) {
            if (!sender.hasPermission("yoursudo.sudoall")) {
                msg(sender, "access-denied");
                return true;
            }
       
            Bukkit.getOnlinePlayers().forEach(allp -> { allp.chat(sudomsg); });
            msg("yoursudo.notify", "notify", data);
            if (sender.hasPermission("yoursudo.notify")) return true;
       
            msg(sender, "notify", data);
            return true;
        }
    
        Player target = Bukkit.getPlayerExact(args[0]);
        if (target == null) {
            msg(sender, "offline");
            return true;
        }
    
        if (target.hasPermission("yoursudo.exempt")) {
            msg(sender, "restricted");
            return true;
        }
    
        target.chat(sudomsg);
        msg("yoursudo.notify", "notify", data);
        if (sender.hasPermission("yoursudo.notify")) return true;
    
        msg(sender, "notify", data);
        return true;
    }
    
    public void msg(Object sender, String msgcode) {
        msg(sender, msgcode, Collections.emptyMap());
    }
    
    public void msg(Object sender, String msgcode, Map<String, String> data) {
        String msg = getConfig().getString("messages." + msgcode);
        if (msg == null) return;
    
        for (Map.Entry<String, String> datum : data.entrySet()) {
            msg = msg.replace(datum.getKey(), datum.getValue());
        }
    
        for (String line : msg.split("%br%")) {
            if (sender instanceof String) {
                Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', line), String.valueOf(sender));
                continue;
            }
         
            ((CommandSender)sender).sendMessage(ChatColor.translateAlternateColorCodes('&', line));
        }
    }
     
    Last edited: Mar 11, 2021
Thread Status:
Not open for further replies.

Share This Page