-1

i was working on a very simple minecraft plugin for a school project and i can't seem to get this return false; to be "reached"

im using the eclipse IDE, heres my code

package com.genuishour.me;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Events implements CommandExecutor{
    
    private Main plugin;
    
    public Events(Main plugin) {
        this.plugin = plugin;
        
    plugin.getCommand("hello").setExecutor(this);
        
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("Only players can execute this command!");
            return true;
            
            
        }
        
        Player p = (Player) sender;
        
        String name = sender.getName();
        
        if (p.hasPermission("hello.use")) {
            p.sendMessage("Hello " + name + "!");
            return true;
            } else {
                p.sendMessage("You do not have permission to execute this command");
                return true;
            }
        
        return false; //unreachable code
    }       
    

}
  • 1
    When do you expect it to be reached when you return when p has the permission "hello.use" and you return if it doesn't. Which situation would skip both if and else? Since your `return false` doesn't work there, you need to find a better place for it and there is one. When you think about it, then you'll find it. – Tom Jun 16 '21 at 19:46
  • 3
    Because it is. Both branches of your `if (p.hasPermission("hello.use"))` return first. If you indent your code, you'll probably see it better. – timemage Jun 16 '21 at 19:48

3 Answers3

1

You have a "dangling else" - these two would be equivalent:

if (p.hasPermission("hello.use")) {
    p.sendMessage("Hello " + name + "!");
    return true;
} else {
    p.sendMessage("You do not have permission to execute this command");
    return true;
}
   
return false; //unreachable code

and

if (p.hasPermission("hello.use")) {
    p.sendMessage("Hello " + name + "!");
    return true;
}
p.sendMessage("You do not have permission to execute this command");
return true;
   
return false; //unreachable code

which should be clear why the last one is unreachable

dave
  • 62,300
  • 5
  • 72
  • 93
0

Because here:

if (p.hasPermission("hello.use")) {
            p.sendMessage("Hello " + name + "!");
            return true;
            } else {
                p.sendMessage("You do not have permission to execute this command");
                return true;
            }

no matter what the result of p.hasPermission("hello.use") is, you will always exit the code by returning true.

timthedev07
  • 454
  • 1
  • 6
  • 17
-2

Based on how you indented the code, I guess you wanted the last return false to be the last statement in the function. But right now it is in the block started by (p.hasPermission("hello.use")) {

So to sum up: Move it down one line it is at the end of the function.

MTilsted
  • 5,425
  • 9
  • 44
  • 76