Registering plugin block changes as commandsender place events

Discussion in 'Plugin Development' started by BadReuben, May 18, 2013.

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

    BadReuben

    I am looking for a way to treat block changes that result from a onCommand Boolean as Player block place events. I believe this would be a good method for ensuring plugin compatibility while staying within the Bukkit API (such as compatibility with block logging plugins and protection plugins) and to ensure that the command's sender has permission to build where the blocks end up being placed.

    Example of current code:
    Code:
    player.getWorld().getBlockAt(x, y, z).setType(Material.STONE);
     
  2. Offline

    Milkywayz

    Code:
            Player p = (Player)sender;
            Block b = p.getLocation().add(0, 3, 0).getBlock();
            BlockPlaceEvent bpe = new BlockPlaceEvent(b, b.getState(), b.getRelative(BlockFace.DOWN), p.getItemInHand(), p, true);
            if(!bpe.isCancelled()) {
                b.setTypeId(20);
            }
    
    Just a crude example. Always remember type casting safety as the commandsender is not always a player. Also the event arguments aren't tested.

    This lets a player place a glass above them with a command while respecting stuff like regions or factions and what not.

    You can get the players line of sight and calculate where to place the block, but I won't get into that as I'm about to go to sleep.
     
    BlakkDock likes this.
  3. Offline

    BadReuben

    Thanks! This helped a lot in my AFK Boxing plugin. I am using the method below. Does anyone have suggestions for making this method loggable by block logging plugins, such as CoreProtect?

    Code:
    private boolean SetToStone(Player p, Block blk){
            BlockPlaceEvent bpe = new BlockPlaceEvent(blk, blk.getState(), blk.getRelative(BlockFace.DOWN), p.getItemInHand(), p, true);
            Bukkit.getPluginManager().callEvent(bpe);
            if(bpe.isCancelled()) {
                p.sendMessage(ChatColor.RED + "AFKBoxing: Insufficient build Permissions in that area.");
                return false;
            } else {
                blk.setTypeId(1);
                return true;
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page