Solved Config help

Discussion in 'Plugin Development' started by shadow5353, May 17, 2014.

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

    shadow5353

    Hello,

    I just have a little simple question. I have made a config file where it will storage players location to teleport them, but when I try to check if they are in the config it will not work.

    The file:
    Code:
    data:
      3d4c07bc-7aba-4253-a2a7-63a858638f22:
        name: shadow5353
        world: world
        x: 54.03596086714993
        y: 91.0
        z: 347.55201810583975
    
    I have tried to check with this code in a event:
    Code:
    if(!(settings.getConfig().getStringList("data").contains(uuid))) {
                mm.getInstance().error(p, "Home not found! Talk with an inkeeper to set your home");
                return;
            }
    Full Code:
    Code:
    public void onPlayerInteract(PlayerInteractEvent e){
            //Hearthstone Item
            ItemStack hearthstone = new ItemStack(Material.NETHER_STAR, 1);
            ItemMeta hearthstonemeta = hearthstone.getItemMeta();
            hearthstonemeta.setDisplayName(ChatColor.AQUA + "Hearthstone");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Talk with an inkeeper to set your home");
            lore.add(ChatColor.GRAY + "and use this hearthstone to teleport to your home location.");
            hearthstonemeta.setLore(lore);
            hearthstone.setItemMeta(hearthstonemeta);
             
            //Check for click
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
       
            //check for item
            if(!(e.getItem().equals(hearthstone))) return;
       
            final Player p = e.getPlayer();
            final String uuid = p.getUniqueId().toString();
       
            if(cooldown.contains(p)){
                mm.getInstance().error(p, "Your hearthstone is on cooldown!");
                return;
            }
       
            if(!(settings.getConfig().contains("data." + uuid))) {
                mm.getInstance().error(p, "Home not found! Talk with an inkeeper to set your home");
                return;
            }
            cooldown.add(p);
     
            mm.getInstance().good(p, "Teleporting in 10 seconds, do not move");
       
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                public void run(){
                    World w = Bukkit.getServer().getWorld(settings.getData().getString("data" + "." + uuid + ".world"));
                    double x = settings.getData().getDouble("data" + "." + uuid + ".x");
                    double y = settings.getData().getDouble("data" + "." + uuid + ".y");
                    double z = settings.getData().getDouble("data" + "." + uuid + ".z");
                    p.teleport(new Location(w, x, y, z));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
                    mm.getInstance().good(p, "You have been teleported to your home");
                }
            }, 200);
       
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                public void run(){
                    mm.getInstance().good(p, "Your hearthstone is now available");
                    cooldown.remove(p);
                }
            }, 5120);
            }
     
  2. Offline

    rsod

    Make sure that uuid is actually your uuid by printing it to console before searching like
    System.out.println(uuid);
    and then check if it's the same.
     
  3. Offline

    shadow5353

    rsod It is the same uuid I have in the console. But when I try to check if im not it in the file, it say I am not in the config, but I can see that I am

    How should I check for it?
     
  4. Offline

    rsod

    shadow5353
    just use
    settings.getConfig().contains("data."+uuid)
    should be enough.
     
  5. Offline

    shadow5353

    rsod Still not working like I want it too. It still say im not in the file.

    Is there something I do wrong in the code:
    Code:
    public void onPlayerInteract(PlayerInteractEvent e){
            //Hearthstone Item
            ItemStack hearthstone = new ItemStack(Material.NETHER_STAR, 1);
            ItemMeta hearthstonemeta = hearthstone.getItemMeta();
            hearthstonemeta.setDisplayName(ChatColor.AQUA + "Hearthstone");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Talk with an inkeeper to set your home");
            lore.add(ChatColor.GRAY + "and use this hearthstone to teleport to your home location.");
            hearthstonemeta.setLore(lore);
            hearthstone.setItemMeta(hearthstonemeta);
                 
            //Check for click
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
           
            //check for item
            if(!(e.getItem().equals(hearthstone))) return;
           
            final Player p = e.getPlayer();
            final String uuid = p.getUniqueId().toString();
           
            if(cooldown.contains(p)){
                mm.getInstance().error(p, "Your hearthstone is on cooldown!");
                return;
            }
           
            if(!(settings.getConfig().contains("data." + uuid))) {
                mm.getInstance().error(p, "Home not found! Talk with an inkeeper to set your home");
                return;
            }
            cooldown.add(p);
     
            mm.getInstance().good(p, "Teleporting in 10 seconds, do not move");
           
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                public void run(){
                    World w = Bukkit.getServer().getWorld(settings.getData().getString("data" + "." + uuid + ".world"));
                    double x = settings.getData().getDouble("data" + "." + uuid + ".x");
                    double y = settings.getData().getDouble("data" + "." + uuid + ".y");
                    double z = settings.getData().getDouble("data" + "." + uuid + ".z");
                    p.teleport(new Location(w, x, y, z));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 2));
                    mm.getInstance().good(p, "You have been teleported to your home");
                }
            }, 200);
           
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                public void run(){
                    mm.getInstance().good(p, "Your hearthstone is now available");
                    cooldown.remove(p);
                }
            }, 5120);
            }
     
  6. Offline

    rsod

    Well, code seems legit. Sorry, but I can't help you. I didn't worked with UUID yet, so I might not know something.
     
  7. Offline

    shadow5353

    rsod Thank you for you tried :) Hope there are someone else that can help
     
  8. Offline

    xTigerRebornx

    shadow5353
    Code:
    settings.getConfig().contains("data." + uuid)
    May we see the code for your 'settings' class?
     
  9. Offline

    shadow5353

    xTigerRebornx Here you go:
    Code:
    public class SettingsManager {
     
          private SettingsManager() { }
         
          static SettingsManager instance = new SettingsManager();
         
          public static SettingsManager getInstance() {
                  return instance;
          }
         
          Plugin p;
         
          FileConfiguration config;
          File cfile;
         
          FileConfiguration data;
          File dfile;
         
          public void setup(Plugin p) {
                  cfile = new File(p.getDataFolder(), "HChords.yml");
                  config = p.getConfig();
                  //config.options().copyDefaults(true);
                  //saveConfig();
                 
                  if (!p.getDataFolder().exists()) {
                          p.getDataFolder().mkdir();
                  }
                 
                  dfile = new File(p.getDataFolder(), "HChords.yml");
                 
                  if (!dfile.exists()) {
                          try {
                                  dfile.createNewFile();
                          }
                          catch (IOException e) {
                                  Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create HChords.yml!");
                          }
                  }
                 
                  data = YamlConfiguration.loadConfiguration(dfile);
          }
         
          public FileConfiguration getData() {
                  return data;
          }
         
          public void saveData() {
                  try {
                          data.save(dfile);
                  }
                  catch (IOException e) {
                          Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save HChords.yml!");
                  }
          }
         
          public void reloadData() {
                  data = YamlConfiguration.loadConfiguration(dfile);
          }
         
          public FileConfiguration getConfig() {
                  return config;
          }
         
          public void saveConfig() {
                  try {
                          config.save(cfile);
                  }
                  catch (IOException e) {
                          Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save HChords.yml!");
                  }
          }
         
          public void reloadConfig() {
                  config = YamlConfiguration.loadConfiguration(cfile);
          }
         
          public PluginDescriptionFile getDesc() {
                  return p.getDescription();
          }
    }
     
  10. Offline

    xTigerRebornx

    shadow5353 Instead of using contains, try using FileConfiguration#getConfigurationSection() and seeing if that is null. If it is null, then there is no section for it. If it is not null, you have the section for the UUID.
     
  11. Offline

    shadow5353

    xTigerRebornx Like this?
    Code:
    if(settings.getConfig().getConfigurationSection("data." + uuid).contains(uuid)){
    Or is it something in the settingsmanager i need to change?
     
  12. Offline

    xTigerRebornx

    shadow5353 It'd be more like
    Code:
    if(settings.getConfig().getConfigurationSection("data." + uuid) != null
    That'd mean that the section is there.
     
    shadow5353 likes this.
  13. Offline

    shadow5353

Thread Status:
Not open for further replies.

Share This Page