Java Code Examples for org.bukkit.block.BlockState#update()

The following examples show how to use org.bukkit.block.BlockState#update() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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 Project: Crazy-Crates   File: QuadCrateSession.java    License: MIT License 6 votes vote down vote up
private void rotateChest(Block chest, Integer direction) {
    BlockFace blockFace;
    switch (direction) {
        case 0://East
            blockFace = BlockFace.WEST;
            break;
        case 1://South
            blockFace = BlockFace.NORTH;
            break;
        case 2://West
            blockFace = BlockFace.EAST;
            break;
        case 3://North
            blockFace = BlockFace.SOUTH;
            break;
        default:
            blockFace = BlockFace.DOWN;
            break;
    }
    BlockState state = chest.getState();
    state.setData(new Chest(blockFace));
    state.update();
}
 
Example 3
@EventHandler
public void onBlockGrow(BlockGrowEvent event) {
    try {
        final Block block = event.getBlock();
        final Location location = block.getLocation();
        final IslandManager islandManager = IridiumSkyblock.getIslandManager();
        final Island island = islandManager.getIslandViaLocation(location);
        if (island == null) return;

        if (island.getFarmingBooster() == 0) return;

        final Material material = block.getType();
        if (!XBlock.isCrops(material)) return;

        event.setCancelled(true);

        final Crops crops = new Crops(CropState.RIPE);
        final BlockState blockState = block.getState();
        blockState.setData(crops);
        blockState.update();
    } catch (Exception e) {
        IridiumSkyblock.getInstance().sendErrorMessage(e);
    }
}
 
Example 4
@Override
protected Void execute(String playerID) throws QuestRuntimeException {
    Block block = loc.getLocation(playerID).getBlock();
    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof Openable) {
        Openable openable = (Openable) data;
        switch (type) {
            case ON:
                openable.setOpen(true);
                break;
            case OFF:
                openable.setOpen(false);
                break;
            case TOGGLE:
                openable.setOpen(!openable.isOpen());
                break;
        }
        state.setData((MaterialData) openable);
        state.update();
    }
    return null;
}
 
Example 5
public static void setAge(Block block, int age) {
    if (XMaterial.ISFLAT) {
        if (!(block.getBlockData() instanceof Ageable)) return;
        Ageable ageable = (Ageable) block.getBlockData();
        ageable.setAge(age);
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    data.setData((byte) age);
    state.update(true);
}
 
Example 6
/**
 * Sets the type of any block that can be colored.
 *
 * @param block the block to color.
 * @param color the color to use.
 * @return true if the block can be colored, otherwise false.
 */
public static boolean setColor(Block block, DyeColor color) {
    if (XMaterial.ISFLAT) {
        String type = block.getType().name();
        if (type.endsWith("WOOL")) block.setType(Material.getMaterial(color.name() + "_WOOL"));
        else if (type.endsWith("BED")) block.setType(Material.getMaterial(color.name() + "_BED"));
        else if (type.endsWith("STAINED_GLASS"))
            block.setType(Material.getMaterial(color.name() + "_STAINED_GLASS"));
        else if (type.endsWith("STAINED_GLASS_PANE"))
            block.setType(Material.getMaterial(color.name() + "_STAINED_GLASS_PANE"));
        else if (type.endsWith("TERRACOTTA")) block.setType(Material.getMaterial(color.name() + "_TERRACOTTA"));
        else if (type.endsWith("GLAZED_TERRACOTTA"))
            block.setType(Material.getMaterial(color.name() + "_GLAZED_TERRACOTTA"));
        else if (type.endsWith("BANNER")) block.setType(Material.getMaterial(color.name() + "_BANNER"));
        else if (type.endsWith("WALL_BANNER")) block.setType(Material.getMaterial(color.name() + "_WALL_BANNER"));
        else if (type.endsWith("CARPET")) block.setType(Material.getMaterial(color.name() + "_CARPET"));
        else if (type.endsWith("SHULKER_BOX")) block.setType(Material.getMaterial(color.name() + "_SHULKERBOX"));
        else if (type.endsWith("CONCRETE")) block.setType(Material.getMaterial(color.name() + "_CONCRETE"));
        else if (type.endsWith("CONCRETE_POWDER"))
            block.setType(Material.getMaterial(color.name() + "_CONCRETE_POWDER"));
        else return false;
        return true;
    }

    BlockState state = block.getState();
    state.setRawData(color.getWoolData());
    state.update(true);
    return false;
}
 
Example 7
public static boolean setWooden(Block block, XMaterial species) {
    block.setType(species.parseMaterial());
    if (XMaterial.ISFLAT) return true;

    TreeSpecies type = species == XMaterial.SPRUCE_LOG ? TreeSpecies.REDWOOD :
            TreeSpecies.valueOf(species.name().substring(0, species.name().indexOf('_')));

    BlockState state = block.getState();
    MaterialData data = state.getData();
    ((Wood) data).setSpecies(type);
    state.update(true);
    return true;
}
 
Example 8
public void toggleDoor(Block b) {
    BlockState state = b.getState();
    if (state instanceof Door) {
        Door op = (Door) state.getData();
        op.setOpen(!op.isOpen());
        state.setData(op);
        state.update();
    }
}
 
Example 9
Source Project: XSeries   File: XBlock.java    License: MIT License 5 votes vote down vote up
public static void setAge(Block block, int age) {
    if (ISFLAT) {
        if (!(block.getBlockData() instanceof Ageable)) return;
        Ageable ageable = (Ageable) block.getBlockData();
        ageable.setAge(age);
    }

    BlockState state = block.getState();
    MaterialData data = state.getData();
    data.setData((byte) age);
    state.update(true);
}
 
Example 10
/**
 * This will store the given {@link String} inside the NBT data of the given {@link Block}
 * 
 * @param b
 *            The {@link Block} in which to store the given value
 * @param value
 *            The value to store
 */
public void setBlockData(Block b, String value) {
    BlockState state = b.getState();

    if (state instanceof TileState) {
        setString((TileState) state, namespacedKey, value);
        state.update();
    }
}
 
Example 11
Source Project: XSeries   File: XBlock.java    License: MIT License 5 votes vote down vote up
public static boolean setWooden(Block block, XMaterial species) {
    block.setType(species.parseMaterial());
    if (ISFLAT) return true;

    TreeSpecies type = species == XMaterial.SPRUCE_LOG ? TreeSpecies.REDWOOD :
            TreeSpecies.valueOf(species.name().substring(0, species.name().indexOf('_')));

    BlockState state = block.getState();
    MaterialData data = state.getData();
    ((Wood) data).setSpecies(type);
    state.update(true);
    return true;
}
 
Example 12
public void toggleDoor(Block b) {
    BlockState state = b.getState();
    if (state instanceof Door) {
        Door op = (Door) state.getData();
        if (!op.isOpen())
            op.setOpen(true);
        else
            op.setOpen(false);
        state.setData(op);
        state.update();
    }
}
 
Example 13
@Override
public void setFacing(Block block, BlockFace facing) {
    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (!(data instanceof Directional)) {
        throw new IllegalArgumentException("Block is not Directional");
    }
    ((Directional) data).setFacingDirection(facing);
    state.setData(data);
    state.update();
}
 
Example 14
public void toggleDoor(Block b) {
    BlockState state = b.getState();
    if (state instanceof Door) {
        Door op = (Door) state.getData();
        if (!op.isOpen())
            op.setOpen(true);
        else
            op.setOpen(false);
        state.setData(op);
        state.update();
    }
}
 
Example 15
@SuppressWarnings("deprecation")
private void fall(long pos, @Nullable ParticipantState breaker) {
  // Block must be removed BEFORE spawning the FallingBlock, or it will not appear on the client
  // https://bugs.mojang.com/browse/MC-72248
  Block block = blockAt(match.getWorld(), pos);
  BlockState oldState = block.getState();
  block.setType(Material.AIR, false);
  FallingBlock fallingBlock =
      block
          .getWorld()
          .spawnFallingBlock(block.getLocation(), oldState.getType(), oldState.getRawData());

  BlockFallEvent event = new BlockFallEvent(block, fallingBlock);
  match.callEvent(
      breaker == null
          ? new BlockTransformEvent(event, block, Material.AIR)
          : new ParticipantBlockTransformEvent(event, block, Material.AIR, breaker));

  if (event.isCancelled()) {
    fallingBlock.remove();
    oldState.update(true, false); // Restore the old block if the fall is cancelled
  } else {
    block.setType(
        Material.AIR,
        true); // This is already air, but physics have not been applied yet, so do that now
  }
}
 
Example 16
public static boolean handleBlockFormEvent(World world, BlockPos pos, IBlockState block, @Nullable Entity entity) {
    BlockState blockState = world.getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ()).getState();
    blockState.setType(CraftMagicNumbers.getMaterial(block.getBlock()));
    blockState.setRawData((byte) block.getBlock().getMetaFromState(block));

    BlockFormEvent event = (entity == null) ? new BlockFormEvent(blockState.getBlock(), blockState) : new EntityBlockFormEvent(entity.getBukkitEntity(), blockState.getBlock(), blockState);
    world.getServer().getPluginManager().callEvent(event);

    if (!event.isCancelled()) {
        blockState.update(true);
    }

    return !event.isCancelled();
}
 
Example 17
public static void handleBlockSpreadEvent(Block block, Block source, net.minecraft.block.Block type, int data) {
    BlockState state = block.getState();
    state.setTypeId(net.minecraft.block.Block.getIdFromBlock(type));
    state.setRawData((byte) data);

    BlockSpreadEvent event = new BlockSpreadEvent(block, source, state);
    Bukkit.getPluginManager().callEvent(event);

    if (!event.isCancelled()) {
        state.update(true);
    }
}
 
Example 18
private void replaceBlock(BlockDrops drops, Block block, MatchPlayer player) {
    if(drops.replacement != null) {
        EntityChangeBlockEvent event = new EntityChangeBlockEvent(player.getBukkit(), block, drops.replacement);
        getMatch().callEvent(event);

        if(!event.isCancelled()) {
            BlockState state = block.getState();
            state.setType(drops.replacement.getItemType());
            state.setData(drops.replacement);
            state.update(true, true);
        }
    }
}
 
Example 19
@SuppressWarnings("deprecation")
@Override
public void a(World world, int i, int j, int k, Random random) {
	if(world.getGameRules().getBoolean("doFireTick")) {
		boolean flag = world.getType(i, j - 1, k) == Blocks.NETHERRACK;
		if(((world.worldProvider instanceof WorldProviderTheEnd)) && (world.getType(i, j - 1, k) == Blocks.BEDROCK)) {
			flag = true;
		}
		if(!canPlace(world, i, j, k)) {
			fireExtinguished(world, i, j, k);
		}
		if((!flag) && (world.Q()) && ((world.isRainingAt(i, j, k)) || (world.isRainingAt(i - 1, j, k)) || (world.isRainingAt(i + 1, j, k)) || (world.isRainingAt(i, j, k - 1)) || (world.isRainingAt(i, j, k + 1)))) {
			fireExtinguished(world, i, j, k);
		}
		else {
			int l = world.getData(i, j, k);
			if(l < 15) {
				world.setData(i, j, k, l + random.nextInt(3) / 2, 4);
			}
			world.a(i, j, k, this, a(world) + random.nextInt(10));
			if((!flag) && (!e(world, i, j, k))) {
				if((!World.a(world, i, j - 1, k)) || (l > 3)) {
					fireExtinguished(world, i, j, k);
				}
			}
			else if((!flag) && (!e(world, i, j - 1, k)) && (l == 15) && (random.nextInt(4) == 0)) {
				fireExtinguished(world, i, j, k);
			}
			else {
				boolean flag1 = world.z(i, j, k);
				byte b0 = 0;
				if(flag1) {
					b0 = -50;
				}
				a(world, i + 1, j, k, 300 + b0, random, l);
				a(world, i - 1, j, k, 300 + b0, random, l);
				a(world, i, j - 1, k, 250 + b0, random, l);
				a(world, i, j + 1, k, 250 + b0, random, l);
				a(world, i, j, k - 1, 300 + b0, random, l);
				a(world, i, j, k + 1, 300 + b0, random, l);
				for(int i1 = i - 1; i1 <= i + 1; i1++) {
					for(int j1 = k - 1; j1 <= k + 1; j1++) {
						for(int k1 = j - 1; k1 <= j + 4; k1++) {
							if((i1 != i) || (k1 != j) || (j1 != k)) {
								int l1 = 100;
								if(k1 > j + 1) {
									l1 += (k1 - (j + 1)) * 100;
								}
								int i2 = m(world, i1, k1, j1);
								if(i2 > 0) {
									int j2 = (i2 + 40 + world.difficulty.a() * 7) / (l + 30);
									if(flag1) {
										j2 /= 2;
									}
									if((j2 > 0) && (random.nextInt(l1) <= j2) && ((!world.Q()) || (!world.isRainingAt(i1, k1, j1))) && (!world.isRainingAt(i1 - 1, k1, k)) && (!world.isRainingAt(i1 + 1, k1, j1)) && (!world.isRainingAt(i1, k1, j1 - 1)) && (!world.isRainingAt(i1, k1, j1 + 1))) {
										int k2 = l + random.nextInt(5) / 4;
										if(k2 > 15) {
											k2 = 15;
										}
										if((world.getType(i1, k1, j1) != this) && (!CraftEventFactory.callBlockIgniteEvent(world, i1, k1, j1, i, j, k).isCancelled())) {
											Server server = world.getServer();
											org.bukkit.World bworld = world.getWorld();
											BlockState blockState = bworld.getBlockAt(i1, k1, j1).getState();
											blockState.setTypeId(Block.getId(this));
											blockState.setData(new MaterialData(Block.getId(this), (byte) k2));

											BlockSpreadEvent spreadEvent = new BlockSpreadEvent(blockState.getBlock(), bworld.getBlockAt(i, j, k), blockState);
											server.getPluginManager().callEvent(spreadEvent);
											if(!spreadEvent.isCancelled()) {
												blockState.update(true);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
 
Example 20
/**
 * 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.");
                }
            }));
}