Static And non static methods and classes

Discussion in 'Plugin Development' started by Mike111177, May 16, 2012.

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

    Mike111177

  2. Offline

    r0306

    Mike111177
    Why do you need to make the listener static? Remove the static declaration and it should work.
     
  3. Offline

    Mike111177

    r0306

    If i do that then it just tells me to make it static again
     
  4. Offline

    r0306

    Mike111177
    You need to create an instance that refers to your main class in a static way. Add this to your listener class:

    Code:
    private Main plugin;
    public CensoredWordProcess(Main plugin) {
    this.plugin = plugin;
    }
    
    Never make an event listener static because it will only create one instance of the handler.

    By the way, your censoredWordProcess class needs to implement Listener and there should be an @EventHandler above the PlayerChatEvent method. Also, you should move the list out of the method as you only need one instance of the list. Also, you only need to create one instance of the list in the main class and refer to it from the other classes.
    Code:
    public class CensoredWordProccess implements Listener{
     
    private Main plugin;
    public CensoredWordProcess(Main plugin) {
    this.plugin = plugin;
    }
     
    @EventHandler
    public void onSpeak(PlayerChatEvent event) {
    String request = "Anti_Spam.censoring.blocked_words";
    List<String> words = plugin.findConfigValueList(request); //NOTE: I used 'plugin' instead of 'Main'
    }
     
    }
     
  5. Offline

    Mike111177

    r0306
    this isn't the event handler the event handler is under listeners.Events
     
  6. Offline

    r0306

    Mike111177
    In your method's constructor, you are using a PlayerChatEvent. If it is not an event, what is this class used for?
     
  7. Offline

    Mike111177

  8. Offline

    r0306

    Mike111177
    Oh okay. Create an instance of your main class like you did in your event listeners, using that to refer to the main class and your static problem should be solved.
     
  9. Offline

    Mike111177

    Oh so i just put the word new before Main.findConfigValueList(request);
     
  10. Offline

    r0306

    Mike111177
    No lol. You should create a static instance that refers to the main class just like you did in your event listeners. That way, instead of using Main.findConfigValueList(request), you would use plugin.findConfigValueList(request) [I named my static reference 'plugin' as in the example above.] (see post above for example code).
     
  11. Offline

    Mike111177

    but when i do that it makes me add void to public Main which then brings me to the same issue with findConfig.... wanting to be static.
     
  12. Offline

    r0306

    Mike111177
    Can you post the code that you are having trouble with?
     
  13. Offline

    Mike111177

    Code:
    
    package net.othercraft.steelsecurity.antispam;
    
    
    import java.util.List;
    
    import net.othercraft.steelsecurity.Main;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.player.PlayerChatEvent;
    
    public class CensoredWordProccess{
    	
    	private Main plugin;
    	// the line below makes me add void
    	public/* here*/ CensoredWordProcess(Main plugin) {
    	this.plugin = plugin;
    	}
    	
    	public static void onSpeak(PlayerChatEvent event) {
    		String request = "Anti_Spam.censoring.blocked_words";
    		//then this make me go to the main class and change findConfigValueList to static
    	    List<String> words = plugin.findConfigValueList(request); 
    	}
    
    }
    
     
  14. Offline

    r0306

    Mike111177
    Your spelling does not match:
    Code:
    public class CensoredWordProccess{
    public/* here*/ CensoredWordProcess(Main plugin)
    
    (Add an extra "c" to Process?)
     
  15. Offline

    Mike111177

    LOL i copied it from your example i fixed that but it still wants me to change the method in Main to static
     
  16. Offline

    r0306

    Remove the static declaration in your method.
    Code:
        public static void onSpeak(PlayerChatEvent event) {
    to
    Code:
        public void onSpeak(PlayerChatEvent event) {
     
  17. Offline

    Mike111177

    When i do that
    Code:
    
    package net.othercraft.steelsecurity.listeners;
    
    import net.othercraft.steelsecurity.antispam.*;
    
    import org.bukkit.event.player.PlayerChatEvent;
    
    public class PlayerChatListener {
    /* Line below wants me to make the onspeak static*/
    	public static void onChat(PlayerChatEvent event) {
    		CensoredWordProccess.onSpeak(event);
    		
    	}
    
    }
    
    I removed that static but then this one wanted the one above for the same
    Code:
    
    package net.othercraft.steelsecurity.listeners;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.*;
    
    import net.othercraft.steelsecurity.Main;
    
    public class Events implements Listener {
    	Main plugin;
    
    	public Events(Main instance) {
    		plugin=instance;
    	}
    	@EventHandler
    	public void onPlayerChat(PlayerChatEvent event) {
    /*here*/		PlayerChatListener.onChat(event);
    	}
    	@EventHandler
    	public void onPlayerMove(PlayerMoveEvent event) {
    		PlayerMovementListener.onMove(event);
    	}
    
    }
    
     
  18. Offline

    r0306

    Mike111177
    Yes, then remove the static from that one as well. Event listeners should not be static.
     
  19. Offline

    Mike111177

    the Events class wants me to make PlayerChatListener Static though
     
  20. Offline

    r0306

    Mike111177
    Like I said, make everything non-static. :cool:
    Code:
    public void onChat(PlayerChatEvent event) {
    AntiFlood.onSpeak(event);
    AntiCaps.onSpeak(event);
    CensoredWordProccess.onSpeak(event);
     
    }
     
    
     
  21. Offline

    Mike111177

    Code:
    
    package net.othercraft.steelsecurity.listeners;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.*;
    
    import net.othercraft.steelsecurity.Main;
    
    public class Events implements Listener {
    	Main plugin;
    
    	public Events(Main instance) {
    		plugin=instance;
    	}
    	@EventHandler
    	public void onPlayerChat(PlayerChatEvent event) {
    		PlayerChatListener.onChat(event); // this line is making me turn onChat(event) back into static
    	}
    	@EventHandler
    	public void onPlayerMove(PlayerMoveEvent event) {
    		PlayerMovementListener.onMove(event); 
    	}
    
    }
    
     
  22. Offline

    TheRealZuriki

    Why is PlayerChatListener not implementing Listener?
     
  23. Offline

    r0306

    Mike111177
    Go to your onChat method and make it non-static....

    He's passing the events on to the separate methods.

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

    Mike111177

    @ TheRealZuriki

    Player chat listener is not the accuall listener. Events sends the event to PlayerChatListener.

    r0306

    i did that and it wants me to change it back
     
  25. Offline

    r0306

  26. Offline

    Mike111177

    Last edited by a moderator: May 25, 2016
  27. Offline

    r0306

    Also, you will need to make AntiFlood and AntiCaps non-static as well since those are called inside the Listener.
     
  28. Offline

    Mike111177

    r0306
    not right now they arnet im focused on the antispam
     
  29. Offline

    r0306

    Still, you need to change it or else it will tell you to make the listener static.
     
  30. Offline

    Mike111177

    r0306
    i removed them them for now anyway you'll see if you refresh
     
Thread Status:
Not open for further replies.

Share This Page