下面列出了怎么用org.bukkit.block.data.Directional的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
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);
}
}
}
}
@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);
});
}
}
}
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;
}
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;
}
/**
* 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;
}
}
/**
* 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;
}
@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");
}
}
@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);
}
public Block getBlockRelative(Block block) {
if (block.getState() instanceof Sign) {
Directional dir = (Directional) block.getBlockData();
return block.getRelative(dir.getFacing().getOppositeFace());
}
return null;
}
public Block getBlockRelative(Block block) {
if (block.getState() instanceof Sign) {
Directional dir = (Directional) block.getBlockData();
return block.getRelative(dir.getFacing().getOppositeFace());
}
return null;
}
/**
* 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);
}
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();
}
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;
}
@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));
}
}
}
@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;
}
@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();
}
}
}
}
private void sendInfo(Player p, Block b) {
SlimefunItem item = BlockStorage.check(b);
p.sendMessage(" ");
p.sendMessage(ChatColors.color("&d" + b.getType() + " &[email protected] 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(" ");
}