Blocks changing into farmland even when in an if statement

Discussion in 'Plugin Development' started by Ballistic3051, Aug 21, 2021.

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

    Ballistic3051

    Hey!

    I'm new to making plugins and I'm trying to make it so when I break a block an area of 2x2 will be farmland. But when I do it, it still changes blocks that aren't dirt into farmland, such as air and cobblestone.

    This is my code for this specific problem.

    Code:
    if (blockBroken.getType() == Material.DIRT) {
                    e.setCancelled(true);
    
                    blockBroken.setType(Material.FARMLAND);
    
                    int coordX = e.getBlock().getLocation().getBlockX();
                    int coordY = e.getBlock().getLocation().getBlockY();
                    int coordZ = e.getBlock().getLocation().getBlockZ();
                    if (e.getPlayer().getWorld().getBlockAt(coordX + 1, coordY, coordZ).equals(Material.DIRT)) {
    
                        e.getPlayer().getWorld().getBlockAt(coordX + 1, coordY, coordZ).setType(Material.FARMLAND);
    
                    }
                    if (e.getPlayer().getWorld().getBlockAt(coordX + 1, coordY, coordZ + 1).equals(Material.DIRT)) {
    
    
    
                        e.getPlayer().getWorld().getBlockAt(coordX + 1, coordY, coordZ + 1).setType(Material.FARMLAND);
    
                    }
                    if (e.getPlayer().getWorld().getBlockAt(coordX, coordY, coordZ + 1).equals(Material.DIRT)) {
    
                        e.getPlayer().getWorld().getBlockAt(coordX, coordY, coordZ + 1).setType(Material.FARMLAND);
    
                    }
     
    Last edited by a moderator: Aug 21, 2021
  2. Offline

    c7dev

    After you get the block, it should have a getType() after it.
    e.getPlayer().getWorld().getBlockAt(coordX + 1, coordY, coordZ + 1).getType().equals(Material.DIRT)
     
  3. Offline

    ZweiGeschleim

    try this:

    Code:
    if(blockBroken.getType() == Material.DIRT) {
        e.setCancelled(true);
       
        Block b1 = blockBroken.getRelative(1, 0, 0);
        Block b2 = blockBroken.getRelative(1, 0, 1);
        Block b3 = blockBroken.getRelative(0, 0, 1);
    
        if(b1.getType==Material.DIRT){bl.setType(Material.FARMLAND);}
        if(b2.getType==Material.DIRT){b2.setType(Material.FARMLAND);}
        if(b3.getType==Material.DIRT){b3.setType(Material.FARMLAND);}
    
    }
    if you need objects several times then create variables.

    The way you coded it is much more inefficient, as the functions have to be carried out each time so that you can get your object.
     
Thread Status:
Not open for further replies.

Share This Page