Search in sources :

Example 61 with BlockType

use of com.sk89q.worldedit.world.block.BlockType 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)

Example 62 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project WorldGuard by EngineHub.

the class SpongeUtil method clearSpongeWater.

/**
 * Remove water around a sponge.
 *
 * @param world The world the sponge is in
 * @param ox The x coordinate of the 'sponge' block
 * @param oy The y coordinate of the 'sponge' block
 * @param oz The z coordinate of the 'sponge' block
 */
public static void clearSpongeWater(World world, int ox, int oy, int oz) {
    WorldConfiguration wcfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(world);
    for (int cx = -wcfg.spongeRadius; cx <= wcfg.spongeRadius; cx++) {
        for (int cy = -wcfg.spongeRadius; cy <= wcfg.spongeRadius; cy++) {
            for (int cz = -wcfg.spongeRadius; cz <= wcfg.spongeRadius; cz++) {
                BlockVector3 vector = BlockVector3.at(ox + cx, oy + cy, oz + cz);
                BaseBlock block = world.getFullBlock(vector);
                BlockType blockType = block.getBlockType();
                if (isReplacable(blockType)) {
                    try {
                        world.setBlock(vector, BlockTypes.AIR.getDefaultState());
                    } catch (WorldEditException e) {
                        e.printStackTrace();
                    }
                } else {
                    @SuppressWarnings("unchecked") Property<Object> waterloggedProp = waterloggable.computeIfAbsent(blockType, (bt -> (Property<Object>) bt.getPropertyMap().get("waterlogged")));
                    if (waterloggedProp != null) {
                        try {
                            world.setBlock(vector, block.with(waterloggedProp, false));
                        } catch (WorldEditException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
Also used : Property(com.sk89q.worldedit.registry.state.Property) WorldEditException(com.sk89q.worldedit.WorldEditException) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockType(com.sk89q.worldedit.world.block.BlockType) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) WorldGuard(com.sk89q.worldguard.WorldGuard) Map(java.util.Map) World(com.sk89q.worldedit.world.World) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Maps(com.google.common.collect.Maps) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) BlockType(com.sk89q.worldedit.world.block.BlockType) BlockVector3(com.sk89q.worldedit.math.BlockVector3) WorldEditException(com.sk89q.worldedit.WorldEditException) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) Property(com.sk89q.worldedit.registry.state.Property)

Example 63 with BlockType

use of com.sk89q.worldedit.world.block.BlockType in project WorldGuard by EngineHub.

the class TargetMatcherParser method fromInput.

public TargetMatcher fromInput(String input) throws TargetMatcherParseException {
    input = input.toLowerCase().trim();
    BlockType blockType = BlockTypes.get(input);
    if (blockType != null) {
        if (blockType.hasItemType()) {
            return new ItemBlockMatcher(blockType);
        } else {
            return new BlockMatcher(blockType);
        }
    } else {
        ItemType itemType = ItemTypes.get(input);
        if (itemType == null) {
            throw new TargetMatcherParseException("Unknown block or item name: " + input);
        }
        return new ItemMatcher(itemType);
    }
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType) ItemType(com.sk89q.worldedit.world.item.ItemType)

Aggregations

BlockType (com.sk89q.worldedit.world.block.BlockType)63 BlockState (com.sk89q.worldedit.world.block.BlockState)20 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)18 Map (java.util.Map)12 HashMap (java.util.HashMap)9 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)8 ArrayList (java.util.ArrayList)8 TextureUtil (com.fastasyncworldedit.core.util.TextureUtil)7 World (com.sk89q.worldedit.world.World)7 List (java.util.List)7 CompoundTag (com.sk89q.jnbt.CompoundTag)5 Tag (com.sk89q.jnbt.Tag)5 EditSession (com.sk89q.worldedit.EditSession)5 Property (com.sk89q.worldedit.registry.state.Property)5 Direction (com.sk89q.worldedit.util.Direction)5 IOException (java.io.IOException)5 Locale (java.util.Locale)5 Set (java.util.Set)5 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)4 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)4