Checking if the block a player is looking at is something

Discussion in 'Plugin Development' started by yewtree8, Apr 22, 2014.

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

    yewtree8

    So i want to make it so on the command it checks if the block is something, but I can't seem to find out how to do it? can anyone help me?

    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String name, String[] args) {
    2. Player player = (Player) sender; {
    3. if(command.getName().equalsIgnoreCase("mb")) {
    4. if(args.length == 0) {
    5. player.sendMessage(Prefix + ChatColor.BLUE + "Commands:");
    6. return true;
    7.  
    8. } else if(args.length == 1) {
    9. if(args[0].equalsIgnoreCase("create")) {
    10.  
    11. }
    12.  
    13. }
    14.  
    15.  
    16. }
    17.  
    18.  
    19. }
    20.  
    21. return true;
    22. }
    23.  



    what "if" statement would I use?
     
  2. Offline

    yewtree8

    AdamQpzm I have seen that ¬_¬ but it doesn't seem to work?

    Please could you just tell me how I would do what i want to do?
     
  3. yewtree8 Well, what didn't work? How did you try and use it? I'm not going to just tell you how to do it.
     
  4. Offline

    yewtree8

    AdamQpzm

    It keeps on coming up as depricated with the error that it doesn't compute with the type living entity
     
  5. yewtree8 Deprecation is a warning, it should still work. Also post the code with your attempt to use it please.
     
  6. Offline

    yewtree8

    AdamQpzm

    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String name, String[] args) {
    2. Player player = (Player) sender; {
    3. if(command.getName().equalsIgnoreCase("mb")) {
    4. if(args.length == 0) {
    5. player.sendMessage(Prefix + ChatColor.BLUE + "Commands:");
    6. return true;
    7.  
    8. } else if(args.length == 1) {
    9. if(args[0].equalsIgnoreCase("create")) {
    10. if(player.getTargetBlock(34, 100)) { // IT HAS A RED LINE UNDERNEATH
    11.  
    12.  
    13.  
    14. }
    15. }
    16.  
    17. }
    18.  
    19.  
    20. }
    21.  
    22.  
    23. }
    24.  
    25. return true;
    26. }
    27.  
    28.  
    29.  
    30.  
    31. }
    32.  
     
  7. Offline

    BlazingBroGamer

    Mabe you could get the target block location and use
    Code:
    player.getWorld().getBlockAt(x,y,z)
     
  8. Offline

    yewtree8

  9. Offline

    BlazingBroGamer

    yewtree8
    Nevermind... I found an easier way.
    Code:
    Player p = (Player) sender;
    p.getTargetBlock(null,100).setType(Material.WOOL);
    
    I used wool for example purposes, and usually when I do null for the target block, I get it to work. Hope it works!
     
  10. Offline

    yewtree8

    BlazingBroGamer

    It says that you can't convert void to boolean

    BlazingBroGamer

    I want to check IF the block is something

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

    BlazingBroGamer

    yewtree8
    Well I think I didn't read the question right. I think what your looking for is this:
    Code:
    Player p = (Player) sender;
                    if(p.getTargetBlock(null, 100).getType() == Material.WOOL && p.getTargetBlock(null, 100).getType  != null){
                        //Code here!
                    }
    
    Again, I used wool for example purposes. I think it is important for a null check, since there is a high chance it might be null.

    yewtree8
    Oh and also, you can check if the block is air or not, then return a boolean. So it might go something like this:
    Code:
    public boolean blockCheck(Player p){
    if(p.getTargetBlock(null,100).getType != Material.AIR && p.getTargetBlock(null,100).getType != null){
    return true;
    }else{
    return false;
    
    That should work I think

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

    yewtree8

  13. Offline

    BlazingBroGamer

    yewtree8
    Welcome, and also remember to mark the problem as solved!
     
  14. BlazingBroGamer I can't even begin to imagine why you check whether it's a wool block and then check whether it's a null type.

    yewtree8 Was 34 meant to mean anything?
     
  15. Offline

    BlazingBroGamer

    AdamQpzm
    Hmm... True... But the question is to check if a player is looking at a block, and I guess I didn't read the question properly, therefore I used a wool block, as an example. My last code is working, I think, according to yewtree8
     
  16. Offline

    yewtree8

    BlazingBroGamer


    By any chance would you know how I could add this target block to an arraylist?
     
  17. Offline

    BlazingBroGamer

    yewtree8
    For a block arraylist, it would be like this:
    Code:
    List<Block> block = new ArrayList<Block>();
    Block b = p.getTargetBlock(null, 100).getType();
    block.add(b);
    
     
  18. Offline

    BlazingBroGamer

    AdamQpzm
    Right.... Forgot to remove the type.. I got so used to using Materials... Well for block, you don't need the getType(), but if you want a material, u getType()
     
Thread Status:
Not open for further replies.

Share This Page