Search in sources :

Example 56 with BlockType

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

the class EditSession method fall.

public <B extends BlockStateHolder<B>> int fall(final Region region, boolean fullHeight, final B replace) {
    FlatRegion flat = asFlatRegion(region);
    final int startPerformY = region.getMinimumPoint().getBlockY();
    final int startCheckY = fullHeight ? getMinY() : startPerformY;
    final int endY = region.getMaximumPoint().getBlockY();
    RegionVisitor visitor = new RegionVisitor(flat, pos -> {
        int x = pos.getX();
        int z = pos.getZ();
        int freeSpot = startCheckY;
        for (int y = startCheckY; y <= endY; y++) {
            if (y < startPerformY) {
                if (!getBlockType(x, y, z).getMaterial().isAir()) {
                    freeSpot = y + 1;
                }
                continue;
            }
            BlockType block = getBlockType(x, y, z);
            if (!block.getMaterial().isAir()) {
                if (freeSpot != y) {
                    setBlock(x, freeSpot, z, block);
                    setBlock(x, y, z, replace);
                }
                freeSpot++;
            }
        }
        return true;
    }, this);
    Operations.completeBlindly(visitor);
    return this.changes;
}
Also used : RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) FlatRegionVisitor(com.sk89q.worldedit.function.visitor.FlatRegionVisitor) BlockType(com.sk89q.worldedit.world.block.BlockType) FlatRegion(com.sk89q.worldedit.regions.FlatRegion) Regions.asFlatRegion(com.sk89q.worldedit.regions.Regions.asFlatRegion)

Example 57 with BlockType

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

the class TextureUtil method getNearestBlock.

protected BlockType getNearestBlock(BlockType block, boolean darker) {
    int color = getColor(block);
    if (color == 0) {
        return block;
    }
    BlockType darkerBlock = getNearestBlock(color, darker);
    return darkerBlock != null ? darkerBlock : block;
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 58 with BlockType

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

the class TextureUtil method loadModTextures.

public void loadModTextures() throws IOException {
    Int2ObjectOpenHashMap<Integer> colorMap = new Int2ObjectOpenHashMap<>();
    Int2ObjectOpenHashMap<Long> distanceMap = new Int2ObjectOpenHashMap<>();
    Gson gson = new Gson();
    if (folder.exists()) {
        // Get all the jar files
        File[] files = folder.listFiles((dir, name) -> name.endsWith(".jar"));
        if (files.length == 0) {
            new File(Fawe.platform().getDirectory() + "/" + Settings.settings().PATHS.TEXTURES + "/").mkdirs();
            try (BufferedInputStream in = new BufferedInputStream(new URL("https://launcher.mojang.com/v1/objects/2e9a3e3107cca00d6bc9c97bf7d149cae163ef21/client.jar").openStream());
                FileOutputStream fileOutputStream = new FileOutputStream(Fawe.platform().getDirectory() + "/" + Settings.settings().PATHS.TEXTURES + "/1.18.2.jar")) {
                byte[] dataBuffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                    fileOutputStream.write(dataBuffer, 0, bytesRead);
                }
                fileOutputStream.close();
                files = folder.listFiles((dir, name) -> name.endsWith(".jar"));
            } catch (IOException e) {
                LOGGER.error("Could not download version jar. Please do so manually by creating a `FastAsyncWorldEdit/textures` " + "folder with a `.minecraft/versions` jar or mods in it.");
                LOGGER.error("If the file exists, please make sure the server has read access to the directory.");
            }
        }
        if ((files.length > 0)) {
            for (File file : files) {
                ZipFile zipFile = new ZipFile(file);
                // Get all the groups in the current jar
                // The vanilla textures are in `assets/minecraft`
                // A jar may contain textures for multiple mods
                String modelsDir = "assets/%1$s/models/block/%2$s.json";
                String texturesDir = "assets/%1$s/textures/%2$s.png";
                Type typeToken = new TypeToken<Map<String, Object>>() {
                }.getType();
                for (BlockType blockType : BlockTypesCache.values) {
                    if (!blockType.getMaterial().isFullCube() || blockType.getId().toLowerCase().contains("shulker")) {
                        continue;
                    }
                    switch(blockType.getId().toLowerCase(Locale.ROOT)) {
                        case "slime_block":
                        case "honey_block":
                        case "mob_spawner":
                        case "spawner":
                            continue;
                    }
                    int combined = blockType.getInternalId();
                    String id = blockType.getId();
                    String[] split = id.split(":", 2);
                    String name = split.length == 1 ? id : split[1];
                    String nameSpace = split.length == 1 ? "" : split[0];
                    // Read models
                    String modelFileName = String.format(modelsDir, nameSpace, name);
                    ZipEntry entry = getEntry(zipFile, modelFileName);
                    if (entry == null) {
                        LOGGER.error("Cannot find {} in {}", modelFileName, file);
                        continue;
                    }
                    String textureFileName;
                    try (InputStream is = zipFile.getInputStream(entry)) {
                        JsonReader reader = new JsonReader(new InputStreamReader(is, StandardCharsets.UTF_8));
                        Map<String, Object> root = gson.fromJson(reader, typeToken);
                        Map<String, Object> textures = (Map) root.get("textures");
                        if (textures == null) {
                            continue;
                        }
                        Set<String> models = new HashSet<>();
                        // Get models
                        for (Map.Entry<String, Object> stringObjectEntry : textures.entrySet()) {
                            Object value = stringObjectEntry.getValue();
                            if (value instanceof String) {
                                models.add((String) value);
                            } else if (value instanceof Map) {
                                value = ((Map<?, ?>) value).get("model");
                                if (value != null) {
                                    models.add((String) value);
                                }
                            }
                        }
                        if (models.size() != 1) {
                            continue;
                        }
                        String[] texSplit = models.iterator().next().split(":");
                        String texture = texSplit[texSplit.length - 1];
                        textureFileName = String.format(texturesDir, nameSpace, texture);
                    }
                    BufferedImage image = readImage(zipFile, textureFileName);
                    if (image == null) {
                        LOGGER.error("Cannot find {}", textureFileName);
                        continue;
                    }
                    int color = ImageUtil.getColor(image);
                    long distance = getDistance(image, color);
                    distanceMap.put(combined, (Long) distance);
                    colorMap.put(combined, (Integer) color);
                }
                Integer grass = null;
                {
                    String grassFileName = String.format(texturesDir, "minecraft", "block/grass_block_top");
                    BufferedImage image = readImage(zipFile, grassFileName);
                    if (image != null) {
                        grass = ImageUtil.getColor(image);
                    }
                }
                if (grass != null) {
                    colorMap.put(BlockTypes.GRASS_BLOCK.getInternalId(), grass);
                    // assets\minecraft\textures\colormap
                    ZipEntry grassEntry = getEntry(zipFile, "assets/minecraft/textures/colormap/grass.png");
                    if (grassEntry != null) {
                        try (InputStream is = zipFile.getInputStream(grassEntry)) {
                            BufferedImage image = ImageIO.read(is);
                            // Update biome colors
                            for (BiomeColor biome : biomes) {
                                float adjTemp = MathMan.clamp(biome.temperature, 0.0f, 1.0f);
                                float adjRainfall = MathMan.clamp(biome.rainfall, 0.0f, 1.0f) * adjTemp;
                                int x = (int) (255 - adjTemp * 255);
                                int z = (int) (255 - adjRainfall * 255);
                                biome.grass = image.getRGB(x, z);
                            }
                        }
                        // swampland: perlin - avoid
                        biomes[6].grass = 0;
                        biomes[134].grass = 0;
                        // roofed forest: averaged w/ 0x28340A
                        biomes[29].grass = multiplyColor(biomes[29].grass, 0x28340A + (255 << 24));
                        biomes[157].grass = multiplyColor(biomes[157].grass, 0x28340A + (255 << 24));
                        // mesa : 0x90814D
                        biomes[37].grass = 0x90814D + (255 << 24);
                        biomes[38].grass = 0x90814D + (255 << 24);
                        biomes[39].grass = 0x90814D + (255 << 24);
                        biomes[165].grass = 0x90814D + (255 << 24);
                        biomes[166].grass = 0x90814D + (255 << 24);
                        biomes[167].grass = 0x90814D + (255 << 24);
                        List<BiomeColor> valid = new ArrayList<>();
                        for (BiomeColor biome : biomes) {
                            // biome.grass = multiplyColor(biome.grass, grass);
                            if (biome.grass != 0 && !biome.name.equalsIgnoreCase("Unknown Biome")) {
                                valid.add(biome);
                            }
                            biome.grassCombined = multiplyColor(grass, biome.grass);
                        }
                        this.validBiomes = valid.toArray(new BiomeColor[0]);
                        ArrayList<BiomeColor> uniqueColors = new ArrayList<>();
                        Set<Integer> uniqueBiomesColors = new IntArraySet();
                        for (BiomeColor color : validBiomes) {
                            if (uniqueBiomesColors.add(color.grass)) {
                                uniqueColors.add(color);
                            }
                        }
                        uniqueBiomesColors.clear();
                        LongArrayList layerIds = new LongArrayList();
                        LongArrayList layerColors = new LongArrayList();
                        for (int i = 0; i < uniqueColors.size(); i++) {
                            for (int j = i; j < uniqueColors.size(); j++) {
                                for (int k = j; k < uniqueColors.size(); k++) {
                                    BiomeColor c1 = uniqueColors.get(i);
                                    BiomeColor c2 = uniqueColors.get(j);
                                    BiomeColor c3 = uniqueColors.get(k);
                                    int average = averageColor(c1.grass, c2.grass, c3.grass);
                                    if (uniqueBiomesColors.add(average)) {
                                        layerColors.add(average);
                                        layerIds.add((c1.id) + ((long) c2.id << 8) + ((long) c3.id << 16));
                                    }
                                }
                            }
                        }
                        validMixBiomeColors = new int[layerColors.size()];
                        for (int i = 0; i < layerColors.size(); i++) {
                            validMixBiomeColors[i] = (int) layerColors.getLong(i);
                        }
                        validMixBiomeIds = layerIds.toLongArray();
                    }
                }
                // Close the file
                zipFile.close();
            }
        }
    }
    // Convert the color map to a simple array
    validBlockIds = new int[colorMap.size()];
    validColors = new int[colorMap.size()];
    int index = 0;
    for (Int2ObjectMap.Entry<Integer> entry : colorMap.int2ObjectEntrySet()) {
        int combinedId = entry.getIntKey();
        int color = entry.getValue();
        blockColors[combinedId] = color;
        validBlockIds[index] = combinedId;
        validColors[index] = color;
        index++;
    }
    ArrayList<Long> distances = new ArrayList<>(distanceMap.values());
    Collections.sort(distances);
    this.distances = new long[distances.size()];
    for (int i = 0; i < this.distances.length; i++) {
        this.distances[i] = distances.get(i);
    }
    for (Int2ObjectMap.Entry<Long> entry : distanceMap.int2ObjectEntrySet()) {
        blockDistance[entry.getIntKey()] = entry.getValue();
    }
    calculateLayerArrays();
}
Also used : Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) BlockTypes(com.sk89q.worldedit.world.block.BlockTypes) BufferedInputStream(java.io.BufferedInputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) URL(java.net.URL) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) SingleFilterBlock(com.fastasyncworldedit.core.extent.filter.block.SingleFilterBlock) Fawe(com.fastasyncworldedit.core.Fawe) JsonReader(com.google.gson.stream.JsonReader) ImageUtil(com.fastasyncworldedit.core.util.image.ImageUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Gson(com.google.gson.Gson) Locale(java.util.Locale) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) LogManagerCompat(com.sk89q.worldedit.internal.util.LogManagerCompat) BlockType(com.sk89q.worldedit.world.block.BlockType) BufferedImage(java.awt.image.BufferedImage) IntArraySet(it.unimi.dsi.fastutil.ints.IntArraySet) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) Settings(com.fastasyncworldedit.core.configuration.Settings) BlockTypesCache(com.sk89q.worldedit.world.block.BlockTypesCache) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Type(java.lang.reflect.Type) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) Mask(com.sk89q.worldedit.function.mask.Mask) Collections(java.util.Collections) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) BufferedInputStream(java.io.BufferedInputStream) JsonReader(com.google.gson.stream.JsonReader) HashSet(java.util.HashSet) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) IntArraySet(it.unimi.dsi.fastutil.ints.IntArraySet) IOException(java.io.IOException) BlockType(com.sk89q.worldedit.world.block.BlockType) Type(java.lang.reflect.Type) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) ZipFile(java.util.zip.ZipFile) BlockType(com.sk89q.worldedit.world.block.BlockType) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) Map(java.util.Map) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap)

Example 59 with BlockType

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

the class PropertyGroup method set.

public <B extends BlockStateHolder<B>> BlockStateHolder<B> set(BlockStateHolder<B> state, A value) {
    BlockType type = state.getBlockType();
    PropertyFunction func = states[type.getInternalId()];
    if (func != null) {
        value = (A) func.setFunc.apply(value);
        state = state.with(func.key, value);
    }
    return state;
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 60 with BlockType

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

the class RandomTextureUtil method getIsBlockCloserThanBiome.

@Override
public boolean getIsBlockCloserThanBiome(char[] blockAndBiomeIdOutput, int color, int biomePriority) {
    BlockType block = getNearestBlock(color);
    int[] mix = biomeMixes.getOrDefault(color, null);
    if (mix == null) {
        int average = getBiomeMix(biomeMixBuffer, color);
        mix = new int[4];
        System.arraycopy(biomeMixBuffer, 0, mix, 0, 3);
        mix[3] = average;
        biomeMixes.put(color, mix);
    }
    if (++index > 2) {
        index = 0;
    }
    int biomeId = mix[index];
    int biomeAvColor = mix[3];
    int blockColor = getColor(block);
    blockAndBiomeIdOutput[0] = block.getDefaultState().getOrdinalChar();
    blockAndBiomeIdOutput[1] = (char) biomeId;
    return colorDistance(biomeAvColor, color) - biomePriority > colorDistance(blockColor, color);
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType)

Aggregations

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