instanceof Problem

Discussion in 'Plugin Development' started by SimplyCoded, Jul 26, 2014.

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

    SimplyCoded

    Ok so I tried a various of different things and I do not understand why this will not work I have changed my code around. Tried different things but it will not work. heres the code:

    Code:
    @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player)sender;
            CommandSender s = sender;
            FileConfiguration fc = this.getPlayerStats(p);
            if(label.equalsIgnoreCase("shop")){
                shop.show(p);
            }
            if (label.equalsIgnoreCase("ban")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Could not find player " + args[0] + "!");
                    return true;
                }
                if (target.getName().equalsIgnoreCase("iStrafeHD")) {
                    if(!(s instanceof Player)) {
                        s.sendMessage("Nope xD");
                    }else{
                    p.sendMessage("You are not allowed to ban iStrafeHD....");
                    }
                    return true;
                }
                target.kickPlayer(ChatColor.RED + "You have been banned!");
                target.setBanned(true);
                fc.set("stats.banned", true);
                Bukkit.getServer().broadcastMessage(ChatColor.BLUE + target.getName() + ChatColor.AQUA + " has been banned by " +
                ChatColor.BLUE + sender.getName() + ChatColor.AQUA + "!");
            }
            return true;
        }
    And the error:

    P.S. DO NOT SAY GO LEARN JAVA PLEASE.
     
  2. Offline

    Drew1080

    SimplyCoded
    The third line in the code you pasted you cast the Player to the sender. So your instance of check further down is useless by that point, as the server has already thrown a class cast exception, as shown in your stacktrace.

    Code:
    [Server] INFO Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player 
     
  3. Offline

    SimplyCoded

    Drew1080
    So should i replace that with CommandSender s = sender; or move that above the Player p?
     
  4. Offline

    teej107

    Always put an instanceof check right before you are going to be casting.
     
  5. Offline

    Drew1080

    SimplyCoded
    You need to check if the variable sender from the parameter is an instance of a Player before you cast a Player to it. Also why are you creating a new variable for CommandSender as 's' when you can use the variable 'sender' from the parameters of the method?
     
  6. Offline

    SimplyCoded

    Drew1080 I was testing something let me try something else

    Drew1080
    I can't get this can you give me an example of what you mean i'm not asking for spoon feeding just an example

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    Drew1080

    SimplyCoded
    I really don't think you need an example as you have done an instance of check further down in your code. But here you go anyway.

    Code:java
    1. if(sender instanceof Player)
    2. {
    3. Player p = (Player)sender;
    4. }
     
  8. Offline

    SimplyCoded

    Im so confused atm Drew1080 Is that the check i need or what I am very confused
     
  9. Offline

    Drew1080

    SimplyCoded
    Yes that is the check you need to-do before casting the Player to the sender.
     
  10. Offline

    SimplyCoded

    Drew1080

    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            CommandSender s = sender;
            Player pv = (Player)sender;
            FileConfiguration fc = this.getPlayerStats(pv);
            if(label.equalsIgnoreCase("shop")){
                shop.show(pv);
            }
            if (label.equalsIgnoreCase("ban")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Could not find player " + args[0] + "!");
                    return true;
                }
                if (target.getName().equalsIgnoreCase("iStrafeHD")) {
                    if(!(s instanceof Player)) {
                        Player p = (Player)sender;
                        p.sendMessage("Nope xD");
                    }else{
                    pv.sendMessage("You are not allowed to ban iStrafeHD....");
                    }
                    return true;
                }
    like that?
     
  11. Offline

    Drew1080

    SimplyCoded
    Nope.
    Your still casting the player to the sender without checking first on line 3. This will throw a ClassCastException is the sender is not a Player.
    Then what you have here:
    Code:
     if(!(s instanceof Player)) {
                        Player p = (Player)sender;
    That won't work either as your checking is the CommandSender is not an instance of a Player, then you proceed to cast sender to the Player anyway, so that also will throw a ClassCastException.

    Edit: Since you clearly don't know what your doing and I'm sick of repeating myself. Read this: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html and learn about the type comparison operator instanceof.

    Edit 2: Take a look at this as well. http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html
     
    Rocoty likes this.
  12. Offline

    SimplyCoded

    Drew1080
    IM STILL CONFUSED? this?
    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            CommandSender s = sender;
            Player pv = (Player)sender;
            FileConfiguration fc = this.getPlayerStats(pv);
            if(label.equalsIgnoreCase("shop")){
                shop.show(pv);
            }
            if (label.equalsIgnoreCase("ban")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    sender.sendMessage(ChatColor.RED + "Could not find player " + args[0] + "!");
                    return true;
                }
                if (target.getName().equalsIgnoreCase("iStrafeHD")) {
                    if(!(s instanceof Player)) {
                        pv.sendMessage("You are not allowed to ban iStrafeHD....");
                    }else{
                    sender.sendMessage("You are not allowed to ban iStrafeHD....");
                    }
                    return true;
                }
                target.kickPlayer(ChatColor.RED + "You have been banned!");
                target.setBanned(true);
                fc.set("stats.banned", true);
                Bukkit.getServer().broadcastMessage(ChatColor.BLUE + target.getName() + ChatColor.AQUA + " has been banned by " +
                ChatColor.BLUE + sender.getName() + ChatColor.AQUA + "!");
            }
            return true;
        }
    i feel so dumb im confusing myself
     
  13. Offline

    Drew1080

    SimplyCoded
    Your not even listening to me now and that is clearly evident. I have told you the line number in the code you have posted on what your doing wrong and you still have failed to fix it even after when I spoonfeed you the code you needed. All you needed to-do with that was copy and paste.

    I know in your original post you didn't want people to tell you to go learn java, but please do, because it is very obvious you need to. Take a look at the link I posted above, then come back here and try to solve your issue with the repeated help I have given you.
     
  14. Offline

    fireblast709

    SimplyCoded
    Code:
    if (target.getName().equalsIgnoreCase("iStrafeHD")) {
                    if(!(s instanceof Player)) {
                        s.sendMessage("Nope xD");
                    }else{
                    p.sendMessage("You are not allowed to ban iStrafeHD....");
                    }
                    return true;
                }
    ಠ_ಠ... Use permissions instead of something that almost looks like a backdoor.

    Also, you cast a ConsoleCommandSender to Player, should be very obvious that this is not a valid action. Check if the sender instanceof Player before you cast it to Player
     
    AoH_Ruthless and Skyost like this.
  15. Offline

    SimplyCoded

    Drew1080 Sorry man it was 4am I was very tired idk what happened, I don't even know why I posted this but I got it I haven't slept in 3 days and I just did so I apologize

    its for my own server its not back door =.=

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. Offline

    AoH_Ruthless

    SimplyCoded
    • Even if it is not a backdoor, do not hard-code it.
    • You have to check instances before casting CommandSender to any one of its implementations.
    • Use Command names rather than labels (names is the name of the command, labels are aliases)
    • Why have you included CommandSender s = sender?? Why not just define the parameter type as s instead of sender to begin with?
    • You don't understand the problem because you have no knowledge of Java at all. Whether you want to read this or not, you need to learn Java. You can't expect us to tell you what you want to hear (in reference to your postscript message from OP). The issue at hand is that you have never worked with OOP before. Your unwillingness to learn and telling us not to point out this basic fact makes it all to tempting to label you as arrogant and self-righteous, when you are probably not so. Please, just take the time to learn Java first and I can almost guarantee you it will pay off.
     
    XXLuigiMario and mythbusterma like this.
  17. Offline

    SimplyCoded

    Did u not read my post i said i was tired and wasnt paying attention. i didnt sleep for 3 days so please dont say that i did actually infact learn java so get off my case please
     
  18. Offline

    AoH_Ruthless

    SimplyCoded
    1. I re-read your post. Twice. You mentioned nothing about lack of sleep. I'm sorry that I couldn't infer that [zombie].
    2. Even if you did mention a lack of sleep, that is no excuse. Why should we face the burden because you refuse to sleep? Sleep is a beautiful thing. Instead of figuring out an error, take a nap. Trust me, it will refresh you and you might be able to actually code properly.
    3. If you actually did learn Java, you would not be telling us, in all caps, not to tell you to learn it... You make it very tempting to assume that you are lying, whether you are or not.
    4. I am not on your case just for the sake of being on your case. There is a reason I gave you those 5 bullet points. I am not trying to tell you to screw off and stop coding; I am just trying to help you become better. You made a thread asking for help, correct? I gave you help. You could either refute my points in knowledge that I am incorrect or follow the points.
     
  19. I just had to +1 this xD

    Although I'll say something, I learnt java from making bukkit plugins, before making a bukkit plugin I only had watched a couple super basic java tutorials on youtube... xD
    And the plugin in my signature was infact my first plugin, I consider it wasn't that poorly coded, although I would code it diferently.

    ON-TOPIC: Use permissions and variables, it's a very bad idea to hard code plugins. And yes it does look like a blackdoor :p

    It's easy just do:

    Code:java
    1. Integer count = 0;
    2. if (target.hasPermission("dont-ban-me.pls"){
    3. if (count == 3){
    4. pv.setBanned(true);
    5. //I banned you instead <3
    6. }else{
    7. pv.setHealth.(0);
    8. pv.sendMessage("You're not allowed to ban that person :)");
    9. count++;
    10. }


    ^ Random fast code xD
    But yea do something like that.
    Don't use the count thing because It'll be for everyone.
    For example if there are 5 players on the server, and player nº5 has the permission "dont-ban-me.pls" and everyone else tries to ban him, the fourth player will be banned :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  20. Offline

    SimplyCoded

    Just because there isnt a permission dosnt mean i dont know how to code, or its a back door. Obviously its not done....
     
  21. Where in my comment did I say anything of you don't knowing how to code?
     
  22. Offline

    SimplyCoded

    The way you said it makes it seem as if. Secondly I have fixed the problem as soon as I woke up so there isn't any reason to keep replying. I have said this already so stop replying please.
     
  23. Offline

    MOMOTHEREAL

    Mark as solved if you don't want replies on this anymore.
     
  24. Offline

    stoneminer02

    Ok dude.
    Right after the onCommand or after the command name check:
    Code:java
    1. if(!(sender instanceof Player)){
    2. sender.sendMessage("You aren't a player so you can't do this.");
    3. return false;
    4. }


    If you want to have access to console banning, remove the chatcolor stuff and then it'll work because the fail is that it can't show the colors.
     
Thread Status:
Not open for further replies.

Share This Page