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

    r0306

    Ok. Stuff should work out now. Tell me if you get any more errors.
     
  2. Offline

    Mike111177

    Cannot make a static reference to the non-static method onChat(PlayerChatEvent) from the type PlayerChatListener Events.java /Steel-Security/src/net/othercraft/steelsecurity/listeners line 17 Java Problem

    Cannot make a static reference to the non-static method onSpeak(PlayerChatEvent) from the type CensoredWordProccess PlayerChatListener.java /Steel-Security/src/net/othercraft/steelsecurity/listeners line 10 Java Problem
     
  3. Offline

    r0306

    Mike111177
    Change your CensoredWordProcessed code to this:
    Do the same with the other class.
    Code:
    package net.othercraft.steelsecurity.listeners;
     
    import net.othercraft.steelsecurity.antispam.*;
     
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class PlayerChatListener {
    private CensoredWordProcess listener;
    public PlayerChatListener(CensoredWordProcess listener) {
    this.listener = listener;
    }
     
    public void onChat(PlayerChatEvent event) {
    listener.onSpeak(event);
     
    }
     
    }
     
  4. Offline

    Mike111177

    r0306
    i still get
    Cannot make a static reference to the non-static method onChat(PlayerChatEvent) from the type PlayerChatListenerCannot make a static reference to the non-static method onChat(PlayerChatEvent) from the type PlayerChatListener Events.java
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    Then don't make a static reference.
     
  6. Offline

    Mike111177

  7. Offline

    Sagacious_Zed Bukkit Docs

    In that case, closing and reopening the project might get it to revalidate the code.
     
  8. Offline

    r0306

    Same thing for the events:
    Code:
    Main plugin;
    private PlayerChatListener chatListener;
     
    public Events(Main instance) {
    plugin=instance;
    }
    public Events(PlayerChatListener chatListener) {
    this.chatListener = chatListener;
    }
    public
    @EventHandler
    public void onPlayerChat(PlayerChatEvent event) {
    chatListener.onChat(event);
    }
     
  9. Offline

    Mike111177

    Duplicate modifier for the method onPlayerChat in type Events
    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;
    	private PlayerChatListener chatListener;
    	 
    	public Events(Main instance) {
    	plugin=instance;
    	}
    	public Events(PlayerChatListener chatListener) {
    	this.chatListener = chatListener;
    	}
    	public
    	@EventHandler
    	public void onPlayerChat(PlayerChatEvent event) {
    	chatListener.onChat(event);
    	}
    	@EventHandler
    	public void onPlayerMove(PlayerMoveEvent event) {
    		PlayerMovementListener.onMove(event);
    	}
    
    }
    
     
  10. Offline

    r0306

    Mike111177
    Combine them like this:
    Code:
        public Events(Main instance, PlayerChatListener chatListener) {
        plugin=instance;
        this.chatListener = chatListener;
        }
     
  11. Offline

    Mike111177

    Like this?
    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;
    	private PlayerChatListener chatListener;
    	 
    	public Events(Main instance) {
    	plugin=instance;
    	}
    	public Events(Main instance, PlayerChatListener chatListener) {
    	    plugin=instance;
    	    this.chatListener = chatListener;
    	}
    }
    
     
  12. Offline

    r0306

    No, like this:
    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;
        private PlayerChatListener chatListener;
       
        public Events(Main instance, PlayerChatListener chatListener) {
            plugin=instance;
            this.chatListener = chatListener;
        }
    }
     
  13. Offline

    Mike111177

    what about @Eventhandler?
     
  14. Offline

    r0306

    No, don't put that on there.
     
  15. Offline

    Mike111177

    ERROR: The constructor Events(Main) is undefined

    Code:
    package net.othercraft.steelsecurity;
    
    import java.util.Arrays;
    import java.util.List;
    
    import net.othercraft.steelsecurity.commands.Commands;
    import net.othercraft.steelsecurity.listeners.Events;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    	
    	 FileConfiguration config = this.getConfig();
    	
    	/*here*/ public final Events eve = new Events(this);
    	
    	private Commands myExecutor;
    	
    	public void onEnable() {
    		myExecutor = new Commands(this);
    		PluginManager pm = this.getServer().getPluginManager();
    		pm.registerEvents(eve, this);
    		getCommand("sts").setExecutor(myExecutor);
    		getCommand("freezeall").setExecutor(myExecutor);
    		loadConfiguration();
    	}
    	     
    	public void onDisable() {
    		
    	}
    	public void loadConfiguration(){
    		 config.addDefault("Anti_Spam.censoring.enabled", false );
    		 config.addDefault("Anti_Spam.censoring.blocked_words", Arrays.asList("Avo","ICHG","Nodus"));
    	     getConfig().options().copyDefaults(true);
    	     saveConfig();
    	}
    	public void configReload(){
    		reloadConfig();
    	}
    	public List<String> findConfigValueList(String request) {
    		List cvalue = getConfig().getList(request);
    		return cvalue;
    	}
    }
    
     
  16. Offline

    r0306

    Mike111177
    Remove that line and in the onEnable() method, add change it to this:
    Code:
        public void onEnable() {
            myExecutor = new Commands(this);
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(new Events(this, PlayerChatListener), this);
            getCommand("sts").setExecutor(myExecutor);
            getCommand("freezeall").setExecutor(myExecutor);
            loadConfiguration();
        }
     
  17. Offline

    Mike111177

    PlayerChatListener cannot be resolved to a variable
     
  18. Offline

    Sagacious_Zed Bukkit Docs

    feed it the instance of PlayerChatListener
     
  19. Offline

    r0306

    Mike111177
    Here:
    Code:
    public class Main extends JavaPlugin {
       
        FileConfiguration config = this.getConfig();
        private PlayerChatListener cl;
        private Commands myExecutor;
       
        public void onEnable() {
            myExecutor = new Commands(this);
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(new Events(this, cl), this);
            getCommand("sts").setExecutor(myExecutor);
            getCommand("freezeall").setExecutor(myExecutor);
            loadConfiguration();
        }
     
  20. Offline

    Mike111177

    YAY NO ERRORS! thank you so much for your help!
     
  21. Offline

    r0306

    Phew lol. No problem. :p
     
  22. Offline

    Mike111177

    lol its been 6 hours and its midnight for me goodnight! :D
     
Thread Status:
Not open for further replies.

Share This Page