Search in sources :

Example 1 with DataFixer

use of com.sk89q.worldedit.world.DataFixer in project FastAsyncWorldEdit by IntellectualSites.

the class ChunkStoreHelper method getChunk.

/**
 * Convert a chunk NBT tag into a {@link Chunk} implementation.
 *
 * @param rootTag     the root tag of the chunk
 * @param entitiesTag supplier to provide entities tag. Only required for 1.17+ where entities are stored in a separate
 *                    location
 * @return a Chunk implementation
 * @throws DataException if the rootTag is not valid chunk data
 * @since TODO
 */
public static Chunk getChunk(CompoundTag rootTag, Supplier<CompoundTag> entitiesTag) throws DataException {
    // FAWE end
    int dataVersion = rootTag.getInt("DataVersion");
    if (dataVersion == 0) {
        dataVersion = -1;
    }
    final Platform platform = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING);
    final int currentDataVersion = platform.getDataVersion();
    if ((dataVersion > 0 || hasLevelSections(rootTag)) && dataVersion < currentDataVersion) {
        // only fix up MCA format, DFU doesn't support MCR chunks
        final DataFixer dataFixer = platform.getDataFixer();
        if (dataFixer != null) {
            // FAWE start - CBT
            rootTag = (CompoundTag) AdventureNBTConverter.fromAdventure(dataFixer.fixUp(DataFixer.FixTypes.CHUNK, rootTag.asBinaryTag(), dataVersion));
            // FAWE end
            dataVersion = currentDataVersion;
        }
    }
    if (dataVersion >= Constants.DATA_VERSION_MC_1_18) {
        return new AnvilChunk18(rootTag, entitiesTag);
    }
    Map<String, Tag> children = rootTag.getValue();
    CompoundTag tag = null;
    // Find Level tag
    for (Map.Entry<String, Tag> entry : children.entrySet()) {
        if (entry.getKey().equals("Level")) {
            if (entry.getValue() instanceof CompoundTag) {
                tag = (CompoundTag) entry.getValue();
                break;
            } else {
                throw new ChunkStoreException("CompoundTag expected for 'Level'; got " + entry.getValue().getClass().getName());
            }
        }
    }
    if (tag == null) {
        throw new ChunkStoreException("Missing root 'Level' tag");
    }
    // FAWE start - biome and entity restore
    if (dataVersion >= Constants.DATA_VERSION_MC_1_17) {
        return new AnvilChunk17(tag, entitiesTag);
    }
    // FAWE end
    if (dataVersion >= Constants.DATA_VERSION_MC_1_16) {
        return new AnvilChunk16(tag);
    }
    // FAWE start - biome and entity restore
    if (dataVersion >= Constants.DATA_VERSION_MC_1_15) {
        return new AnvilChunk15(tag);
    }
    // FAWE end
    if (dataVersion >= Constants.DATA_VERSION_MC_1_13) {
        return new AnvilChunk13(tag);
    }
    Map<String, Tag> tags = tag.getValue();
    if (tags.containsKey("Sections")) {
        return new AnvilChunk(tag);
    }
    return new OldChunk(tag);
}
Also used : AnvilChunk18(com.sk89q.worldedit.world.chunk.AnvilChunk18) Platform(com.sk89q.worldedit.extension.platform.Platform) OldChunk(com.sk89q.worldedit.world.chunk.OldChunk) AnvilChunk13(com.sk89q.worldedit.world.chunk.AnvilChunk13) AnvilChunk15(com.sk89q.worldedit.world.chunk.AnvilChunk15) AnvilChunk16(com.sk89q.worldedit.world.chunk.AnvilChunk16) AnvilChunk17(com.sk89q.worldedit.world.chunk.AnvilChunk17) AnvilChunk(com.sk89q.worldedit.world.chunk.AnvilChunk) CompoundTag(com.sk89q.jnbt.CompoundTag) Tag(com.sk89q.jnbt.Tag) DataFixer(com.sk89q.worldedit.world.DataFixer) Map(java.util.Map) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 2 with DataFixer

use of com.sk89q.worldedit.world.DataFixer in project FastAsyncWorldEdit by IntellectualSites.

the class LegacyMapper method loadFromResource.

/**
 * Attempt to load the data from file.
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    URL url = resourceLoader.getResource(LegacyMapper.class, "legacy.json");
    if (url == null) {
        throw new IOException("Could not find legacy.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
    }.getType());
    DataFixer fixer = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataFixer();
    ParserContext parserContext = new ParserContext();
    parserContext.setPreferringWildcard(false);
    parserContext.setRestricted(false);
    // This is legacy. Don't match itself.
    parserContext.setTryLegacy(false);
    for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
        String id = blockEntry.getKey();
        final String value = blockEntry.getValue();
        // FAWE start
        Integer combinedId = getCombinedId(blockEntry.getKey());
        blockEntries.put(id, value);
        // FAWE end
        BlockState state = null;
        // FAWE start
        try {
            state = BlockState.get(null, blockEntry.getValue());
            BlockType type = state.getBlockType();
            if (type.hasProperty(PropertyKey.WATERLOGGED)) {
                state = state.with(PropertyKey.WATERLOGGED, false);
            }
        } catch (InputParseException f) {
            BlockFactory blockFactory = WorldEdit.getInstance().getBlockFactory();
            // if fixer is available, try using that first, as some old blocks that were renamed share names with new blocks
            if (fixer != null) {
                try {
                    String newEntry = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, value, Constants.DATA_VERSION_MC_1_13_2);
                    state = blockFactory.parseFromInput(newEntry, parserContext).toImmutableState();
                } catch (InputParseException ignored) {
                }
            }
            // if it's still null, the fixer was unavailable or failed
            if (state == null) {
                try {
                    state = blockFactory.parseFromInput(value, parserContext).toImmutableState();
                } catch (InputParseException ignored) {
                }
            }
            // if it's still null, both fixer and default failed
            if (state == null) {
                LOGGER.error("Unknown block: {}. Neither the DataFixer nor defaulting worked to recognize this block.", value);
            } else {
                // it's not null so one of them succeeded, now use it
                blockToStringMap.put(state, id);
                stringToBlockMap.put(id, state);
            }
        }
        // FAWE start
        if (state != null) {
            blockArr[combinedId] = state.getInternalId();
            blockStateToLegacyId4Data.put(state.getInternalId(), combinedId);
            blockStateToLegacyId4Data.putIfAbsent(state.getInternalBlockTypeId(), combinedId);
        }
    }
    for (int id = 0; id < 256; id++) {
        int combinedId = id << 4;
        int base = blockArr[combinedId];
        if (base != 0) {
            for (int data_ = 0; data_ < 16; data_++, combinedId++) {
                if (blockArr[combinedId] == 0) {
                    blockArr[combinedId] = base;
                }
            }
        }
    }
    for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
        String id = itemEntry.getKey();
        String value = itemEntry.getValue();
        ItemType type = ItemTypes.get(value);
        if (type == null && fixer != null) {
            value = fixer.fixUp(DataFixer.FixTypes.ITEM_TYPE, value, Constants.DATA_VERSION_MC_1_13_2);
            type = ItemTypes.get(value);
        }
        if (type == null) {
            LOGGER.error("Unknown item: {}. Neither the DataFixer nor defaulting worked to recognize this item.", value);
        } else {
            try {
                itemMap.put(getCombinedId(id), type);
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) BlockFactory(com.sk89q.worldedit.extension.factory.BlockFactory) VectorAdapter(com.sk89q.worldedit.util.gson.VectorAdapter) ItemType(com.sk89q.worldedit.world.item.ItemType) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) IOException(java.io.IOException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) TypeToken(com.google.gson.reflect.TypeToken) DataFixer(com.sk89q.worldedit.world.DataFixer) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap)

Aggregations

DataFixer (com.sk89q.worldedit.world.DataFixer)2 Map (java.util.Map)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeToken (com.google.gson.reflect.TypeToken)1 CompoundTag (com.sk89q.jnbt.CompoundTag)1 Tag (com.sk89q.jnbt.Tag)1 BlockFactory (com.sk89q.worldedit.extension.factory.BlockFactory)1 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)1 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)1 Platform (com.sk89q.worldedit.extension.platform.Platform)1 VectorAdapter (com.sk89q.worldedit.util.gson.VectorAdapter)1 BlockState (com.sk89q.worldedit.world.block.BlockState)1 BlockType (com.sk89q.worldedit.world.block.BlockType)1 AnvilChunk (com.sk89q.worldedit.world.chunk.AnvilChunk)1 AnvilChunk13 (com.sk89q.worldedit.world.chunk.AnvilChunk13)1 AnvilChunk15 (com.sk89q.worldedit.world.chunk.AnvilChunk15)1 AnvilChunk16 (com.sk89q.worldedit.world.chunk.AnvilChunk16)1