method - run from a different class file [Help]

Discussion in 'Plugin Development' started by vYN, Feb 21, 2012.

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

    vYN

    Hi. I'm trying to run a method from my main class. The method is in a class file named "WarpConfig". And my method name is "loadWarps" But i can't run it from my main class. or rather I'm not sure how to.
     
  2. Offline

    dillyg10

    The syntax for this is...
    Code:
    WarpConfig.loadWarps() // put any params you have setup in the ()
     
  3. You forgot a ; but not that important.
    Depending on if it's static or not, you might need to create an instance first.
     
  4. Offline

    vYN

    But when i do WarpConfig. it doens't show me anything. or if i just put loadWarps() after the "." it just gives me red lines.
    Code:java
    1. public void loadWarps() {
    2. this.getWarps().options().copyDefaults(true);
    3. saveWarps();
    4. }
     
  5. Offline

    SirTyler

    If you are going to access it a static way then it needs to be a static method.
    Code:java
    1. public static void loadWarps() {
    2. this.getWarps().options().copyDefaults(true);
    3. saveWarps();
    4. }
     
  6. Offline

    vYN

    if not?
     
  7. Offline

    SirTyler

    Code:java
    1.  
    2. WarpConfig wc = new WarpConfig(this);
    3. wc.loadWarps();
    4.  
     
  8. Offline

    Drakonix

    Why you putted this as an arguement? You don't need to unless you created a constructor in a main method. And you don't know if he created that constructor.
     
  9. Offline

    vYN

    i get spammed with this during load:

    Code:
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:136)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:286)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:201)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:164)
        at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:469)
        at org.bukkit.Bukkit.reload(Bukkit.java:188)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:402)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:398)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:574)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.StackOverflowError
    Code:
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
        at src.HCbasics.Main.Basics.<init>(Basics.java:14)
        at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:15)
    Basic 14: WarpConfig wc = new WarpConfig(this);

    WarpConfig 15:
    Code:
        public WarpConfig(Basics instance) {
            plugin = instance;
        }
     
  10. Offline

    Darkman2412

    You cannot access the 'this' keyword in a static method... Just saying :/
     
  11. Offline

    vYN

    i know. Not using static method.

    So I'm not sure what to do...

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

    nisovin

    It seems your WarpConfig's constructor is creating an instance of your main class. You're then going into an infinite loop with the two classes continually creating each other.
     
  13. Offline

    vYN

    Ok. i will try to fix that. Give me a sec. will post if i fixed it or just fail over and over again.

    1 question. i got
    Code:
        public WarpConfig(Basics basics) {
            plugin = basics;
        }
    that in WarpConfig. and Basics is my main class. U see anything wrong with that lol ??

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

    nisovin

    There's nothing wrong with that. It's probably something else. If you can't figure it out on your own, you'll have to post your entire source.
     
  15. Offline

    vYN

    ok...
    main class:
    Code:java
    1. public class Basics extends JavaPlugin {
    2. WarpConfig wc = new WarpConfig(this);
    3. public static Basics plugin;
    4. FileConfiguration config;
    5. public final Logger logger = Logger.getLogger("Minecraft");
    6.  
    7. public void onDisable(){
    8.  
    9. }
    10.  
    11. public void onEnable() {
    12. regCom(this);
    13. PluginManager pm = getServer().getPluginManager();
    14. pm.registerEvents(new SpawnLoc(), this);
    15.  
    16. //LoadConfigs
    17. wc.loadWarps();
    18. }
    19.  
    20. private void regCom(Basics Basics) {
    21. this.getCommand("setspawn").setExecutor(new SpawnSetCommand(this));
    22. this.getCommand("spawn").setExecutor(new SpawnCommand(this));
    23. this.getCommand("warp").setExecutor(new WarpCommand());
    24. this.getCommand("setwarp").setExecutor(new WarpSetCommand());
    25. }
    26. }


    WarpConfig class:
    Code:java
    1. public class WarpConfig extends Basics {
    2. public FileConfiguration warps = null;
    3. public File warpsFile = null;
    4.  
    5. public WarpConfig(Basics basics) {
    6. plugin = basics;
    7. }
    8.  
    9. public void loadWarps() {
    10. this.getWarps().options().copyDefaults(true);
    11. saveWarps();
    12. }
    13.  
    14. // Reloads the warps
    15. public void reloadWarps() {
    16. if (warpsFile == null) {
    17. warpsFile = new File(getDataFolder(), "warps.yml");
    18. }
    19. warps = YamlConfiguration.loadConfiguration(warpsFile);
    20.  
    21. InputStream defConfigStream = getResource("warps.yml");
    22. if (defConfigStream != null) {
    23. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    24. warps.setDefaults(defConfig);
    25. }
    26. }
    27.  
    28. // Get warps
    29. public FileConfiguration getWarps() {
    30. if (warps == null) {
    31. reloadWarps();
    32. }
    33. return warps;
    34. }
    35.  
    36. // Save Warps
    37. public void saveWarps() {
    38. if (warps == null || warpsFile == null) {
    39. return;
    40. }
    41. try {
    42. warps.save(warpsFile);
    43. } catch (IOException ex) {
    44. Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Error saving warps to " + warpsFile, ex);
    45. }
    46. }
    47. }
     
  16. Offline

    dillyg10

    Ok, if you want the method to be nonstatic do what SirTyler said. But, if this method is in your main class file, you really don't need to use the this.getWarps(), you can just use getWarps() {I belive unless this is a constructor for your class that links elsewere... please post full soruce, so that we can help you :D}
     
  17. Offline

    vYN

    Uhm. just did... look at the post above yours xD
     
  18. Offline

    dillyg10

    1. public static void loadWarps() {
    2. getWarps().options().copyDefaults(true);
    3. saveWarps();
    4. }
     
  19. Offline

    vYN

    stackoverflow....
    at src.HCbasics.Main.WarpConfig.<init>(WarpConfig.java:20)
    at src.HCbasics.Main.Basics.<init>(Basics.java:16)


    WarpConfig line 5 in the post above with the code
    Basics line 2 in the post above with the code

    Getting that during load.
     
  20. Offline

    dillyg10

    errr, why are you doing new warpConfig(this)?
    and I would recomend if you are trying to get the plugin do..
    Code:
    Basics plugin;
    public static WarpConfig(Basics instance) {
          plugin = instance;
    }
     
    //then call that from your Basics class with
    WarpConfig(this);
    also, how do you java highlighting :D
     
  21. Offline

    vYN

    uhm. I'm a bit confused.. What to put where?

    in my WarpConfig?
    Code:
    Basics plugin;
    public static WarpConfig(Basics instance) {
          plugin = instance;
    }
    and this in my main one:

    Code:
    WarpConfig(this);
    if so that doens't work well..
     
  22. Offline

    dillyg10

    WarpConfig(this); call that in your onEnable();
     
  23. Offline

    vYN

    That asks me to just create a method in my main class.
     
  24. Offline

    dillyg10

    No, you do WarpConfig.WarpConfig(this);
     
  25. Offline

    vYN

    Ok. i didn't get the option WarpConfig after WarpConfig. But

    What if i do it like this:

    put this in the warpconfig:

    Code:
        public static void plugin(Basics basics) {
            loadWarps();
        }
    and this in my main on enable?

    Code:
    WarpConfig.plugin(this);
    haha i fixed it lOl. Changed the code a little from the above post.

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

    nisovin

    I don't know how much of this you figured out, but your problem was that WarpConfig extends Basics. It shouldn't do that.
     
  27. Offline

    vYN

    what.... Now it gives me an NPE on:
    Code:
    Basics.plugin.getServer().getScheduler().scheduleSyncDelayedTask(Basics.plugin, new Runnable() {
    ok. i'm not sure what i did... but i still works after i removed the code lol

    But stills gives me that NPE on getSchedular xD i will try to fix that on my own xD

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

    dillyg10

    I would say to keep all config stuff in one class so that you don't have to deal with this kinda stuff. I feel stupid for not chatching the extends Basics :)
     
  29. Offline

    Waterflames

    btw for java hightlighting:
    Code:
    [syntax=java]
    [/syntax]
     
Thread Status:
Not open for further replies.

Share This Page