How to use args from an onCommand with AysncPlayerChatEvent

Discussion in 'Plugin Development' started by PumpMelon, Aug 20, 2016.

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

    PumpMelon

    I tested it without and with the dot. Neither worked.
     
  2. @PumpMelon
    Did it solve the IllegalArgumentException?
     
  3. Offline

    PumpMelon

    @AlvinB
    Nope :(
    My path isn't empty either though...
    Code:
    public void loadConfiguration(){
            String path = "Player.Prefix";
            getConfig().addDefault(path, "prefixstring");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
     
  4. Offline

    Zombie_Striker

    For this, you are setting the path "Player" to hold the player's name, but then you are overriding it by setting the prefix at that location. Remove the first line, since it does not work and has not real use, and either keep the second line the same if you want all players to have this prefix, or change this to "prefix.(Player's UUID)" if you want each individual player to have their own prefix.(which is what I think you want)
     
  5. @PumpMelon
    Well, that error is thrown if the path is invalid, which "Player." is, since it ends with a dot. Are you sure editing that doesn't fix it?

    Also, how do you want your config to look? I think you're doing a mistake somehow, and you haven't told us what the config should look like. Because right now it'll look like this:
    Code:text
    1. Player: <insert UUID here>
    2. Player:
    3. Prefix: <insert prefix here>

    And now that I think about it.. that doesn't work, since you can't have two keys named "Player".

    EDIT: And.. Ninja'd..
     
  6. Offline

    PumpMelon

    @Zombie_Striker


    Yeah, each having their own is what I want.
    Ok so I've tried
    Code:
     getConfig().set(prefixstr, pUUID); 
    but that makes it apply for all players when I tested it. Not sure if that's wrong or if my PlayerChatEvent is wrong.
    Code:
    @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            if(getConfig().getString("Player.") != null){
                p.setDisplayName(getConfig().getString("Player.Prefix") + " " + p.getName());
                }
                }
       
        }
    
    
    
    @AlvinB

    something like
    Code:
    Player: PlayerUUID
       Prefix: Playerprefix
    Player: OtherPlayerUUID
       Prefix: OtherPlayerprefix
    
     
    Last edited: Aug 26, 2016
  7. That will always be null as it is not a valid path.

    Also, you cannot do Player: UUID then have a key of Prefix, you will need to do something like this:

    Code:
    Players:
      <UUID>:
        Prefix: 
    Code:
    Then to set just get the path and set
    config.set("Players." + UUID + ".Prefix", "Blah");
    config.getString("Players." + UUID + ".Prefix");
     
  8. Offline

    PumpMelon

    @bwfcwalshy
    How would I set my path to include strings and UUID?
    Code:
      String path = "Player.UUID.Prefix";
    Or should I just create my own config.yml and copyDefault that?

    Also
    Code:
     
            if(getConfig().getString("Players." + "UUID" + ".Prefix"){
    
    Says I cannot convert from String to Boolean (I know the "UUID" is wrong, but just wanted to hear your opinion first on how I should set the path to include an UUID)
     
  9. +PumpMelon

    1. String path should be "Player." + p.getUUID() + ".Prefix"
    2. String prefixstr should be = args[1] and not args[1].toString() as a String does not need to be converted to a string
    3. I would recommend putting spaces on if(){} for readability. The easier to read, the quicker we can help (at least me)
    4. Quick tip: use e.setFormat(prefixstr + e.getMessage()) instead of setting the displayName
     
    Last edited: Aug 26, 2016
  10. Offline

    PumpMelon

    @TheEnderCrafter9
    Code:
    public void loadConfiguration(){
            Player p = null;
            String path = "Player." + p.getUniqueId() +  ".Prefix";
            getConfig().addDefault(path, "prefixstring");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
    1. Is that what you mean?
    2. Thanks, edited.


    Right now my config.yml lwhen I ran it looks like this ~
    Code:
    Player:
      Prefix: hi
    hi: &id001 !!java.util.UUID {}
    bye: *id001
    t: *id001
    
    The 'hi' 'bye' and 't' were all things I set in doing /settitle <user> <then the word>
    I think I'm doing this wrong with my paths, heres my code
    Code:
    package me.PumpMelon.SetTitle;
    
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SetTitle extends JavaPlugin implements Listener{
       
       
    
    
       
    
        public void loadConfiguration(){
            Player p = null;
            String path = "Player." + p.getUniqueId() +  ".Prefix";
            getConfig().addDefault(path, "prefixstring");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            loadConfiguration(); 
            }
        public String prefix = "§e[§fSetTitle§e] "; 
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("settitle") && sender.hasPermission("settitle.set")){
               
                if(args.length == 0){
                   
                    sender.sendMessage(" ");
                    sender.sendMessage(prefix + "§fVersion v1.0 developed by PumpMelon");
                    sender.sendMessage(prefix + "§fProper usage: /settitle <player> <prefix> ");
                    sender.sendMessage(" ");
                    return false;
                }
                else if(args.length < 2 || args.length > 2){
                   
                    sender.sendMessage(prefix + "§cProper usage: /settitle <player> <prefix> ");
                    return false;
                }
               
                else if(args.length == 2){
                   
                    @SuppressWarnings("deprecation")
                    UUID pUUID = Bukkit.getPlayer(args[0]).getUniqueId(); 
                    if(pUUID == null){
                        sender.sendMessage(prefix + "§cThat player's UUID does not exist!");
                        return false; 
                    }else{
                       
                    String prefixstr = args[1]; 
                    getConfig().set("Players." + pUUID + ".Prefix", prefixstr);
                    saveConfig();
                   
                    return true; 
                    }
                }
                sender.sendMessage(prefix + ChatColor.GREEN + "The title has been set!");
                return true;
           
               
               
               
       
                }
                return false; 
            }
       
           
       
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            if(getConfig().getString("Players." + p.getUniqueId() + ".Prefix") != null){
                p.setDisplayName(getConfig().getString("Player.Prefix") + " " + p.getName());
                }
                }
           
        }
       
       
    
    
     
  11. Tell me the issue with that.
     
  12. Offline

    PumpMelon

    @bwfcwalshy
    Well, I knew that wouldn't work. Just wanted to get rid of the error for the time being so I just threw that in.
     
  13. @PumpMelon
    Well, that's why you're getting a NullPointerException. And the reason why the config file looks weird is because you're providing a straight up UUID and YAML is trying to serialize it and fails. "UUID.toString()" should do the job.
     
  14. Huh. Now that I think of it, I use

    Code:java
    1.  
    2. new PlayerConfig(playerUUID.toString() + ".yml", EnderPlugin.getInstance());
    3.  


    Oh. Yup I also use toString(), best part is that I forgot.
     
  15. Offline

    PumpMelon

    @AlvinB
    @TheEnderCrafter9
    Ok,
    I used toString and this is what I got
    Code:
    else if(args.length == 2){
                   
                    @SuppressWarnings("deprecation")
                    UUID pUUID = Bukkit.getPlayer(args[0]).getUniqueId(); 
                    String psUUID = pUUID.toString(); 
                    if(psUUID == null){
                        sender.sendMessage(prefix + "§cThat player's UUID does not exist!");
                        return false; 
                    }else{
                       
                    String prefixstr = args[1]; 
                    getConfig().set("Players." + psUUID + ".Prefix", prefixstr);
                    saveConfig();
                   
                    return true; 
                    }
                }
                sender.sendMessage(prefix + ChatColor.GREEN + "The title has been set!");
                return true;
    
    for my path though, I'm still having troubles. What should I get the UUID of?
    Code:
    public void loadConfiguration(){
           
            String path = "Player." + p.getUniqueId().toString() +  ".Prefix";
            getConfig().addDefault(path, "prefixstring");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
    How should I initialize p? or if I should even be trying to do that.
     
  16. @PumpMelon Learn Java, at least the basics. That's how you do it and how you will solve these issues, I haven't seen one that isn't simple Java. Not being mean, I'm being truthful.
     
  17. @bwfcwalshy has a point. I don't see anything but faulty code and baddly written code..
     
  18. Last edited: Aug 28, 2016
Thread Status:
Not open for further replies.

Share This Page