Can't Access method from another class

Discussion in 'Plugin Development' started by S1ant, Sep 21, 2017.

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

    S1ant

    Hello, lots of you may know me(partly because I live here when developing a plugin), but when I try to access the method of another from the Main class it doesn't work.

    So in my method class, I have the method as a public static void, with the parameters Player p. When I access it from the Main class like this:

    <classname>.<methodname>(p);
    So class is Apple and Method is cutter I'd do:
    Apple.cutter(p);
    The method cutter doesn't run.

    I don't really want to post my code, so if I Could avoid it that'd be great!

    It is kind of weird because in another class that isn't the main class I can run the method perfectly fine. No Stacktraces either.
     
  2. Offline

    yPedx

    @S1ant
    It's kind of hard without the code though.. You don't need to post your whole code, only what is included in this.
     
  3. Offline

    S1ant

    Yes kind of true xd

    Here it is:
    Code:java
    1.  
    2. Fixed the Problem
    3.  
    4.  
     
    Last edited: Sep 24, 2017
  4. Offline

    Zombie_Striker

    @S1ant
    1. Are you sure the class is registered? Are you sure the event ever gets called?
    2. Are you sure the second line is exactly equal to the Yellow colorcode+[Class]?
    3. Are you sure the third line is exactly equal to Red colorcode+ Diamond?
    Also, some other notes
    1. You don't need to cast getPlayer to a player. It is already a player.
    2. Instead of adding more lines of code by inverting the check and returning, just check if the action is right clicking a block and that the state is a sign.
    3. Unless you plan on using the setDiamond class in multiple classes, the method should be included in the main class.
     
  5. Offline

    S1ant

    1. The class is registered? I don't know maybe? When I type it in it pops up for auto type. The Class where I have DiamondClass.setDiamondClass(p), I checked with Sys.out.println earlier.
    2 & 3 Yes I'm sure those are the names.

    Note Replies:
    1. I wanted to type p instead of Player.
    2. Done
    3. I am using the method in two different classes.
     
    Last edited: Sep 21, 2017
  6. Offline

    Zombie_Striker

    @S1ant
    You register the class by calling Bukkit.getPluginManager().registerEvents(this,this);. Are you calling this method for that class?
     
  7. Offline

    S1ant

    In my Main class this is what I'm doing, I have a feeling that this is what's causing the problem now that I look into it.
    Code:java
    1. public void onEnable(){
    2. Bukkit.getPluginManager().registerEvents(this, this);
    3. getCommand("kits").setExecutor(new kitsCommand());
    4. Bukkit.getPluginManager().registerEvents(new kitsCommand(), this);
    5. }
     
  8. Offline

    Caderape2

    @S1ant You are creating two differents instance of the class.
    Field that are not static will not be the same.
    Code:
    KitComand kc = new KitCommand();
    getCommand("kits").setExecutor(kc);
    Bukkit.getPluginManager().registerEvents(kc, this);
     
  9. Offline

    Minesuchtiiii

    Had a problem with colored text on signs too, the event wasn't triggered because of that, what I did was:
    Code:
    String raw= s.getLine(2);
                   String clean = raw.replace("§a", "");
    Now I could work with the string clean.
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    Minesuchtiiii

    I tried that, but somehow the event wasn't triggered too and I got errors in the console, so I played around with it a bit and ended up with the code above
     
  12. Offline

    S1ant

    ok, I'll do that, but can I do Bukkit.getPluginManager().registerEvents(this, this);
    Along with that?

    EDIT:
    That didn't work. @Minesuchtiiii or @timtower You got any ideas?
     
    Last edited: Sep 22, 2017
  13. Offline

    timtower Administrator Administrator Moderator

  14. Offline

    S1ant

    Code:java
    1.  
    2. Nothing
    3.  


    kitsCommand is perfectly fine, in that class when I set the class I do DiamondClass.setDiamondClass(p); too.
     
    Last edited: Sep 22, 2017
  15. Offline

    Minesuchtiiii

    @timtower yes of course, I checked it and it didn't give me any errors for my classes, looks like bukkit couldn't handle something.. I don't know, I changed the code and it's working for me now, so I don't wanna mess with it anymore :D

    @S1ant what I am using for my SignChangeEvent and works for me:
    Code:
    if(s.getLine(0).equalsIgnoreCase("§7[§dPermissionShop§7]")) {
                    if(!(s.getLine(1).isEmpty())) {
                        if(s.getLine(1).equalsIgnoreCase("§cbuy")) {
    
    //do stuff
    
    }
    }
    }
     
  16. Offline

    S1ant

    Are you even reading what I'm saying? The sign change event works fine, it's just that the PlayerInterectEvent doesn't loadout the player's inventory Like I tell it to with:
    DiamondClass.setDiamondClass(p);

    Thank's for the help everyone, I'm just stupid, but I figured it out on my own.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  17. Offline

    Minesuchtiiii

    So what was the problem?
     
  18. Offline

    S1ant

    I had Diamond instead of <Diamond> I should've ran a check for it.
     
Thread Status:
Not open for further replies.

Share This Page