org.bukkit.block.data.Directional Java Examples

The following examples show how to use org.bukkit.block.data.Directional. 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: XBlock.java    From IridiumSkyblock with GNU General Public License v2.0 6 votes vote down vote up
public static boolean setDirection(Block block, BlockFace facing) {
    if (XMaterial.ISFLAT) {
        if (!(block.getBlockData() instanceof Directional)) return false;
        Directional direction = (Directional) block.getBlockData();
        direction.setFacing(facing);
        return true;
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof org.bukkit.material.Directional) {
        ((org.bukkit.material.Directional) data).setFacingDirection(facing);
        state.update(true);
        return true;
    }
    return false;
}
 
Example #2
Source File: DispenserListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onBlockDispensing(BlockDispenseEvent e) {
    Block b = e.getBlock();

    if (b.getType() == Material.DISPENSER && b.getRelative(BlockFace.DOWN).getType() != Material.HOPPER) {
        SlimefunItem machine = BlockStorage.check(b);

        if (machine != null) {
            machine.callItemHandler(BlockDispenseHandler.class, handler -> {
                Dispenser dispenser = (Dispenser) b.getState();
                BlockFace face = ((Directional) b.getBlockData()).getFacing();
                Block block = b.getRelative(face);
                handler.onBlockDispense(e, dispenser, block, machine);
            });
        }
    }
}
 
Example #3
Source File: DuctListener.java    From Transport-Pipes with MIT License 6 votes vote down vote up
private void setDirectionalBlockFace(Location b, BlockData bd, Player p) {
    if (bd instanceof Directional) {
        Vector dir = new Vector(b.getX() + 0.5d, b.getY() + 0.5d, b.getZ() + 0.5d);
        dir.subtract(p.getEyeLocation().toVector());
        double absX = Math.abs(dir.getX());
        double absY = Math.abs(dir.getY());
        double absZ = Math.abs(dir.getZ());
        if (((Directional) bd).getFaces().contains(BlockFace.UP) && ((Directional) bd).getFaces().contains(BlockFace.DOWN)) {
            if (absX >= absY && absX >= absZ) {
                ((Directional) bd).setFacing(dir.getX() > 0 ? BlockFace.WEST : BlockFace.EAST);
            } else if (absY >= absX && absY >= absZ) {
                ((Directional) bd).setFacing(dir.getY() > 0 ? BlockFace.DOWN : BlockFace.UP);
            } else {
                ((Directional) bd).setFacing(dir.getZ() > 0 ? BlockFace.NORTH : BlockFace.SOUTH);
            }
        } else {
            if (absX >= absZ) {
                ((Directional) bd).setFacing(dir.getX() > 0 ? BlockFace.WEST : BlockFace.EAST);
            } else {
                ((Directional) bd).setFacing(dir.getZ() > 0 ? BlockFace.NORTH : BlockFace.SOUTH);
            }
        }
    }
}
 
Example #4
Source File: XBlock.java    From XSeries with MIT License 6 votes vote down vote up
public static boolean setDirection(Block block, BlockFace facing) {
    if (ISFLAT) {
        if (!(block.getBlockData() instanceof Directional)) return false;
        Directional direction = (Directional) block.getBlockData();
        direction.setFacing(facing);
        return true;
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof org.bukkit.material.Directional) {
        ((org.bukkit.material.Directional) data).setFacingDirection(facing);
        state.update(true);
        return true;
    }
    return false;
}
 
Example #5
Source File: Util.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fetches the block which the given sign is attached to
 *
 * @param b The block which is attached
 * @return The block the sign is attached to
 */
@Nullable
public static Block getAttached(@NotNull Block b) {
    final BlockData blockData = b.getBlockData();
    if (blockData instanceof Directional) {
        final Directional directional = (Directional) blockData;
        return b.getRelative(directional.getFacing().getOppositeFace());
    } else {
        return null;
    }
}
 
Example #6
Source File: WallMatcher.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Try and match a wall block
 *
 * @param block
 * @param matchingFace
 * @return
 */
private Block tryMatchBlock(Block block, BlockFace matchingFace) {
    byte direction = block.getData();
    BlockData blockData = block.getBlockData();

    // Blocks such as wall signs or banners
    if (PROTECTABLES_WALL.contains(block.getType()) && blockData instanceof Directional) {
        if (((Directional) block.getState().getBlockData()).getFacing() == matchingFace) {
            return block;
        }
    }

    // Levers, buttons
    else if (PROTECTABLES_LEVERS_ET_AL.contains(block.getType())) {
        byte EAST = 0x4;
        byte WEST = 0x3;
        byte SOUTH = 0x1;
        byte NORTH = 0x2;

        if (matchingFace == BlockFace.EAST && (direction & EAST) == EAST) {
            return block;
        } else if (matchingFace == BlockFace.WEST && (direction & WEST) == WEST) {
            return block;
        } else if (matchingFace == BlockFace.SOUTH && (direction & SOUTH) == SOUTH) {
            return block;
        } else if (matchingFace == BlockFace.NORTH && (direction & NORTH) == NORTH) {
            return block;
        }
    }

    return null;
}
 
Example #7
Source File: XBlock.java    From XSeries with MIT License 5 votes vote down vote up
public static BlockFace getDirection(Block block) {
    if (ISFLAT) {
        if (!(block.getBlockData() instanceof Directional)) return BlockFace.SELF;
        Directional direction = (Directional) block.getBlockData();
        return direction.getFacing();
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof org.bukkit.material.Directional) {
        return ((org.bukkit.material.Directional) data).getFacing();
    }
    return null;
}
 
Example #8
Source File: BlockAdapterBlockData.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BlockFace getFacing(Block block) {
    if (block.getBlockData() instanceof Directional) {
        return ((Directional) block.getBlockData()).getFacing();
    } else if (block.getBlockData() instanceof Rotatable) {
        return ((Rotatable) block.getBlockData()).getRotation();
    } else {
        throw new IllegalArgumentException("Block is not Directional or Rotatable");
    }
}
 
Example #9
Source File: BlockAdapterBlockData.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setFacing(Block block, BlockFace facing) {
    BlockData data = block.getBlockData();
    if (data instanceof Directional) {
        ((Directional) data).setFacing(facing);
    } else if (data instanceof Rotatable) {
        ((Rotatable) data).setRotation(facing);
    } else {
        throw new IllegalArgumentException("Block is not Directional or Rotatable");
    }
    block.setBlockData(data, false);
}
 
Example #10
Source File: VersionHelperLatest.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public Block getBlockRelative(Block block) {
    if (block.getState() instanceof Sign) {
        Directional dir = (Directional) block.getBlockData();
        return block.getRelative(dir.getFacing().getOppositeFace());
    }
    return null;
}
 
Example #11
Source File: VersionHelper113.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public Block getBlockRelative(Block block) {
    if (block.getState() instanceof Sign) {
        Directional dir = (Directional) block.getBlockData();
        return block.getRelative(dir.getFacing().getOppositeFace());
    }
    return null;
}
 
Example #12
Source File: Shop.java    From ShopChest with MIT License 5 votes vote down vote up
/**
 * Runs everything that needs to be called synchronously in order 
 * to prepare creating the hologram.
 */
private PreCreateResult preCreateHologram() {
    plugin.debug("Creating hologram (#" + id + ")");

    InventoryHolder ih = getInventoryHolder();

    if (ih == null) return null;

    Chest[] chests = new Chest[2];
    BlockFace face;

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

        chests[0] = r;
        chests[1] = l;
    } else {
        chests[0] = (Chest) ih;
    }

    if (Utils.getMajorVersion() < 13) {
        face = ((org.bukkit.material.Directional) chests[0].getData()).getFacing();
    } else {
        face = ((Directional) chests[0].getBlockData()).getFacing();
    }

    return new PreCreateResult(ih.getInventory(), chests, face);
}
 
Example #13
Source File: XBlock.java    From IridiumSkyblock with GNU General Public License v2.0 5 votes vote down vote up
public static BlockFace getDirection(Block block) {
    if (XMaterial.ISFLAT) {
        if (!(block.getBlockData() instanceof Directional)) return BlockFace.SELF;
        Directional direction = (Directional) block.getBlockData();
        return direction.getFacing();
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof org.bukkit.material.Directional) {
        return ((org.bukkit.material.Directional) data).getFacing();
    }
    return null;
}
 
Example #14
Source File: ChestTerminalNetwork.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
protected static Optional<Block> getAttachedBlock(Block block) {
    if (block.getType() == Material.PLAYER_WALL_HEAD) {
        BlockFace face = ((Directional) block.getBlockData()).getFacing().getOppositeFace();
        return Optional.of(block.getRelative(face));
    }

    return Optional.empty();
}
 
Example #15
Source File: BuildersWandListener.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
private boolean placeBlock(Block b, Player player, Location l, Location loc, ItemStack item, Vector vector) {
	if (!b.getWorld().getBlockAt(l).getType().equals(b.getType())) {
		return false;
	}

	Material type = b.getWorld().getBlockAt(loc).getType();

	if (!(type == Material.AIR || type == Material.CAVE_AIR ||
			type == Material.WATER || type == Material.BUBBLE_COLUMN ||
			type == Material.LAVA || type == Material.GRASS)) {

		return false;
	}

	//triggers a pseudoevent to find out if the Player can build
	Block block = b.getWorld().getBlockAt(loc);

	BlockPlaceEvent placeEvent = new BlockPlaceEvent(block, block.getState(), b, item, player, true, EquipmentSlot.HAND);
	Bukkit.getPluginManager().callEvent(placeEvent);

	//check the pseudoevent
	if (!placeEvent.canBuild() || placeEvent.isCancelled()) {
		return false;
	}

	Block nb = b.getWorld().getBlockAt(loc);
	Block behind = nb.getWorld().getBlockAt(loc.clone().subtract(vector));
	if (behind.getBlockData() instanceof Slab) {
		if (((Slab) behind.getBlockData()).getType().equals(Slab.Type.DOUBLE)) {
			if (item.getAmount() - 2 < 0) {
				return false;
			}
		}
	}

	nb.setType(item.getType());
	BlockData bd = nb.getBlockData();

	if (bd instanceof Directional) {
		((Directional) bd).setFacing(((Directional) behind.getBlockData()).getFacing());
	}

	if (bd instanceof Slab) {
		((Slab) bd).setType(((Slab) behind.getBlockData()).getType());
	}

	nb.setBlockData(bd);

	return true;
}
 
Example #16
Source File: BlockListener.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlace(BlockPlaceEvent e) {

    final Material type = e.getBlock().getType();
    final Block placingBlock = e.getBlock();
    final Player player = e.getPlayer();

    if (type != Material.CHEST) {
        return;
    }
    Block chest = null;
    //Chest combine mechanic based checking
    if (player.isSneaking()) {
        Block blockAgainst = e.getBlockAgainst();
        if (blockAgainst.getType() == Material.CHEST && placingBlock.getFace(blockAgainst) != BlockFace.UP && placingBlock.getFace(blockAgainst) != BlockFace.DOWN && !(((Chest) blockAgainst.getState()).getInventory() instanceof DoubleChestInventory)) {
            chest = e.getBlockAgainst();
        } else {
            return;
        }
    } else {
        //Get all chest in vertical Location
        BlockFace placingChestFacing = ((Directional) (placingBlock.getState().getBlockData())).getFacing();
        for (BlockFace face : Util.getVerticalFacing()) {
            //just check the right side and left side
            if (face != placingChestFacing && face != placingChestFacing.getOppositeFace()) {
                Block nearByBlock = placingBlock.getRelative(face);
                if (nearByBlock.getType() == Material.CHEST
                        //non double chest
                        && !(((Chest) nearByBlock.getState()).getInventory() instanceof DoubleChestInventory)
                        //same facing
                        && placingChestFacing == ((Directional) nearByBlock.getState().getBlockData()).getFacing()) {
                    if (chest == null) {
                        chest = nearByBlock;
                    } else {
                        //when multiply chests competed, minecraft will always combine with right side
                        if (placingBlock.getFace(nearByBlock) == Util.getRightSide(placingChestFacing)) {
                            chest = nearByBlock;
                        }
                    }
                }
            }
        }
    }
    if (chest == null) {
        return;
    }

    Shop shop = getShopPlayer(chest.getLocation(), false);
    if (shop != null) {
        if (!QuickShop.getPermissionManager().hasPermission(player, "quickshop.create.double")) {
            e.setCancelled(true);
            MsgUtil.sendMessage(player, MsgUtil.getMessage("no-double-chests", player));

        } else if (!shop.getModerator().isModerator(player.getUniqueId())) {
            e.setCancelled(true);
            MsgUtil.sendMessage(player, MsgUtil.getMessage("not-managed-shop", player));
        }
    }
}
 
Example #17
Source File: ArmorStandDisplayItem.java    From QuickShop-Reremake with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Location getDisplayLocation() {
    BlockFace containerBlockFace = BlockFace.NORTH; // Set default vaule
    if (this.shop.getLocation().getBlock().getBlockData() instanceof Directional) {
        containerBlockFace =
                ((Directional) this.shop.getLocation().getBlock().getBlockData())
                        .getFacing(); // Replace by container face.
    }
    // Fix specific block facing
    Material type = this.shop.getLocation().getBlock().getType();
    if (type.name().contains("ANVIL")
            || type.name().contains("FENCE")
            || type.name().contains("WALL")) {
        switch (containerBlockFace) {
            case SOUTH:
                containerBlockFace = BlockFace.WEST;
                break;
            case NORTH:
                containerBlockFace = BlockFace.EAST;
            case EAST:
                containerBlockFace = BlockFace.NORTH;
            case WEST:
                containerBlockFace = BlockFace.SOUTH;
            default:
                break;
        }
    }

    Location asloc = getCenter(this.shop.getLocation());
    Util.debugLog("containerBlockFace " + containerBlockFace);
    if (this.originalItemStack.getType().isBlock()) {
        asloc.add(0, 0.5, 0);
    }
    switch (containerBlockFace) {
        case SOUTH:
            asloc.add(0, -0.5, 0);
            asloc.setYaw(0);
            Util.debugLog("Block face as SOUTH");
            break;
        case WEST:
            asloc.add(0, -0.5, 0);
            asloc.setYaw(90);
            Util.debugLog("Block face as WEST");
            break;
        case EAST:
            asloc.add(0, -0.5, 0);
            asloc.setYaw(-90);
            Util.debugLog("Block face as EAST");
            break;
        case NORTH:
            asloc.add(0, -0.5, 0);
            asloc.setYaw(180);
            Util.debugLog("Block face as NORTH");
            break;
        default:
            break;
    }
    return asloc;
}
 
Example #18
Source File: SitListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClickBlock(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && !event.hasItem()) {
        Block block = event.getClickedBlock();
        BlockFace face = event.getBlockFace();
        if (face == BlockFace.DOWN || block.isLiquid() || !plugin.cfg.sit_blocks.contains(block.getType())) {
            return;
        }
        Block relative = block.getRelative(0, 1, 0);
        Player player = event.getPlayer();
        if (messageCooldown.getIfPresent(player.getUniqueId()) != null) {
            return;
        }
        messageCooldown.put(player.getUniqueId(), true);
        if (!player.hasPermission("nu.sit") || !enabledPlayers.contains(player.getUniqueId()) || player.isInsideVehicle() || !player.getPassengers().isEmpty() || player.getGameMode() == GameMode.SPECTATOR || !player.isOnGround()) {
            return;
        }
        if (relative.isLiquid() || !(relative.isEmpty() || relative.isPassable())) {
            player.sendMessage(I18n.format("user.sit.invalid_location"));
            return;
        }
        Vector vector = block.getBoundingBox().getCenter().clone();
        Location loc = vector.setY(block.getBoundingBox().getMaxY()).toLocation(player.getWorld()).clone();
        for (SitLocation sl : plugin.cfg.sit_locations.values()) {
            if (sl.blocks != null && sl.x != null && sl.y != null && sl.z != null && sl.blocks.contains(block.getType().name())) {
                loc.add(sl.x, sl.y, sl.z);
            }
        }
        if (block.getBlockData() instanceof Directional) {
            face = ((Directional) block.getBlockData()).getFacing();
            if (face == BlockFace.EAST) {
                loc.setYaw(90);
            } else if (face == BlockFace.WEST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(-180);
            }
        } else {
            if (face == BlockFace.WEST) {
                loc.setYaw(90);
            } else if (face == BlockFace.EAST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(-180);
            } else {
                loc.setYaw(player.getEyeLocation().getYaw());
            }
        }
        for (Entity e : loc.getWorld().getNearbyEntities(loc, 0.5, 0.7, 0.5)) {
            if (e instanceof LivingEntity) {
                if (e.hasMetadata(metadata_key) || (e instanceof Player && e.isInsideVehicle() && e.getVehicle().hasMetadata(metadata_key))) {
                    player.sendMessage(I18n.format("user.sit.invalid_location"));
                    return;
                }
            }
        }
        Location safeLoc = player.getLocation().clone();
        ArmorStand armorStand = loc.getWorld().spawn(loc, ArmorStand.class, (e) -> {
            e.setVisible(false);
            e.setPersistent(false);
            e.setCanPickupItems(false);
            e.setBasePlate(false);
            e.setArms(false);
            e.setMarker(true);
            e.setInvulnerable(true);
            e.setGravity(false);
        });
        if (armorStand != null) {
            armorStand.setMetadata(metadata_key, new FixedMetadataValue(plugin, true));
            if (armorStand.addPassenger(player)) {
                safeLocations.put(player.getUniqueId(), safeLoc);
            } else {
                armorStand.remove();
            }
        }
    }
}
 
Example #19
Source File: DebugFishListener.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
private void sendInfo(Player p, Block b) {
    SlimefunItem item = BlockStorage.check(b);

    p.sendMessage(" ");
    p.sendMessage(ChatColors.color("&d" + b.getType() + " &e@ X: " + b.getX() + " Y: " + b.getY() + " Z: " + b.getZ()));
    p.sendMessage(ChatColors.color("&dId: " + "&e" + item.getID()));
    p.sendMessage(ChatColors.color("&dPlugin: " + "&e" + item.getAddon().getName()));

    if (b.getState() instanceof Skull) {
        p.sendMessage(ChatColors.color("&dSkull: " + enabledTooltip));

        // Check if the skull is a wall skull, and if so use Directional instead of Rotatable.
        if (b.getType() == Material.PLAYER_WALL_HEAD) {
            p.sendMessage(ChatColors.color("  &dFacing: &e" + ((Directional) b.getBlockData()).getFacing().toString()));
        }
        else {
            p.sendMessage(ChatColors.color("  &dRotation: &e" + ((Rotatable) b.getBlockData()).getRotation().toString()));
        }
    }

    if (BlockStorage.getStorage(b.getWorld()).hasInventory(b.getLocation())) {
        p.sendMessage(ChatColors.color("&dInventory: " + enabledTooltip));
    }
    else {
        p.sendMessage(ChatColors.color("&dInventory: " + disabledTooltip));
    }

    TickerTask ticker = SlimefunPlugin.getTickerTask();

    if (item.isTicking()) {
        p.sendMessage(ChatColors.color("&dTicker: " + enabledTooltip));
        p.sendMessage(ChatColors.color("  &dAsync: &e" + (BlockStorage.check(b).getBlockTicker().isSynchronized() ? disabledTooltip : enabledTooltip)));
        p.sendMessage(ChatColors.color("  &dTimings: &e" + NumberUtils.getAsMillis(ticker.getTimings(b))));
        p.sendMessage(ChatColors.color("  &dTotal Timings: &e" + NumberUtils.getAsMillis(ticker.getTimings(BlockStorage.checkID(b)))));
        p.sendMessage(ChatColors.color("  &dChunk Timings: &e" + NumberUtils.getAsMillis(ticker.getTimings(b.getChunk()))));
    }
    else if (item.getEnergyTicker() != null) {
        p.sendMessage(ChatColors.color("&dTicking: " + "&3Indirect"));
        p.sendMessage(ChatColors.color("  &dTimings: &e" + NumberUtils.getAsMillis(ticker.getTimings(b))));
        p.sendMessage(ChatColors.color("  &dChunk Timings: &e" + NumberUtils.getAsMillis(ticker.getTimings(b.getChunk()))));
    }
    else {
        p.sendMessage(ChatColors.color("&dTicker: " + disabledTooltip));
        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&dTicking: " + disabledTooltip));
    }

    if (ChargableBlock.isChargable(b)) {
        p.sendMessage(ChatColors.color("&dChargeable: " + enabledTooltip));
        p.sendMessage(ChatColors.color("  &dEnergy: &e" + ChargableBlock.getCharge(b) + " / " + ChargableBlock.getMaxCharge(b)));
    }
    else {
        p.sendMessage(ChatColors.color("&dChargeable: " + disabledTooltip));
    }

    p.sendMessage(ChatColors.color("  &dEnergyNet Type: &e" + EnergyNet.getComponent(b.getLocation())));

    p.sendMessage(ChatColors.color("&6" + BlockStorage.getBlockInfoAsJson(b)));
    p.sendMessage(" ");
}