Solved Group creator command

Discussion in 'Plugin Development' started by djbomber36, Jun 20, 2019.

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

    djbomber36

    Ok!
    I'll search
     
  2. Offline

    KarimAKL

    @djbomber36 If you create the 'groups.yml' file in the project then you can create it in your plugin's folder using
    Code:Java
    1. JavaPlugin#saveResource(String path, boolean replace)

    Where the path starts from the src folder. (At least it seems that way from what i've tried)
    If you don't want to create it in the project then you can simply create it using default Java methods.
    Create file object: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#File-java.io.File-java.lang.String-
    Create file at path: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#createNewFile--
    You'll also have to create the parent directory if it doesn't already exist.
    You can do that using these:
    Get parent directory: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#getParentFile--
    Check if it exists: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#exists--
    Create directory: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#mkdirs--
    For the second method you can add JavaPlugin#getDataFolder() as the parent.
    Then you can get the FileConfiguration using YamlConfiguration#loadConfiguration(File file)
     
  3. Offline

    djbomber36

    @KarimAKL But how can I create the groups?

    [EDIT]Wait! Before I create a command for creating groups, I need to have a structure that get the permissions from the config.yml and then if the user have the permission gets the prefix
     
  4. Offline

    KarimAKL

    Depends on how you want them saved.
    If you want them in the groups.yml file then you could set them in the file using FileConfiguration#set(String path, Object value)
     
  5. Offline

    djbomber36

    I stand on the config.yml
    Tonight in the bed I thought about this part of code

    Code:
    String Prefix = getConfig().getString("groups." + key + ".prefix");
            String Permission = getConfig().getString("groups." + key + ".permission");
    
            if (p.hasPermission(Permission))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        Prefix + p.getDisplayName() + "&7>> " + event.getMessage()));
    But I think I need to verify that "key" of "groups." + key + ".prefix" is equal to key of "groups." + key + ".permission"
     
  6. Online

    timtower Administrator Administrator Moderator

    @djbomber36 That is why you have the key, if the code is together like that then the key will be the same.
     
  7. Offline

    djbomber36

    So I don't need to verify that.
    And so the code is right?
     
  8. Online

    timtower Administrator Administrator Moderator

    The only thing that I am missing is the loop around it ;)
     
  9. Offline

    djbomber36

    How I do the loop? Is the loop the return true?
     
  10. Online

    timtower Administrator Administrator Moderator

    No, the loop that loops over all keys.
     
  11. Offline

    djbomber36

    How I do it?
     
  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    djbomber36

    So I do a loadConfig() And a return true?
     
  14. Online

    timtower Administrator Administrator Moderator

    loadConfig should only be called in the onEnable in this case.
    The return true depends on the code, please post yours. Full class.
     
  15. Offline

    djbomber36

    I'll post it later because now I have to get a shower.

    Here's the Main.java
    Code:
    package chat;
    
    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 Main extends JavaPlugin implements Listener {
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getCommand("ChatX").setExecutor(this);
            this.getCommand("chatx_help");
    
        }
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            String group = getConfig().getString("groups.");
            String Prefix = getConfig().getString("groups." + group + ".prefix");
            String Permission = getConfig().getString("groups." + group + ".permission");
    
            if (p.hasPermission(Permission))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        Prefix + p.getDisplayName() + "&7>> " + event.getMessage()));
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("ChatX")) {
                if (args.length == 0) {
                    if (sender.hasPermission("chatx.basic")) {
                        sender.sendMessage(ChatColor.RED + "Invalid Syntax! " + ChatColor.GRAY + "Try use /ChatX help");
                        return true;
                    }
                }
                if (args[0].equalsIgnoreCase("reload")) {
                    if (sender.hasPermission("chatx.reload")) {
                        reloadConfig();
                        sender.sendMessage(ChatColor.GREEN + "ChatX reloaded successfully");
                        return true;
    
                    }
                    }
                else if (args[0].equalsIgnoreCase("help")) {
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&a-------------Help For ChatX-------------"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&7You can visit &1bukkit.com/ChatX &7for an helpfull guide"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&6chatx help: &fOpen this guide of the commands"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&6chatx reload: &fReload the configuration file"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&c-------------Group Commands-------------"));
                    sender.sendMessage(ChatColor.translateAlternateColorCodes('&',"&6Group commands will be available soon"));
                    return true;
                }
                return true;
    
            }
            return false;
        }
    }
    And the config file
    Code:
    Groups:
       Owner:
          Prefix: '&4&lOWNER &7'
          Permission: chatx.owner
       Head-Admin:
          Prefix: '&4&lH-ADMIN &c'
          Permission: chatx.hadmin
       Admin:
          Prefix: '&c&lADMIN &c'
          Permission: chatx.admin
       Builder:
          Prefix: '&6&lBUILDER &6'
          Permission: chatx.builder
       Head-Mod:
          Prefix: '&1&lH-MOD &9'
          Permission: chatx.hmod
       Mod:
          Prefix: '&1&lMOD &9'
          Permission: chatx.mod
       Dev:
          Prefix: '&a&lD&f&lE&c&l &7'
          Permission: chatx.dev
       Helper:
          Prefix: '&2&lHELPER &a'
          Permission: chatx.helper
       User:
          Prefix: '&8&lUSER &7'
          Permission: chatx.user
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 21, 2019
  16. Online

    timtower Administrator Administrator Moderator

    @djbomber36 The capitalization of your config and your code don't match.
    You need to add the loop in the onChat.
     
  17. Offline

    djbomber36

    How I do the loop?
     
  18. Online

    timtower Administrator Administrator Moderator

    You posted it already, I linked it again.
    Not gonna spoon feed
     
  19. Offline

    djbomber36

    I'm not understanding.
    But I have to do something like this for the capitalization:
    Config
    Code:
    Groups:
          Owner:
               Prefix:
               Permission:
          User:
               Prefix:
               Permission:
    String
    Code:
    String Prefix = getConfig().getString("Groups." + group + ".Prefix");
            String Permission = getConfig().getString("Groups." + group + ".Permission");
     
  20. Online

    timtower Administrator Administrator Moderator

    @djbomber36 Capitalization is correct now.
    But you need to get the group variable from somewhere.
     
  21. Offline

    djbomber36

  22. Online

    timtower Administrator Administrator Moderator

  23. Offline

    djbomber36

    Code:
    for(String key : FileConfiguration#getConfigurationSection("Groups").getKeys(false)) {
        permission = FileConfiguration("groups." + key + ".permission");
    }
     
  24. Online

    timtower Administrator Administrator Moderator

    @djbomber36 And now all you need to do is to put it in the right place and change the right things.
     
  25. Offline

    djbomber36

    Code:
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
           
    for(String key : FileConfiguration#getConfigurationSection("Groups").getKeys(false)) {
    Permission = FileConfiguration("Groups." + key + ".Permission");
    Prefix = FileConfiguration("Groups." + key + ".Prefix");
    }
    
            if (p.hasPermission(Permission))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        Prefix + p.getDisplayName() + "&7>> " + event.getMessage()));
    Can you if it is correct?
    When I place it in my code he gives me errors

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 21, 2019
  26. Offline

    KarimAKL

    @djbomber36 You have to get the FileConfiguration from somewhere. Create a local variable of type FileConfiguration, initialize it, use the methods from that.
    Also, Java doesn't use '#'.
     
  27. Offline

    djbomber36

    I'm using the config.yml. I really need to do what you said if I have this strings
    Code:
    String Prefix = getConfig().getString("Groups." + group + ".Prefix");
            String Permission = getConfig().getString("Groups." + group + ".Permission");
     
  28. Online

    timtower Administrator Administrator Moderator

    @djbomber36 You need to get the group from somewhere. You do that with the loop over the keys.
     
  29. Offline

    djbomber36

    I'm having troubles with this
    Code:
    for(String key : FileConfiguration#getConfigurationSection("Groups").getKeys(false)) {
    Permission = FileConfiguration("Groups." + key + ".Permission");
    Prefix = FileConfiguration("Groups." + key + ".Prefix");
    }
    [EDIT]Modified code

    Code:
    @EventHandler
        public void onChat(AsyncPlayerChatEvent event) {
            Player p = event.getPlayer();
            for (String Groups : getConfig().getConfigurationSection("Groups").getKeys(false)) {
                String Permission = getConfig().getString("Groups." + Groups + ".Permission");
                String Prefix = getConfig().getString("Groups." + Groups + ".Prefix");
                if (p.hasPermission(Permission))
                    event.setFormat(ChatColor.translateAlternateColorCodes('&',
                            Prefix + p.getDisplayName() + "&7>> " + event.getMessage()));
                }
           
    
        }
     
    Last edited: Jun 21, 2019
  30. Offline

    KarimAKL

    @djbomber36 You've almost got it now. Make a local variable of type ConfigurationSection and set it to 'getConfig().getConfigurationSection("Groups")', then loop through the keys of that and then inside the loop you can create another ConfigurationSection variable and set that to 'yourFirstSection.getConfigurationSection(key)'.
    Also, read this: https://www.oracle.com/technetwork/java/codeconventions-135099.html
    I would also recommend doing this only when you load the groups, then save the group's permission and then check for that permission when in your 'onChat', that way you don't loop your config everytime you type in chat.
     
Thread Status:
Not open for further replies.

Share This Page