Search in sources :

Example 1 with ShortTag

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

the class MobSpawnerBlock method setNbtData.

@Override
public void setNbtData(CompoundTag rootTag) {
    if (rootTag == null) {
        return;
    }
    Map<String, Tag> values = rootTag.getValue();
    Tag t = values.get("id");
    if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals(getNbtId())) {
        throw new RuntimeException(String.format("'%s' tile entity expected", getNbtId()));
    }
    CompoundTag spawnDataTag;
    String mobType;
    ShortTag delayTag;
    try {
        spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class);
        mobType = spawnDataTag.getString("id");
        if (mobType.equals("")) {
            throw new InvalidFormatException("No spawn id.");
        }
        this.mobType = mobType;
        this.spawnData = spawnDataTag;
    } catch (InvalidFormatException ignored) {
        throw new RuntimeException("Invalid mob spawner data: no SpawnData and/or no Delay");
    }
    try {
        delayTag = NBTUtils.getChildTag(values, "Delay", ShortTag.class);
        this.delay = delayTag.getValue();
    } catch (InvalidFormatException ignored) {
        this.delay = -1;
    }
    ShortTag spawnCountTag = null;
    ShortTag spawnRangeTag = null;
    ShortTag minSpawnDelayTag = null;
    ShortTag maxSpawnDelayTag = null;
    ShortTag maxNearbyEntitiesTag = null;
    ShortTag requiredPlayerRangeTag = null;
    ListTag spawnPotentialsTag = null;
    try {
        spawnCountTag = NBTUtils.getChildTag(values, "SpawnCount", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        spawnRangeTag = NBTUtils.getChildTag(values, "SpawnRange", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        minSpawnDelayTag = NBTUtils.getChildTag(values, "MinSpawnDelay", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        maxSpawnDelayTag = NBTUtils.getChildTag(values, "MaxSpawnDelay", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        maxNearbyEntitiesTag = NBTUtils.getChildTag(values, "MaxNearbyEntities", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        requiredPlayerRangeTag = NBTUtils.getChildTag(values, "RequiredPlayerRange", ShortTag.class);
    } catch (InvalidFormatException ignored) {
    }
    try {
        spawnPotentialsTag = NBTUtils.getChildTag(values, "SpawnPotentials", ListTag.class);
    } catch (InvalidFormatException ignored) {
    }
    if (spawnCountTag != null) {
        this.spawnCount = spawnCountTag.getValue();
    }
    if (spawnRangeTag != null) {
        this.spawnRange = spawnRangeTag.getValue();
    }
    if (minSpawnDelayTag != null) {
        this.minSpawnDelay = minSpawnDelayTag.getValue();
    }
    if (maxSpawnDelayTag != null) {
        this.maxSpawnDelay = maxSpawnDelayTag.getValue();
    }
    if (maxNearbyEntitiesTag != null) {
        this.maxNearbyEntities = maxNearbyEntitiesTag.getValue();
    }
    if (requiredPlayerRangeTag != null) {
        this.requiredPlayerRange = requiredPlayerRangeTag.getValue();
    }
    if (spawnPotentialsTag != null) {
        this.spawnPotentials = new ListTag(CompoundTag.class, spawnPotentialsTag.getValue());
    }
}
Also used : StringTag(com.sk89q.jnbt.StringTag) 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) InvalidFormatException(com.sk89q.worldedit.world.storage.InvalidFormatException) ListTag(com.sk89q.jnbt.ListTag) CompoundTag(com.sk89q.jnbt.CompoundTag) ShortTag(com.sk89q.jnbt.ShortTag)

Example 2 with ShortTag

use of com.sk89q.jnbt.ShortTag 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 3 with ShortTag

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

the class SpongeSchematicWriter method write2.

/**
 * Writes a version 2 schematic file.
 *
 * @param clipboard The clipboard
 * @return The schematic map
 */
private Map<String, Tag> write2(Clipboard clipboard) {
    Region region = clipboard.getRegion();
    BlockVector3 origin = clipboard.getOrigin();
    BlockVector3 min = region.getMinimumPoint();
    BlockVector3 offset = min.subtract(origin);
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > MAX_SIZE) {
        throw new IllegalArgumentException("Width of region too large for a .schematic");
    }
    if (height > MAX_SIZE) {
        throw new IllegalArgumentException("Height of region too large for a .schematic");
    }
    if (length > MAX_SIZE) {
        throw new IllegalArgumentException("Length of region too large for a .schematic");
    }
    Map<String, Tag> schematic = new HashMap<>();
    schematic.put("Version", new IntTag(CURRENT_VERSION));
    schematic.put("DataVersion", new IntTag(WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion()));
    Map<String, Tag> metadata = new HashMap<>();
    metadata.put("WEOffsetX", new IntTag(offset.getBlockX()));
    metadata.put("WEOffsetY", new IntTag(offset.getBlockY()));
    metadata.put("WEOffsetZ", new IntTag(offset.getBlockZ()));
    metadata.put("FAWEVersion", new IntTag(Fawe.instance().getVersion().build));
    schematic.put("Metadata", new CompoundTag(metadata));
    schematic.put("Width", new ShortTag((short) width));
    schematic.put("Height", new ShortTag((short) height));
    schematic.put("Length", new ShortTag((short) length));
    // The Sponge format Offset refers to the 'min' points location in the world. That's our 'Origin'
    schematic.put("Offset", new IntArrayTag(new int[] { min.getBlockX(), min.getBlockY(), min.getBlockZ() }));
    int paletteMax = 0;
    Map<String, Integer> palette = new HashMap<>();
    List<CompoundTag> tileEntities = new ArrayList<>();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * height * length);
    for (int y = 0; y < height; y++) {
        int y0 = min.getBlockY() + y;
        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 point = BlockVector3.at(x0, y0, z0);
                BaseBlock block = clipboard.getFullBlock(point);
                if (block.getNbtData() != null) {
                    Map<String, Tag> values = new HashMap<>(block.getNbtData().getValue());
                    // Remove 'id' if it exists. We want 'Id'
                    values.remove("id");
                    // Positions are kept in NBT, we don't want that.
                    values.remove("x");
                    values.remove("y");
                    values.remove("z");
                    values.put("Id", new StringTag(block.getNbtId()));
                    values.put("Pos", new IntArrayTag(new int[] { x, y, z }));
                    tileEntities.add(new CompoundTag(values));
                }
                String blockKey = block.toImmutableState().getAsString();
                int blockId;
                if (palette.containsKey(blockKey)) {
                    blockId = palette.get(blockKey);
                } else {
                    blockId = paletteMax;
                    palette.put(blockKey, blockId);
                    paletteMax++;
                }
                while ((blockId & -128) != 0) {
                    buffer.write(blockId & 127 | 128);
                    blockId >>>= 7;
                }
                buffer.write(blockId);
            }
        }
    }
    schematic.put("PaletteMax", new IntTag(paletteMax));
    Map<String, Tag> paletteTag = new HashMap<>();
    palette.forEach((key, value) -> paletteTag.put(key, new IntTag(value)));
    schematic.put("Palette", new CompoundTag(paletteTag));
    schematic.put("BlockData", new ByteArrayTag(buffer.toByteArray()));
    schematic.put("BlockEntities", new ListTag(CompoundTag.class, tileEntities));
    // version 2 stuff
    if (clipboard.hasBiomes()) {
        writeBiomes(clipboard, schematic);
    }
    if (!clipboard.getEntities().isEmpty()) {
        writeEntities(clipboard, schematic);
    }
    return schematic;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) IntArrayTag(com.sk89q.jnbt.IntArrayTag) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) ListTag(com.sk89q.jnbt.ListTag) ShortTag(com.sk89q.jnbt.ShortTag) Region(com.sk89q.worldedit.regions.Region) 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)

Aggregations

CompoundTag (com.sk89q.jnbt.CompoundTag)3 IntTag (com.sk89q.jnbt.IntTag)3 ListTag (com.sk89q.jnbt.ListTag)3 ShortTag (com.sk89q.jnbt.ShortTag)3 StringTag (com.sk89q.jnbt.StringTag)3 Tag (com.sk89q.jnbt.Tag)3 HashMap (java.util.HashMap)2 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)1 IntArrayTag (com.sk89q.jnbt.IntArrayTag)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Region (com.sk89q.worldedit.regions.Region)1 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)1 InvalidFormatException (com.sk89q.worldedit.world.storage.InvalidFormatException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1