Solved Configuration API tries to cast a String to Configuration.

Discussion in 'BungeeCord Plugin Development' started by Enderaura, Aug 7, 2018.

Thread Status:
Not open for further replies.
  1. I was getting this error trying to implement BungeeCord support into my plugin:
    Code (Text):

    > 17:28:47 [SEVERE] java.lang.ClassCastException: java.lang.String cannot be cast to net.md_5.bungee.config.Configuration
    > 17:28:47 [SEVERE] at net.md_5.bungee.config.Configuration.getSectionFor(Configuration.java:63)
    > 17:28:47 [SEVERE] at net.md_5.bungee.config.Configuration.get(Configuration.java:76)
    > 17:28:47 [SEVERE] at net.md_5.bungee.config.Configuration.getString(Configuration.java:382)
    > 17:28:47 [SEVERE] at net.md_5.bungee.config.Configuration.getString(Configuration.java:377)
    > 17:28:47 [SEVERE] at me.enderaura.dcmcbungee.discord.ReadyListener.onReady(ReadyListener.java:36)
    > 17:28:47 [SEVERE] at sx.blah.discord.api.events.EventDispatcher$MethodEventHandler.handle(EventDispatcher.java:680)
    > 17:28:47 [SEVERE] at sx.blah.discord.api.events.EventDispatcher.lambda$null$17(EventDispatcher.java:610)
    > 17:28:47 [SEVERE] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    > 17:28:47 [SEVERE] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    > 17:28:47 [SEVERE] at java.lang.Thread.run(Thread.java:748)
     
    I have loaded the configuration properly (I'm sure of that, it's grabbed other Strings from the config and used them etc.). I have verified that the value in the config exists, and have tried it with and without quotation marks, none of these solved the error. Here is the code where it occurs, if you have any ideas.

    Code (Java):
        for (String key : DiscordMinecraftBungee.getInstance().getConfig().getKeys()) {
                if (key.startsWith("command-")) {
                    String name = key.substring(8);
                    String role = DiscordMinecraftBungee.getInstance().getConfig().getString("command-" + name + ".role");

                    DiscordMinecraftBungee.getInstance().getDiscordCommandManager().registerCommand(new BaseCommand(new Permission((message, user) -> {
                        boolean hasRole = role.equalsIgnoreCase("everyone");

                        if(hasRole)
                            return true;

                        IRole requiredRole = null;

                        for (IRole iRole : DiscordMinecraftBungee.getInstance().getGuild().getRolesByName(role)) {
                            requiredRole = iRole;
                            break;
                        }

                        if(requiredRole == null)
                            return false;

                        return PermissionUtils.isUserHigher(DiscordMinecraftBungee.getInstance().getGuild(), user, Collections.singletonList(requiredRole));
                    }), name), name);

                }
            }
    I did some digging into the BungeeCord API and found where it is trying to cast the String to Configuration. It calls these in chronological order, the last line being where the error occurs.

    Code (Java):
    public String getString(String path) {
        Object def = getDefault( path );
        return getString( path, ( def instanceof String ) ? (String) def : "" );
    }

    public String getString(String path, String def) {
        Object val = get( path, def );
        return ( val instanceof String ) ? (String) val : def;
    }

    public <T> T get(String path, T def) {
        Configuration section = getSectionFor( path );
        Object val;
        if ( section == this ) {
            val = self.get( path );
        } else {
            val = section.get( getChild( path ), def );
        }
        if ( val == null && def instanceof Configuration ) {
            self.put( path, def );
        }
        return ( val != null ) ? (T) val : def;
    }

    private Configuration getSectionFor(String path) {
        int index = path.indexOf( SEPARATOR );
        if ( index == -1 ) {
            return this;
        }
        String root = path.substring( 0, index );
        Object section = self.get( root );
        if ( section == null ) {
            section = new Configuration( ( defaults == null ) ? null : defaults.getSection( root ) );
            self.put( root, section );
        }
        return (Configuration) section;
    }
     
    If anyone has any ideas about what may be causing the error and any possible solutions, I would appreciate it :)
     
    #1 Enderaura, Aug 7, 2018
    Last edited: Aug 7, 2018
Thread Status:
Not open for further replies.