Search in sources :

Example 1 with IntTag

use of com.sk89q.jnbt.IntTag in project FastAsyncWorldEdit by IntellectualSites.

the class SpongeSchematicReader method readBiomes.

private void readBiomes(BlockArrayClipboard clipboard, Map<String, Tag> schematic) throws IOException {
    ByteArrayTag dataTag = requireTag(schematic, "BiomeData", ByteArrayTag.class);
    IntTag maxTag = requireTag(schematic, "BiomePaletteMax", IntTag.class);
    CompoundTag paletteTag = requireTag(schematic, "BiomePalette", CompoundTag.class);
    Map<Integer, BiomeType> palette = new HashMap<>();
    if (maxTag.getValue() != paletteTag.getValue().size()) {
        throw new IOException("Biome palette size does not match expected size.");
    }
    for (Entry<String, Tag> palettePart : paletteTag.getValue().entrySet()) {
        String key = palettePart.getKey();
        if (fixer != null) {
            key = fixer.fixUp(DataFixer.FixTypes.BIOME, key, dataVersion);
        }
        BiomeType biome = BiomeTypes.get(key);
        if (biome == null) {
            LOGGER.warn("Unknown biome type :" + key + " in palette. Are you missing a mod or using a schematic made in a newer version of Minecraft?");
        }
        Tag idTag = palettePart.getValue();
        if (!(idTag instanceof IntTag)) {
            throw new IOException("Biome mapped to non-Int tag.");
        }
        palette.put(((IntTag) idTag).getValue(), biome);
    }
    int width = clipboard.getDimensions().getX();
    byte[] biomes = dataTag.getValue();
    int biomeIndex = 0;
    int biomeJ = 0;
    int bVal;
    int varIntLength;
    BlockVector3 min = clipboard.getMinimumPoint();
    while (biomeJ < biomes.length) {
        bVal = 0;
        varIntLength = 0;
        while (true) {
            bVal |= (biomes[biomeJ] & 127) << (varIntLength++ * 7);
            if (varIntLength > 5) {
                throw new IOException("VarInt too big (probably corrupted data)");
            }
            if (((biomes[biomeJ] & 128) != 128)) {
                biomeJ++;
                break;
            }
            biomeJ++;
        }
        int z = biomeIndex / width;
        int x = biomeIndex % width;
        BiomeType type = palette.get(bVal);
        for (int y = 0; y < clipboard.getRegion().getHeight(); y++) {
            clipboard.setBiome(min.add(x, y, z), type);
        }
        biomeIndex++;
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) StringTag(com.sk89q.jnbt.StringTag) ShortTag(com.sk89q.jnbt.ShortTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag)

Example 2 with IntTag

use of com.sk89q.jnbt.IntTag in project FastAsyncWorldEdit by IntellectualSites.

the class BannerBlockCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    Tag typeTag = values.get("Base");
    if (typeTag instanceof IntTag) {
        boolean isWall = block.getBlockType() == BlockTypes.WHITE_WALL_BANNER;
        String bannerType = convertBannerType(((IntTag) typeTag).getValue(), isWall);
        if (bannerType != null) {
            BlockType type = BlockTypes.get("minecraft:" + bannerType);
            if (type != null) {
                BlockState state = type.getDefaultState();
                if (isWall) {
                    Property<Direction> facingProp = type.getProperty("facing");
                    state = state.with(facingProp, block.getState(FacingProperty));
                } else {
                    Property<Integer> rotationProp = type.getProperty("rotation");
                    state = state.with(rotationProp, block.getState(RotationProperty));
                }
                values.remove("Base");
                Tag patternsTag = values.get("Patterns");
                if (patternsTag instanceof ListTag) {
                    List<Tag> tempList = new ArrayList<>();
                    for (Tag pattern : ((ListTag) patternsTag).getValue()) {
                        if (pattern instanceof CompoundTag) {
                            Map<String, Tag> patternMap = ((CompoundTag) pattern).getValue();
                            Tag colorTag = patternMap.get("Color");
                            CompoundTagBuilder builder = CompoundTagBuilder.create();
                            builder.putAll(patternMap);
                            if (colorTag instanceof IntTag) {
                                builder.putInt("Color", 15 - ((IntTag) colorTag).getValue());
                            }
                            tempList.add(builder.build());
                        } else {
                            tempList.add(pattern);
                        }
                    }
                    values.put("Patterns", new ListTag(((ListTag) patternsTag).getType(), tempList));
                }
                return state;
            }
        }
    }
    return block;
}
Also used : ArrayList(java.util.ArrayList) Direction(com.sk89q.worldedit.util.Direction) ListTag(com.sk89q.jnbt.ListTag) CompoundTagBuilder(com.sk89q.jnbt.CompoundTagBuilder) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 3 with IntTag

use of com.sk89q.jnbt.IntTag in project FastAsyncWorldEdit by IntellectualSites.

the class SpongeSchematicWriter method writeBiomes.

private void writeBiomes(Clipboard clipboard, Map<String, Tag> schematic) {
    BlockVector3 min = clipboard.getMinimumPoint();
    int width = clipboard.getRegion().getWidth();
    int length = clipboard.getRegion().getLength();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * length);
    int paletteMax = 0;
    Map<String, Integer> palette = new HashMap<>();
    for (int z = 0; z < length; z++) {
        int z0 = min.getBlockZ() + z;
        for (int x = 0; x < width; x++) {
            int x0 = min.getBlockX() + x;
            BlockVector3 pt = BlockVector3.at(x0, min.getBlockY(), z0);
            BiomeType biome = clipboard.getBiome(pt);
            String biomeKey = biome.getId();
            int biomeId;
            if (palette.containsKey(biomeKey)) {
                biomeId = palette.get(biomeKey);
            } else {
                biomeId = paletteMax;
                palette.put(biomeKey, biomeId);
                paletteMax++;
            }
            while ((biomeId & -128) != 0) {
                buffer.write(biomeId & 127 | 128);
                biomeId >>>= 7;
            }
            buffer.write(biomeId);
        }
    }
    schematic.put("BiomePaletteMax", new IntTag(paletteMax));
    Map<String, Tag> paletteTag = new HashMap<>();
    palette.forEach((key, value) -> paletteTag.put(key, new IntTag(value)));
    schematic.put("BiomePalette", new CompoundTag(paletteTag));
    schematic.put("BiomeData", new ByteArrayTag(buffer.toByteArray()));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) StringTag(com.sk89q.jnbt.StringTag) ShortTag(com.sk89q.jnbt.ShortTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) ByteArrayTag(com.sk89q.jnbt.ByteArrayTag)

Example 4 with IntTag

use of com.sk89q.jnbt.IntTag in project FastAsyncWorldEdit by IntellectualSites.

the class MobSpawnerBlock method getNbtData.

@Override
public CompoundTag getNbtData() {
    Map<String, Tag> values = new HashMap<>();
    values.put("Delay", new ShortTag(delay));
    values.put("SpawnCount", new ShortTag(spawnCount));
    values.put("SpawnRange", new ShortTag(spawnRange));
    values.put("MinSpawnDelay", new ShortTag(minSpawnDelay));
    values.put("MaxSpawnDelay", new ShortTag(maxSpawnDelay));
    values.put("MaxNearbyEntities", new ShortTag(maxNearbyEntities));
    values.put("RequiredPlayerRange", new ShortTag(requiredPlayerRange));
    if (spawnData == null) {
        values.put("SpawnData", new CompoundTag(ImmutableMap.of("id", new StringTag(mobType))));
    } else {
        values.put("SpawnData", new CompoundTag(spawnData.getValue()));
    }
    if (spawnPotentials == null) {
        values.put("SpawnPotentials", new ListTag(CompoundTag.class, ImmutableList.of(new CompoundTag(ImmutableMap.of("Weight", new IntTag(1), "Entity", new CompoundTag(ImmutableMap.of("id", new StringTag(mobType))))))));
    } else {
        values.put("SpawnPotentials", new ListTag(CompoundTag.class, spawnPotentials.getValue()));
    }
    return new CompoundTag(values);
}
Also used : StringTag(com.sk89q.jnbt.StringTag) HashMap(java.util.HashMap) ListTag(com.sk89q.jnbt.ListTag) StringTag(com.sk89q.jnbt.StringTag) ShortTag(com.sk89q.jnbt.ShortTag) IntTag(com.sk89q.jnbt.IntTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) ListTag(com.sk89q.jnbt.ListTag) ShortTag(com.sk89q.jnbt.ShortTag) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag)

Example 5 with IntTag

use of com.sk89q.jnbt.IntTag in project FastAsyncWorldEdit by IntellectualSites.

the class MinecraftStructure method read.

@Override
public Clipboard read(UUID clipboardId) throws IOException {
    NamedTag rootTag = inputStream.readNamedTag();
    // MC structures are all unnamed, but this doesn't seem to be necessary? might remove this later
    if (!rootTag.getName().isEmpty()) {
        throw new IOException("Root tag has name - are you sure this is a structure?");
    }
    Map<String, Tag> tags = ((CompoundTag) rootTag.getTag()).getValue();
    ListTag size = (ListTag) tags.get("size");
    int width = size.getInt(0);
    int height = size.getInt(1);
    int length = size.getInt(2);
    // Init clipboard
    BlockVector3 origin = BlockVector3.at(0, 0, 0);
    CuboidRegion region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
    Clipboard clipboard = new BlockArrayClipboard(region, clipboardId);
    // Blocks
    ListTag blocks = (ListTag) tags.get("blocks");
    if (blocks != null) {
        // Palette
        List<CompoundTag> palette = (List<CompoundTag>) tags.get("palette").getValue();
        BlockState[] combinedArray = new BlockState[palette.size()];
        for (int i = 0; i < palette.size(); i++) {
            CompoundTag compound = palette.get(i);
            Map<String, Tag> map = compound.getValue();
            String name = ((StringTag) map.get("Name")).getValue();
            BlockType type = BlockTypes.get(name);
            BlockState state = type.getDefaultState();
            CompoundTag properties = (CompoundTag) map.get("Properties");
            if (properties != null) {
                for (Map.Entry<String, Tag> entry : properties.getValue().entrySet()) {
                    String key = entry.getKey();
                    String value = ((StringTag) entry.getValue()).getValue();
                    Property<Object> property = type.getProperty(key);
                    state = state.with(property, property.getValueFor(value));
                }
            }
            combinedArray[i] = state;
        }
        // Populate blocks
        List<CompoundTag> blocksList = (List<CompoundTag>) tags.get("blocks").getValue();
        try {
            for (CompoundTag compound : blocksList) {
                Map<String, Tag> blockMap = compound.getValue();
                IntTag stateTag = (IntTag) blockMap.get("state");
                ListTag posTag = (ListTag) blockMap.get("pos");
                BlockState state = combinedArray[stateTag.getValue()];
                int x = posTag.getInt(0);
                int y = posTag.getInt(1);
                int z = posTag.getInt(2);
                if (state.getBlockType().getMaterial().hasContainer()) {
                    CompoundTag nbt = (CompoundTag) blockMap.get("nbt");
                    if (nbt != null) {
                        BaseBlock block = state.toBaseBlock(nbt);
                        clipboard.setBlock(x, y, z, block);
                        continue;
                    }
                }
                clipboard.setBlock(x, y, z, state);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Entities
    ListTag entities = (ListTag) tags.get("entities");
    if (entities != null) {
        List<CompoundTag> entityList = (List<CompoundTag>) (List<?>) entities.getValue();
        for (CompoundTag entityEntry : entityList) {
            Map<String, Tag> entityEntryMap = entityEntry.getValue();
            ListTag posTag = (ListTag) entityEntryMap.get("pos");
            CompoundTag nbtTag = (CompoundTag) entityEntryMap.get("nbt");
            String id = nbtTag.getString("Id");
            Location location = NBTConversions.toLocation(clipboard, posTag, nbtTag.getListTag("Rotation"));
            if (!id.isEmpty()) {
                BaseEntity state = new BaseEntity(EntityTypes.get(id), nbtTag);
                clipboard.createEntity(location, state);
            }
        }
    }
    return clipboard;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) NamedTag(com.sk89q.jnbt.NamedTag) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ListTag(com.sk89q.jnbt.ListTag) IOException(java.io.IOException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) StringTag(com.sk89q.jnbt.StringTag) ListTag(com.sk89q.jnbt.ListTag) IntTag(com.sk89q.jnbt.IntTag) NamedTag(com.sk89q.jnbt.NamedTag) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) HashMap(java.util.HashMap) Map(java.util.Map) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) Location(com.sk89q.worldedit.util.Location)

Aggregations

IntTag (com.sk89q.jnbt.IntTag)12 Tag (com.sk89q.jnbt.Tag)12 CompoundTag (com.sk89q.jnbt.CompoundTag)10 HashMap (java.util.HashMap)9 ListTag (com.sk89q.jnbt.ListTag)7 StringTag (com.sk89q.jnbt.StringTag)7 ShortTag (com.sk89q.jnbt.ShortTag)5 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)5 BlockState (com.sk89q.worldedit.world.block.BlockState)5 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)4 IntArrayTag (com.sk89q.jnbt.IntArrayTag)4 NamedTag (com.sk89q.jnbt.NamedTag)3 BlockType (com.sk89q.worldedit.world.block.BlockType)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IntTriple (com.fastasyncworldedit.core.math.IntTriple)2 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 Region (com.sk89q.worldedit.regions.Region)2 Direction (com.sk89q.worldedit.util.Direction)2