Search in sources :

Example 61 with BlockFace

use of org.bukkit.block.BlockFace in project BKCommonLib by bergerhealer.

the class CommonMapController method onEntityRightClick.

@EventHandler(priority = EventPriority.LOWEST)
protected void onEntityRightClick(PlayerInteractEntityEvent event) {
    if (!(event.getRightClicked() instanceof ItemFrame)) {
        return;
    }
    ItemFrame itemFrame = (ItemFrame) event.getRightClicked();
    if (lastClickOffset != null) {
        Vector pos = lastClickOffset;
        lastClickOffset = null;
        BlockFace attachedFace = itemFrame.getAttachedFace();
        double dx, dy;
        if (FaceUtil.isAlongZ(attachedFace)) {
            dx = pos.getX() + 0.5;
            dy = 1.0 - (pos.getY() + 0.5);
            if (attachedFace == BlockFace.SOUTH) {
                dx = 1.0 - dx;
            }
        } else {
            dx = pos.getZ() + 0.5;
            dy = 1.0 - (pos.getY() + 0.5);
            if (attachedFace == BlockFace.WEST) {
                dx = 1.0 - dx;
            }
        }
        event.setCancelled(dispatchClickAction(event.getPlayer(), itemFrame, dx, dy, MapAction.RIGHT_CLICK));
    } else {
        event.setCancelled(dispatchClickActionApprox(event.getPlayer(), itemFrame, MapAction.RIGHT_CLICK));
    }
}
Also used : BlockFace(org.bukkit.block.BlockFace) ItemFrame(org.bukkit.entity.ItemFrame) Vector(org.bukkit.util.Vector) EventHandler(org.bukkit.event.EventHandler)

Example 62 with BlockFace

use of org.bukkit.block.BlockFace in project BKCommonLib by bergerhealer.

the class CommonMapController method dispatchClickActionApprox.

private boolean dispatchClickActionApprox(Player player, ItemFrame itemFrame, MapAction action) {
    // Calculate the vector position on the map that was clicked
    BlockFace attachedFace = itemFrame.getAttachedFace();
    Location playerPos = player.getEyeLocation();
    Vector dir = playerPos.getDirection();
    Block itemBlock = EntityUtil.getHangingBlock(itemFrame);
    double target_x = (double) itemBlock.getX() + 1.0;
    double target_y = (double) itemBlock.getY() + 1.0;
    double target_z = (double) itemBlock.getZ() + 1.0;
    // offset from wall
    final double FRAME_OFFSET = 0.0625;
    double dx, dy;
    if (FaceUtil.isAlongZ(attachedFace)) {
        if (attachedFace == BlockFace.NORTH) {
            target_z -= 1.0;
        }
        target_z -= attachedFace.getModZ() * FRAME_OFFSET;
        dir.multiply((target_z - playerPos.getZ()) / dir.getZ());
        dx = target_x - (playerPos.getX() + dir.getX());
        dy = target_y - (playerPos.getY() + dir.getY());
        if (attachedFace == BlockFace.NORTH) {
            dx = 1.0 - dx;
        }
    } else {
        if (attachedFace == BlockFace.WEST) {
            target_x -= 1.0;
        }
        target_x -= attachedFace.getModX() * FRAME_OFFSET;
        dir.multiply((target_x - playerPos.getX()) / dir.getX());
        dx = target_z - (playerPos.getZ() + dir.getZ());
        dy = target_y - (playerPos.getY() + dir.getY());
        if (attachedFace == BlockFace.EAST) {
            dx = 1.0 - dx;
        }
    }
    return dispatchClickAction(player, itemFrame, dx, dy, action);
}
Also used : BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 63 with BlockFace

use of org.bukkit.block.BlockFace in project RedProtect by FabioZumbi12.

the class RPBlockListener method onBlockBreak.

@EventHandler(priority = EventPriority.LOWEST)
public void onBlockBreak(BlockBreakEvent e) {
    RedProtect.get().logger.debug("BlockListener - Is BlockBreakEvent event! Cancelled? " + e.isCancelled());
    if (e.isCancelled()) {
        return;
    }
    Player p = e.getPlayer();
    Block b = e.getBlock();
    if (RPUtil.pBorders.containsKey(p.getName()) && b != null && b.getType().equals(RPConfig.getMaterial("region-settings.border.material"))) {
        RPLang.sendMessage(p, "blocklistener.cantbreak.borderblock");
        e.setCancelled(true);
        return;
    }
    World w = p.getWorld();
    Boolean antih = RPConfig.getBool("region-settings.anti-hopper");
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation());
    if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass")) {
        int x = b.getX();
        int y = b.getY();
        int z = b.getZ();
        Block ib = w.getBlockAt(x, y + 1, z);
        if ((antih && !cont.canBreak(p, ib)) || !cont.canBreak(p, b)) {
            RPLang.sendMessage(p, "blocklistener.container.breakinside");
            e.setCancelled(true);
            return;
        }
    }
    if (r == null && RPConfig.getGlobalFlagList(p.getWorld().getName() + ".if-build-false.break-blocks").contains(b.getType().name())) {
        return;
    }
    // remove more sign
    if (b.getType().equals(Material.WALL_SIGN)) {
        Material btype = cont.getBlockRelative(b).getType();
        BlockFace[] aside = new BlockFace[] { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
        for (BlockFace bf : aside) {
            Block brel = b.getRelative(bf);
            if (brel != null && brel.getType().equals(Material.WALL_SIGN) && cont.validateMoreSign(brel) && cont.getBlockRelative(brel).getType().equals(btype)) {
                brel.breakNaturally();
                break;
            }
        }
    }
    if (r != null && b.getType().equals(Material.MOB_SPAWNER) && r.allowSpawner(p)) {
        return;
    }
    if (r != null && !r.canBuild(p) && !r.canTree(b) && !r.canMining(b) && !r.canCrops(b) && !r.canBreak(b.getType())) {
        RPLang.sendMessage(p, "blocklistener.region.cantbuild");
        e.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) Material(org.bukkit.Material) World(org.bukkit.World) EventHandler(org.bukkit.event.EventHandler)

Example 64 with BlockFace

use of org.bukkit.block.BlockFace in project AreaShop by NLthijs48.

the class RegionSign method update.

/**
 * Update this sign.
 * @return true if the update was successful, otherwise false
 */
public boolean update() {
    // Ignore updates of signs in chunks that are not loaded
    Location signLocation = getLocation();
    if (!signLocation.getWorld().isChunkLoaded(signLocation.getBlockX() >> 4, signLocation.getBlockZ() >> 4)) {
        return false;
    }
    if (getRegion().isDeleted()) {
        return false;
    }
    YamlConfiguration regionConfig = getRegion().getConfig();
    ConfigurationSection signConfig = getProfile();
    Block block = signLocation.getBlock();
    if (signConfig == null || !signConfig.isSet(getRegion().getState().getValue())) {
        block.setType(Material.AIR);
        return true;
    }
    ConfigurationSection stateConfig = signConfig.getConfigurationSection(getRegion().getState().getValue());
    // Get the lines
    String[] signLines = new String[4];
    boolean signEmpty = true;
    for (int i = 0; i < 4; i++) {
        signLines[i] = stateConfig.getString("line" + (i + 1));
        signEmpty &= (signLines[i] == null || signLines[i].isEmpty());
    }
    if (signEmpty) {
        block.setType(Material.AIR);
        return true;
    }
    Sign signState = null;
    // Place the sign back (with proper rotation and type) after it has been hidden or (indirectly) destroyed
    if (block.getType() != Material.WALL_SIGN && block.getType() != Material.SIGN_POST) {
        Material signType = getMaterial();
        if (signType != Material.WALL_SIGN && signType != Material.SIGN_POST) {
            AreaShop.debug("Setting sign", key, "of region", getRegion().getName(), "failed, could not set sign block back");
            return false;
        }
        block.setType(signType);
        signState = (Sign) block.getState();
        org.bukkit.material.Sign signData = (org.bukkit.material.Sign) signState.getData();
        BlockFace signFace = getFacing();
        if (signFace != null) {
            signData.setFacingDirection(signFace);
            signState.setData(signData);
        }
    }
    if (signState == null) {
        signState = (Sign) block.getState();
    }
    // Save current rotation and type
    org.bukkit.material.Sign signData = (org.bukkit.material.Sign) signState.getData();
    if (!regionConfig.isString("general.signs." + key + ".signType")) {
        getRegion().setSetting("general.signs." + key + ".signType", signState.getType().toString());
    }
    if (!regionConfig.isString("general.signs." + key + ".facing")) {
        getRegion().setSetting("general.signs." + key + ".facing", signData.getFacing().toString());
    }
    // Apply replacements and color and then set it on the sign
    for (int i = 0; i < signLines.length; i++) {
        if (signLines[i] == null) {
            signState.setLine(i, "");
            continue;
        }
        signLines[i] = Message.fromString(signLines[i]).replacements(getRegion()).getSingle();
        signLines[i] = Utils.applyColors(signLines[i]);
        signState.setLine(i, signLines[i]);
    }
    signState.update();
    return true;
}
Also used : BlockFace(org.bukkit.block.BlockFace) Material(org.bukkit.Material) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) Location(org.bukkit.Location) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 65 with BlockFace

use of org.bukkit.block.BlockFace in project AreaShop by NLthijs48.

the class TeleportFeature method getStartLocation.

/**
 * Get the start location of a safe teleport search.
 * @param player The player to get it for
 * @param toSign true to try teleporting to the first sign, false for teleporting to the region
 * @return The start location
 */
private Location getStartLocation(Player player, Value<Boolean> toSign) {
    Location startLocation = null;
    ProtectedRegion worldguardRegion = getRegion().getRegion();
    // Try to get sign location
    List<RegionSign> signs = getRegion().getSignsFeature().getSigns();
    boolean signAvailable = !signs.isEmpty();
    if (toSign.get()) {
        if (signAvailable) {
            // Use the location 1 below the sign to prevent weird spawing above the sign
            // .subtract(0.0, 1.0, 0.0);
            startLocation = signs.get(0).getLocation();
            startLocation.setPitch(player.getLocation().getPitch());
            startLocation.setYaw(player.getLocation().getYaw());
            // Move player x blocks away from the sign
            double distance = getRegion().getDoubleSetting("general.teleportSignDistance");
            if (distance > 0) {
                BlockFace facing = getRegion().getSignsFeature().getSigns().get(0).getFacing();
                Vector facingVector = new Vector(facing.getModX(), facing.getModY(), facing.getModZ()).normalize().multiply(distance);
                startLocation.setX(startLocation.getBlockX() + 0.5);
                startLocation.setZ(startLocation.getBlockZ() + 0.5);
                startLocation.add(facingVector);
            }
        } else {
            // No sign available
            getRegion().message(player, "teleport-changedToNoSign");
            toSign.set(false);
        }
    }
    // Use teleportation location that is set for the region
    if (startLocation == null && hasTeleportLocation()) {
        startLocation = getTeleportLocation();
    }
    // Calculate a default location
    if (startLocation == null) {
        // Set to block in the middle, y configured in the config
        com.sk89q.worldedit.Vector middle = com.sk89q.worldedit.Vector.getMidpoint(worldguardRegion.getMaximumPoint(), worldguardRegion.getMinimumPoint());
        String configSetting = getRegion().getStringSetting("general.teleportLocationY");
        if ("bottom".equalsIgnoreCase(configSetting)) {
            middle = middle.setY(worldguardRegion.getMinimumPoint().getBlockY());
        } else if ("top".equalsIgnoreCase(configSetting)) {
            middle = middle.setY(worldguardRegion.getMaximumPoint().getBlockY());
        } else if ("middle".equalsIgnoreCase(configSetting)) {
            middle = middle.setY(middle.getBlockY());
        } else {
            try {
                int vertical = Integer.parseInt(configSetting);
                middle = middle.setY(vertical);
            } catch (NumberFormatException e) {
                AreaShop.warn("Could not parse general.teleportLocationY: '" + configSetting + "'");
            }
        }
        startLocation = new Location(getRegion().getWorld(), middle.getX(), middle.getY(), middle.getZ(), player.getLocation().getYaw(), player.getLocation().getPitch());
    }
    // Set location in the center of the block
    startLocation.setX(startLocation.getBlockX() + 0.5);
    startLocation.setZ(startLocation.getBlockZ() + 0.5);
    return startLocation;
}
Also used : BlockFace(org.bukkit.block.BlockFace) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionSign(me.wiefferink.areashop.features.signs.RegionSign) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

BlockFace (org.bukkit.block.BlockFace)136 Block (org.bukkit.block.Block)53 GlowBlock (net.glowstone.block.GlowBlock)32 Location (org.bukkit.Location)23 Material (org.bukkit.Material)23 Vector (org.bukkit.util.Vector)18 ArrayList (java.util.ArrayList)12 ItemStack (org.bukkit.inventory.ItemStack)12 Sign (org.bukkit.block.Sign)10 BlockState (org.bukkit.block.BlockState)9 EventHandler (org.bukkit.event.EventHandler)8 MaterialData (org.bukkit.material.MaterialData)8 List (java.util.List)7 ItemTable (net.glowstone.block.ItemTable)7 Player (org.bukkit.entity.Player)7 GlowBlockState (net.glowstone.block.GlowBlockState)6 GlowPlayer (net.glowstone.entity.GlowPlayer)6 UUID (java.util.UUID)5 BlockType (net.glowstone.block.blocktype.BlockType)5 PulseTask (net.glowstone.scheduler.PulseTask)5