How do i place a torch on a side of a block?

Discussion in 'Plugin Development' started by Danteland, Jan 6, 2015.

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

    Danteland

    Hi, basicly what im trying to do is to place a torch on a side of a block. Do you guys know how i can see if there is a solid block relative to a location and if there is it will attach a torch to that block?
     
  2. 1. block.getRelative(BlockFace.NORTH); (for north, east, south, west)
    2. check if block is material.torch
    Show Spoiler

    Code:
    if (block.getRelative(BlockFace.NORTH).getType() == Material.TORCH) {
     
  3. Offline

    sirrus86

    Taking a quick stab at this...

    Code:
    public void placeTorch(Location loc) { //Attempt to place a torch on any side available
        if (loc.getBlock().getType() == Material.AIR) { //Make sure this is an empty block first
            for (BlockFace face : BlockFace.values()) {
                switch(face) {
                    case EAST: case NORTH: case SOUTH: case WEST: {
                        if (loc.getBlock().getRelative(face).getType().isOccluding()
                                && loc.getBlock().getRelative(face).getType().isSolid()) { //Make sure this side supports a torch
                            placeTorch(loc, face);
                            return; //Attachable side found, no need to keep looping
                        }
                        else continue;
                    }
                    default: continue;
                }
            }
            placeTorch(loc, BlockFace.DOWN); //No attachable sides found, place it on the ground
        }
    }
    
    public void placeTorch(Location loc, BlockFace face) { //Place a torch on a specified side
        if (loc.getBlock().getType() == Material.AIR) { //Make sure this is an empty block first
            if (loc.getBlock().getRelative(face).getType().isOccluding()
                    && loc.getBlock().getRelative(face).getType().isSolid()) {
                loc.getBlock().setType(Material.TORCH);
                Torch torch = (Torch) loc.getBlock().getState().getData();
                torch.setFacingDirection(face);
            }
        }
    }
    Haven't tested it, but looks like it'd work.
     
  4. Offline

    Danteland

    @FisheyLP Thanks but i think you misunderstood the question

    @sirrus86 Omg thanks thats exactly what i was looking for

    edit: I cant cast a Torch object to data since data is of typ Byte
    *Edit* I tried almost what you did with the code but it didn't work. this is what I did
    Code:
           Arrow ar = (Arrow) event.getEntity();
           Location loc = ar.getLocation().clone();
           loc.getBlock().setType(Material.TORCH);
           Torch torch = (Torch) loc.getBlock().getState().getData();
           if(loc.getBlock().getRelative(BlockFace.DOWN).getType().isSolid())
               torch.setFacingDirection(BlockFace.DOWN);
           else if(loc.getBlock().getRelative(BlockFace.NORTH).getType().isSolid())
               torch.setFacingDirection(BlockFace.NORTH);
           else if(loc.getBlock().getRelative(BlockFace.EAST).getType().isSolid())
               torch.setFacingDirection(BlockFace.EAST);
           else if(loc.getBlock().getRelative(BlockFace.WEST).getType().isSolid())
               torch.setFacingDirection(BlockFace.WEST);
           else
               torch.setFacingDirection(BlockFace.SOUTH);
           loc.getBlock().getState().setData(torch);
    
    It can place the torch on the ground but not on the walls
     
    Last edited: Jan 6, 2015
Thread Status:
Not open for further replies.

Share This Page