Checking properties

Discussion in 'Plugin Development' started by beatcomet, Jul 11, 2011.

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

    beatcomet

    How can I check if a property exists ?

    Here is my main class :

    Code:java
    1.  
    2. package me.beatcomet.Freeze;
    3.  
    4. import java.io.File;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.event.Event;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.PluginManager;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.util.config.Configuration;
    12.  
    13. public class Frz extends JavaPlugin{
    14.  
    15. //Getting info from plugin.yml
    16. PluginDescriptionFile PDFile = this.getDescription();
    17. //Creating a new data file
    18. File configFile = new File("plugins/" + PDFile.getName()
    19. + "/data.dat");
    20. Configuration config = new Configuration(configFile);
    21. //getting Minecraft logger to send console messages
    22. Logger log = Logger.getLogger("Minecraft");
    23. //creating Player Listener reference
    24. PListener playerListener = new PListener(this);
    25. //What happens when the plugin is being disabled
    26. public void onDisable(){
    27. //saving the data
    28. config.save();
    29. //sending console message saying the plugin has been disabled
    30. log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is DISABLED!");
    31. }
    32. public void onEnable(){
    33. //Sending a console message saying the plugin has been enabled
    34. log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is ENABLED!");
    35. //making a new file if a data file dose not exist
    36. new File("plugins/" + PDFile.getName()).mkdir();
    37. if (!configFile.exists()) {
    38. try {
    39. configFile.createNewFile();
    40.  
    41. } catch (Exception e) {
    42. //sending console message in case the data file could not be created
    43. log.info("[GlassBreak] Error when creating config file.");
    44. }
    45. }
    46. //Loading data file
    47. config.load();
    48. //getting the plugin manager to register events
    49. PluginManager pm = this.getServer().getPluginManager();
    50. //player join event registration
    51. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
    52. }
    53. }
    54.  


    I'm trying to check if the config file contains a player's name onPlayerJoinEvent but I have no idea how...
     
  2. Offline

    cholo71796

    I believe you could use config.getNode, but I'm a huge noob, so I don't really know.
     
  3. Offline

    krinsdeath

    if the player's name is contained at the top level:

    Code:
    public void onPlayerJoin(PlayerJoinEvent event) {
      if (Frz.config.getKeys(event.getPlayer().getName()) != null) {
        // do stuff here
      }
    }
    
    This will check the top level of your config file (the first node) for a key equal to the joining player's name, and return a List<String> containing all of the keys directly underneath it

    If you just want to check the value of the key (for example, if the file is "krinsdeath: 13" and contains the number of times I've logged in), you'd do the following:

    Code:
    Frz.config.getInt(event.getPlayer().getName(), 1);
    
    Where the "1" indicates some default value if the key returns null or doesn't exist
     
  4. Offline

    beatcomet

    can I use this one ?
    Code:java
    1.  
    2. Player player = event.getName();
    3. String name = p.getName();
    4. if (!(plugin.config.getBoolean(name, true)|| plugin.config.getBoolean(name, false))
    5. {
    6. //do something if the players name value is not true or false.
    7. }else
    8. {
    9. //do something if the players name value is true/false.
    10. }
    11.  
     
  5. Offline

    krinsdeath

    That if statement is really ambiguous. You're giving two different default values (one will return true if the key doesn't exist, the other will return false)

    You don't need the ||. If you want to check if the value is true or, if there is no value, default it to true, specify a default of true -> else (the value is returned as false successfully) do something else:

    Code:
    if (plugin.config.getBoolean(name, false)) {
      // the name was found and had a value of true
    } else {
      // the name was not found, OR the value returned was false
    }
    
     
  6. Offline

    beatcomet


    The idea is that the player enters and the plugin checks if the player appears in the data file.
    so I tried checking if the player's name value is true/false.
    If it is true or false, the plyer exists in the data file.
    If it's not the player dose not exist in the data file.

    Do you understand what I tried to do?

    What you gave me just checks if the value is false or true, but I this value will be used later on , after the admin will use a command.
     
  7. Offline

    krinsdeath

    In that case, you want to do:

    Code:
    if (plugin.config.getProperty(player.getName()) != null) {
      // player name was found and had a value
    } else {
      // player name wasn't found
    }
    
     
    beatcomet likes this.
  8. Offline

    beatcomet

    Thanks,

    I have another question,
    I'm using onCommand, and I need to check if args[0] equals to a players name in the config.
    How can I do that?
     
  9. Offline

    krinsdeath

    Code:
    if (sender instanceof Player) {
      if (((Player) sender).getServer().getPlayer(arg[0]) != null) {
        // do stuff here
      }
    }
    edit: sorry, that's to check if the player is online

    if you want to check it against the config file, do the same thing:

    Code:
    if (plugin.config.getProperty(arg[0]) != null) {
      // do stuff
    }
    
    edit 2: if you need more help tonight you'll be on your own, my eyes are shutting of their own accord at the moment

    good luck
     
  10. Offline

    beatcomet

    lol thanks a lot !!
    You are the best

    Here is what I did few seconds after I posted the comment

    Code:java
    1.  
    2. if(commandLabel.equalsIgnoreCase("Freeze") || commandLabel.equalsIgnoreCase("f")){
    3. String name = args[0];
    4. if((Boolean) plugin.config.getProperty(name) == false){
    5. plugin.config.setProperty(name, true);
    6. }
    7. if((Boolean) plugin.config.getProperty(name) == true){
    8. plugin.config.setProperty(name, false);
    9. }
    10. }
    11.  
     
  11. Offline

    maystorm

    Hi,

    I am not an expert in Java programming, just have basic knowledge, however, I am wondering why this works:
    Code:
    public class Frz extends JavaPlugin{
    
    //Getting info from plugin.yml
    PluginDescriptionFile PDFile = this.getDescription();
    When "this.getDescription()" is called, the object has not run through its whole instantiation process which in turn means that "this" cannot be resolved yet. It should throw an NPE or such.

    Where is the mistake in my thinking?
     
Thread Status:
Not open for further replies.

Share This Page