plugin.yml issues

Discussion in 'Plugin Development' started by killeBr, Jan 5, 2020.

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

    killeBr

    let me send you what my lines of code are:
    Player p = (Player) sender;
    if (p.hasPermission("speed.use")) {
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 600, 2));
    p.sendMessage(ChatColor.BOLD + Color.AQUA + "Speed Enabled!");

    Under ChatColor.BOLD + Color.AQUA is underlined with the following error.
     
  2. Online

    timtower Moderator Moderator

    @killeBr Put an empty string in between the two.
     
  3. Offline

    killeBr

    Like I said previously, Im really new to coding and just trying to learn my basics first. Im so sorry for all the questions, but how o I make an empty string.

    I am so sorry this is not a joke I legit dont know how to write this out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 9, 2020
  4. Offline

    KarimAKL

    @killeBr
    1. An empty string is just a string without any characters. (like this: "")
    2. I think you're looking for ChatColor.AQUA, not Color.AQUA.
    3. You can fix the error by using the toString() on the colors. (like this: ChatColor.BOLD.toString() + ChatColor.AQUA.toString())
     
  5. Offline

    killeBr

    thank you so much
     
  6. Offline

    Strahan

    Another option is to use the translator, so you could also do:
    Code:
    p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&l&bSpeed Enabled!"));
    PS as that's a lot to type, and I prefer to avoid hard coding messages anyway, I use a function that I call for messaging. You could do like:
    Code:
    private String cc(String msg) {
      return ChatColor.translateAlternateColorCodes('&', msg);
    }
    As I prefer flexible messaging, I do my config.yml like:
    Code:
    messages:
      access-denied: '&cAccess denied!'
      feature-enabled: '&aEnabled!'
      feature-disabled: '&cDisabled!'
      banned: '&eI have banned &6%player% &efor &6%reason%&e!'
    
    Then I have this to send the msg:
    Code:
    public void msg(String msgCode, CommandSender sender) {
        if (getConfig().getString("messages." + msgCode) == null) return;
        msgTransmit(getConfig().getString("messages." + msgCode), sender);
    }
    
    public void msg(String msgCode, CommandSender sender, Map<String, String> data) {
        if (getConfig().getString("messages." + msgCode) == null) return;
        String tmp = getConfig().getString("messages." + msgCode, msgCode);
        for (Map.Entry<String, String> mapData : data.entrySet()) {
          tmp = tmp.replace(mapData.getKey(), mapData.getValue());
        }
        msgTransmit(tmp, sender);
    }
    
    private void msgTransmit(String msg, CommandSender sender) {
        for (String m : (msg + " ").split("%br%")) {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', m));
        }
    }
    I put those in my main class, which is being passed to my command with dependency injection. Then I can leverage it thus:
    Code:
    onCommand() {
      Map<String, String> data = new HashMap<>();
      switch (args[0].toLowerCase()) {
      case "toggle":
        plugin.toggleFeature();
        plugin.msg("feature-" + (plugin.getFeatureStatus()?"enabled":"disabled"), sender);
        break;
    
      case "ban":
        Player target = plugin.getServer().getPlayer(args[1]);
        String reason = StringUtils.join(args, " ", 2, args.length);
    
        data.put("%player%", args[1]);
        data.put("%reason%", reason);
    
        plugin.banPlayer(target, reason);
        plugin.msg("banned", sender, data);
        break;
    }
    I'm skipping all the usual data/arg validation I do, and typically I prefer to call functions to handle the meat of a command, but as this is just for an example I'm not writing all that out heh. I'm fairly new to Java, so this may be a dumb method lol, it's just what I do.
     
    Last edited: Jan 10, 2020
Thread Status:
Not open for further replies.

Share This Page