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

The following are Jave code examples for showing how to use update() of the org.bukkit.block.BlockState class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: HCFCore   File: DeathSignListener.java   Source Code and License Vote up 7 votes
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event) {
    Block block = event.getBlock();
    if (isDeathSign(block)) {
        BlockState state = block.getState();
        Sign sign = (Sign) state;
        ItemStack stack = new ItemStack(Material.SIGN, 1);
        ItemMeta meta = stack.getItemMeta();
        meta.setDisplayName(DEATH_SIGN_ITEM_NAME);
        meta.setLore(Arrays.asList(sign.getLines()));
        stack.setItemMeta(meta);

        Player player = event.getPlayer();
        World world = player.getWorld();
        if (player.getGameMode() != GameMode.CREATIVE && world.isGameRule("doTileDrops")) {
            world.dropItemNaturally(block.getLocation(), stack);
        }

        // Manually handle the dropping
        event.setCancelled(true);
        block.setType(Material.AIR);
        state.update();
    }
}
 
Example 2
Project: ProjectAres   File: Renewable.java   Source Code and License Vote up 6 votes
boolean renew(BlockVector pos, MaterialData material) {
    // We need to do the entity check here rather than canRenew, because we are not
    // notified when entities move in our out of the way.
    if(!isClearOfEntities(pos)) return false;

    Location location = pos.toLocation(match.getWorld());
    Block block = location.getBlock();
    BlockState newState = location.getBlock().getState();
    newState.setMaterialData(material);

    BlockRenewEvent event = new BlockRenewEvent(block, newState, this);
    match.callEvent(event); // Our own handler will get this and remove the block from the pool
    if(event.isCancelled()) return false;

    newState.update(true, true);

    if(definition.particles) {
        NMSHacks.playBlockBreakEffect(match.getWorld(), pos, material.getItemType());
    }

    if(definition.sound) {
        NMSHacks.playBlockPlaceSound(match.getWorld(), pos, material.getItemType(), 1f);
    }

    return true;
}
 
Example 3
Project: ProjectAres   File: FallingBlocksMatchModule.java   Source Code and License Vote up 6 votes
@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(this.getMatch().getWorld(), pos);
    BlockState oldState = block.getState();
    block.setType(Material.AIR, false);
    FallingBlock fallingBlock = oldState.spawnFallingBlock();

    BlockFallEvent event = new BlockFallEvent(block, fallingBlock);
    getMatch().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 {
        // This is already air, but physics have not been applied yet, so do that now
        block.simulateChangeForNeighbors(oldState.getMaterialData(), new MaterialData(Material.AIR));
    }
}
 
Example 4
Project: HCFCore   File: EventSignListener.java   Source Code and License Vote up 6 votes
@EventHandler(ignoreCancelled=true, priority=EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event)
{
  Block block = event.getBlock();
  if (isEventSign(block))
  {
    BlockState state = block.getState();
    Sign sign = (Sign)state;
    ItemStack stack = new ItemStack(Material.SIGN, 1);
    ItemMeta meta = stack.getItemMeta();
    meta.setDisplayName(EVENT_SIGN_ITEM_NAME);
    meta.setLore(Arrays.asList(sign.getLines()));
    stack.setItemMeta(meta);
    
    Player player = event.getPlayer();
    World world = player.getWorld();
    Location blockLocation = block.getLocation();
    if ((player.getGameMode() != GameMode.CREATIVE) && (world.isGameRule("doTileDrops"))) {
      world.dropItemNaturally(blockLocation, stack);
    }
    event.setCancelled(true);
    block.setType(Material.AIR);
    state.update();
  }
}
 
Example 5
Project: HCFCore   File: EventSignListener.java   Source Code and License Vote up 6 votes
@EventHandler(ignoreCancelled=true, priority=EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event)
{
  Block block = event.getBlock();
  if (isEventSign(block))
  {
    BlockState state = block.getState();
    Sign sign = (Sign)state;
    ItemStack stack = new ItemStack(Material.SIGN, 1);
    ItemMeta meta = stack.getItemMeta();
    meta.setDisplayName(EVENT_SIGN_ITEM_NAME);
    meta.setLore(Arrays.asList(sign.getLines()));
    stack.setItemMeta(meta);
    
    Player player = event.getPlayer();
    World world = player.getWorld();
    Location blockLocation = block.getLocation();
    if ((player.getGameMode() != GameMode.CREATIVE) && (world.isGameRule("doTileDrops"))) {
      world.dropItemNaturally(blockLocation, stack);
    }
    event.setCancelled(true);
    block.setType(Material.AIR);
    state.update();
  }
}
 
Example 6
Project: HCFCore   File: DeathSignListener.java   Source Code and License Vote up 6 votes
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event) {
    Block block = event.getBlock();
    if (isDeathSign(block)) {
        BlockState state = block.getState();
        Sign sign = (Sign) state;
        ItemStack stack = new ItemStack(Material.SIGN, 1);
        ItemMeta meta = stack.getItemMeta();
        meta.setDisplayName(DEATH_SIGN_ITEM_NAME);
        meta.setLore(Arrays.asList(sign.getLines()));
        stack.setItemMeta(meta);

        Player player = event.getPlayer();
        World world = player.getWorld();
        if (player.getGameMode() != GameMode.CREATIVE && world.isGameRule("doTileDrops")) {
            world.dropItemNaturally(block.getLocation(), stack);
        }

        // Manually handle the dropping
        event.setCancelled(true);
        block.setType(Material.AIR);
        state.update();
    }
}
 
Example 7
Project: Uranium   File: CraftEventFactory.java   Source Code and License Vote up 5 votes
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 8
Project: ProjectAres   File: BlockDropsMatchModule.java   Source Code and License Vote up 5 votes
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 9
Project: MT_Core   File: GeneratorListener.java   Source Code and License Vote up 5 votes
@EventHandler
public void onLeverOrButton(PlayerInteractEvent event) {
	Block clickedBlock = event.getClickedBlock();
	Player player = event.getPlayer();

	if (clickedBlock == null)
		return;

	String chunk = clickedBlock.getLocation().getChunk().getX() + ";"
			+ clickedBlock.getLocation().getChunk().getZ();
	if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
		return;
	if (!powerable.containsKey(clickedBlock.getWorld().getName()))
		return;
	if (powerable.get(clickedBlock.getWorld().getName()).getList(chunk).contains(clickedBlock.getLocation()))
		return;

	// We cancel; send smoke particles for button, and we just turn off the
	// lever (if it was, for some reason, on).
	if (clickedBlock.getType() == Material.STONE_BUTTON || clickedBlock.getType() == Material.WOOD_BUTTON) {

		clickedBlock.getWorld().spawnParticle(Particle.SMOKE_NORMAL, clickedBlock.getLocation().add(0.5, 1, 0.5), 7,
				0, 0.2, 0, 0.03);
		player.sendMessage(MortuusTerraCore.NOTI_PREFIX + ChatColor.RED + " There is no generator in range!");

	} else if (clickedBlock.getType() == Material.LEVER) {
		BlockState state = clickedBlock.getState();
		Lever lever = (Lever) state.getData();

		lever.setPowered(false);
		state.setData(lever);
		state.update();
		clickedBlock.getWorld().spawnParticle(Particle.SMOKE_NORMAL, clickedBlock.getLocation().add(0.5, 1, 0.5), 7,
				0, 0.2, 0, 0.03);
		player.sendMessage(MortuusTerraCore.NOTI_PREFIX + ChatColor.RED + " There is no generator in range!");
	}

}
 
Example 10
Project: Uranium   File: BlockStateListPopulator.java   Source Code and License Vote up 4 votes
public void updateList() {
    for (BlockState state : list) {
        state.update(true);
    }
}
 
Example 11
Project: KingdomFactions   File: BlockPlace.java   Source Code and License Vote up 4 votes
public void setSpawner(Block block, EntityType ent) {
	BlockState blockState = block.getState();
	CreatureSpawner spawner = ((CreatureSpawner) blockState);
	spawner.setSpawnedType(ent);
	blockState.update();
}
 
Example 12
Project: WebSandboxMC   File: BlockBridge.java   Source Code and License Vote up 4 votes
public void clientNewSign(ChannelHandlerContext ctx, int x, int y, int z, int face, String text) {
    if (!allowSigns) {
        webSocketServerThread.sendLine(ctx.channel(), "T,Writing on signs is not allowed");
        // TODO: revert on client
        return;
    }

    BlockFace blockFace;
    switch (face) {
        case 0: // west
            blockFace = BlockFace.WEST;
            x -= 1;
            break;
        case 1: // east
            blockFace = BlockFace.EAST;
            x += 1;
            break;
        default:
        case 2: // north
            blockFace = BlockFace.NORTH;
            z -= 1;
            break;
        case 3: // south
            blockFace = BlockFace.SOUTH;
            z += 1;
            break;
    }
    org.bukkit.material.Sign signDirection = new org.bukkit.material.Sign();
    signDirection.setFacingDirection(blockFace);

    Location location = toBukkitLocation(x, y, z);
    if (!withinSandboxRange(location)) {
        webSocketServerThread.log(Level.FINEST, "client tried to write a sign outside sandbox range");
        return;
    }

    // Create the sign
    Block block = location.getWorld().getBlockAt(location);
    /*
    block.setTypeIdAndData(Material.WALL_SIGN.getId(), data, applyPhysics);
    webSocketServerThread.log(Level.FINEST, "setting sign at "+location+" data="+data);
    */
    BlockState blockState = block.getState();
    blockState.setType(Material.WALL_SIGN);
    blockState.setData(signDirection);
    boolean force = true;
    boolean applyPhysics = false;
    blockState.update(force, applyPhysics);
    webSocketServerThread.log(Level.FINEST, "setting sign at "+location+" blockFace="+blockFace);

    // Set the sign text
    blockState = block.getState();
    if (!(blockState instanceof Sign)) {
        webSocketServerThread.log(Level.WARNING, "failed to place sign at "+location);
        return;
    }
    Sign sign = (Sign) blockState;

    // TODO: text lines by 15 characters into 5 lines
    sign.setLine(0, text);
    sign.update(force, applyPhysics);
    webSocketServerThread.log(Level.FINEST, "set sign text="+text+", signDirection="+signDirection+", blockFace="+blockFace+", block="+block+", face="+face);

    // SignChangeEvent not posted when signs created programmatically; notify web clients ourselves
    notifySignChange(location, block.getType(), block.getState(), sign.getLines());
}