Java Code Examples for org.bukkit.block.Block#setTypeIdAndData()

The following examples show how to use org.bukkit.block.Block#setTypeIdAndData() . 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 check out the related API usage on the sidebar.
Example 1
Source File: FallingBlocksMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return the number of unsupported blocks connected to any blocks neighboring the given location.
 * An air block is placed there temporarily if it is not already air. The search may bail out
 * early when the count is >= the given limit, though this cannot be guaranteed.
 */
public int countUnsupportedNeighbors(Block block, int limit) {
  BlockState state = null;
  if (block.getType() != Material.AIR) {
    state = block.getState();
    block.setTypeIdAndData(0, (byte) 0, false);
  }

  int count = countUnsupportedNeighbors(encodePos(block), limit);

  if (state != null) {
    block.setTypeIdAndData(state.getTypeId(), state.getRawData(), false);
  }

  return count;
}
 
Example 2
Source File: FallingBlocksMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return the number of unsupported blocks connected to any blocks neighboring the given location.
 * An air block is placed there temporarily if it is not already air. The search may bail out early
 * when the count is >= the given limit, though this cannot be guaranteed.
 */
public int countUnsupportedNeighbors(Block block, int limit) {
    BlockState state = null;
    if(block.getType() != Material.AIR) {
        state = block.getState();
        block.setTypeIdAndData(0, (byte) 0, false);
    }

    int count = countUnsupportedNeighbors(encodePos(block), limit);

    if(state != null) {
        block.setTypeIdAndData(state.getTypeId(), state.getRawData(), false);
    }

    return count;
}
 
Example 3
Source File: Destroyable.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void replaceBlocks(MaterialData newMaterial) {
  // Calling this method causes all non-destroyed blocks to be replaced, and the world
  // list to be replaced with one containing only the new block. If called on a multi-stage
  // destroyable, i.e. one which is affected by block replacement rules, it effectively ceases
  // to be multi-stage. Even if there are block replacement rules for the new block, the
  // replacements will not be in the world list, and so those blocks will be considered
  // destroyed the first time they are mined. This can have some strange effects on the health
  // of the destroyable: individual block health can only decrease, while the total health
  // percentage can only increase.

  for (Block block : this.getBlockRegion().getBlocks()) {
    BlockState oldState = block.getState();
    int oldHealth = this.getBlockHealth(oldState);

    if (oldHealth > 0) {
      block.setTypeIdAndData(newMaterial.getItemTypeId(), newMaterial.getData(), true);
    }
  }

  // Update the world list on switch
  this.materialPatterns.clear();
  this.materials.clear();
  addMaterials(new SingleMaterialMatcher(newMaterial));

  // If there is a block health map, get rid of it, since there is now only one world in the list
  this.blockMaterialHealth = null;

  this.recalculateHealth();
}
 
Example 4
Source File: Core.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public void replaceBlocks(MaterialData newMaterial) {
  for (Block block : this.getCasingRegion().getBlocks()) {
    if (this.isObjectiveMaterial(block)) {
      block.setTypeIdAndData(newMaterial.getItemTypeId(), newMaterial.getData(), true);
    }
  }
  this.material = newMaterial;
}
 
Example 5
Source File: CraftBlockState.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public boolean update(boolean force, boolean applyPhysics) {
    if (!isPlaced()) {
        return true;
    }
    Block block = getBlock();

    if (block.getType() != getType()) {
        if (!force) {
            return false;
        }
    }

    BlockPos pos = new BlockPos(x, y, z);
    IBlockState newBlock = CraftMagicNumbers.getBlock(getType()).getStateFromMeta(this.getRawData());
    block.setTypeIdAndData(getTypeId(), getRawData(), applyPhysics);
    world.getHandle().notifyBlockUpdate(
            pos,
            CraftMagicNumbers.getBlock(block).getStateFromMeta(block.getData()),
            newBlock,
            3
    );

    // Update levers etc
    if (applyPhysics && getData() instanceof Attachable) {
        world.getHandle().notifyNeighborsOfStateChange(pos.offset(CraftBlock.blockFaceToNotch(((Attachable) getData()).getAttachedFace())), newBlock.getBlock(), false);
    }

    if (nbt != null) {
        TileEntity te = world.getHandle().getTileEntity(new BlockPos(this.x, this.y, this.z));
        if (te != null) {
            te.readFromNBT(nbt);
        }
    }
    return true;
}
 
Example 6
Source File: Destroyable.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void replaceBlocks(MaterialData newMaterial) {
    // Calling this method causes all non-destroyed blocks to be replaced, and the material
    // list to be replaced with one containing only the new block. If called on a multi-stage
    // destroyable, i.e. one which is affected by block replacement rules, it effectively ceases
    // to be multi-stage. Even if there are block replacement rules for the new block, the
    // replacements will not be in the material list, and so those blocks will be considered
    // destroyed the first time they are mined. This can have some strange effects on the health
    // of the destroyable: individual block health can only decrease, while the total health
    // percentage can only increase.
    double oldCompletion = getCompletion();

    for (Block block : this.getBlockRegion().getBlocks(match.getWorld())) {
        BlockState oldState = block.getState();
        int oldHealth = this.getBlockHealth(oldState);

        if (oldHealth > 0) {
            block.setTypeIdAndData(newMaterial.getItemTypeId(), newMaterial.getData(), true);
        }
    }

    // Update the materials list on switch
    this.materialPatterns.clear();
    this.materials.clear();
    addMaterials(new MaterialPattern(newMaterial));

    // If there is a block health map, get rid of it, since there is now only one material in the list
    this.blockMaterialHealth = null;

    this.recalculateHealth();

    if(oldCompletion != getCompletion()) {
        match.callEvent(new DestroyableHealthChangeEvent(match, this, null));
    }
}
 
Example 7
Source File: Core.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public void replaceBlocks(MaterialData newMaterial) {
    for(Block block : this.getCasingRegion().getBlocks(match.getWorld())) {
        if(this.isObjectiveMaterial(block)) {
            block.setTypeIdAndData(newMaterial.getItemTypeId(), newMaterial.getData(), true);
        }
    }
    this.material = newMaterial;
}
 
Example 8
Source File: BukkitWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setBlock(Vector position, BaseBlock block, boolean notifyAndLight) throws WorldEditException {
    BukkitImplAdapter adapter = BukkitQueue_0.getAdapter();
    if (adapter != null) {
        return adapter.setBlock(adapt(getWorld(), position), block, notifyAndLight);
    } else {
        Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
        return bukkitBlock.setTypeIdAndData(block.getType(), (byte) block.getData(), notifyAndLight);
    }
}
 
Example 9
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void setBlockSuperFast(Block b, int blockId, byte data, boolean applyPhysics) {
    net.minecraft.server.v1_7_R4.World w = ((CraftWorld) b.getWorld()).getHandle();
    net.minecraft.server.v1_7_R4.Chunk chunk = w.getChunkAt(b.getX() >> 4, b.getZ() >> 4);
    try {
        Field f = chunk.getClass().getDeclaredField("sections");
        f.setAccessible(true);
        ChunkSection[] sections = (ChunkSection[]) f.get(chunk);
        ChunkSection chunksection = sections[b.getY() >> 4];

        if (chunksection == null) {
            chunksection = sections[b.getY() >> 4] = new ChunkSection(b.getY() >> 4 << 4, !chunk.world.worldProvider.f);
        }
        net.minecraft.server.v1_7_R4.Block mb = net.minecraft.server.v1_7_R4.Block.getById(blockId);
        chunksection.setTypeId(b.getX() & 15, b.getY() & 15, b.getZ() & 15, mb);
        chunksection.setData(b.getX() & 15, b.getY() & 15, b.getZ() & 15, data);
        if (applyPhysics) {
            w.update(b.getX(), b.getY(), b.getZ(), mb);
        }
    } catch (Exception e) {
        //Bukkit.getLogger().info("Error");
        b.setTypeIdAndData(blockId, data, applyPhysics);
    }


}
 
Example 10
Source File: MonumentModes.java    From CardinalPGM with MIT License 4 votes vote down vote up
public void changeBlock(Block block) {
    block.setTypeIdAndData(this.material.getLeft().getId(), (byte) (int) this.material.getRight(), true);
}
 
Example 11
Source File: BlockDropsMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This is not an event handler. It is called explicitly by BlockTransformListener after all event
 * handlers have been called.
 */
@SuppressWarnings("deprecation")
public void doBlockDrops(final BlockTransformEvent event) {
  if (!causesDrops(event.getCause())) {
    return;
  }

  final BlockDrops drops = event.getDrops();
  if (drops != null) {
    event.setCancelled(true);
    final BlockState oldState = event.getOldState();
    final BlockState newState = event.getNewState();
    final Block block = event.getOldState().getBlock();
    final int newTypeId = newState.getTypeId();
    final byte newData = newState.getRawData();

    block.setTypeIdAndData(newTypeId, newData, true);

    if (event.getCause() instanceof EntityExplodeEvent) {
      EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
      final float yield = explodeEvent.getYield();

      if (drops.fallChance != null
          && oldState.getType().isBlock()
          && oldState.getType() != Material.AIR
          && match.getRandom().nextFloat() < drops.fallChance) {

        FallingBlock fallingBlock =
            match
                .getWorld()
                .spawnFallingBlock(
                    block.getLocation(),
                    event.getOldState().getType(),
                    event.getOldState().getRawData());
        fallingBlock.setDropItem(false);

        if (drops.landChance != null && match.getRandom().nextFloat() >= drops.landChance) {
          this.fallingBlocksThatWillNotLand.add(fallingBlock);
        }

        Vector v = fallingBlock.getLocation().subtract(explodeEvent.getLocation()).toVector();
        double distance = v.length();
        v.normalize().multiply(BASE_FALL_SPEED * drops.fallSpeed / Math.max(1d, distance));

        // A very simple deflection model. Check for a solid
        // neighbor block and "bounce" the velocity off of it.
        Block west = block.getRelative(BlockFace.WEST);
        Block east = block.getRelative(BlockFace.EAST);
        Block down = block.getRelative(BlockFace.DOWN);
        Block up = block.getRelative(BlockFace.UP);
        Block north = block.getRelative(BlockFace.NORTH);
        Block south = block.getRelative(BlockFace.SOUTH);

        if ((v.getX() < 0 && west != null && west.getType().isSolid())
            || v.getX() > 0 && east != null && east.getType().isSolid()) {
          v.setX(-v.getX());
        }

        if ((v.getY() < 0 && down != null && down.getType().isSolid())
            || v.getY() > 0 && up != null && up.getType().isSolid()) {
          v.setY(-v.getY());
        }

        if ((v.getZ() < 0 && north != null && north.getType().isSolid())
            || v.getZ() > 0 && south != null && south.getType().isSolid()) {
          v.setZ(-v.getZ());
        }

        fallingBlock.setVelocity(v);
      }

      // Defer item drops so the explosion doesn't destroy them
      match
          .getExecutor(MatchScope.RUNNING)
          .execute(() -> dropItems(drops, newState.getLocation(), yield));
    } else {
      MatchPlayer player = ParticipantBlockTransformEvent.getParticipant(event);
      if (player == null
          || player.getBukkit().getGameMode()
              != GameMode.CREATIVE) { // Don't drop items in creative mode
        dropItems(drops, newState.getLocation(), 1d);
        dropExperience(drops, newState.getLocation());
      }
    }
  }
}
 
Example 12
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void setFlowerPotBlock(Block block, ItemStack itemStack) {
    block.setTypeIdAndData(itemStack.getTypeId(), itemStack.getData().getData(), false);

}
 
Example 13
Source File: NMSHandler.java    From askyblock with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void setBlockSuperFast(Block b, int blockId, byte data, boolean applyPhysics) {
    b.setTypeIdAndData(blockId, data, applyPhysics);
}
 
Example 14
Source File: ItemManager.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void setTypeIdAndData(Block block, int type, int data, boolean update) {
	block.setTypeIdAndData(type, (byte)data, update);
}
 
Example 15
Source File: BlockAdapterMagicValues.java    From DungeonsXL with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setBlockWoolColor(Block block, Color color) {
    block.setTypeIdAndData(Material.WOOL.getId(), color.getDyeColor().getWoolData(), false);
}
 
Example 16
Source File: BukkitChunk_All.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
public void setBlock(Block block, int id, byte data) {
    block.setTypeIdAndData(id, data, false);
}
 
Example 17
Source File: BlockDropsMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This is not an event handler. It is called explicitly by BlockTransformListener
 * after all event handlers have been called.
 */
@SuppressWarnings("deprecation")
public void doBlockDrops(final BlockTransformEvent event) {
    if(!causesDrops(event.getCause())) {
        return;
    }

    final BlockDrops drops = event.getDrops();
    if(drops != null) {
        event.setCancelled(true);
        final BlockState oldState = event.getOldState();
        final BlockState newState = event.getNewState();
        final Block block = event.getOldState().getBlock();
        final int newTypeId = newState.getTypeId();
        final byte newData = newState.getRawData();

        block.setTypeIdAndData(newTypeId, newData, true);

        boolean explosion = false;
        MatchPlayer player = ParticipantBlockTransformEvent.getParticipant(event);

        if(event.getCause() instanceof EntityExplodeEvent) {
            EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
            explosion = true;

            if(drops.fallChance != null &&
               oldState.getType().isBlock() &&
               oldState.getType() != Material.AIR &&
               this.getMatch().getRandom().nextFloat() < drops.fallChance) {

                FallingBlock fallingBlock = event.getOldState().spawnFallingBlock();
                fallingBlock.setDropItem(false);

                if(drops.landChance != null && this.getMatch().getRandom().nextFloat() >= drops.landChance) {
                    this.fallingBlocksThatWillNotLand.add(fallingBlock);
                }

                Vector v = fallingBlock.getLocation().subtract(explodeEvent.getLocation()).toVector();
                double distance = v.length();
                v.normalize().multiply(BASE_FALL_SPEED * drops.fallSpeed / Math.max(1d, distance));

                // A very simple deflection model. Check for a solid
                // neighbor block and "bounce" the velocity off of it.
                Block west = block.getRelative(BlockFace.WEST);
                Block east = block.getRelative(BlockFace.EAST);
                Block down = block.getRelative(BlockFace.DOWN);
                Block up = block.getRelative(BlockFace.UP);
                Block north = block.getRelative(BlockFace.NORTH);
                Block south = block.getRelative(BlockFace.SOUTH);

                if((v.getX() < 0 && west != null && Materials.isColliding(west.getType())) ||
                    v.getX() > 0 && east != null && Materials.isColliding(east.getType())) {
                    v.setX(-v.getX());
                }

                if((v.getY() < 0 && down != null && Materials.isColliding(down.getType())) ||
                    v.getY() > 0 && up != null && Materials.isColliding(up.getType())) {
                    v.setY(-v.getY());
                }

                if((v.getZ() < 0 && north != null && Materials.isColliding(north.getType())) ||
                    v.getZ() > 0 && south != null && Materials.isColliding(south.getType())) {
                    v.setZ(-v.getZ());
                }

                fallingBlock.setVelocity(v);
            }
        }

        dropObjects(drops, player, newState.getLocation(), 1d, explosion);

    }
}
 
Example 18
Source File: BlockImage.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
public void restore(Block block) {
  int offset = this.offset(block.getLocation().toVector().toBlockVector());
  block.setTypeIdAndData(this.blockIds[offset], this.blockData[offset], true);
}