Java Code Examples for com.sk89q.jnbt.Tag

The following examples show how to use com.sk89q.jnbt.Tag. These examples are extracted from open source projects. 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 Project: FastAsyncWorldedit   Author: boy0001   File: BukkitChunk_1_11.java    License: 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 #2
Source Project: FastAsyncWorldedit   Author: boy0001   File: FastWorldEditExtent.java    License: 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 #3
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweClipboard.java    License: 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 #4
Source Project: FastAsyncWorldedit   Author: boy0001   File: CPUOptimizedClipboard.java    License: 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 #5
Source Project: FastAsyncWorldedit   Author: boy0001   File: MemoryOptimizedClipboard.java    License: 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 #6
Source Project: FastAsyncWorldedit   Author: boy0001   File: MCAWorld.java    License: 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 #7
Source Project: HeavySpleef   Author: xaniox   File: CylinderRegionSchematicCodec.java    License: 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 Project: HeavySpleef   Author: xaniox   File: CylinderRegionSchematicCodec.java    License: 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 #9
Source Project: HeavySpleef   Author: xaniox   File: CuboidRegionSchematicCodec.java    License: 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 #10
Source Project: HeavySpleef   Author: xaniox   File: CuboidRegionSchematicCodec.java    License: 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 #11
Source Project: HeavySpleef   Author: xaniox   File: Polygonal2DRegionSchematicCodec.java    License: 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 #12
Source Project: FastAsyncWorldedit   Author: boy0001   File: AsyncSign.java    License: 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 #13
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAdapter_1_9.java    License: 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 #14
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAdapter_All.java    License: 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;
}
 
Example #15
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAdapter_All.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Tag toNative(Object nmsTag) {
    try {
        return NMSToWE.get(nmsTag.getClass()).construct(nmsTag);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #16
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAdapter_All.java    License: GNU General Public License v3.0 5 votes vote down vote up
public Object fromNative(Tag tag) {
    try {
        return WEToNMS.get(tag.getClass()).construct(tag);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #17
Source Project: FastAsyncWorldedit   Author: boy0001   File: BukkitQueue_0.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Tag toNative(Object tag) {
    BukkitImplAdapter adapter = getAdapter();
    if (adapter == null) {
        if (backupAdaper != null) return backupAdaper.toNative(tag);
        return null;
    }
    try {
        return (Tag) methodToNative.invoke(adapter, tag);
    } catch (InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #18
Source Project: FastAsyncWorldedit   Author: boy0001   File: BukkitQueue_0.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Object fromNative(Tag tag) {
    BukkitImplAdapter adapter = getAdapter();
    if (adapter == null) {
        if (backupAdaper != null) return backupAdaper.fromNative(tag);
        return null;
    }
    try {
        return methodFromNative.invoke(adapter, tag);
    } catch (InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #19
Source Project: FastAsyncWorldedit   Author: boy0001   File: NBTConverter.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, cn.nukkit.nbt.tag.Tag> getMap(cn.nukkit.nbt.tag.CompoundTag other) {
    try {
        return (Map<String, cn.nukkit.nbt.tag.Tag>) tagsField.get(other);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #20
Source Project: FastAsyncWorldedit   Author: boy0001   File: NBTConverter.java    License: 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 #21
Source Project: FastAsyncWorldedit   Author: boy0001   File: NBTConverter.java    License: 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 #22
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweCache.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static CompoundTag asTag(Map<String, Object> value) {
    HashMap<String, Tag> map = new HashMap<>();
    for (Map.Entry<String, Object> entry : value.entrySet()) {
        Object child = entry.getValue();
        Tag tag = asTag(child);
        map.put(entry.getKey(), tag);
    }
    return new CompoundTag(map);
}
 
Example #23
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweCache.java    License: 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 #24
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweCache.java    License: 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 #25
Source Project: FastAsyncWorldedit   Author: boy0001   File: BlockBagChangeSet.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addTileCreate(CompoundTag nbt) {
    if (nbt.containsKey("items")) {
        Map<String, Tag> map = ReflectionUtils.getMap(nbt.getValue());
        map.remove("items");
    }
    super.addTileCreate(nbt);
}
 
Example #26
Source Project: FastAsyncWorldedit   Author: boy0001   File: StripNBTExtent.java    License: GNU General Public License v3.0 5 votes vote down vote up
public <T extends NbtValued> T stripNBT(T block) {
    if (!block.hasNbtData()) return block;
    CompoundTag nbt = block.getNbtData();
    Map<String, Tag> value = nbt.getValue();
    for (String key : strip) {
        value.remove(key);
    }
    return block;
}
 
Example #27
Source Project: FastAsyncWorldedit   Author: boy0001   File: MutableEntityChange.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void delete(UndoContext context) {
    Extent extent = context.getExtent();
    ExtentTraverser<FastWorldEditExtent> find = new ExtentTraverser(extent).find(FastWorldEditExtent.class);
    if (find != null) {
        FastWorldEditExtent fwee = find.get();
        Map<String, Tag> map = tag.getValue();
        long most;
        long least;
        if (map.containsKey("UUIDMost")) {
            most = ((LongTag) map.get("UUIDMost")).getValue();
            least = ((LongTag) map.get("UUIDLeast")).getValue();
        } else if (map.containsKey("PersistentIDMSB")) {
            most = ((LongTag) map.get("PersistentIDMSB")).getValue();
            least = ((LongTag) map.get("PersistentIDLSB")).getValue();
        } else {
            Fawe.debug("Skipping entity without uuid.");
            return;
        }
        List<DoubleTag> pos = (List<DoubleTag>) map.get("Pos").getValue();
        int x = MathMan.roundInt(pos.get(0).getValue());
        int y = MathMan.roundInt(pos.get(1).getValue());
        int z = MathMan.roundInt(pos.get(2).getValue());
        UUID uuid = new UUID(most, least);
        fwee.getQueue().removeEntity(x, y, z, uuid);
    } else {
        Fawe.debug("FAWE doesn't support: " + context + " for " + getClass() + " (bug Empire92)");
    }
}
 
Example #28
Source Project: FastAsyncWorldedit   Author: boy0001   File: MutableEntityChange.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void perform(FaweQueue queue) {
    Map<String, Tag> map = tag.getValue();
    Tag posTag = map.get("Pos");
    if (posTag == null) {
        Fawe.debug("Missing pos tag: " + tag);
        return;
    }
    List<DoubleTag> pos = (List<DoubleTag>) posTag.getValue();
    int x = MathMan.roundInt(pos.get(0).getValue());
    int y = MathMan.roundInt(pos.get(1).getValue());
    int z = MathMan.roundInt(pos.get(2).getValue());
    queue.setEntity(x, y, z, tag);
}
 
Example #29
Source Project: FastAsyncWorldedit   Author: boy0001   File: MutableTileChange.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void perform(FaweQueue queue) {
    Map<String, Tag> map = tag.getValue();
    int x = ((IntTag) map.get("x")).getValue();
    int y = ((IntTag) map.get("y")).getValue();
    int z = ((IntTag) map.get("z")).getValue();
    queue.setTile(x, y, z, tag);
}
 
Example #30
Source Project: FastAsyncWorldedit   Author: boy0001   File: DiskOptimizedClipboard.java    License: 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;
}