下面列出了怎么用org.bukkit.block.data.Waterlogged的API类实例代码及写法,或者点击链接到github查看源代码。
private void placeLiquid(Block block, boolean water) {
if (block.getType() == Material.AIR || block.getType() == Material.CAVE_AIR || block.getType() == Material.VOID_AIR) {
block.setType(water ? Material.WATER : Material.LAVA);
}
else {
if (water && block.getBlockData() instanceof Waterlogged) {
Waterlogged wl = (Waterlogged) block.getBlockData();
wl.setWaterlogged(true);
block.setBlockData(wl, false);
block.getWorld().playSound(block.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1F, 1F);
return;
}
if (BlockStorage.hasBlockInfo(block)) {
BlockStorage.clearBlockInfo(block);
}
}
runPostTask(block, water ? Sound.ENTITY_PLAYER_SPLASH : Sound.BLOCK_LAVA_POP, 1);
}
/**
* This method checks whether a given {@link Material} array contains any {@link Material}
* that would result in a {@link Waterlogged} {@link BlockData}.
* This is done to save performance, so we don't have to validate {@link BlockData} at
* runtime.
*
* @param materials
* The {@link Material} Array to check
*
* @return Whether the array contained any {@link Waterlogged} materials
*/
private boolean containsWaterlogged(Material[] materials) {
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
// BlockData is not available to us during Unit Tests :/
return false;
}
for (Material type : materials) {
// This BlockData is purely virtual and only created on startup, it should have
// no impact on performance, in fact it should save performance as it preloads
// the data but also saves heavy calls for non-waterlogged Materials
if (type.createBlockData() instanceof Waterlogged) {
return true;
}
}
return false;
}
@Override
public void tick(Block b, SlimefunItem item, Config data) {
if (b.getType() == Material.AIR) {
// The block was broken, setting the Material now would result in a
// duplication glitch
return;
}
if (waterlogged) {
BlockData blockData = b.getBlockData();
b.setType(material, true);
if (blockData instanceof Waterlogged && ((Waterlogged) blockData).isWaterlogged()) {
Waterlogged block = (Waterlogged) b.getBlockData();
block.setWaterlogged(true);
b.setBlockData(block);
}
}
else {
b.setType(material, false);
}
}
private void pasteTree(ChunkPopulateEvent e, int x, int z, Tree tree) {
for (int y = e.getWorld().getMaxHeight(); y > 30; y--) {
Block current = e.getWorld().getBlockAt(x, y, z);
if (!current.getType().isSolid() && current.getType() != Material.WATER && current.getType() != Material.SEAGRASS && current.getType() != Material.TALL_SEAGRASS && !(current.getBlockData() instanceof Waterlogged && ((Waterlogged) current.getBlockData()).isWaterlogged()) && tree.isSoil(current.getRelative(0, -1, 0).getType()) && isFlat(current)) {
Schematic.pasteSchematic(new Location(e.getWorld(), x, y, z), tree);
break;
}
}
}
/**
* Create a shop use Shop and Info object.
*
* @param shop The shop object
* @param info The info object
*/
public void createShop(@NotNull Shop shop, @NotNull Info info) {
Player player = Bukkit.getPlayer(shop.getOwner());
if (player == null) {
throw new IllegalStateException("The owner creating the shop is offline or not exist");
}
ShopCreateEvent shopCreateEvent = new ShopCreateEvent(shop, player);
if (Util.fireCancellableEvent(shopCreateEvent)) {
Util.debugLog("Cancelled by plugin");
return;
}
if (info.getSignBlock() != null && autoSign) {
if (Util.isAir(info.getSignBlock().getType()) || info.getSignBlock().getType() == Material.WATER) {
info.getSignBlock().setType(Util.getSignMaterial());
BlockState bs = info.getSignBlock().getState();
if (info.getSignBlock().getType() == Material.WATER && (bs.getBlockData() instanceof Waterlogged)) {
Waterlogged waterable = (Waterlogged) bs.getBlockData();
waterable.setWaterlogged(true); // Looks like sign directly put in water
}
if (bs.getBlockData() instanceof WallSign) {
WallSign signBlockDataType = (WallSign) bs.getBlockData();
BlockFace bf = info.getLocation().getBlock().getFace(info.getSignBlock());
if (bf != null) {
signBlockDataType.setFacing(bf);
bs.setBlockData(signBlockDataType);
}
} else {
plugin.getLogger().warning("Sign material " + bs.getType().name() + " not a WallSign, make sure you using correct sign material.");
}
bs.update(true);
} else {
if (!plugin.getConfig().getBoolean("shop.allow-shop-without-space-for-sign")) {
MsgUtil.sendMessage(player, MsgUtil.getMessage("failed-to-put-sign", player));
Util.debugLog("Sign cannot placed cause no enough space(Not air block)");
return;
}
}
}
//load the shop finally
shop.onLoad();
//first init
shop.setSignText();
//sync add to prevent compete issue
addShop(shop.getLocation().getWorld().getName(), shop);
//save to database
plugin.getDatabaseHelper().createShop(
shop,
null,
e -> Bukkit.getScheduler().runTask(plugin, () -> {
//also remove from memory when failed
shop.delete(true);
plugin.getLogger().warning("Shop create failed, trying to auto fix the database...");
boolean backupSuccess = Util.backupDatabase();
if (backupSuccess) {
plugin.getDatabaseHelper().removeShop(shop);
} else {
plugin.getLogger().warning("Failed to backup the database, all changes will revert after a reboot.");
}
}));
}