issues with config.yml and mysql

Discussion in 'Plugin Development' started by Jeff.Halbert, Jun 6, 2012.

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

    Jeff.Halbert

    I'm attempting to rewrite my plugin ChunkProtection to have a config file, along with mysql requirements. Also, to do away with sloppy code :). BUT....(there's always a but).. I seem to have reached a stand still. While laying down some basic coding, (before actually getting into what the plugin does..lol), I caught a ton load of errors during testing. Can anyone help?

    these are my errors.....
    [​IMG]

    this is my main class: chunkprotection.....
    [​IMG]
    this is my listener class: chunklistener.....
    [​IMG]
    this is my log class.....lol (don't ask, just how i like things :) )
    [​IMG]

    this is my config.yml.....
    [​IMG]
    and finally, my sql class.....
    [​IMG]
    [​IMG]
    [​IMG]

    what am i doing wrong????
     
  2. When you create a new instance of your class sql, which extends your main class, you can use the getConfig() method. However, it'll return null since you haven't applied the values for variables that bukkit would do normally with your main class. Hence you need to use the instance that bukkit creates. E.g. you need to pass the main instance over to the connect function and use that instead of creating one.
     
  3. Offline

    Jeff.Halbert

    ok, so. I think I understand... but just to make sure... Inside of my connect() method, I need to call the getConfig().options().copyDefaults(true) for the new instance of sql() ?????

    (edit)
    wait wait wait.... after reading it again.... how do I pass the main instance over without it being static? The issue I was having before this was... I couldn't seem to pass around the data from the .getConfig() method because it was unable to be passed to a static. How would I go about passing the main instance to the connect() method witout creating a new instance for each connect? Would this cause issues ingame, if more than one person was passing sql statements to the server simultaniously?
     
  4. Code:
    public static Connection connect(ChunkProtection mainInstance)
    {
      //CODE...
    }
     
    //in your checkDatabase method:
    sql.connect(this);
    Depends. If you want to, for example, insert 900 rows at once it *might* cause lags, but small queries shouldn't make any effect on the server.

    ....

    You almost had it but then destroyed it. When you pass over the main instance but the re-create it, which is wrong, you're doing the same thing as before. That makes passing it over pointless. Just remove that line and everything will be fine.

    If you have warnings in your class, you'll get a NoClassDefFoundError, I don't know why. Doesn't matte what kind of warning, you'll get that error.
    Moreover, if you wouldn't get that error, you'll get the same error as before. The reason is the same as I told you the first time. When you create a new instance of your main class the needed values for the config for example aren't there since the variables weren't assigned to them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  5. Offline

    Jeff.Halbert

    ok, so i attempted to simulate the event using a simple plugin that changes the name of a player when that player logs in. I finally understand that I have to use the instance of JavaPlugin that bukkit provides by passing the mainInstance over to any other class or method. So I did that... and it still throws errors :-(
    Code:
    package me.GBCsubspec.namechanger;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class namechanger extends JavaPlugin {
     
        public void onDisable() {
            System.out.println("namechanger disabled!");
        }
       
        public void onEnable() {
            getConfig();
            saveConfig();
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(new listener(), this);
        }
    }
    
    Code:
    package me.GBCsubspec.namechanger;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class listener extends JavaPlugin implements Listener{
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            String newName = getName();
            event.getPlayer().setDisplayName(newName);
        }
       
        public static String getName(namechanger nc){
            String newName = nc.getConfig().getString("name");
            return newName;
        }
    }
     
  6. Offline

    Sorroko

    Only your main class should extend JavaPlugin, pass your plugins instance into the constructor of you listener.
    kumpelblase2 A NoClassDefFoundError is not caused by warnings, it is most likely a setting in your IDE that is not exporting the classes with warnings.
    Also try not to use static as it can just cause memory leaks an issues.
     
  7. Offline

    Jeff.Halbert

    [SOLVED!!!!]
    I found the issue. Turns out, you where 100% right! lol. It's a matter of passing the mainInstance created by bukkit back and forth! Thanks man!

    Check it out, you'll be proud of me lmao!
    Code:
    package me.GBCsubspec.namechanger;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class namechanger extends JavaPlugin implements Listener {
     
        public void onDisable() {
            System.out.println("namechanger disabled!");
        }
       
        public void onEnable() {
            getConfig();
            saveConfig();
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
            namer.whoAgain(this);
        }
       
        @EventHandler
        public void playerJoin(PlayerJoinEvent event) {
            String newName = namer.getName(this);
            event.getPlayer().setDisplayName(newName);
        }
    }
    
    Code:
    package me.GBCsubspec.namechanger;
     
    public class namer {
     
        public static  String getName(namechanger nc) {
            String newName = nc.getConfig().getString("name");
            return newName;
        }
       
        public static void whoAgain(namechanger nc) {
            String newName = getName(nc);
            System.out.println(newName);
        }
    }
     
  8. Offline

    Veromca-2012

    How would I go about passing the main instance to the connect() method witout creating a new instance for each connect?

    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page