Help about plugiN!

Discussion in 'Spigot Plugin Development' started by Ognjen-123, Jul 2, 2018.

  1. Hello guys i was wondering if somebody could help me about this...
    I'm a really new to coding so i made a command with cooldown and it works perfectly but i want to add config.yml to save players who entered command!
    Code (Text):

    if(cmd.getName().equalsIgnoreCase("rasanacija")) {
                  if (cooldownTime.containsKey(p)) {
                      p.sendMessage(ChatColor.RED + "Ova komanda se moze uneti samo jednom!");
                      return true;
              }
           

                   
                if(args.length == 0) {
                    sender.sendMessage("Moras da uneses covek/vilenjak/patuljak/ork");
                    return true;
                }
                if(args[0].contains("covek")) {
                    Player player = (Player) sender;
                    player.performCommand("card nation gradljudi");
                    player.performCommand("card race covek");
                    int randomPlayerNumber = r.nextInt(Bukkit.getServer().getOnlinePlayers().size());              
                    cooldownTime.put(p, 5);
                    cooldownTask.put(p, new BukkitRunnable() {
                            public void run() {
                                    cooldownTime.put(p, cooldownTime.get(p) - 1);
                                    if (cooldownTime.get(p) == 0) {
                                            cooldownTime.remove(p);
                                            cooldownTask.remove(p);
                                            cancel();
                                    }
                            }
                    });
                 
                    cooldownTask.get(p).runTaskTimer(this, 200000000, 200000000);
                 
                    return true;

                }
     
     
  2. No :unsure:
     
  3. Really..is that really needed here?
     
  4. follow the link there is my post on another topic explaining how to
     
  5. Sure..thanks
     
  6. Code (Java):
    public class Configurations {
        private FileConfiguration config;
        private File configFile;

        public void createConfig() {
            // First, you need a method to actually create your config file
            // You should create a config.yml (or whatever.yml) in your src folder first
            configFile = new File(/*instance of your main class*/.getDataFolder(), "config.yml");
            if (!configFile.exists()) {
                // First, make sure the config doesn't already exist; if not, create it
                configFile.getParentFile().mkdirs();
                /*instance of main class*/.saveResource("config.yml");
            }
            config = new YamlConfiguration();
            try {
                // Load the configuration on the file to the YamlConfiguration object
                config.load(configFile);
            } catch (InvalidConfigurationException | IOException e) {
                e.printStackTrace();
                // never seen issues with either of these exceptions, so you probably did something wrong if you do
            }
        }

        public FileConfiguration getConfig() {
            // Simply get and use the configuration from memory
            return config;
        }

        public void saveConfig() {
            // This saves the information in our config object to the file
            try {
                config.save(configFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void reloadConfig() {
            // This, on the other hand, reloads the configuration in the file and uses that
            try {
                config.load(configFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Hopefully you know how to get the instance of your main class, at least, because I did everything else for you :p
    You would call createConfig() in your onEnable and the other methods respectively where needed
     
    • Winner Winner x 1
  7. This is unnecessary, I think he just wants to use the Bukkit config file which case all the methods and handling are already there, he just has to use them.

    I'm going to give you some guidance, but not code. It is okay to struggle, it is actually better if you struggle.

    First, you need to create the config yml. Which is literally one method. Do this when the plugin loads.

    Then you need to write to the configuration file, which is also one method. Do this when you put a player in the cooldown and use their name as the value.

    Then you need to save the config file, which again, is one method. Do this after you set the config with the player's name.
     
  8. I'd recommend using this.
    I'd also recommend simply modifying a list (or set depending on what you're doing), and whenever you update it (I'd recommend having a method that both adds the information to your list and this), call an async bukkit runnable to automatically update your file without interrupting your server (which sometimes happens when you have very large files you're trying to save).
     
    • Like Like x 1
  9. He said he wants to use the config.yml, why not just use that when all the methods and handling is done for you?
     
  10. If they end up using multiple configuration files, they have all the tools they need to add another at their disposal.
     
    • Winner Winner x 1
  11. 1.) As @Rabbitual said above
    But also, it gives you a slight bit more freedom, especially since the config.yml normally isn't used to store rapidly changing information (although it can), it's normally used for static options that don't change between restarts (which is reinforced by the ability to create a "default" easily in your resources folder next to your plugin.yml).
    Even if its somewhat stretching the context of the original question, I personally believe its better to provide them with extra information, as it may spark them to use an alternative solution that meets their end goal better, as those of us answering questions here on the forums frequently don't know exactly what OP is attempting to do.
    TL;DR: I just enjoy providing many different solutions to their problem, so they aren't confined by the knowledge they have gotten but instead by their intuition.

    Also I'd recommend OP look into Object Output Streams, as in some cases it's much faster to use this.
     
    • Like Like x 1
  12. But much slower if you want to change anything, as well as in terms of performance ;)
     
    • Agree Agree x 1
  13. True, for this specific usecase it wouldn't be very smart to use. I was just thinking since they were asking about saving information, that perhaps one day that would be the best method available to them, like saving thousands of Maps (Then again, using a database would be better there too...)
     
  14. Finally..thanks!
     
  15. Hey..thanks for the guide