Search in sources :

Example 6 with BlockDataManager

use of net.glowstone.block.data.BlockDataManager in project Glowstone by GlowstoneMC.

the class CompoundTag method tryGetMaterial.

/**
 * Reads a material from a string ID or numeric ID, depending on the tag type. Returns null if
 * the tag isn't present, its type is neither string nor any integral type, or its value isn't
 * a valid material ID.
 *
 * @param key the key to look up
 * @return the Material denoted by that key, if present and readable; an empty Optional otherwise
 */
public Optional<Material> tryGetMaterial(@NonNls String key) {
    if (!containsKey(key)) {
        return Optional.empty();
    }
    BlockDataManager blockDataManager = ((GlowServer) Bukkit.getServer()).getBlockDataManager();
    switch(value.get(key).getType()) {
        case STRING:
            @NonNls String id = getString(key);
            if (id.isEmpty()) {
                return Optional.empty();
            }
            if (!id.contains(":")) {
                // There is no namespace, so prepend the default minecraft: namespace
                id = "minecraft:" + id;
            }
            Material type = ItemIds.getBlock(id);
            if (type == null) {
                // Not a block, might be an item
                type = ItemIds.getItem(id);
            }
            return Optional.ofNullable(type);
        case INT:
            return Optional.of(blockDataManager.convertToBlockData(getInt(key)).getMaterial());
        case SHORT:
            return Optional.of(blockDataManager.convertToBlockData(getShort(key)).getMaterial());
        case BYTE:
            return Optional.of(blockDataManager.convertToBlockData(getByte(key)).getMaterial());
        default:
            return Optional.empty();
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Material(org.bukkit.Material) GlowServer(net.glowstone.GlowServer) BlockDataManager(net.glowstone.block.data.BlockDataManager)

Example 7 with BlockDataManager

use of net.glowstone.block.data.BlockDataManager in project Glowstone by GlowstoneMC.

the class StructureBuilder method setBlockDownward.

/**
 * Builds a 1x1 column out of the given block, replacing non-solid blocks starting at a given
 * location and proceeding downward until a solid block is reached.
 *
 * @param pos the highest point to possibly replace, relative to this structure's root
 *         point
 * @param type the block type to fill
 * @param data the block data
 */
public void setBlockDownward(Vector pos, Material type, MaterialData data) {
    Vector vec = translate(pos);
    BlockDataManager blockDataManager = ((GlowServer) Bukkit.getServer()).getBlockDataManager();
    if (boundingBox.isVectorInside(vec)) {
        int x = vec.getBlockX();
        int y = vec.getBlockY();
        int z = vec.getBlockZ();
        while (!world.getBlockAt(x, y, z).getType().isSolid() && y > 1) {
            delegate.setTypeAndData(world, x, y, z, type, blockDataManager.createBlockData(type));
            y--;
        }
    }
}
Also used : GlowServer(net.glowstone.GlowServer) Vector(org.bukkit.util.Vector) BlockDataManager(net.glowstone.block.data.BlockDataManager)

Example 8 with BlockDataManager

use of net.glowstone.block.data.BlockDataManager in project Glowstone by GlowstoneMC.

the class NbtSerialization method readItem.

/**
 * Read an item stack in from an NBT tag.
 *
 * <p>Returns null if no item exists.
 *
 * @param tag The tag to read from.
 * @return The resulting ItemStack, or null.
 */
public static ItemStack readItem(CompoundTag tag) {
    BlockDataManager blockDataManager = ((GlowServer) Bukkit.getServer()).getBlockDataManager();
    final Material[] material = { null };
    if ((!tag.readString("id", id -> material[0] = ItemIds.getItem(id)) && !tag.readShort("id", id -> material[0] = blockDataManager.convertToBlockData(id).getMaterial())) || material[0] == null || material[0] == Material.AIR) {
        return null;
    }
    final byte[] count = { 0 };
    tag.readByte("Count", x -> count[0] = x);
    if (count[0] == 0) {
        return null;
    }
    final short[] damage = { 0 };
    tag.readShort("Damage", x -> damage[0] = x);
    ItemStack stack = new ItemStack(material[0], count[0], damage[0]);
    // This is slightly different than what tag.readItem would do, since we specify the
    // material separately.
    tag.readCompound("tag", subtag -> stack.setItemMeta(GlowItemFactory.instance().readNbt(material[0], subtag)));
    return stack;
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) Arrays(java.util.Arrays) NamespacedKey(org.bukkit.NamespacedKey) BlockData(org.bukkit.block.data.BlockData) BlockDataManager(net.glowstone.block.data.BlockDataManager) UUID(java.util.UUID) GlowItemFactory(net.glowstone.inventory.GlowItemFactory) InventoryUtil(net.glowstone.util.InventoryUtil) ItemStack(org.bukkit.inventory.ItemStack) ArrayList(java.util.ArrayList) ItemIds(net.glowstone.constants.ItemIds) Vector(org.bukkit.util.Vector) List(java.util.List) Location(org.bukkit.Location) World(org.bukkit.World) Optional(java.util.Optional) GlowServer(net.glowstone.GlowServer) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) Material(org.bukkit.Material) GlowServer(net.glowstone.GlowServer) ItemStack(org.bukkit.inventory.ItemStack) BlockDataManager(net.glowstone.block.data.BlockDataManager)

Aggregations

GlowServer (net.glowstone.GlowServer)8 BlockDataManager (net.glowstone.block.data.BlockDataManager)8 Vector (org.bukkit.util.Vector)5 GlowChunk (net.glowstone.chunk.GlowChunk)2 BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)2 Material (org.bukkit.Material)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 StatefulBlockData (net.glowstone.block.data.states.StatefulBlockData)1 ItemIds (net.glowstone.constants.ItemIds)1 GlowItemFactory (net.glowstone.inventory.GlowItemFactory)1 InventoryUtil (net.glowstone.util.InventoryUtil)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 Bukkit (org.bukkit.Bukkit)1 Location (org.bukkit.Location)1 NamespacedKey (org.bukkit.NamespacedKey)1 World (org.bukkit.World)1