Check if TNTPrimed is touching block

Discussion in 'Plugin Development' started by Zenya4, Jun 6, 2017.

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

    Zenya4

    Hi all,

    I'm making this command which spawns tnt that explodes when it comes to contact with any block other than air. Here is my current code.
    Code:
    
    TNTPrimed tntprimed = player.getWorld().spawn(player.getEyeLocation(), TNTPrimed.class);
    
    tntprimed.setVelocity(player.getLocation().getDirection().multiply(1.5)); //CONFIG
    
    tntprimed.setFuseTicks(100);
    
    tntprimed.setIsIncendiary(true); //CONFIG
    
    int x = tntprimed.getLocation().getBlockX();
    
    int y = tntprimed.getLocation().getBlockY();
    
    int z = tntprimed.getLocation().getBlockZ();
    
    Block blockxplus1 = tntprimed.getWorld().getBlockAt(x + 1, y, z);
    
    Block blockxminus1 = tntprimed.getWorld().getBlockAt(x - 1, y , z);
    
    Block blockyplus1 = tntprimed.getWorld().getBlockAt(x, y + 1, z);
    
    Block blockyminus1 = tntprimed.getWorld().getBlockAt(x, y - 1, z);
    
    Block blockzplus1 = tntprimed.getWorld().getBlockAt(x, y , z + 1);
    
    Block blockzminus1 = tntprimed.getWorld().getBlockAt(x, y , z - 1);
    
    if(blockxplus1.getType() != Material.AIR || blockxminus1.getType() != Material.AIR || blockyplus1.getType() != Material.AIR || blockyminus1.getType() != Material.AIR || blockzplus1.getType() != Material.AIR || blockzminus1.getType() != Material.AIR) {
    
    tntprimed.setFuseTicks(0);
    
    I know its not very efficient, but it doesnt work either. I think this is so as it checks the block instantly when the tnt spawns and not when it touches the ground. I've tried using
    Code:
    tntprimed.isOnGround == true;
    but that doesnt work either. So any help woule be appreciated. Thanks!
     
  2. Offline

    Zombie_Striker

    @Zenya4

    Are you sure all of those blocks are air? Print out the values for all those blocks.
     
  3. Offline

    Zenya4

    @Zombie_Striker

    I returned the value of blockxplus1.getType(); and its air. Same for the other blocks.
     
  4. Offline

    Zombie_Striker

    @Zenya4
    Try adding this line somewhere in the code and post what it prints out:
    Code:
    player.sendMessage("+X "+
    blockxplus1.getType() != Material.AIR +", -X "+ blockxminus1.getType() != Material.AIR +", +Y"+ blockyplus1.getType() != Material.AIR+", -Y"+ blockyminus1.getType() != Material.AIR +", +Z"+ blockzplus1.getType() != Material.AIR+", -Z "+ blockzminus1.getType() != Material.AIR);
    What this does is prints out all the booleans for that if statement. If they really are all false, that the problem is not with your if statement but with another section of your plugin/ another plugin entirely.
     
  5. Offline

    Zenya4

    It gives an error "Incompatible operand types boolean and String". (In eclipse)

    What's that supposed to mean?
     
  6. Offline

    Zombie_Striker

    @Zenya4
    Forgot to encapsulate the booleans. Use this.
    Code:
    player.sendMessage("+X "+
    (blockxplus1.getType() != Material.AIR) +", -X "+ (blockxminus1.getType() != Material.AIR )+", +Y"+ (blockyplus1.getType() != Material.AIR)+", -Y"+ (blockyminus1.getType() != Material.AIR )+", +Z"+ (blockzplus1.getType() != Material.AIR)+", -Z "+ (blockzminus1.getType() != Material.AIR));
     
  7. Offline

    Zenya4

    Ok thanks. I'm outside right now but I'll test it when I get home and edit in the result here


    EDIT: @Zombie_Striker

    All false. Buttt, when I enclose myself in a 2x1 air space with blocks all around, the TNT explodes with +Y being true. And, when I face a wall and summon the TNT, it also explodes with one of the coords returning true.

    bump

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

    FlorianIsStoer

    You can check when it is on ground by: if(Block.isOnGround);
     
Thread Status:
Not open for further replies.

Share This Page