Solved Checking Block Data

Discussion in 'Spigot Plugin Development' started by Harieo, Apr 20, 2017.

  1. Hi,

    I am trying to create a method which checks whether the block under where another block is placed is a certain material. However, some of the materials I want to compare don't have enums and I don't know how to check for them.

    This is what I tried:
    Code (Text):
    if (location.subtract(0, 1, 0).getBlock() != null) {

                    if (location.subtract(0, 1, 0).getBlock().getType() == Material.SMOOTH_BRICK
                            && location.subtract(0, 1, 0).getBlock().getData() == 3) {

                        Bukkit.broadcastMessage("check mark 1");
                        return true;

                    } else {

                        Bukkit.broadcastMessage("check mark 2");
                        return false;

                    }

                } else {

                    Bukkit.broadcastMessage("check mark 3");
                    return false;

                }

            }
    However, it returned check mark 2 (bug fix code) and false.

    Anyone know how to do this?

    Many thanks,
    Harieo

    EDIT: Sorry for the syntax formatting, the forum changed it like that.
     
  2. location.subtract(0, 1, 0).getBlock().getData() == 3 ?? Try to Broadcast the getBlock().getDate()
     
  3. The data value is a byte value, I tried casting the 3 as a byte but it didn't work.
     
  4. You try the broadcast ?
     
  5. The broadcast requires a String, I don't have a String to broadcast?
     
  6. Then just cast your object to a string
    Otherwise you could also just use String.valueOf(object);
     
  7. I was not aware of the method, thank you. However, this has just made things more confusing.

    The output:
    Code (Text):
    [18:28:20] [Server thread/INFO]: 0
    [18:28:20] [Server thread/INFO]: check mark 2[m
    [18:28:23] [Server thread/INFO]: 15
    [18:28:23] [Server thread/INFO]: check mark 2[m
    [18:28:28] [Server thread/INFO]: 6
    [18:28:28] [Server thread/INFO]: check mark 2[m
    [18:28:36] [Server thread/INFO]: 0
    [18:28:36] [Server thread/INFO]: check mark 2[m
    [18:28:38] [Server thread/INFO]: 15
    [18:28:38] [Server thread/INFO]: check mark 2[m
    [18:28:42] [Server thread/INFO]: 5
    [18:28:42] [Server thread/INFO]: check mark 2[m
    The String value of the data seems to be completely random o_O
     
  8. Just wondering, what do you now broadcast as output? the block.getData() value? When yes, check mark 2 is active so i see that only check mark 1 does what you want to have so maybe broadcast also the block type as material.<TYPE>.name() so maybe its something like a flower because that could have different id values
     
  9. I am the most confused person in this entire room right now. Besides from Spigot assuming subtract 1 means subtract nothing, then changing it's mind half way through without any changes to the code, it spits out Material.SMOOTH_BRICK for Chiseled Stone Brick
     
  10. lol xd is it possible to post the new outputs, so i can understand what you ment there?
     
  11. Code (Text):

    [18:45:50] [Server thread/INFO]: Y: 2[m
    [18:45:50] [Server thread/INFO]: OLD Y: 2[m
    [18:45:50] [Server thread/INFO]: check mark 2[m
    [18:45:51] [Server thread/INFO]: STAINED_GLASS[m
    [18:45:51] [Server thread/INFO]: Y: 2[m
    [18:45:51] [Server thread/INFO]: OLD Y: 2[m
    [18:45:51] [Server thread/INFO]: check mark 2[m
    [18:45:53] [Server thread/INFO]: STONE[m
    [18:45:53] [Server thread/INFO]: Y: 1[m
    [18:45:53] [Server thread/INFO]: OLD Y: 1[m
    [18:45:53] [Server thread/INFO]: check mark 2[m
    [18:49:44] [Server thread/INFO]: DOUBLE_STEP[m
    [18:49:44] [Server thread/INFO]: X: 1413[m
    [18:49:44] [Server thread/INFO]: Y: 3[m
    [18:49:44] [Server thread/INFO]: Z: -513[m
    [18:49:44] [Server thread/INFO]: OLD X: 1413[m
    [18:49:44] [Server thread/INFO]: OLD Y: 2[m
    [18:49:44] [Server thread/INFO]: OLD Z: -513[m
    [18:49:44] [Server thread/INFO]: check mark 2[m
    [18:49:46] [Server thread/INFO]: DOUBLE_STEP[m
    [18:49:46] [Server thread/INFO]: X: 1413[m
    [18:49:46] [Server thread/INFO]: Y: 3[m
    [18:49:46] [Server thread/INFO]: Z: -513[m
    [18:49:46] [Server thread/INFO]: OLD X: 1413[m
    [18:49:46] [Server thread/INFO]: OLD Y: 2[m
    [18:49:46] [Server thread/INFO]: OLD Z: -513[m
    [18:49:46] [Server thread/INFO]: check mark 2[m
    You can see I stopped for a few minutes to cry, then I tried it on a different section of the server.
     
  12. ohhh haha i know why it prints those different values, add after "location" this: ".clone()" that should solve your issue
     
  13. The location isn't my issue, or at least it wasn't one of them until now, the problem is I don't know how to check whether a block is a sub-material of a material. Like the main material is stone brick (98) and the sub-material for chiselled stone brick is (98:3)

    I've tested the method on just stone brick itself and it works.
     
  14. I saw that you tryed something like this already but maybe check this with the cast of the byte class

    Code (Text):

    Block block = yourBlock;

    if(block.getType() == Material.SMOOTH_BRICK){
        if(block.getData() == (byte)3){
            //Do your stuff
        }
    }

     
    Edit:


    Code (Text):


    if (location.clone().subtract(0, 1, 0).getBlock().getType() == Material.SMOOTH_BRICK
                            && location.clone().subtract(0, 1, 0).getBlock().getData() == (byte)3) {
     //Do your stuff
    }

     
     
    • Winner Winner x 1
  15. It works! Cloning the location has solved the problem. Thank you :D