1.12.2 Set block relative to placed block

Discussion in 'Spigot Plugin Development' started by CyricYT, Apr 4, 2020.

  1. im making a plugin and i need to make it when the player places the beacon block it turns it into a lvl 1 beacon (1 lvl of iron underneath) how do i do this?

    Here is my code for the listener:
    Code (Java):
    public class BeaconListener implements Listener {

        private Main plugin;

        private static int BlockX = 0;
        private static int BlockY = 0;
        private static int BlockZ = 0;

        public BeaconListener(Main plugin){
            this.plugin = plugin;

            Bukkit.getPluginManager().registerEvents(this, plugin);
        }

        @EventHandler
        public void bPlaced(BlockPlaceEvent e){
            Bukkit.getConsoleSender().sendMessage("Block placed");
            Block block = e.getBlock();
            Player p = e.getPlayer();

            if(block.getType().equals(Material.BEACON)){
                p.sendMessage("you placed a beacon");
            }

            if(block.getLocation().subtract(0,1,0).getBlock().getType().equals(Material.DIRT)){
                p.sendMessage("hi");
            }
        }
    }
     
  2. Look into block.getRelativeBlock(BlockFace.DOWN).setType();
     
  3. Code (Java):
    @EventHandler
        public void bPlaced(BlockPlaceEvent e){
            Bukkit.getConsoleSender().sendMessage("Block placed");
            Block block = e.getBlock();
            Player p = e.getPlayer();

            if(block.getType().equals(Material.BEACON)){
                p.sendMessage("you placed a beacon");
            }


            Location blockLocation = block.getLocation();
            World world = blockLocation.getWorld();
            int x = blockLocation.getBlockX() - 1;
            int y = blockLocation.getBlockY() - 1;
            int z = blockLocation.getBlockZ() - 1;

            if(world.getBlockAt(x+1, y, z+1).getType().equals(Material.DIRT)){ // Check if beacon placed on grass block
                for (int dx = 0; dx <= 2; dx++) {
                    for (int dz = 0; dz <= 2; dz++) {
                        world.getBlockAt(x+dx, y, z+dz).setType(Material.IRON_BLOCK);
                    }
                }
            }
        }
    This should roughly do what you want. The idea is that you want to somehow loop through all the blocks you want to update.
    The concept of nested loops is quite a common occurrence in programming so if this looks completely new to you it would be a good idea to try and read more into it.
     
  4. If i'm reading this correctly, this should work for you :p
    Code (Java):
        @EventHandler
        public void onPlace(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            Block block = event.getBlock();

            if(block.getType() == Material.BEACON) {
                player.sendMessage("You placed a beacon.");
            }
            if(block.getRelative(BlockFace.DOWN).getType() == Material.DIRT) {
                block.getRelative(BlockFace.DOWN).setType(Material.BEACON);
                player.sendMessage("hi");
            }
        }
    }