Java Code Examples for com.sk89q.jnbt.IntTag

The following examples show how to use com.sk89q.jnbt.IntTag. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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;
}
 
Example #10
Source Project: FastAsyncWorldedit   Author: boy0001   File: SchematicWriter.java    License: 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 #11
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAdapter_1_9.java    License: 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());
}
 
Example #12
Source Project: FastAsyncWorldedit   Author: boy0001   File: LevelDBToMCAFile.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static void copyLevelDat(File folderTo, File in) throws IOException {
    File levelDat = new File(folderTo, "level.dat");
    if (!levelDat.exists()) {
        folderTo.mkdirs();
        levelDat.createNewFile();
    }
    try (LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(new FileInputStream(in))) {
        int version = ledis.readInt(); // Ignored
        int length = ledis.readInt(); // Ignored
        NBTInputStream nis = new NBTInputStream((DataInput) ledis);
        NamedTag named = nis.readNamedTag();
        com.sk89q.jnbt.CompoundTag tag = (CompoundTag) named.getTag();
        Map<String, com.sk89q.jnbt.Tag> map = ReflectionUtils.getMap(tag.getValue());

        Map<String, String> gameRules = new HashMap<>();
        gameRules.put("firedamage", "firedamage");
        gameRules.put("falldamage", "falldamage");
        gameRules.put("dofiretick", "doFireTick");
        gameRules.put("drowningdamage", "drowningdamage");
        gameRules.put("doentitydrops", "doEntityDrops");
        gameRules.put("keepinventory", "keepInventory");
        gameRules.put("sendcommandfeedback", "sendCommandFeedback");
        gameRules.put("dodaylightcycle", "doDaylightCycle");
        gameRules.put("commandblockoutput", "commandBlockOutput");
        gameRules.put("domobloot", "doMobLoot");
        gameRules.put("domobspawning", "doMobSpawning");
        gameRules.put("doweathercycle", "doWeatherCycle");
        gameRules.put("mobgriefing", "mobGriefing");
        gameRules.put("dotiledrops", "doTileDrops");

        HashMap<String, com.sk89q.jnbt.Tag> ruleTagValue = new HashMap<>();
        for (Map.Entry<String, String> rule : gameRules.entrySet()) {
            com.sk89q.jnbt.Tag value = map.remove(rule.getKey());
            if (value instanceof ByteTag) {
                value = new StringTag((Byte) value.getValue() == 1 ? "true" : "false");
            }
            if (value != null) {
                ruleTagValue.put(rule.getValue(), value);
            }
        }

        HashSet<String> allowed = new HashSet<>(Arrays.asList(
                "lightningTime", "pvp", "LevelName", "Difficulty", "GameType", "Generator", "LastPlayed", "RandomSeed", "StorageVersion", "Time", "commandsEnabled", "currentTick", "rainTime", "SpawnX", "SpawnY", "SpawnZ", "SizeOnDisk"
        ));
        Iterator<Map.Entry<String, com.sk89q.jnbt.Tag>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, com.sk89q.jnbt.Tag> entry = iterator.next();
            if (!allowed.contains(entry.getKey())) {
                iterator.remove();
            }
        }

        {
            map.put("GameRules", new CompoundTag(ruleTagValue));

            map.put("version", new IntTag(19133));
            map.put("DataVersion", new IntTag(1343));
            map.put("initialized", new ByteTag((byte) 1));
            map.putIfAbsent("SizeOnDisk", new LongTag(0));

            // generator
            int generator = tag.getInt("Generator");
            String name;
            switch (generator) {
                default:
                case 1:
                    name = "default";
                    break;
                case 2:
                    name = "flat";
                    break;
            }
            map.put("generatorName", new StringTag(name));
            map.put("generatorOptions", new StringTag(""));
            map.put("generatorVersion", new IntTag(1));
            map.put("Difficulty", new ByteTag((byte) tag.getInt("Difficulty")));
            map.put("DifficultyLocked", new ByteTag((byte) 0));
            map.put("MapFeatures", new ByteTag((byte) 1));
            map.put("allowCommands", new ByteTag(tag.getByte("commandsEnabled")));
            long time = tag.getLong("Time");
            if (time == 0) time = tag.getLong("CurrentTick");
            map.put("Time", new LongTag(time));
            map.put("spawnMobs", new ByteTag((byte) 1));
            Long lastPlayed = tag.getLong("LastPlayed");
            if (lastPlayed != null && lastPlayed < Integer.MAX_VALUE) {
                lastPlayed = lastPlayed * 1000;
                map.put("LastPlayed", new LongTag(lastPlayed));
            }

            HashMap<String, com.sk89q.jnbt.Tag> data = new HashMap<>();
            data.put("Data", new CompoundTag(map));
            CompoundTag root = new CompoundTag(data);

            try (NBTOutputStream nos = new NBTOutputStream(new PGZIPOutputStream(new FileOutputStream(levelDat)))) {
                nos.writeNamedTag("level.dat", root);
            }
        }
    }
}
 
Example #13
Source Project: FastAsyncWorldedit   Author: boy0001   File: NBTConverter.java    License: GNU General Public License v3.0 4 votes vote down vote up
private static IntTag fromNative(cn.nukkit.nbt.tag.IntTag other) {
    return new IntTag(other.data);
}
 
Example #14
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweAPI.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param is
 * @param loc
 * @throws IOException
 * @deprecated Since I haven't finished it yet
 * If a schematic is too large to be pasted normally<br>
 * - Skips any block history
 * - Ignores some block data
 * - Not actually streaming from disk, but it does skip a lot of overhead
 */
@Deprecated
public static void streamSchematic(final InputStream is, final FaweLocation loc) throws IOException {
    final NBTInputStream stream = new NBTInputStream(new GZIPInputStream(is));
    Tag tag = stream.readNamedTag().getTag();
    stream.close();

    Map<String, Tag> tagMap = (Map<String, Tag>) tag.getValue();

    final short width = ShortTag.class.cast(tagMap.get("Width")).getValue();
    final short length = ShortTag.class.cast(tagMap.get("Length")).getValue();
    final short height = ShortTag.class.cast(tagMap.get("Height")).getValue();
    byte[] ids = ByteArrayTag.class.cast(tagMap.get("Blocks")).getValue();
    byte[] datas = ByteArrayTag.class.cast(tagMap.get("Data")).getValue();

    final String world = loc.world;

    final int x_offset = loc.x + IntTag.class.cast(tagMap.get("WEOffsetX")).getValue();
    final int y_offset = loc.y + IntTag.class.cast(tagMap.get("WEOffsetY")).getValue();
    final int z_offset = loc.z + IntTag.class.cast(tagMap.get("WEOffsetZ")).getValue();

    FaweQueue queue = SetQueue.IMP.getNewQueue(getWorld(loc.world), true, true);

    for (int y = 0; y < height; y++) {
        final int yy = y_offset + y;
        if (yy > 255) {
            continue;
        }
        final int i1 = y * width * length;
        for (int z = 0; z < length; z++) {
            final int i2 = (z * width) + i1;
            final int zz = z_offset + z;
            for (int x = 0; x < width; x++) {
                final int i = i2 + x;
                final int xx = x_offset + x;
                final short id = (short) (ids[i] & 0xFF);
                queue.setBlock(xx, yy, zz, id, datas[i]);
            }
        }
    }

    queue.enqueue();
}
 
Example #15
Source Project: FastAsyncWorldedit   Author: boy0001   File: FaweCache.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static IntTag asTag(int value) {
    return new IntTag(value);
}
 
Example #16
Source Project: FastAsyncWorldedit   Author: boy0001   File: JSON2NBT.java    License: GNU General Public License v3.0 4 votes vote down vote up
public Tag parse() throws NBTException {
    try {
        if (DOUBLE.matcher(this.jsonValue).matches()) {
            return new DoubleTag(Double.parseDouble(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
        }

        if (FLOAT.matcher(this.jsonValue).matches()) {
            return new FloatTag(Float.parseFloat(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
        }

        if (BYTE.matcher(this.jsonValue).matches()) {
            return new ByteTag(Byte.parseByte(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
        }

        if (LONG.matcher(this.jsonValue).matches()) {
            return new LongTag(Long.parseLong(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
        }

        if (SHORT.matcher(this.jsonValue).matches()) {
            return new ShortTag(Short.parseShort(this.jsonValue.substring(0, this.jsonValue.length() - 1)));
        }

        if (INTEGER.matcher(this.jsonValue).matches()) {
            return new IntTag(Integer.parseInt(this.jsonValue));
        }

        if (DOUBLE_UNTYPED.matcher(this.jsonValue).matches()) {
            return new DoubleTag(Double.parseDouble(this.jsonValue));
        }

        if ("true".equalsIgnoreCase(this.jsonValue) || "false".equalsIgnoreCase(this.jsonValue)) {
            return new ByteTag((byte) (Boolean.parseBoolean(this.jsonValue) ? 1 : 0));
        }
    } catch (NumberFormatException var6) {
        this.jsonValue = this.jsonValue.replaceAll("\\\\\"", "\"");
        return new StringTag(this.jsonValue);
    }

    if (this.jsonValue.startsWith("[") && this.jsonValue.endsWith("]")) {
        String var7 = this.jsonValue.substring(1, this.jsonValue.length() - 1);
        String[] var8 = (String[]) ((String[]) Iterables.toArray(SPLITTER.split(var7), String.class));

        try {
            int[] var5 = new int[var8.length];

            for (int j = 0; j < var8.length; ++j) {
                var5[j] = Integer.parseInt(var8[j].trim());
            }

            return new IntArrayTag(var5);
        } catch (NumberFormatException var51) {
            return new StringTag(this.jsonValue);
        }
    } else {
        if (this.jsonValue.startsWith("\"") && this.jsonValue.endsWith("\"")) {
            this.jsonValue = this.jsonValue.substring(1, this.jsonValue.length() - 1);
        }

        this.jsonValue = this.jsonValue.replaceAll("\\\\\"", "\"");
        StringBuilder stringbuilder = new StringBuilder();

        for (int i = 0; i < this.jsonValue.length(); ++i) {
            if (i < this.jsonValue.length() - 1 && this.jsonValue.charAt(i) == 92 && this.jsonValue.charAt(i + 1) == 92) {
                stringbuilder.append('\\');
                ++i;
            } else {
                stringbuilder.append(this.jsonValue.charAt(i));
            }
        }

        return new StringTag(stringbuilder.toString());
    }
}
 
Example #17
Source Project: worldedit-adapters   Author: EngineHub   File: Spigot_v1_16_R1.java    License: 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 #18
Source Project: worldedit-adapters   Author: EngineHub   File: Spigot_v1_15_R2.java    License: 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 #19
Source Project: worldedit-adapters   Author: EngineHub   File: Spigot_v1_14_R4.java    License: 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 #20
Source Project: worldedit-adapters   Author: EngineHub   File: Spigot_v1_13_R2_2.java    License: 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());
    }
}