com.sk89q.jnbt.Tag Java Examples

The following examples show how to use com.sk89q.jnbt.Tag. 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: FaweClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public List<CompoundTag> getTileEntities() {
    final List<CompoundTag> tiles = new ArrayList<>();
    forEach(new BlockReader() {
        private int index = 0;

        @Override
        public void run(int x, int y, int z, BaseBlock block) {
            CompoundTag tag = block.getNbtData();
            if (tag != null) {
                Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
                values.put("x", new IntTag(x));
                values.put("y", new IntTag(y));
                values.put("z", new IntTag(z));
                tiles.add(tag);
            }
        }
    }, false);
    return tiles;
}
 
Example #2
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 #3
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 #4
Source File: BukkitChunk_1_11.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public boolean storeEntity(Entity ent) throws InvocationTargetException, IllegalAccessException {
    if (ent instanceof EntityPlayer) {
        return false;
    }
    int x = (MathMan.roundInt(ent.locX) & 15);
    int z = (MathMan.roundInt(ent.locZ) & 15);
    int y = (MathMan.roundInt(ent.locY) & 0xFF);
    int i = FaweCache.CACHE_I[y][z][x];
    int j = FaweCache.CACHE_J[y][z][x];
    String id = EntityTypes.b(ent);
    if (id != null) {
        NBTTagCompound tag = new NBTTagCompound();
        ent.e(tag); // readEntityIntoTag
        CompoundTag nativeTag = (CompoundTag) getParent().toNative(tag);
        Map<String, Tag> map = ReflectionUtils.getMap(nativeTag.getValue());
        map.put("Id", new StringTag(id));
        setEntity(nativeTag);
        return true;
    } else {
        return false;
    }
}
 
Example #5
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 #6
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 #7
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 #8
Source File: CPUOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<CompoundTag> getTileEntities() {
    convertTilesToIndex();
    for (Map.Entry<Integer, CompoundTag> entry : nbtMapIndex.entrySet()) {
        int index = entry.getKey();
        CompoundTag tag = entry.getValue();
        Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
        if (!values.containsKey("x")) {
            int y = index / area;
            index -= y * area;
            int z = index / width;
            int x = index - (z * width);
            values.put("x", new IntTag(x));
            values.put("y", new IntTag(y));
            values.put("z", new IntTag(z));
        }
    }
    return new ArrayList<>(nbtMapIndex.values());
}
 
Example #9
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 #10
Source File: MemoryOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<CompoundTag> getTileEntities() {
    convertTilesToIndex();
    for (Map.Entry<Integer, CompoundTag> entry : nbtMapIndex.entrySet()) {
        int index = entry.getKey();
        CompoundTag tag = entry.getValue();
        Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
        if (!values.containsKey("x")) {
            int y = index / area;
            index -= y * area;
            int z = index / width;
            int x = index - (z * width);
            values.put("x", new IntTag(x));
            values.put("y", new IntTag(y));
            values.put("z", new IntTag(z));
        }
    }
    return new ArrayList<>(nbtMapIndex.values());
}
 
Example #11
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 #12
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 #13
Source File: SchematicReader.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
private static <T extends Tag> T requireTag(Map<String, Tag> items, String key, Class<T> expected) throws IOException {
    if (!items.containsKey(key)) {
        throw new IOException("Schematic file is missing a \"" + key + "\" tag");
    }

    Tag tag = items.get(key);
    if (!expected.isInstance(tag)) {
        throw new IOException(key + " tag is not of tag type " + expected.getName());
    }

    return expected.cast(tag);
}
 
Example #14
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 #15
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 #16
Source File: MemoryOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public boolean setTile(int index, CompoundTag tag) {
    nbtMapIndex.put(index, tag);
    Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
    values.remove("x");
    values.remove("y");
    values.remove("z");
    return true;
}
 
Example #17
Source File: DiskOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setTile(int x, int y, int z, CompoundTag tag) {
    nbtMap.put(new IntegerTrio(x, y, z), tag);
    Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
    values.put("x", new IntTag(x));
    values.put("y", new IntTag(y));
    values.put("z", new IntTag(z));
    return true;
}
 
Example #18
Source File: JSON2NBT.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public Tag parse() throws NBTException {
    HashMap<String, Tag> map = new HashMap<String, Tag>();
    Iterator var2 = this.tagList.iterator();

    while (var2.hasNext()) {
        JSON2NBT.Any JSON2NBT$any = (JSON2NBT.Any) var2.next();
        map.put(JSON2NBT$any.json, JSON2NBT$any.parse());
    }

    return new CompoundTag(map);
}
 
Example #19
Source File: ExtentBlockCopy.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Transform NBT data in the given block state and return a new instance
 * if the NBT data needs to be transformed.
 *
 * @param state the existing state
 * @return a new state or the existing one
 */
private BaseBlock transformNbtData(BaseBlock state) {
    CompoundTag tag = state.getNbtData();
    if (tag != null) {
        // Handle blocks which store their rotation in NBT
        if (tag.containsKey("Rot")) {
            int rot = tag.asInt("Rot");

            Direction direction = MCDirections.fromRotation(rot);

            if (direction != null) {
                Vector applyAbsolute = transform.apply(direction.toVector());
                Vector applyOrigin = transform.apply(Vector.ZERO);
                applyAbsolute.mutX(applyAbsolute.getX() - applyOrigin.getX());
                applyAbsolute.mutY(applyAbsolute.getY() - applyOrigin.getY());
                applyAbsolute.mutZ(applyAbsolute.getZ() - applyOrigin.getZ());

                Direction newDirection = Direction.findClosest(applyAbsolute, Flag.CARDINAL | Flag.ORDINAL | Flag.SECONDARY_ORDINAL);

                if (newDirection != null) {
                    Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
                    values.put("Rot", new ByteTag((byte) MCDirections.toRotation(newDirection)));
                }
            }
        }
    }
    return state;
}
 
Example #20
Source File: CPUOptimizedClipboard.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public boolean setTile(int index, CompoundTag tag) {
    nbtMapIndex.put(index, tag);
    Map<String, Tag> values = ReflectionUtils.getMap(tag.getValue());
    values.remove("x");
    values.remove("y");
    values.remove("z");
    return true;
}
 
Example #21
Source File: SchematicReader.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private static <T extends Tag> T getTag(CompoundTag tag, Class<T> expected, String key) {
    Map<String, Tag> items = tag.getValue();

    if (!items.containsKey(key)) {
        return null;
    }

    Tag test = items.get(key);
    if (!expected.isInstance(test)) {
        return null;
    }

    return expected.cast(test);
}
 
Example #22
Source File: SchematicWriter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
public ForEach(int[] yarea, int[] zwidth, byte[] blocks, byte[] blockData, List<Tag> tileEntities) {
    this.yarea = yarea;
    this.zwidth = zwidth;
    this.blocks = blocks;
    this.blockData = blockData;
    this.tileEntities = tileEntities;
}
 
Example #23
Source File: SchematicWriter.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run(int x, int y, int z, BaseBlock block) {
    if (this.x == x - 1 && this.y == y && this.z == z) {
        this.x++;
        index++;
    } else {
        index = yarea[this.y = y] + zwidth[this.z = z] + (this.x = x);
    }
    int id = block.getId();
    blocks[index] = (byte) id;
    if (FaweCache.hasData(id)) {
        blockData[index] = (byte) block.getData();
        if (id > 255) {
            if (addBlocks == null) { // Lazily create section
                addBlocks = new byte[((blocks.length + 1) >> 1)];
            }
            addBlocks[index >> 1] = (byte) (((index & 1) == 0) ? addBlocks[index >> 1] & 0xF0 | (id >> 8) & 0xF : addBlocks[index >> 1] & 0xF | ((id >> 8) & 0xF) << 4);
        }
    }
    CompoundTag rawTag = block.getNbtData();
    if (rawTag != null) {
        Map<String, Tag> values = ReflectionUtils.getMap(rawTag.getValue());
        values.put("id", new StringTag(block.getNbtId()));
        values.put("x", new IntTag(x));
        values.put("y", new IntTag(y));
        values.put("z", new IntTag(z));
        tileEntities.add(rawTag);
    }
}
 
Example #24
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 #25
Source File: BaseBlock.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String getNbtId() {
    CompoundTag nbtData = getNbtData();
    if (nbtData == null) {
        return "";
    }
    Tag idTag = nbtData.getValue().get("id");
    if (idTag != null && idTag instanceof StringTag) {
        return ((StringTag) idTag).getValue();
    } else {
        return "";
    }
}
 
Example #26
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 #27
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 #28
Source File: AsyncSign.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setLine(int index, String line) throws IndexOutOfBoundsException {
    CompoundTag nbt = getNbtData();
    if (nbt != null) {
        Map<String, Tag> map = ReflectionUtils.getMap(nbt.getValue());
        map.put("Text" + (index + 1), new StringTag(toJson(line)));
    }
}
 
Example #29
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 #30
Source File: FaweAdapter_All.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity state) {
    try {
        if (classEntity == null) return null;
        World world = location.getWorld();
        Object nmsWorld = getHandleWorld.invoke(world);

        Object createdEntity = createEntityFromId(state.getTypeId(), nmsWorld);

        if (createdEntity != null) {
            CompoundTag nativeTag = state.getNbtData();
            Map<String, Tag> rawMap = ReflectionUtils.getMap(nativeTag.getValue());
            for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
                rawMap.remove(name);
            }
            if (nativeTag != null) {
                Object tag = fromNative(nativeTag);
                readTagIntoEntity.invoke(createdEntity, tag);
            }

            setLocation.invoke(createdEntity, location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());

            addEntity.invoke(nmsWorld, createdEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
            return (Entity) getBukkitEntity.invoke(createdEntity);
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    return null;
}