Solved Group creator command

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

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

    djbomber36

    Hello!
    Is there a method to create a command that create string of code with the informations from the command?

    Here's my class

    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 P1 = getConfig().getString("Owner.Prefix");
            String Pe1 = getConfig().getString("Owner.Permission");
            String P2 = getConfig().getString("Head-Admin.Prefix");
            String Pe2 = getConfig().getString("Head-Admin.Permission");
            String P3 = getConfig().getString("Admin.Prefix");
            String Pe3 = getConfig().getString("Admin.Permission");
            String P4 = getConfig().getString("Builder.Prefix");
            String Pe4 = getConfig().getString("Builder.Permission");
            String P5 = getConfig().getString("Head-Mod.Prefix");
            String Pe5 = getConfig().getString("Head-Mod.Permission");
            String P6 = getConfig().getString("Mod.Prefix");
            String Pe6 = getConfig().getString("Mod.Permission");
            String P7 = getConfig().getString("Dev.Prefix");
            String Pe7 = getConfig().getString("Dev.Permission");
            String P8 = getConfig().getString("Helper.Prefix");
            String Pe8 = getConfig().getString("Helper.Permission");
            String P9 = getConfig().getString("User.Prefix");
            String Pe9 = getConfig().getString("User.Permission");
    
            if (p.hasPermission(Pe1))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P1 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe2))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P2 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe3))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P3 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe4))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P4 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe5))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P5 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe6))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P6 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe7))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P7 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe8))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P8 + p.getDisplayName() + "&7>> " + event.getMessage()));
            if (p.hasPermission(Pe9))
                event.setFormat(ChatColor.translateAlternateColorCodes('&',
                        P9 + p.getDisplayName() + "&7>> " + event.getMessage()));
            else
                event.setFormat(p.getDisplayName() + ChatColor.GRAY + ": " + ChatColor.GRAY + 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;
        }
    }
    Thanks in advance!

    [EDIT]I want to create more groups by command and then modify them throught the config file
     
    Last edited: Jun 20, 2019
  2. Online

    timtower Administrator Administrator Moderator

    @djbomber36 Make an example of the config that you want.
     
  3. Offline

    djbomber36

    This is my config.yml

    Code:
    Owner:
       Prefix: '&4&lOWNER '
       Permission: 'chatx.owner'
    Head-Admin:
       Prefix: '&5ha'
       Permission: 'chatx.hadmin'
    Admin:
       Prefix: '&5a'
       Permission: 'chatx.admin'
    Builder:
       Prefix: '&5b'
       Permission: 'chatx.builder'
    Head-Mod:
       Prefix: '&5hm'
       Permission: 'chatx.hmod'
    Mod:
       Prefix: '&5m'
       Permission: 'chatx.mod'
    Dev:
       Prefix: '&5d'
       Permission: 'chatx.dev'
    Helper:
       Prefix: '&5h'
       Permission: 'chatx.helper'
    User:
       Prefix: '&5u'
       Permission: 'chatx.user'
    I want to add groups like this throught command
     
  4. Online

    timtower Administrator Administrator Moderator

    @djbomber36 Did you manage to get code running that works with this structure already? Without looking at the command to add groups.
     
  5. Offline

    djbomber36

    The groups in the config.yml are standard groups. I want to have the possibility of making new groups
     
  6. Online

    timtower Administrator Administrator Moderator

    But your code still needs to be able with these groups, if they are default or not.
    In my opinion it is better to make the reading before you start the writing.
     
  7. Offline

    djbomber36

    I'm not understanding
     
  8. Online

    timtower Administrator Administrator Moderator

    You can make a command to add groups.
    But the rest of your code is not able to use those groups yet. Make that first.
     
  9. Offline

    djbomber36

    How I make that first?
     
  10. Online

    timtower Administrator Administrator Moderator

    You already have the config.
    You need to get the root nodes, then the permissions, if the player has the permission then you change the prefix.
     
  11. Offline

    djbomber36

    How I get the permissions?
     
  12. Online

    timtower Administrator Administrator Moderator

    config.getString
     
  13. Offline

    djbomber36

    But I have to do a If-else or I have to do something different
     
  14. Online

    timtower Administrator Administrator Moderator

    If you use if-else then you can't add new groups using commands because then you would need to modify the code as well.
    You need a loop.
     
  15. Offline

    djbomber36

    So I have to get the permission and then the plugin watch if the user has the permission and give him the prefix?
     
  16. Online

    timtower Administrator Administrator Moderator

    Yes
     
  17. Offline

    djbomber36

    But how can I do it?
     
  18. Online

    timtower Administrator Administrator Moderator

    Like this:
     
  19. Offline

    djbomber36

    So I do

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

    timtower Administrator Administrator Moderator

    @djbomber36 You don't make a new FileConfiguration, you get a single string.
    But you need to get the config itself first.
     
  21. Offline

    djbomber36

    How I get the configuration?

    I have to a loadConfig()
     
  22. Online

    timtower Administrator Administrator Moderator

    Depends on what config you want to use.
     
  23. Offline

    djbomber36

    I want to use the config that I send to you before
     
  24. Online

    timtower Administrator Administrator Moderator

    And how will the config be called? Because the config.yml is easier than groups.yml
     
  25. Offline

    djbomber36

    so i have to create a groups.yml?
    If yes ok
     
  26. Online

    timtower Administrator Administrator Moderator

    That is your call what you want to call the file.
     
  27. Offline

    djbomber36

    so I create the groups.yml by plugin or manually?

    [EDIT]I have to create it manualy in eclipse, then throught commands I can add or remove groups(also the standards) throught command
     
    Last edited: Jun 20, 2019
  28. Online

    timtower Administrator Administrator Moderator

    By plugin is the nicest thing. But I can't help you with that.
     
  29. Offline

    djbomber36

    But how I load a config file that isn't named config.yml but groups.yml?
     
  30. Online

    timtower Administrator Administrator Moderator

    I don't know
     
Thread Status:
Not open for further replies.

Share This Page