Search in sources :

Example 6 with BiomeType

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

the class SpongeSchematicWriter method writeBiomes.

private void writeBiomes(Clipboard clipboard, Map<String, Tag> schematic) {
    BlockVector3 min = clipboard.getMinimumPoint();
    int width = clipboard.getRegion().getWidth();
    int length = clipboard.getRegion().getLength();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(width * length);
    int paletteMax = 0;
    Map<String, Integer> palette = new HashMap<>();
    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 pt = BlockVector3.at(x0, min.getBlockY(), z0);
            BiomeType biome = clipboard.getBiome(pt);
            String biomeKey = biome.getId();
            int biomeId;
            if (palette.containsKey(biomeKey)) {
                biomeId = palette.get(biomeKey);
            } else {
                biomeId = paletteMax;
                palette.put(biomeKey, biomeId);
                paletteMax++;
            }
            while ((biomeId & -128) != 0) {
                buffer.write(biomeId & 127 | 128);
                biomeId >>>= 7;
            }
            buffer.write(biomeId);
        }
    }
    schematic.put("BiomePaletteMax", new IntTag(paletteMax));
    Map<String, Tag> paletteTag = new HashMap<>();
    palette.forEach((key, value) -> paletteTag.put(key, new IntTag(value)));
    schematic.put("BiomePalette", new CompoundTag(paletteTag));
    schematic.put("BiomeData", new ByteArrayTag(buffer.toByteArray()));
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) 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)

Example 7 with BiomeType

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

the class WorldEditPlugin method setupBiomes.

// FAWE start
private void setupBiomes(boolean expectFail) {
    if (this.adapter.value().isPresent()) {
        // The WorldServer get-registries method simply delegates to the MinecraftServer method.
        for (final NamespacedKey biome : ((BukkitImplAdapter<?>) adapter.value().get()).getRegisteredBiomes()) {
            BiomeType biomeType;
            if ((biomeType = BiomeType.REGISTRY.get(biome.toString())) == null) {
                // only register once
                biomeType = new BiomeType(biome.toString());
                BiomeType.REGISTRY.register(biome.toString(), biomeType);
            }
            biomeType.setLegacyId(adapter.value().get().getInternalBiomeId(biomeType));
        }
    } else {
        if (!expectFail) {
            LOGGER.warn("Failed to load biomes via adapter (not present). Will load via bukkit");
        }
        for (Biome biome : Biome.values()) {
            // Custom is bad
            if (biome.name().equals("CUSTOM")) {
                continue;
            }
            String lowerCaseBiome = biome.getKey().toString().toLowerCase(Locale.ROOT);
            // only register once
            if (BiomeType.REGISTRY.get(lowerCaseBiome) == null) {
                BiomeType.REGISTRY.register(lowerCaseBiome, new BiomeType(lowerCaseBiome));
            }
        }
    }
}
Also used : BiomeType(com.sk89q.worldedit.world.biome.BiomeType) Biome(org.bukkit.block.Biome) NamespacedKey(org.bukkit.NamespacedKey) BukkitImplAdapter(com.sk89q.worldedit.bukkit.adapter.BukkitImplAdapter)

Example 8 with BiomeType

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

the class AbstractChangeSet method processSet.

@Override
public synchronized IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) {
    int bx = chunk.getX() << 4;
    int bz = chunk.getZ() << 4;
    Map<BlockVector3, CompoundTag> tilesFrom = get.getTiles();
    Map<BlockVector3, CompoundTag> tilesTo = set.getTiles();
    if (!tilesFrom.isEmpty()) {
        for (Map.Entry<BlockVector3, CompoundTag> entry : tilesFrom.entrySet()) {
            BlockVector3 pos = entry.getKey();
            BlockState fromBlock = get.getBlock(pos.getX() & 15, pos.getY(), pos.getZ() & 15);
            BlockState toBlock = set.getBlock(pos.getX() & 15, pos.getY(), pos.getZ() & 15);
            if (fromBlock != toBlock || tilesTo.containsKey(pos)) {
                addTileRemove(MainUtil.setPosition(entry.getValue(), entry.getKey().getX(), entry.getKey().getY(), entry.getKey().getZ()));
            }
        }
    }
    if (!tilesTo.isEmpty()) {
        for (Map.Entry<BlockVector3, CompoundTag> entry : tilesTo.entrySet()) {
            BlockVector3 pos = entry.getKey();
            addTileCreate(MainUtil.setPosition(entry.getValue(), pos.getX() + bx, pos.getY(), pos.getZ() + bz));
        }
    }
    Set<UUID> entRemoves = set.getEntityRemoves();
    if (!entRemoves.isEmpty()) {
        for (UUID uuid : entRemoves) {
            CompoundTag found = get.getEntity(uuid);
            if (found != null) {
                addEntityRemove(found);
            }
        }
    }
    Set<CompoundTag> ents = set.getEntities();
    if (!ents.isEmpty()) {
        for (CompoundTag tag : ents) {
            addEntityCreate(tag);
        }
    }
    for (int layer = get.getMinSectionPosition(); layer <= get.getMaxSectionPosition(); layer++) {
        if (!set.hasSection(layer)) {
            continue;
        }
        // add each block and tile
        char[] blocksGet;
        char[] tmp = get.load(layer);
        if (tmp == null) {
            blocksGet = FaweCache.INSTANCE.EMPTY_CHAR_4096;
        } else {
            System.arraycopy(tmp, 0, (blocksGet = new char[4096]), 0, 4096);
        }
        char[] blocksSet;
        // loadIfPresent shouldn't be null if set.hasSection(layer) is true
        System.arraycopy(Objects.requireNonNull(set.loadIfPresent(layer)), 0, (blocksSet = new char[4096]), 0, 4096);
        // Account for negative layers
        int by = layer << 4;
        for (int y = 0, index = 0; y < 16; y++) {
            int yy = y + by;
            for (int z = 0; z < 16; z++) {
                int zz = z + bz;
                for (int x = 0; x < 16; x++, index++) {
                    int xx = bx + x;
                    int from = blocksGet[index];
                    if (from == BlockTypesCache.ReservedIDs.__RESERVED__) {
                        from = BlockTypesCache.ReservedIDs.AIR;
                    }
                    final int combinedFrom = from;
                    final int combinedTo = blocksSet[index];
                    if (combinedTo != 0) {
                        add(xx, yy, zz, combinedFrom, combinedTo);
                    }
                }
            }
        }
    }
    BiomeType[][] biomes = set.getBiomes();
    if (biomes != null) {
        for (int layer = get.getMinSectionPosition(); layer <= get.getMaxSectionPosition(); layer++) {
            if (!set.hasBiomes(layer)) {
                continue;
            }
            BiomeType[] biomeSection = biomes[layer - set.getMinSectionPosition()];
            int index = 0;
            int yy = layer << 4;
            for (int y = 0; y < 16; y += 4) {
                for (int z = 0; z < 16; z += 4) {
                    for (int x = 0; x < 16; x += 4, index++) {
                        BiomeType newBiome = biomeSection[index];
                        if (newBiome != null) {
                            BiomeType oldBiome = get.getBiomeType(x, yy + y, z);
                            if (oldBiome != newBiome) {
                                addBiomeChange(bx + x, yy + y, bz + z, oldBiome, newBiome);
                            }
                        }
                    }
                }
            }
        }
    }
    return set;
}
Also used : BlockVector3(com.sk89q.worldedit.math.BlockVector3) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) BlockState(com.sk89q.worldedit.world.block.BlockState) UUID(java.util.UUID) Map(java.util.Map) CompoundTag(com.sk89q.jnbt.CompoundTag)

Example 9 with BiomeType

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

the class BiomeMaskParser method parseFromInput.

@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
    if (!input.startsWith("$")) {
        return null;
    }
    // FAWE start - richer parsing
    if (input.charAt(1) == '[') {
        int end = input.lastIndexOf(']');
        if (end == -1) {
            return null;
        }
        input = input.substring(2, end);
    } else {
        input = input.substring(1);
    }
    // FAWE end
    Set<BiomeType> biomes = new HashSet<>();
    // FAWE start - richer parsing
    for (String biomeName : Splitter.on(",").split(input)) {
        // FAWE end
        BiomeType biome = BiomeType.REGISTRY.get(biomeName);
        if (biome == null) {
            throw new NoMatchException(Caption.of("worldedit.error.unknown-biome", TextComponent.of(biomeName)));
        }
        biomes.add(biome);
    }
    return new BiomeMask(context.requireExtent(), biomes);
}
Also used : BiomeType(com.sk89q.worldedit.world.biome.BiomeType) BiomeMask(com.sk89q.worldedit.function.mask.BiomeMask) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) HashSet(java.util.HashSet)

Example 10 with BiomeType

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

the class PaperweightAdapter method regenForWorld.

@SuppressWarnings("unchecked")
private void regenForWorld(Region region, Extent extent, ServerLevel serverWorld, RegenOptions options) throws WorldEditException {
    List<CompletableFuture<ChunkAccess>> chunkLoadings = submitChunkLoadTasks(region, serverWorld);
    BlockableEventLoop<Runnable> executor;
    try {
        executor = (BlockableEventLoop<Runnable>) mainThreadProcessorField.get(serverWorld.getChunkSource());
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Couldn't get executor for chunk loading.", e);
    }
    executor.managedBlock(() -> {
        // bail out early if a future fails
        if (chunkLoadings.stream().anyMatch(ftr -> ftr.isDone() && Futures.getUnchecked(ftr) == null)) {
            return false;
        }
        return chunkLoadings.stream().allMatch(CompletableFuture::isDone);
    });
    Map<ChunkPos, ChunkAccess> chunks = new HashMap<>();
    for (CompletableFuture<ChunkAccess> future : chunkLoadings) {
        @Nullable ChunkAccess chunk = future.getNow(null);
        checkState(chunk != null, "Failed to generate a chunk, regen failed.");
        chunks.put(chunk.getPos(), chunk);
    }
    for (BlockVector3 vec : region) {
        BlockPos pos = new BlockPos(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ());
        ChunkAccess chunk = chunks.get(new ChunkPos(pos));
        final net.minecraft.world.level.block.state.BlockState blockData = chunk.getBlockState(pos);
        BlockStateHolder<?> state = ((PaperweightFaweAdapter) WorldEditPlugin.getInstance().getBukkitImplAdapter()).adapt(blockData);
        Objects.requireNonNull(state);
        BlockEntity blockEntity = chunk.getBlockEntity(pos);
        if (blockEntity != null) {
            net.minecraft.nbt.CompoundTag tag = blockEntity.saveWithId();
            // FAWE start - BinaryTag
            state = state.toBaseBlock(((CompoundBinaryTag) toNativeBinary(tag)));
        // FAWE end
        }
        extent.setBlock(vec, state.toBaseBlock());
        if (options.shouldRegenBiomes()) {
            Biome origBiome = chunk.getNoiseBiome(vec.getX(), vec.getY(), vec.getZ());
            BiomeType adaptedBiome = adapt(serverWorld, origBiome);
            if (adaptedBiome != null) {
                extent.setBiome(vec, adaptedBiome);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) CompletableFuture(java.util.concurrent.CompletableFuture) Biome(net.minecraft.world.level.biome.Biome) PaperweightFaweAdapter(com.sk89q.worldedit.bukkit.adapter.impl.fawe.v1_18_R1.PaperweightFaweAdapter) ChunkPos(net.minecraft.world.level.ChunkPos) BlockPos(net.minecraft.core.BlockPos) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) StructureBlockEntity(net.minecraft.world.level.block.entity.StructureBlockEntity) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Nullable(javax.annotation.Nullable)

Aggregations

BiomeType (com.sk89q.worldedit.world.biome.BiomeType)18 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)10 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)4 IOException (java.io.IOException)4 CompoundTag (com.sk89q.jnbt.CompoundTag)3 EditSession (com.sk89q.worldedit.EditSession)3 Region (com.sk89q.worldedit.regions.Region)3 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)2 IntArrayTag (com.sk89q.jnbt.IntArrayTag)2 IntTag (com.sk89q.jnbt.IntTag)2 ListTag (com.sk89q.jnbt.ListTag)2 ShortTag (com.sk89q.jnbt.ShortTag)2 StringTag (com.sk89q.jnbt.StringTag)2 Tag (com.sk89q.jnbt.Tag)2 WorldEditException (com.sk89q.worldedit.WorldEditException)2 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2