Solved Group creator command

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

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

    djbomber36

    Tested the plugin! It's working but I have problem with the op.
    If I'm op my prefix in chat is "USER djbomber36>>"
     
  2. Online

    timtower Administrator Administrator Moderator

    @djbomber36 Do you have the permission for the USER group?
     
  3. Offline

    djbomber36

  4. Online

    timtower Administrator Administrator Moderator

    Then what is the issue?
     
  5. Offline

    djbomber36

    If I'm op my prefix in chat is "USER djbomber36>>" but I'm in the group Owner with his permission
    I'm using LuckPerms
     
  6. Online

    timtower Administrator Administrator Moderator

    @djbomber36 If you also have other permissions then the last one in the config will be used.
     
  7. Offline

    djbomber36

    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");
                String PrefixDefault = getConfig().getString("Groups." + ".Prefix");
                if (p.hasPermission(Permission))
                    event.setFormat(ChatColor.translateAlternateColorCodes('&',
                            Prefix + p.getDisplayName() + "&7>> " + event.getMessage()));
                else
                    event.setFormat(ChatColor.translateAlternateColorCodes('&',
                            PrefixDefault + p.getDisplayName() + "&7>> " + event.getMessage()));
            }
    
        }
    How can I make that String PrefixDefault will always be User in the config.yml always if modifying the name and removing the permission?

    But if I make that if opped you can't access to the groups permissions But only to a custom setting?

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

    timtower Administrator Administrator Moderator

    @djbomber36 Now it will set your prefix always to the default unless it is the last group.
    Fix your permissions instead, 1 group, 1 permission.
     
  9. Offline

    djbomber36

    Anyway let's take away the op i will resolve it later.
    How do I create a commands for creating groups?
     
  10. Online

    timtower Administrator Administrator Moderator

    @djbomber36 You make a command that adds a Prefix and permission to the config for the given group name.
    Then you save the config.
     
  11. Offline

    djbomber36

    I'm having trouble understanding how to do that
     
  12. Online

    timtower Administrator Administrator Moderator

    Start by making a command that gets all required arguments.
     
  13. Offline

    djbomber36

    Having trouble with that to
     
  14. Online

    timtower Administrator Administrator Moderator

    What have you tried?
     
  15. Offline

    djbomber36

    I tried to creating a sub commands like chatx group add but aving trouble with addind "add" arg
     
  16. Online

    timtower Administrator Administrator Moderator

    Please post code, what issues did you encounter, what part aren't you able to figure out?
    You have been saying a lot that you don't know how, but please show some effort that you have made.
     
  17. Offline

    djbomber36

    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();
            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()));
            }
    
        }
    
        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;
                }
            } else if (args[0].equalsIgnoreCase("group")) {
                if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("add")) {
                        {
                            if(args.length != 2)
                            {
                                sender.sendMessage(ChatColor.RED + "Usage: /chatx group add <Group Name>");
                            }
                            else
                            {
                                //Insert something in the config.yml
                            }
                        }
                return true;
            }
                return true;
        }
    }
            return false;}}
    I'm not founding how to Insert something in the config.yml
     
  18. Online

    timtower Administrator Administrator Moderator

    @djbomber36 Find all arguments first to put in the config.
    Group name
    Prefix
    Permission
     
  19. Offline

    Minesuchtiiii

    Code:
    File yourfile = new File(getDataFolder() + "/YourFile.yml");
    FileConfiguration yourfilecfg = YamlConfiguration.loadConfiguration(yourfile);
    also create a method that check if the file exists, it not create the file and add defaults to it if you want. Put this method in the onEnable part.


    It looks like you're trying to create a chat related plugin. I'd suggest you first learn java basics regarding to strings and file management, as you won't come far without that.
     
  20. Online

    timtower Administrator Administrator Moderator

    @Minesuchtiiii You do realize that he has config loading already right?
     
  21. Offline

    KarimAKL

    @Minesuchtiiii I'm pretty sure it's recommended to use 'File#separator' instead of '/'. You could also use the constructor 'File(File parent, String child)'
     
  22. Offline

    Minesuchtiiii

    This was the answer to his question:

    I'm not aware of any difference that this would make, it's working perfectly for me.
     
  23. Offline

    KarimAKL

  24. Offline

    djbomber36

    My questione are:
    1- How I create a string that is the input of the player in the command?
    2- How I have to create the string to give to the config.yml(prefix And permission)
     
  25. Offline

    KarimAKL

    @djbomber36
    1. In your onCommand method, there's an argument of type String[], that'll give you the arguments that the player typed in the command.
    2. How you save a string to the config? In that case you use FileConfiguration#set(String, Object) and then FileConfiguration#save(File)
     
  26. Offline

    djbomber36

    I understanded how to set a new value in the config.yml but can you tell me the string to get player command input?
     
  27. Offline

    KarimAKL

     
  28. Offline

    djbomber36

    But i can't figure out how?

    [EDIT] is this correct?
    Code:
    if(args.length != 3)
                                    {
                                        sender.sendMessage(ChatColor.RED + "Usage: /chatx group add <group name>");
                                    }
                                    else
                                    {
                                        //set the config file
                                    }
     
    Last edited: Jun 26, 2019
  29. Online

    timtower Administrator Administrator Moderator

  30. Offline

    djbomber36

    Watch my last edited post
     
Thread Status:
Not open for further replies.

Share This Page