下面列出了怎么用org.bukkit.inventory.meta.BlockStateMeta的API类实例代码及写法,或者点击链接到github查看源代码。
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
ItemStack item = event.getItemInHand();
if (block != null && item != null &&
isSign(item.getType()) &&
isSign(block.getType())) {
Player player = event.getPlayer();
if ((player.isOp() && player.getGameMode().equals(GameMode.CREATIVE)) ||
!item.hasItemMeta() || !(item.getItemMeta() instanceof BlockStateMeta) ||
!player.hasPermission("nu.se.player")) {
return;
}
SignContent c = SignContent.fromItemStack(item);
if (!c.getContent().isEmpty()) {
signContents.put(event.getPlayer().getUniqueId(), c);
}
}
}
public static SignContent fromItemStack(ItemStack item) {
SignContent content = new SignContent();
if (item.hasItemMeta() && item.getItemMeta() instanceof BlockStateMeta) {
BlockStateMeta blockStateMeta = (BlockStateMeta) item.getItemMeta();
if (blockStateMeta.hasBlockState() && blockStateMeta.getBlockState() instanceof Sign) {
Sign sign = ((Sign) blockStateMeta.getBlockState());
for (int i = 0; i < 4; i++) {
content.setLine(i, sign.getLine(i));
}
}
}
return content;
}
public ItemStack toItemStack(ItemStack item) {
if (item.getItemMeta() instanceof BlockStateMeta) {
BlockStateMeta blockStateMeta = (BlockStateMeta) item.getItemMeta();
if (blockStateMeta.getBlockState() instanceof Sign) {
Sign sign = ((Sign) blockStateMeta.getBlockState());
for (int i = 0; i < 4; i++) {
sign.setLine(i, getLine(i));
}
blockStateMeta.setBlockState(sign);
}
item.setItemMeta(blockStateMeta);
}
return item;
}
/**
* Applies meta to a shield
*
* @param itemStack shield item
* @param bannerMeta banner meta
* @return shield item
*/
public static ItemStack applyMeta(ItemStack itemStack, BannerMeta bannerMeta) {
if(itemStack.getType() != Material.SHIELD && itemStack.getType() != CompatibilityUtils.Mat.WHITE_BANNER.get()) {
throw new IllegalArgumentException("Passed ItemStack is not a shield nor a banner");
}
ItemMeta meta = itemStack.getItemMeta();
BlockStateMeta blockStateMeta = (BlockStateMeta) meta;
Banner banner = (Banner) blockStateMeta.getBlockState();
applyMeta(banner, bannerMeta);
banner.update();
blockStateMeta.setBlockState(banner);
itemStack.setItemMeta(blockStateMeta);
return itemStack;
}
@Override
public EntityType getSpawnerType(ItemStack item) {
BlockStateMeta bsm = (BlockStateMeta) item.getItemMeta();
CreatureSpawner bs = (CreatureSpawner) bsm.getBlockState();
return bs.getSpawnedType();
}