Java Code Examples for org.bukkit.event.block.BlockPlaceEvent#getBlockPlaced()

The following examples show how to use org.bukkit.event.block.BlockPlaceEvent#getBlockPlaced() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    if (!LWC.ENABLED) {
        return;
    }

    LWC lwc = plugin.getLWC();
    Player player = event.getPlayer();
    Block block = event.getBlockPlaced();

    ProtectionCache cache = lwc.getProtectionCache();
    String cacheKey = cache.cacheKey(block.getLocation());

    // In the event they place a block, remove any known nulls there
    if (cache.isKnownNull(cacheKey)) {
        cache.remove(cacheKey);
    }

    // check if the block is blacklisted
    if (blacklistedBlocks.contains(block.getType())) {
        // it's blacklisted, check for a protected chest
        for (Protection protection : lwc.findAdjacentProtectionsOnAllSides(block)) {
            if (protection != null) {
                if (!lwc.canAccessProtection(player, protection)
                        || (protection.getType() == Protection.Type.DONATION
                        && !lwc.canAdminProtection(player, protection))) {
                    // they can't access the protection ..
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
}
 
Example 2
Source File: BlockListener.java    From ViaVersion with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void placeBlock(BlockPlaceEvent e) {
    if (isOnPipe(e.getPlayer())) {
        Block b = e.getBlockPlaced();
        getUserConnection(e.getPlayer())
                .get(EntityTracker1_9.class)
                .addBlockInteraction(new Position(b.getX(), (short) b.getY(), b.getZ()));
    }
}
 
Example 3
Source File: ShopItemListener.java    From ShopChest with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onBlockPlace(BlockPlaceEvent e) {
    Block b = e.getBlockPlaced();
    Block below = b.getRelative(BlockFace.DOWN);

    if (shopUtils.isShop(below.getLocation())) {
        Shop shop = shopUtils.getShop(below.getLocation());
        if (shop.getItem() != null) {
            shop.getItem().resetForPlayer(e.getPlayer());
        }
        e.setCancelled(true);
    }
}
 
Example 4
Source File: Tower.java    From AACAdditionPro with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event)
{
    final User user = UserManager.getUser(event.getPlayer().getUniqueId());

    // Not bypassed
    if (User.isUserInvalid(user, this.getModuleType())) {
        return;
    }

    // To prevent too fast towering -> Timeout
    if (user.getTimestampMap().recentlyUpdated(TimestampKey.TOWER_TIMEOUT, timeout)) {
        event.setCancelled(true);
        InventoryUtils.syncUpdateInventory(user.getPlayer());
        return;
    }

    // Not flying
    if (!user.getPlayer().isFlying()) {
        final Block blockPlaced = event.getBlockPlaced();

        // Levitation effect
        final Integer levitation;
        switch (ServerVersion.getActiveServerVersion()) {
            case MC188:
                levitation = null;
                break;
            case MC112:
            case MC113:
            case MC114:
            case MC115:
                levitation = PotionUtil.getAmplifier(PotionUtil.getPotionEffect(user.getPlayer(), PotionEffectType.LEVITATION));
                break;
            default:
                throw new UnknownMinecraftVersion();
        }

        // User must stand above the block (placed from above)1
        // Check if the block is tower-placed (Block belows)
        if (event.getBlock().getFace(event.getBlockAgainst()) == BlockFace.DOWN &&
            // The block is placed inside a 2 - block y-radius, this prevents false positives when building from a higher level
            user.getPlayer().getLocation().getY() - blockPlaced.getY() < 2D &&
            // Check if this check applies to the block
            blockPlaced.getType().isSolid() &&
            // Check if the block is placed against one block (face) only
            // Only one block that is not a liquid is allowed (the one which the Block is placed against).
            BlockUtils.getBlocksAround(blockPlaced, false).stream().filter(block -> !BlockUtils.LIQUIDS.contains(block.getType())).count() == 1 &&
            // User is not in water which can cause false positives due to faster swimming on newer versions.
            !EntityUtil.isHitboxInLiquids(user.getPlayer().getLocation(), user.getHitbox()) &&
            // Buffer the block place, continue the check only when we a certain number of block places in check
            user.getTowerData().getBlockPlaces().bufferObject(
                    new TowerBlockPlace(
                            blockPlaced,
                            //Jump boost effect is important
                            PotionUtil.getAmplifier(PotionUtil.getPotionEffect(user.getPlayer(), PotionEffectType.JUMP)),
                            levitation)))
        {
            // [0] = Expected time; [1] = Real time
            final double[] results = user.getTowerData().calculateTimes();

            // Real check
            if (results[1] < results[0]) {
                final int vlToAdd = (int) Math.min(1 + Math.floor((results[0] - results[1]) / 16), 100);

                // Violation-Level handling
                vlManager.flag(event.getPlayer(), vlToAdd, cancelVl, () ->
                {
                    event.setCancelled(true);
                    user.getTimestampMap().updateTimeStamp(TimestampKey.TOWER_TIMEOUT);
                    InventoryUtils.syncUpdateInventory(user.getPlayer());
                    // If not cancelled run the verbose message with additional data
                }, () -> VerboseSender.getInstance().sendVerboseMessage("Tower-Verbose | Player: " + user.getPlayer().getName() + " expected time: " + results[0] + " | real: " + results[1]));
            }
        }
    }
}
 
Example 5
Source File: Scaffold.java    From AACAdditionPro with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event)
{
    final User user = UserManager.getUser(event.getPlayer().getUniqueId());

    // Not bypassed
    if (User.isUserInvalid(user, this.getModuleType())) {
        return;
    }

    final Block blockPlaced = event.getBlockPlaced();

    // Short distance between player and the block (at most 2 Blocks)
    if (LocationUtils.areLocationsInRange(user.getPlayer().getLocation(), blockPlaced.getLocation(), 4D) &&
        // Not flying
        !user.getPlayer().isFlying() &&
        // Above the block
        user.getPlayer().getLocation().getY() > blockPlaced.getY() &&
        // Check if this check applies to the block
        blockPlaced.getType().isSolid() &&
        // Check if the block is placed against one block face only, also implies no blocks above and below.
        // Only one block that is not a liquid is allowed (the one which the Block is placed against).
        BlockUtils.getBlocksAround(blockPlaced, false).stream().filter(block -> !BlockUtils.LIQUIDS.contains(block.getType())).count() == 1 &&
        // In between check to make sure it is somewhat a scaffold movement as the buffering does not work.
        BlockUtils.HORIZONTAL_FACES.contains(event.getBlock().getFace(event.getBlockAgainst())))
    {
        int vl = anglePattern.apply(user, event);
        vl += averagePattern.apply(user, event);
        vl += positionPattern.apply(user, event);

        // --------------------------------------------- Rotations ---------------------------------------------- //

        final float[] angleInformation = user.getLookPacketData().getAngleInformation();

        int rotationVl = rotationTypeOne.apply(user, event) +
                         rotationTypeTwo.apply(user, angleInformation[0]) +
                         rotationTypeThree.apply(user, angleInformation[1]);

        if (rotationVl > 0) {
            if (++user.getScaffoldData().rotationFails >= this.rotationThreshold) {
                // Flag the player
                vl += rotationVl;
            }
        } else if (user.getScaffoldData().rotationFails > 0) {
            user.getScaffoldData().rotationFails--;
        }

        vl += sprintingPattern.apply(user, event);
        vl += safewalkTypeOne.apply(user, event);
        vl += safewalkTypeTwo.apply(user, event);

        if (vl > 0) {
            vlManager.flag(event.getPlayer(), vl, cancelVl, () ->
            {
                event.setCancelled(true);
                user.getTimestampMap().updateTimeStamp(TimestampKey.SCAFFOLD_TIMEOUT);
                InventoryUtils.syncUpdateInventory(user.getPlayer());
            }, () -> {});
        }
    }
}
 
Example 6
Source File: BlocksPlacedListener.java    From Statz with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent event) {

    final PlayerStat stat = PlayerStat.BLOCKS_PLACED;

    // Get player
    final Player player = event.getPlayer();

    // Do general check
    if (!plugin.doGeneralCheck(player, stat))
        return;

    Block blockPlaced = event.getBlockPlaced();
    final String worldName = blockPlaced.getWorld().getName();

    PlayerStatSpecification specification = new BlocksPlacedSpecification(player.getUniqueId(), 1,
            worldName, blockPlaced.getType());

    // Update value to new stat.
    plugin.getDataManager().setPlayerInfo(player.getUniqueId(), stat, specification.constructQuery());

}
 
Example 7
Source File: BlockListener.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void onPlace(BlockPlaceEvent bpe) {
  Player player = bpe.getPlayer();
  Game game = BedwarsRel.getInstance().getGameManager().getGameOfPlayer(player);

  if (game == null) {
    return;
  }

  if (game.getState() == GameState.STOPPED) {
    return;
  }

  if (game.getState() == GameState.WAITING) {
    bpe.setCancelled(true);
    bpe.setBuild(false);
    return;
  }

  if (game.getState() == GameState.RUNNING) {
    if (game.isSpectator(player)) {
      bpe.setCancelled(true);
      bpe.setBuild(false);
      return;
    }

    Block placeBlock = bpe.getBlockPlaced();
    BlockState replacedBlock = bpe.getBlockReplacedState();

    if (placeBlock.getType() == game.getTargetMaterial()) {
      bpe.setCancelled(true);
      bpe.setBuild(false);
      return;
    }

    if (!game.getRegion().isInRegion(placeBlock.getLocation())) {
      bpe.setCancelled(true);
      bpe.setBuild(false);
      return;
    }

    if (replacedBlock != null && !BedwarsRel
        .getInstance().getBooleanConfig("place-in-liquid", true)
        && (replacedBlock.getType().equals(Material.WATER)
        || replacedBlock.getType().equals(Material.STATIONARY_WATER)
        || replacedBlock.getType().equals(Material.LAVA)
        || replacedBlock.getType().equals(Material.STATIONARY_LAVA))) {
      bpe.setCancelled(true);
      bpe.setBuild(false);
      return;
    }

    if (replacedBlock != null && placeBlock.getType().equals(Material.WEB)
        && (replacedBlock.getType().equals(Material.WATER)
        || replacedBlock.getType().equals(Material.STATIONARY_WATER)
        || replacedBlock.getType().equals(Material.LAVA)
        || replacedBlock.getType().equals(Material.STATIONARY_LAVA))) {
      bpe.setCancelled(true);
      bpe.setBuild(false);
      return;
    }

    if (placeBlock.getType() == Material.ENDER_CHEST) {
      Team playerTeam = game.getPlayerTeam(player);
      if (playerTeam.getInventory() == null) {
        playerTeam.createTeamInventory();
      }

      playerTeam.addChest(placeBlock);
    }

    if (!bpe.isCancelled()) {
      game.getRegion().addPlacedBlock(placeBlock,
          (replacedBlock.getType().equals(Material.AIR) ? null : replacedBlock));
    }
  }
}
 
Example 8
Source File: ChestProtectListener.java    From ShopChest with MIT License 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent e) {
    final Player p = e.getPlayer();
    final Block b = e.getBlockPlaced();

    if (!b.getType().equals(Material.CHEST) && !b.getType().equals(Material.TRAPPED_CHEST)) {
        return;
    }
    
    Chest c = (Chest) b.getState();
    Block b2;

    // Can't use Utils::getChestLocations since inventory holder
    // has not been updated yet in this event (for 1.13+)

    if (Utils.getMajorVersion() < 13) {
        InventoryHolder ih = c.getInventory().getHolder();
        if (!(ih instanceof DoubleChest)) {
            return;
        }

        DoubleChest dc = (DoubleChest) ih;
        Chest l = (Chest) dc.getLeftSide();
        Chest r = (Chest) dc.getRightSide();

        if (b.getLocation().equals(l.getLocation())) {
            b2 = r.getBlock();
        } else {
            b2 = l.getBlock();
        }
    } else {
        org.bukkit.block.data.type.Chest data = (org.bukkit.block.data.type.Chest) c.getBlockData();

        if (data.getType() == Type.SINGLE) {
            return;
        }

        BlockFace neighborFacing;

        switch (data.getFacing()) {
            case NORTH:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.EAST : BlockFace.WEST;
                break;
            case EAST:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.SOUTH : BlockFace.NORTH;
                break;
            case SOUTH:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.WEST : BlockFace.EAST;
                break;
            case WEST:
                neighborFacing = data.getType() == Type.LEFT ? BlockFace.NORTH : BlockFace.SOUTH;
                break;
            default:
                neighborFacing = null;
        }

        b2 = b.getRelative(neighborFacing);
    }

    final Shop shop = shopUtils.getShop(b2.getLocation());
    if (shop == null)
        return;

    plugin.debug(String.format("%s tries to extend %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));

    ShopExtendEvent event = new ShopExtendEvent(p, shop, b.getLocation());
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled() && !p.hasPermission(Permissions.EXTEND_PROTECTED)) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.NO_PERMISSION_EXTEND_PROTECTED));
        return;
    }

    if (!p.getUniqueId().equals(shop.getVendor().getUniqueId()) && !p.hasPermission(Permissions.EXTEND_OTHER)) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.NO_PERMISSION_EXTEND_OTHERS));
        return;
    }

    if (!ItemUtils.isAir(b.getRelative(BlockFace.UP).getType())) {
        e.setCancelled(true);
        p.sendMessage(LanguageUtils.getMessage(Message.CHEST_BLOCKED));
        return;
    }

    final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());

    shopUtils.removeShop(shop, true, new Callback<Void>(plugin) {
        @Override
        public void onResult(Void result) {
            newShop.create(true);
            shopUtils.addShop(newShop, true);
            plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));
        }
    });
}