com.sk89q.jnbt.ListTag Java Examples

The following examples show how to use com.sk89q.jnbt.ListTag. 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: Polygonal2DRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply(Map<String, Tag> tags, Polygonal2DRegion region) {
	List<BlockVector2D> points = region.getPoints();
	int minY = region.getMinimumY();
	int maxY = region.getMaximumY();
	
	List<ListTag> pointList = Lists.newArrayList();
	for (BlockVector2D vector : points) {
		List<IntTag> vectorList = Lists.newArrayList();
		vectorList.add(new IntTag(vector.getBlockX()));
		vectorList.add(new IntTag(vector.getBlockZ()));
		
		ListTag vectorListTag = new ListTag(IntTag.class, vectorList);
		pointList.add(vectorListTag);
	}
	
	ListTag pointListTag = new ListTag(ListTag.class, pointList);
	
	tags.put("points", pointListTag);
	tags.put("minY", new IntTag(minY));
	tags.put("maxY", new IntTag(maxY));
}
 
Example #2
Source File: CuboidRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CuboidRegion asRegion(Map<String, Tag> tags) {
	ListTag pos1Tag = (ListTag) tags.get("pos1");
	ListTag pos2Tag = (ListTag) tags.get("pos2");
	
	int pos1X = pos1Tag.getInt(0);
	int pos1Y = pos1Tag.getInt(1);
	int pos1Z = pos1Tag.getInt(2);
	
	int pos2X = pos2Tag.getInt(0);
	int pos2Y = pos2Tag.getInt(1);
	int pos2Z = pos2Tag.getInt(2);
	
	Vector pos1 = new Vector(pos1X, pos1Y, pos1Z);
	Vector pos2 = new Vector(pos2X, pos2Y, pos2Z);
	
	return new CuboidRegion(pos1, pos2);
}
 
Example #3
Source File: CuboidRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply(Map<String, Tag> tags, CuboidRegion region) {
	Vector pos1 = region.getPos1();
	Vector pos2 = region.getPos2();
	
	List<IntTag> pos1List = Lists.newArrayList();
	pos1List.add(new IntTag(pos1.getBlockX()));
	pos1List.add(new IntTag(pos1.getBlockY()));
	pos1List.add(new IntTag(pos1.getBlockZ()));
	
	ListTag pos1Tag = new ListTag(IntTag.class, pos1List);
	
	List<IntTag> pos2List = Lists.newArrayList();
	pos2List.add(new IntTag(pos2.getBlockX()));
	pos2List.add(new IntTag(pos2.getBlockY()));
	pos2List.add(new IntTag(pos2.getBlockZ()));
	
	ListTag pos2Tag = new ListTag(IntTag.class, pos2List);
	
	tags.put("pos1", pos1Tag);
	tags.put("pos2", pos2Tag);
}
 
Example #4
Source File: CylinderRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CylinderRegion asRegion(Map<String, Tag> tags) {
	ListTag centerTag = (ListTag) tags.get("center");
	ListTag radiusTag = (ListTag) tags.get("radius");
	
	int centerX = centerTag.getInt(0);
	int centerY = centerTag.getInt(1);
	int centerZ = centerTag.getInt(2);
	
	int pos2X = radiusTag.getInt(0);
	int pos2Z = radiusTag.getInt(1);
	
	Vector center = new Vector(centerX, centerY, centerZ);
	Vector2D radius = new Vector2D(pos2X, pos2Z);
	int minY = (int) tags.get("minY").getValue();
	int maxY = (int) tags.get("maxY").getValue();
	
	return new CylinderRegion(center, radius, minY, maxY);
}
 
Example #5
Source File: CylinderRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void apply(Map<String, Tag> tags, CylinderRegion region) {
	Vector center = region.getCenter();
	Vector2D radius = region.getRadius();
	int minY = region.getMinimumY();
	int maxY = region.getMaximumY();
	
	List<IntTag> centerList = Lists.newArrayList();
	centerList.add(new IntTag(center.getBlockX()));
	centerList.add(new IntTag(center.getBlockY()));
	centerList.add(new IntTag(center.getBlockZ()));
	
	ListTag centerTag = new ListTag(IntTag.class, centerList);
	
	List<IntTag> radiusList = Lists.newArrayList();
	radiusList.add(new IntTag(radius.getBlockX()));
	radiusList.add(new IntTag(radius.getBlockZ()));
	
	ListTag radiusTag = new ListTag(IntTag.class, radiusList);
	
	tags.put("center", centerTag);
	tags.put("radius", radiusTag);
	tags.put("minY", new IntTag(minY));
	tags.put("maxY", new IntTag(maxY));
}
 
Example #6
Source File: FastWorldEditExtent.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Entity createEntity(final Location loc, final BaseEntity entity) {
    if (entity != null) {
        CompoundTag tag = entity.getNbtData();
        Map<String, Tag> map = ReflectionUtils.getMap(tag.getValue());
        map.put("Id", new StringTag(entity.getTypeId()));
        ListTag pos = (ListTag) map.get("Pos");
        if (pos != null) {
            List<Tag> posList = ReflectionUtils.getList(pos.getValue());
            posList.set(0, new DoubleTag(loc.getX()));
            posList.set(1, new DoubleTag(loc.getY()));
            posList.set(2, new DoubleTag(loc.getZ()));
        }
        queue.setEntity(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), tag);
    }
    return null;
}
 
Example #7
Source File: MCAWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean clearContainerBlockContents(Vector position) {
    BaseBlock block = extent.getLazyBlock(position);
    if (block.hasNbtData()) {
        Map<String, Tag> nbt = ReflectionUtils.getMap(block.getNbtData().getValue());
        if (nbt.containsKey("Items")) {
            nbt.put("Items", new ListTag(CompoundTag.class, new ArrayList<CompoundTag>()));
            try {
                extent.setBlock(position, block);
            } catch (WorldEditException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}
 
Example #8
Source File: Polygonal2DRegionSchematicCodec.java    From HeavySpleef with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Polygonal2DRegion asRegion(Map<String, Tag> tags) {
	ListTag pointsListTag = (ListTag) tags.get("points");
	List<Tag> pointList = pointsListTag.getValue();
	List<BlockVector2D> points = Lists.newArrayList();
	
	for (Tag vectorTag : pointList) {
		if (!(vectorTag instanceof ListTag)) {
			continue;
		}
		
		ListTag vectorListTag = (ListTag) vectorTag;
		int x = vectorListTag.getInt(0);
		int z = vectorListTag.getInt(1);
		
		BlockVector2D vector = new BlockVector2D(x, z);
		points.add(vector);
	}
	
	int minY = (int) tags.get("minY").getValue();
	int maxY = (int) tags.get("maxY").getValue();
	
	Polygonal2DRegion region = new Polygonal2DRegion();
	for (BlockVector2D point : points) {
		region.addPoint(point);
	}
	
	region.setMaximumY(maxY);
	region.setMinimumY(minY);
	return region;
}
 
Example #9
Source File: SchematicWriter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private static Tag writeVector(Vector vector, String name) {
    List<DoubleTag> list = new ArrayList<DoubleTag>();
    list.add(new DoubleTag(vector.getX()));
    list.add(new DoubleTag(vector.getY()));
    list.add(new DoubleTag(vector.getZ()));
    return new ListTag(DoubleTag.class, list);
}
 
Example #10
Source File: JSON2NBT.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public Tag parse() throws NBTException {
    ArrayList<Tag> list = new ArrayList<>();
    Iterator var2 = this.tagList.iterator();

    while (var2.hasNext()) {
        JSON2NBT.Any JSON2NBT$any = (JSON2NBT.Any) var2.next();
        list.add(JSON2NBT$any.parse());
    }
    Class<? extends Tag> tagType = list.isEmpty() ? CompoundTag.class : list.get(0).getClass();
    return new ListTag(tagType, list);
}
 
Example #11
Source File: StructureFormat.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private Tag writeVector(Vector vector, String name) {
    List<DoubleTag> list = new ArrayList<DoubleTag>();
    list.add(new DoubleTag(vector.getX()));
    list.add(new DoubleTag(vector.getY()));
    list.add(new DoubleTag(vector.getZ()));
    return new ListTag(DoubleTag.class, list);
}
 
Example #12
Source File: FaweFormat.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private Tag writeVector(Vector vector, String name) {
    List<DoubleTag> list = new ArrayList<DoubleTag>();
    list.add(new DoubleTag(vector.getX()));
    list.add(new DoubleTag(vector.getY()));
    list.add(new DoubleTag(vector.getZ()));
    return new ListTag(DoubleTag.class, list);
}
 
Example #13
Source File: FaweCache.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public static ListTag asTag(Collection values) {
    Class clazz = null;
    List<Tag> list = new ArrayList<>(values.size());
    for (Object value : values) {
        Tag tag = asTag(value);
        if (clazz == null) {
            clazz = tag.getClass();
        }
        list.add(tag);
    }
    if (clazz == null) clazz = EndTag.class;
    return new ListTag(clazz, list);
}
 
Example #14
Source File: FaweCache.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public static ListTag asTag(Object... values) {
    Class clazz = null;
    List<Tag> list = new ArrayList<>(values.length);
    for (Object value : values) {
        Tag tag = asTag(value);
        if (clazz == null) {
            clazz = tag.getClass();
        }
        list.add(tag);
    }
    if (clazz == null) clazz = EndTag.class;
    return new ListTag(clazz, list);
}
 
Example #15
Source File: NBTConverter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private static ListTag fromNative(cn.nukkit.nbt.tag.ListTag other) {
    other = (cn.nukkit.nbt.tag.ListTag) other.copy();
    List<Tag> list = new ArrayList<Tag>();
    Class<? extends Tag> listClass = StringTag.class;
    int tags = other.size();
    for (int i = 0; i < tags; i++) {
        Tag child = fromNative(other.get(0));
        other.remove(0);
        list.add(child);
        listClass = child.getClass();
    }
    return new ListTag(listClass, list);
}
 
Example #16
Source File: NBTConverter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private static Tag fromNative(cn.nukkit.nbt.tag.Tag other) {
    if (other instanceof cn.nukkit.nbt.tag.IntArrayTag) {
        return fromNative((cn.nukkit.nbt.tag.IntArrayTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.ListTag) {
        return fromNative((cn.nukkit.nbt.tag.ListTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.EndTag) {
        return fromNative((cn.nukkit.nbt.tag.EndTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.LongTag) {
        return fromNative((cn.nukkit.nbt.tag.LongTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.StringTag) {
        return fromNative((cn.nukkit.nbt.tag.StringTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.IntTag) {
        return fromNative((cn.nukkit.nbt.tag.IntTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.ByteTag) {
        return fromNative((cn.nukkit.nbt.tag.ByteTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.ByteArrayTag) {
        return fromNative((cn.nukkit.nbt.tag.ByteArrayTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.CompoundTag) {
        return fromNative((cn.nukkit.nbt.tag.CompoundTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.FloatTag) {
        return fromNative((cn.nukkit.nbt.tag.FloatTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.ShortTag) {
        return fromNative((cn.nukkit.nbt.tag.ShortTag) other);

    } else if (other instanceof cn.nukkit.nbt.tag.DoubleTag) {
        return fromNative((cn.nukkit.nbt.tag.DoubleTag) other);
    } else {
        throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName());
    }
}
 
Example #17
Source File: MCAFile2LevelDB.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private CompoundTag transformItem(CompoundTag item) {
    String itemId = item.getString("id");
    short damage = item.getShort("Damage");
    BaseItem remapped = remapper.remapItem(itemId, damage);
    Map<String, com.sk89q.jnbt.Tag> map = ReflectionUtils.getMap(item.getValue());
    map.put("id", new ShortTag((short) remapped.getType()));
    map.put("Damage", new ShortTag((short) remapped.getData()));
    if (!map.containsKey("Count")) map.put("Count", new ByteTag((byte) 0));

    CompoundTag tag = (CompoundTag) item.getValue().get("tag");
    if (tag != null) {
        Map<String, com.sk89q.jnbt.Tag> tagMap = ReflectionUtils.getMap(tag.getValue());
        ListTag<CompoundTag> enchants = (ListTag<CompoundTag>) tagMap.get("ench");
        if (enchants != null) {
            for (CompoundTag ench : enchants.getValue()) {
                Map<String, com.sk89q.jnbt.Tag> value = ReflectionUtils.getMap(ench.getValue());
                String id = ench.getString("id");
                String lvl = ench.getString("lvl");
                if (id != null && !id.isEmpty()) value.put("id", new ShortTag(Short.parseShort(id)));
                if (lvl != null && !lvl.isEmpty()) value.put("lvl", new ShortTag(Short.parseShort(lvl)));
            }
        }
        CompoundTag tile = (CompoundTag) tagMap.get("BlockEntityTag");
        if (tile != null) {
            tagMap.putAll(tile.getValue());
        }
    }
    return item;
}
 
Example #18
Source File: FaweAdapter_1_9.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private ListTag toNativeList(NBTTagList foreign)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
    List<Tag> values = new ArrayList();
    int type = foreign.d();

    List foreignList = (List)this.nbtListTagListField.get(foreign);
    for (int i = 0; i < foreign.size(); i++)
    {
        NBTBase element = (NBTBase)foreignList.get(i);
        values.add(toNative(element));
    }
    Class<? extends Tag> cls = NBTConstants.getClassFromType(type);
    return new ListTag(cls, values);
}
 
Example #19
Source File: StructureFormat.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private Tag writeRotation(Location location, String name) {
    List<FloatTag> list = new ArrayList<FloatTag>();
    list.add(new FloatTag(location.getYaw()));
    list.add(new FloatTag(location.getPitch()));
    return new ListTag(FloatTag.class, list);
}
 
Example #20
Source File: FaweFormat.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private Tag writeRotation(Location location, String name) {
    List<FloatTag> list = new ArrayList<FloatTag>();
    list.add(new FloatTag(location.getYaw()));
    list.add(new FloatTag(location.getPitch()));
    return new ListTag(FloatTag.class, list);
}
 
Example #21
Source File: SchematicWriter.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private static Tag writeRotation(Location location, String name) {
    List<FloatTag> list = new ArrayList<FloatTag>();
    list.add(new FloatTag(location.getYaw()));
    list.add(new FloatTag(location.getPitch()));
    return new ListTag(FloatTag.class, list);
}
 
Example #22
Source File: Spigot_v1_16_R1.java    From worldedit-adapters with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts from a non-native NMS NBT structure to a native WorldEdit NBT
 * structure.
 *
 * @param foreign non-native NMS NBT structure
 * @return native WorldEdit NBT structure
 */
@SuppressWarnings("unchecked")
Tag toNative(NBTBase foreign) {
    if (foreign == null) {
        return null;
    }
    if (foreign instanceof NBTTagCompound) {
        Map<String, Tag> values = new HashMap<>();
        Set<String> foreignKeys = ((NBTTagCompound) foreign).getKeys(); // map.keySet

        for (String str : foreignKeys) {
            NBTBase base = ((NBTTagCompound) foreign).get(str);
            values.put(str, toNative(base));
        }
        return new CompoundTag(values);
    } else if (foreign instanceof NBTTagByte) {
        return new ByteTag(((NBTTagByte) foreign).asByte());
    } else if (foreign instanceof NBTTagByteArray) {
        return new ByteArrayTag(((NBTTagByteArray) foreign).getBytes()); // data
    } else if (foreign instanceof NBTTagDouble) {
        return new DoubleTag(((NBTTagDouble) foreign).asDouble()); // getDouble
    } else if (foreign instanceof NBTTagFloat) {
        return new FloatTag(((NBTTagFloat) foreign).asFloat());
    } else if (foreign instanceof NBTTagInt) {
        return new IntTag(((NBTTagInt) foreign).asInt());
    } else if (foreign instanceof NBTTagIntArray) {
        return new IntArrayTag(((NBTTagIntArray) foreign).getInts()); // data
    } else if (foreign instanceof NBTTagLongArray) {
        return new LongArrayTag(((NBTTagLongArray) foreign).getLongs()); // data
    } else if (foreign instanceof NBTTagList) {
        try {
            return toNativeList((NBTTagList) foreign);
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
            return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
        }
    } else if (foreign instanceof NBTTagLong) {
        return new LongTag(((NBTTagLong) foreign).asLong());
    } else if (foreign instanceof NBTTagShort) {
        return new ShortTag(((NBTTagShort) foreign).asShort());
    } else if (foreign instanceof NBTTagString) {
        return new StringTag(foreign.asString());
    } else if (foreign instanceof NBTTagEnd) {
        return new EndTag();
    } else {
        throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
    }
}
 
Example #23
Source File: Spigot_v1_15_R2.java    From worldedit-adapters with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts from a non-native NMS NBT structure to a native WorldEdit NBT
 * structure.
 *
 * @param foreign non-native NMS NBT structure
 * @return native WorldEdit NBT structure
 */
@SuppressWarnings("unchecked")
Tag toNative(NBTBase foreign) {
    if (foreign == null) {
        return null;
    }
    if (foreign instanceof NBTTagCompound) {
        Map<String, Tag> values = new HashMap<>();
        Set<String> foreignKeys = ((NBTTagCompound) foreign).getKeys(); // map.keySet

        for (String str : foreignKeys) {
            NBTBase base = ((NBTTagCompound) foreign).get(str);
            values.put(str, toNative(base));
        }
        return new CompoundTag(values);
    } else if (foreign instanceof NBTTagByte) {
        return new ByteTag(((NBTTagByte) foreign).asByte());
    } else if (foreign instanceof NBTTagByteArray) {
        return new ByteArrayTag(((NBTTagByteArray) foreign).getBytes()); // data
    } else if (foreign instanceof NBTTagDouble) {
        return new DoubleTag(((NBTTagDouble) foreign).asDouble()); // getDouble
    } else if (foreign instanceof NBTTagFloat) {
        return new FloatTag(((NBTTagFloat) foreign).asFloat());
    } else if (foreign instanceof NBTTagInt) {
        return new IntTag(((NBTTagInt) foreign).asInt());
    } else if (foreign instanceof NBTTagIntArray) {
        return new IntArrayTag(((NBTTagIntArray) foreign).getInts()); // data
    } else if (foreign instanceof NBTTagLongArray) {
        return new LongArrayTag(((NBTTagLongArray) foreign).getLongs()); // data
    } else if (foreign instanceof NBTTagList) {
        try {
            return toNativeList((NBTTagList) foreign);
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
            return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
        }
    } else if (foreign instanceof NBTTagLong) {
        return new LongTag(((NBTTagLong) foreign).asLong());
    } else if (foreign instanceof NBTTagShort) {
        return new ShortTag(((NBTTagShort) foreign).asShort());
    } else if (foreign instanceof NBTTagString) {
        return new StringTag(foreign.asString());
    } else if (foreign instanceof NBTTagEnd) {
        return new EndTag();
    } else {
        throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
    }
}
 
Example #24
Source File: Spigot_v1_14_R4.java    From worldedit-adapters with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts from a non-native NMS NBT structure to a native WorldEdit NBT
 * structure.
 *
 * @param foreign non-native NMS NBT structure
 * @return native WorldEdit NBT structure
 */
@SuppressWarnings("unchecked")
Tag toNative(NBTBase foreign) {
    if (foreign == null) {
        return null;
    }
    if (foreign instanceof NBTTagCompound) {
        Map<String, Tag> values = new HashMap<>();
        Set<String> foreignKeys = ((NBTTagCompound) foreign).getKeys(); // map.keySet

        for (String str : foreignKeys) {
            NBTBase base = ((NBTTagCompound) foreign).get(str);
            values.put(str, toNative(base));
        }
        return new CompoundTag(values);
    } else if (foreign instanceof NBTTagByte) {
        return new ByteTag(((NBTTagByte) foreign).asByte());
    } else if (foreign instanceof NBTTagByteArray) {
        return new ByteArrayTag(((NBTTagByteArray) foreign).getBytes()); // data
    } else if (foreign instanceof NBTTagDouble) {
        return new DoubleTag(((NBTTagDouble) foreign).asDouble()); // getDouble
    } else if (foreign instanceof NBTTagFloat) {
        return new FloatTag(((NBTTagFloat) foreign).asFloat());
    } else if (foreign instanceof NBTTagInt) {
        return new IntTag(((NBTTagInt) foreign).asInt());
    } else if (foreign instanceof NBTTagIntArray) {
        return new IntArrayTag(((NBTTagIntArray) foreign).getInts()); // data
    } else if (foreign instanceof NBTTagLongArray) {
        return new LongArrayTag(((NBTTagLongArray) foreign).getLongs()); // data
    } else if (foreign instanceof NBTTagList) {
        try {
            return toNativeList((NBTTagList) foreign);
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
            return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
        }
    } else if (foreign instanceof NBTTagLong) {
        return new LongTag(((NBTTagLong) foreign).asLong());
    } else if (foreign instanceof NBTTagShort) {
        return new ShortTag(((NBTTagShort) foreign).asShort());
    } else if (foreign instanceof NBTTagString) {
        return new StringTag(foreign.asString());
    } else if (foreign instanceof NBTTagEnd) {
        return new EndTag();
    } else {
        throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
    }
}
 
Example #25
Source File: Spigot_v1_13_R2_2.java    From worldedit-adapters with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Converts from a non-native NMS NBT structure to a native WorldEdit NBT
 * structure.
 *
 * @param foreign non-native NMS NBT structure
 * @return native WorldEdit NBT structure
 */
@SuppressWarnings("unchecked")
Tag toNative(NBTBase foreign) {
    if (foreign == null) {
        return null;
    }
    if (foreign instanceof NBTTagCompound) {
        Map<String, Tag> values = new HashMap<>();
        Set<String> foreignKeys = ((NBTTagCompound) foreign).getKeys(); // map.keySet

        for (String str : foreignKeys) {
            NBTBase base = ((NBTTagCompound) foreign).get(str);
            values.put(str, toNative(base));
        }
        return new CompoundTag(values);
    } else if (foreign instanceof NBTTagByte) {
        return new ByteTag(((NBTTagByte) foreign).asByte());
    } else if (foreign instanceof NBTTagByteArray) {
        return new ByteArrayTag(((NBTTagByteArray) foreign).c()); // data
    } else if (foreign instanceof NBTTagDouble) {
        return new DoubleTag(((NBTTagDouble) foreign).asDouble()); // getDouble
    } else if (foreign instanceof NBTTagFloat) {
        return new FloatTag(((NBTTagFloat) foreign).asFloat());
    } else if (foreign instanceof NBTTagInt) {
        return new IntTag(((NBTTagInt) foreign).asInt());
    } else if (foreign instanceof NBTTagIntArray) {
        return new IntArrayTag(((NBTTagIntArray) foreign).d()); // data
    } else if (foreign instanceof NBTTagLongArray) {
        return new LongArrayTag(((NBTTagLongArray) foreign).d()); // data
    } else if (foreign instanceof NBTTagList) {
        try {
            return toNativeList((NBTTagList) foreign);
        } catch (Throwable e) {
            logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
            return new ListTag(ByteTag.class, new ArrayList<ByteTag>());
        }
    } else if (foreign instanceof NBTTagLong) {
        return new LongTag(((NBTTagLong) foreign).asLong());
    } else if (foreign instanceof NBTTagShort) {
        return new ShortTag(((NBTTagShort) foreign).asShort());
    } else if (foreign instanceof NBTTagString) {
        return new StringTag(foreign.asString());
    } else if (foreign instanceof NBTTagEnd) {
        return new EndTag();
    } else {
        throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
    }
}
 
Example #26
Source File: FaweAdapter_1_9.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private Tag toNative(NBTBase foreign)
{
    if (foreign == null) {
        return null;
    }
    if ((foreign instanceof NBTTagCompound))
    {
        Map<String, Tag> values = new HashMap();
        Set<String> foreignKeys = ((NBTTagCompound)foreign).c();
        for (String str : foreignKeys)
        {
            NBTBase base = ((NBTTagCompound)foreign).get(str);
            values.put(str, toNative(base));
        }
        return new CompoundTag(values);
    }
    if ((foreign instanceof NBTTagByte)) {
        return new ByteTag(((NBTTagByte)foreign).f());
    }
    if ((foreign instanceof NBTTagByteArray)) {
        return new ByteArrayTag(((NBTTagByteArray)foreign).c());
    }
    if ((foreign instanceof NBTTagDouble)) {
        return new DoubleTag(((NBTTagDouble)foreign).g());
    }
    if ((foreign instanceof NBTTagFloat)) {
        return new FloatTag(((NBTTagFloat)foreign).h());
    }
    if ((foreign instanceof NBTTagInt)) {
        return new IntTag(((NBTTagInt)foreign).d());
    }
    if ((foreign instanceof NBTTagIntArray)) {
        return new IntArrayTag(((NBTTagIntArray)foreign).c());
    }
    if ((foreign instanceof NBTTagList)) {
        try
        {
            return toNativeList((NBTTagList)foreign);
        }
        catch (Throwable e)
        {
            this.logger.log(Level.WARNING, "Failed to convert NBTTagList", e);
            return new ListTag(ByteTag.class, new ArrayList());
        }
    }
    if ((foreign instanceof NBTTagLong)) {
        return new LongTag(((NBTTagLong)foreign).c());
    }
    if ((foreign instanceof NBTTagShort)) {
        return new ShortTag(((NBTTagShort)foreign).e());
    }
    if ((foreign instanceof NBTTagString)) {
        return new StringTag(((NBTTagString)foreign).a_());
    }
    if ((foreign instanceof NBTTagEnd)) {
        return new EndTag();
    }
    throw new IllegalArgumentException("Don't know how to make native " + foreign.getClass().getCanonicalName());
}