Solved Configuration Section not working

Discussion in 'Plugin Development' started by Xp10d3, Jan 23, 2020.

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

    Xp10d3

    I have set up my plugin, but am trying to get a configuration section key. For some reason, Eclipse isn't liking that and has changed String to Set<String> therefore causing an error (sadly, I've lost the error and don't have it. It did point to a line that has Set<String> though). I'm trying to get a configuration section that looks like this:
    Code:
    location:
      world:
        PermissionsEx
          - world
          - Spawn
    
    So speciically I'm trying to get the String PermissionsEx, but that isn't working. Originally, I had the line to get that value be like this:
    Code:
    String targetPlugin = config.getConfigurationSection("location.world").getKeys(false);
    
    But Eclipse complained and gave me this error:
    Code:
    Type mismatch: cannot convert from Set<String> to String
    
    And asked me to change the line to this:
    Code:
    Set<String> targetPlugin = config.getConfigurationSection("location.world").getKeys(false);
    
    The whole class is below. But I also have one more question: How do I get the values world and Spawn in the config I showed above? I can set it so that I only get one World, but I really don't to do that. Thanks!
    -Xp10d3
    Class:
    Code:
    package play.corelia.online;
    
    import java.util.Arrays;
    import java.util.Set;
    import java.util.function.BiPredicate;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerPortalEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredListener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
       
        // Get the value "config"
        FileConfiguration config = getConfig();
       
        public void onEnable() {
            config = getConfig();
            loadConfig();
            new Commands(this);
            /*
            Set<String> targetPlugin = config.getConfigurationSection("location.world").getKeys(false);
            System.out.println(targetPlugin);
            */
        }
       
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        /*
        @SuppressWarnings({ "unlikely-arg-type", "unchecked" })
        public void getWorld() {
            Player player = Bukkit.getPlayer(getName());
           
            World world = Bukkit.getServer().getWorld(player.getWorld().getName());
            List<String> worldConfig = this.getConfig().getStringList("location.world.");
            ListIterator<String> pluginsInstalled = (ListIterator<String>) this.getConfig().getList("plugins.installed.");
            if (world.equals(worldConfig)) {
                if (getServer().getPluginManager().getPlugin(pluginsInstalled.next()) != null) {
                    Bukkit.getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin(pluginsInstalled.next()));
                } else {
                    return;
                }
            }
        }
        */
       
        @SuppressWarnings("unchecked")
        static <Type extends Event> void filter(Class<Type> type, BiPredicate<Plugin, Type> filter) {
              HandlerList handler;
            try {
                handler = (HandlerList) type.getDeclaredMethod("getHandlerList").invoke(null);
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
              Arrays.asList(handler.getRegisteredListeners()).forEach(listener -> {
                handler.unregister(listener);
                handler.register(new RegisteredListener(listener.getListener(), ($, event) -> {
                  if (filter.test(listener.getPlugin(), (Type) event)) listener.callEvent(event);
                }, listener.getPriority(), listener.getPlugin(), false));
              });
        }
       
        @SuppressWarnings("unlikely-arg-type")
        public void supress() {
            Set<String> targetPlugin = config.getConfigurationSection("location.world").getKeys(false);
            String targetWorld = config.getString("location.world." + targetPlugin + ".world");
            filter(PlayerInteractEvent.class, (plugin, event) -> plugin == targetPlugin && event.getPlayer().getWorld().equals(targetWorld));
        }
        public void repeat(PlayerPortalEvent event) {
            for (int i = 0; i >= 0; i++) {
                supress();
            }
        }
    }
    
     
  2. Offline

    CraftCreeper6

    @Xp10d3
    1. In your config "PermissionsEx" should have a colon, like "PermissionsEx:".
    2. Use config.getStringList(); and store it as a List<String> as oppose to a Set<String>

    EDIT: My bad, you want the PermissionsEx string.

    Well still, the first point applies. Definitely try that first.
     
  3. Offline

    caderapee

  4. Offline

    Xp10d3

  5. Offline

    KarimAKL

    @Xp10d3 ConfigurationSection#getKeys(boolean) returns a Set<String> of all the keys in that section; the return type of that method doesn't change just because you only have 1 key in that section.
     
  6. Offline

    Xp10d3

    Oh. I see. So how do I get the string PermissionsEx if Set<String> is what's returned?
     
  7. Offline

    KarimAKL

    @Xp10d3 Use ConfigurationSection#getString("PermissionsEx") if you want to get the value (as a String) from the key "PermissionsEx".
     
  8. Offline

    Xp10d3

    Sorry still a bit confused. ConfigurationSection.getString doesn't seem to work. If I am to set targetPlugin to a configuration section I don't understand how I would create a new variable from that?
    EDIT: Never mind got it xD But I get an error trying to do that. It's a type mismatch trying to do:
    Code:
    ConfigurationSection targetPlugin = config.getString("location.world");
    
    Trying to do #getKeys() just adds more errors.
     
  9. Offline

    KarimAKL

    @Xp10d3 Could you explain to us what it is that you want to do? Then we might be able to help you easier.
     
  10. Offline

    Xp10d3

    Sure. Sorry, I'm trying to disable all events in a specific world. I have one line that does that, but I need to add a few elements so that it works by getting the config. So if my config is like this:
    Code:
    location:
      world:
        PermissionsEx:
          - world
          - Spawn
        WorldEdit:
          - world_nether
          - world_end
    
    I want PermissionsEx events to be disabled in World world and the World Spawn, and I want WorldEdit to be disabled in the nether and the end (just examples). But I am getting some errors with ConfigurationSection itself which is why I came to the Bukkit forums.
     
  11. Offline

    KarimAKL

    @Xp10d3 You should read up on this.
    the config is your FileConfiguration, the ConfigurationSection is a section inside the config.
    ConfigurationSection#getString(String) returns a String, not a ConfigurationSection.
    ConfigurationSection#getKeys() returns a Set<String>, not a String.

    EDIT: You'll probably want to loop the keys and then get the values.
    Something like this should work:
    Code:Java
    1. FileConfiguration config = getConfig();
    2. ConfigurationSection section = config.getConfigurationSection("location.world");
    3. for (String key : section.getKeys(false)) {
    4. List<String> worlds = section.getStringList(key);
    5. // Do something with your worlds
    6. }
     
    Last edited: Jan 23, 2020
  12. Offline

    Xp10d3

    Wait a sec but I did ConfigurationSection#getString(String) if I'm not mistaken, but I still get an error trying to do:
    Code:
    ConfigurationSection targetPlugin = config.getString("location.world");
    
    So that still leaves me with the question how do I fix the error and how do I get the String PermissionsEx in my config? Sorry I'm real confused :'(

    EDIT: Oh I understand. Then how do I get the ConfigurationSection since that's what I want and not Set<String>? Some of my code complains when I use Set<String>.
    EDIT 2: Sorry I think I figured it out. Is it something like the below? Also how do I get a list of entries like:
    Code:
    ...
        PermissionsEx
          - world
          - Spawn
    
    How do I get "world" and "Spawn" if my code is like:
    Code:
     String targetWorld = config.getString("location.world." + targetPlugin + ".world"); 
    Code:
    Code:
        @SuppressWarnings("unlikely-arg-type")
        public void supress() {
            //Set<String> targetPlugin = config.getConfigurationSection("location.world").getKeys(false);
            ConfigurationSection targetPlugin = config.getConfigurationSection("location.world");
            targetPlugin.getKeys(false);
            String targetWorld = config.getString("location.world." + targetPlugin + ".world");
            filter(PlayerInteractEvent.class, (plugin, event) -> plugin == targetPlugin && event.getPlayer().getWorld().equals(targetWorld));
        }
    
     
    Last edited: Jan 23, 2020
  13. Offline

    KarimAKL

    @Xp10d3 I edited my above post, read that.
     
  14. Offline

    Xp10d3

    Tysm. Really helpful :) Marking thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page