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();
}
}
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--;
}
}
}
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;
}
Aggregations