quick question

Discussion in 'Plugin Development' started by Hendrik_the_best, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. how can i use a scheduleSyncRepeatingTask in another class (not in the main class)

    i didnt get it to work (i got this error)
    http://pastebin.com/LhUXsaxx

    so i let it implements Listener (so the plugin isnt null)
    and it works but i dont want it to implements Listener... i dont need it in that class

    scheduleSyncRepeatingTask class:
    Code:java
    1.  
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.Listener;
    6.  
    7. public class Chests implements Listener{
    8.  
    9. static main plugin;
    10.  
    11. public Chests(main instance){
    12. plugin = instance;
    13. }
    14.  
    15. public static void start(){
    16. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    17. public void run(){
    18. Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "test");
    19. }
    20. },10, 2 * 20);
    21. }
    22. }
    23.  


    Thanks
    -Hendrik :)
     
  2. Offline

    Europia79

    Hendrik_the_best

    Yeah, unfortunately, it looks like the plugin field is null eventho you made the assignment in the contructor of Chests.

    I would have to see more code (like the main class) to see what's going on...

    Did you do
    Code:java
    1. getServer().getPluginManager().registerEvents(new Chests(this), this);

    inside onEnable in your main class ?

    The reason why I'm asking that is because you're implementing Listener. But you also don't have any Events in this class ? so it's wierd ?

    How are you instantiating the Chests class ?
    How are you invoking the start() method ?

    Also, there is another way to make an assignment to your main plugin field:

    Code:java
    1. plugin = (main) Bukkit.getPluginManager().getPlugin("Fguns");


    There's also a 3rd way to make the assignment, but I prefer parameter passing (like you're doing), or using getPlugin(string).

    Also, the wiki is a good resource:
    http://wiki.bukkit.org/Scheduler_Programming
     
  3. Europia79
    I made it implements Listener so the plugin wont be null i want to get rid of it thats why i was asking help in the first place :p
    i just want to get rid of the plugin being null but idk how :(
    plugin = (main) Bukkit.getPluginManager().getPlugin("Fguns"); doesnt work for me (my plugin is still null when i remove the implements Listener)
     
  4. Offline

    Europia79

    Hendrik_the_best

    Can you post all your code ? including the plugin.yml

    As far as the method getPlugin("Fguns"); replace "Fguns" with the name of your plugin (taken from the plugin.yml)

    Also, from what I can see, you can get rid of "implements Listener" from your Chests class.

    In which case, you'd instantiate the Chests class differently than registerEvents(new Chests(this), this);

    you'd do

    Code:java
    1. Chests chests = new Chests(this);
    2. /**
    3.  * or
    4.  */
    5. Chests chests = new Chests();
     
  5. Offline

    Rocoty

    Logic does not add up...


    And why do you make the field and method static if you were going to instantiate the class anyway? Remove the static modifiers, instantiate the class and pass the plugin instance to the constructor. Use the now instance method whenever you need to use it.

    Do you know why you implemented Listener there? It doesn't seem to serve any purpose.
     
  6. the only reason why i added implements Listener is so the plugin isnt null....
    I know it isnt logic but atleast my plugin works...


    Thanks for your help my plugin was named FGuns and i did
    plugin = (main) Bukkit.getPluginManager().getPlugin("Fguns"); :p
    i made it
    plugin = (main) Bukkit.getPluginManager().getPlugin("FGuns");
    and now it works thanks!
     
  7. Offline

    Rocoty

    Hendrik_the_best Implementing Listener is not in any way related to whether the plugin field is null. I do not understand where you get that from
     
  8. Rocoty
    I looked in some other plugins from me and they had implements Listener and they worked fine so i did that to my new plugin so i could alteast test it.
     
  9. Offline

    Rocoty

    Hendrik_the_best Good that you are testing. But you should really know why you are testing it as well. Read some tutorials on Inheritance, Abstraction and Polymorphism. Trust me, it will be worthwhile.
     
  10. Rocoty
    Thanks for your help ill look into it :)
     
    Rocoty likes this.
  11. Offline

    Europia79

    Hendrik_the_best

    The keyword "extends SomeClass" means that you'll inherit all the abilities and attributes of SomeClass. Use the Navigator in your IDE to see which methods you've gained access to. Another way to see which methods you've gained access to is to do control+spacebar and you'll get a pop-up menu of all the attributes and abilities (methods). A third way is to type "this." (with the period) and your IDE will do the same pop-up menu.

    The keyword "implements SomeInterface" means you're required to implement the methods defined by SomeInterface. Just click the side button, and click "implement all abstract methods."

    When I very first started with Java, I did NOT understand why you would ever use "implements" when "extends" is so powerful.

    This video on the MouseListener illustrates the power of the implements keyword. (But depending on your level of experience with Java, you might have to start with one of the earlier tutorials:

    http://www.newthinktank.com/2012/03/java-video-tutorial-21/

    This is another great video on the power of the implements keyword to dynamically change the abilities of classes at runtime:

    http://www.newthinktank.com/2012/08/strategy-design-pattern-tutorial/

    Like I said: I don't know where you're at, but Derek Banas' tutorials go super fast... So you'll be able to breeze thru the stuff you already know and get into new stuff, like Design Patterns.

    Here's the entire Java playlist starting with tutorial one:

    Code:
    https://www.youtube.com/watch?v=TBWX97e1E9g&list=PLE7E8B7F4856C9B19
    Good luck!
     
    Hendrik_the_best likes this.
Thread Status:
Not open for further replies.

Share This Page