[Solved] Why doesn't it recognize player in "player.HasPermission("bla")"

Discussion in 'Plugin Development' started by Unscrewed, Dec 19, 2011.

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

    Unscrewed

    Hello,
    I didn't code in Java for quite a few months now, and I wanted to start updating/creating my plugins again. Since I didn't code for a while, I need to 'figure it out' again, so my question is:
    Why doesn't it recognize "player" in "player.HasPermission("iBlock.use")"?
    It sounds like a noob-ish question, but it's hard to start again after a few months.

    Code:
    package me.Unscrewed.iBlock;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class iBlock extends JavaPlugin{
    	private final iBlockBlockListener bListener = new iBlockBlockListener();
    	Logger log = Logger.getLogger("Minecraft");
    	@Override
    	public void onEnable() {
    		log.info("iBlock 2.0 - [Enabled]");
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.BLOCK_PLACE, bListener, Priority.Normal, this);
    	}
    	@Override
    	public void onDisable() {
    		log.info("iBlock 2.0 - [Disabled]");
    	}
    	{
    	if(player.hasPermission("iBlock.use")){
    		//Allow
    	}else{
    		//Disallow
    	}
    }
    }
    Thanks in advance,
    - Unscrewed
     
  2. Offline

    Father Of Time

    Because you never declare the player variable. Also, is that if statement even part of a function, it appears by itself to me.

    It needs to be inside something that passes a player variable to it, like the onCommand function. onCommand passes a variable called "sender" which can be cast to a player:

    Code:
    Player player = (Player)sender;
    and then you have your player variable. Hope this helps, GL! :D
     
    Unscrewed and halley like this.
  3. Offline

    halley

    The code in question is not in a method.

    Your iBlock class, like any class, has fields and methods. Your fields are bListener and log. Your methods are onEnable() and onDisable().

    After onDisable()'s definition, you have { if (player.hasPermission... }. This is not in a method. Now Java technically allows code to show up outside all methods, but it essentially becomes part of the constructor. The real problem is that you're referencing a variable called player that has not been declared anywhere as an argument or class field.

    That sort of code usually appears as a part of an onCommand() method that takes a CommandSender argument that may (or may not) be a Player.

    I suggest you revisit your Java coding books first, then the canonical Bukkit Plugin Tutorial.
     
    Father Of Time and Unscrewed like this.
  4. Offline

    Unscrewed

    @halley
    Thanks, It's a good idea to read some Java books again yes, I hope I didn't forget it and that it's just very deep in my brain and that I will remember it again after some basic Java..

    @Father Of Time
    Oh, that was really dumb of me, thanks. :)
    This fixed it:

    Code:
    package me.Unscrewed.iBlock;
    
    
    import java.util.logging.Logger;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class iBlock extends JavaPlugin{
    	private static final Player sender = null;
    	private final iBlockBlockListener bListener = new iBlockBlockListener();
    	Logger log = Logger.getLogger("Minecraft");
    	Player player = (Player)sender;
    	@Override
    	public void onEnable() {
    		log.info("iBlock 2.0 - [Enabled]");
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.BLOCK_PLACE, bListener, Priority.Normal, this);
    	}
    	@Override
    	public void onDisable() {
    		log.info("iBlock 2.0 - [Disabled]");
    	}
    	{
    		if(player.hasPermission("iBlock.use")){
    			//Allow
    		}else{
    			//Disallow
    		}
    	}
    }
    
     
  5. Offline

    Father Of Time

    Halley's explanation is better than mine; that's what you get when you rush a response while eating your morning muffin at work. :D
     
  6. Offline

    halley

    That satisfied the compiler, but will crash on load.
     
  7. Offline

    Father Of Time

    Again, your statement is free floating, and you've not declared a local player variable:

    Code:
        @Override
        public void onDisable()
        {
            log.info("iBlock 2.0 - [Disabled]");
        }
    
        {
            if(player.hasPermission("iBlock.use"))
            {
                //Allow
            }
            else
            {
                //Disallow
            }
        }
    }
    
    it needs to be part of another function, like onCommand:

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            Player player = (Player)sender;
            if(player.hasPermission("iBlock.use"))
            {
                //Allow
            }
            else
            {
                //Disallow
            }
        }
    
    Notice how on line three I declare the player variable by casting the sender (being passed to the onCommand function) to a Player object.

    Edit:

    This is outside a function:

    Code:
        public void CustomFunction()
        {
        }
    
        Player player = (Player)sender;
    
    and this is inside a function:

    Code:
        public void CustomFunction()
        {
            Player player = (Player)sender;
        }
    
    Not trying to beat a dead horse, just making sure we share the same definition of "inside a function".
     
  8. Offline

    Unscrewed

    Code:
    package me.Unscrewed.iBlock;
    
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class iBlock extends JavaPlugin{
    	private static final Player sender = null;
    	private final iBlockBlockListener bListener = new iBlockBlockListener();
    	Logger log = Logger.getLogger("Minecraft");
    	Player player = (Player)sender;
    
    	@Override
    	public void onEnable() {
    		log.info("iBlock 2.0 - [Enabled]");
    		PluginManager pm = getServer().getPluginManager();
    		pm.registerEvent(Event.Type.BLOCK_PLACE, bListener, Priority.Normal, this);
    	}
    	@Override
    	public void onDisable() {
    		log.info("iBlock 2.0 - [Disabled]");
    	}
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    		if(cmd.getName().equalsIgnoreCase("iBlock") && player.hasPermission("iBlock.use")){
    			//Allow
    			return true;
    		}else{
    			//Disallow
    		}
    		return false;
    	}
    }
    
    This would fix it, I'm guessing? :)
     
  9. Offline

    Father Of Time

    At first glance that looks worlds better, good lob. :p
     
  10. Offline

    halley

    Closer, but no.

    Remove your field declaration.

    private static final Player sender = null;

    Insert a variable declaration in your onCommand().

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if (!(sender instanceof Player)) {
            // Disallow
            // Tell the non-player sender that they can't do this command.
            return true;
        }
        Player player = (Player)sender;
        if(cmd.getName().equalsIgnoreCase("iBlock") && player.hasPermission("iBlock.use")) {
            //Allow
    	return true;
        } else {
            //Disallow
            // if you tell the player what's wrong here, return true
        }
        return false;
    }
    
     
    Unscrewed likes this.
  11. Offline

    Unscrewed

    @halley
    @Father Of Time
    Thank you both a lot! Starting to get into it again!
    Marking this thread as solved. :)
     
  12. Offline

    Father Of Time

    Good catch Halley, I was too focused on the bracket formatting and didn't even catch that.

    My pleasure unscrewed, take care! :D
     
    Unscrewed likes this.
Thread Status:
Not open for further replies.

Share This Page