Search in sources :

Example 1 with StringTag

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

the class SignCompatibilityHandler method updateNBT.

@Override
public <B extends BlockStateHolder<B>> BlockStateHolder<?> updateNBT(B block, Map<String, Tag> values) {
    for (int i = 0; i < 4; ++i) {
        String key = "Text" + (i + 1);
        Tag value = values.get(key);
        if (value instanceof StringTag) {
            String storedString = ((StringTag) value).getValue();
            JsonElement jsonElement = null;
            if (storedString != null && storedString.startsWith("{")) {
                try {
                    jsonElement = new JsonParser().parse(storedString);
                } catch (JsonSyntaxException ex) {
                // ignore: jsonElement will be null in the next check
                }
            }
            if (jsonElement == null) {
                jsonElement = new JsonPrimitive(storedString == null ? "" : storedString);
            }
            if (jsonElement.isJsonObject()) {
                continue;
            }
            if (jsonElement.isJsonNull()) {
                jsonElement = new JsonPrimitive("");
            }
            JsonObject jsonTextObject = new JsonObject();
            jsonTextObject.add("text", jsonElement);
            values.put("Text" + (i + 1), new StringTag(jsonTextObject.toString()));
        }
    }
    return block;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Tag(com.sk89q.jnbt.Tag) StringTag(com.sk89q.jnbt.StringTag) JsonParser(com.google.gson.JsonParser)

Example 2 with StringTag

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

use of com.sk89q.jnbt.StringTag 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 4 with StringTag

use of com.sk89q.jnbt.StringTag 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)

Example 5 with StringTag

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

the class MinecraftStructure method write.

public void write(Clipboard clipboard, String owner) throws IOException {
    Region region = clipboard.getRegion();
    int width = region.getWidth();
    int height = region.getHeight();
    int length = region.getLength();
    if (width > WARN_SIZE || height > WARN_SIZE || length > WARN_SIZE) {
        LOGGER.info("A structure longer than 32 is unsupported by minecraft (but probably still works)");
    }
    Map<String, Object> structure = FaweCache.INSTANCE.asMap("version", 1, "author", owner);
    // ignored: version / owner
    Int2ObjectArrayMap<Integer> indexes = new Int2ObjectArrayMap<>();
    // Size
    structure.put("size", Arrays.asList(width, height, length));
    // Palette
    ArrayList<HashMap<String, Object>> palette = new ArrayList<>();
    for (BlockVector3 point : region) {
        BlockState block = clipboard.getBlock(point);
        int combined = block.getInternalId();
        BlockType type = block.getBlockType();
        if (type == BlockTypes.STRUCTURE_VOID || indexes.containsKey(combined)) {
            continue;
        }
        indexes.put(combined, (Integer) palette.size());
        HashMap<String, Object> paletteEntry = new HashMap<>();
        paletteEntry.put("Name", type.getId());
        if (block.getInternalId() != type.getInternalId()) {
            Map<String, Object> properties = null;
            for (AbstractProperty property : (List<AbstractProperty<?>>) type.getProperties()) {
                int propIndex = property.getIndex(block.getInternalId());
                if (propIndex != 0) {
                    if (properties == null) {
                        properties = new HashMap<>();
                    }
                    Object value = property.getValues().get(propIndex);
                    properties.put(property.getName(), value.toString());
                }
            }
            if (properties != null) {
                paletteEntry.put("Properties", properties);
            }
        }
        palette.add(paletteEntry);
    }
    if (!palette.isEmpty()) {
        structure.put("palette", palette);
    }
    // Blocks
    ArrayList<Map<String, Object>> blocks = new ArrayList<>();
    BlockVector3 min = region.getMinimumPoint();
    for (BlockVector3 point : region) {
        BaseBlock block = clipboard.getFullBlock(point);
        if (block.getBlockType() != BlockTypes.STRUCTURE_VOID) {
            int combined = block.getInternalId();
            int index = indexes.get(combined);
            List<Integer> pos = Arrays.asList(point.getX() - min.getX(), point.getY() - min.getY(), point.getZ() - min.getZ());
            if (!block.hasNbtData()) {
                blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos));
            } else {
                blocks.add(FaweCache.INSTANCE.asMap("state", index, "pos", pos, "nbt", block.getNbtData()));
            }
        }
    }
    if (!blocks.isEmpty()) {
        structure.put("blocks", blocks);
    }
    // Entities
    ArrayList<Map<String, Object>> entities = new ArrayList<>();
    for (Entity entity : clipboard.getEntities()) {
        Location loc = entity.getLocation();
        List<Double> pos = Arrays.asList(loc.getX(), loc.getY(), loc.getZ());
        List<Integer> blockPos = Arrays.asList(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
        BaseEntity state = entity.getState();
        if (state != null) {
            CompoundTag nbt = state.getNbtData();
            Map<String, Tag> nbtMap = nbt.getValue();
            // Replace rotation data
            nbtMap.put("Rotation", writeRotation(entity.getLocation()));
            nbtMap.put("id", new StringTag(state.getType().getId()));
            Map<String, Object> entityMap = FaweCache.INSTANCE.asMap("pos", pos, "blockPos", blockPos, "nbt", nbt);
            entities.add(entityMap);
        }
    }
    if (!entities.isEmpty()) {
        structure.put("entities", entities);
    }
    out.writeNamedTag("", FaweCache.INSTANCE.asTag(structure));
    close();
}
Also used : StringTag(com.sk89q.jnbt.StringTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) Entity(com.sk89q.worldedit.entity.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) AbstractProperty(com.sk89q.worldedit.registry.state.AbstractProperty) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) 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) HashMap(java.util.HashMap) Map(java.util.Map) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) Location(com.sk89q.worldedit.util.Location)

Aggregations

StringTag (com.sk89q.jnbt.StringTag)16 Tag (com.sk89q.jnbt.Tag)15 CompoundTag (com.sk89q.jnbt.CompoundTag)14 IntTag (com.sk89q.jnbt.IntTag)8 ListTag (com.sk89q.jnbt.ListTag)8 HashMap (java.util.HashMap)8 ShortTag (com.sk89q.jnbt.ShortTag)5 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)5 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)5 Location (com.sk89q.worldedit.util.Location)5 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)5 BlockState (com.sk89q.worldedit.world.block.BlockState)5 Region (com.sk89q.worldedit.regions.Region)4 ArrayList (java.util.ArrayList)4 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)3 IntArrayTag (com.sk89q.jnbt.IntArrayTag)3 NamedTag (com.sk89q.jnbt.NamedTag)3 BlockArrayClipboard (com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard)3 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)3 Entity (com.sk89q.worldedit.entity.Entity)2