Search in sources :

Example 26 with BlockType

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

Example 27 with BlockType

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

the class BrushCommands method sphereBrush.

@Command(name = "sphere", aliases = { "s" }, desc = "Choose the sphere brush")
@CommandPermissions("worldedit.brush.sphere")
public void sphereBrush(Player player, InjectedValueAccess context, @Arg(desc = "The pattern of blocks to set") Pattern pattern, @Arg(desc = "The radius of the sphere", def = "2") Expression radius, @Switch(name = 'h', desc = "Create hollow spheres instead") boolean hollow, @Switch(name = 'f', desc = "Create falling spheres instead") boolean falling) throws WorldEditException {
    worldEdit.checkMaxBrushRadius(radius);
    Brush brush;
    if (hollow) {
        brush = new HollowSphereBrush();
    } else {
        // FAWE start - Suggest different brush material if sand or gravel is used
        if (pattern instanceof BlockStateHolder) {
            BlockType type = ((BlockStateHolder) pattern).getBlockType();
            switch(type.getId()) {
                case "minecraft:sand":
                case "minecraft:gravel":
                    player.print(Caption.of("fawe.worldedit.brush.brush.try.other"));
                    falling = true;
                    break;
                default:
                    break;
            }
        }
        if (falling) {
            brush = new FallingSphere();
        } else {
            brush = new SphereBrush();
        }
    }
    // FAWE end
    set(context, brush, "worldedit.brush.sphere").setSize(radius).setFill(pattern);
}
Also used : HollowSphereBrush(com.sk89q.worldedit.command.tool.brush.HollowSphereBrush) BlockType(com.sk89q.worldedit.world.block.BlockType) BlockStateHolder(com.sk89q.worldedit.world.block.BlockStateHolder) FallingSphere(com.fastasyncworldedit.core.command.tool.brush.FallingSphere) HollowSphereBrush(com.sk89q.worldedit.command.tool.brush.HollowSphereBrush) HeightBrush(com.fastasyncworldedit.core.command.tool.brush.HeightBrush) SphereBrush(com.sk89q.worldedit.command.tool.brush.SphereBrush) ScatterBrush(com.fastasyncworldedit.core.command.tool.brush.ScatterBrush) CopyPastaBrush(com.fastasyncworldedit.core.command.tool.brush.CopyPastaBrush) RaiseBrush(com.fastasyncworldedit.core.command.tool.brush.RaiseBrush) SnowSmoothBrush(com.sk89q.worldedit.command.tool.brush.SnowSmoothBrush) ClipboardBrush(com.sk89q.worldedit.command.tool.brush.ClipboardBrush) GravityBrush(com.sk89q.worldedit.command.tool.brush.GravityBrush) FlattenBrush(com.fastasyncworldedit.core.command.tool.brush.FlattenBrush) BlobBrush(com.fastasyncworldedit.core.command.tool.brush.BlobBrush) SweepBrush(com.fastasyncworldedit.core.command.tool.sweep.SweepBrush) CircleBrush(com.fastasyncworldedit.core.command.tool.brush.CircleBrush) SplatterBrush(com.fastasyncworldedit.core.command.tool.brush.SplatterBrush) CatenaryBrush(com.fastasyncworldedit.core.command.tool.brush.CatenaryBrush) RecurseBrush(com.fastasyncworldedit.core.command.tool.brush.RecurseBrush) CylinderBrush(com.sk89q.worldedit.command.tool.brush.CylinderBrush) ScatterOverlayBrush(com.fastasyncworldedit.core.command.tool.brush.ScatterOverlayBrush) StencilBrush(com.fastasyncworldedit.core.command.tool.brush.StencilBrush) LineBrush(com.fastasyncworldedit.core.command.tool.brush.LineBrush) Brush(com.sk89q.worldedit.command.tool.brush.Brush) ButcherBrush(com.sk89q.worldedit.command.tool.brush.ButcherBrush) ErodeBrush(com.fastasyncworldedit.core.command.tool.brush.ErodeBrush) SurfaceSphereBrush(com.fastasyncworldedit.core.command.tool.brush.SurfaceSphereBrush) SplineBrush(com.fastasyncworldedit.core.command.tool.brush.SplineBrush) HollowCylinderBrush(com.sk89q.worldedit.command.tool.brush.HollowCylinderBrush) SmoothBrush(com.sk89q.worldedit.command.tool.brush.SmoothBrush) LayerBrush(com.fastasyncworldedit.core.command.tool.brush.LayerBrush) CommandBrush(com.fastasyncworldedit.core.command.tool.brush.CommandBrush) ImageBrush(com.fastasyncworldedit.core.command.tool.brush.ImageBrush) ShatterBrush(com.fastasyncworldedit.core.command.tool.brush.ShatterBrush) OperationFactoryBrush(com.sk89q.worldedit.command.tool.brush.OperationFactoryBrush) HollowSphereBrush(com.sk89q.worldedit.command.tool.brush.HollowSphereBrush) SphereBrush(com.sk89q.worldedit.command.tool.brush.SphereBrush) SurfaceSphereBrush(com.fastasyncworldedit.core.command.tool.brush.SurfaceSphereBrush) ScatterCommand(com.fastasyncworldedit.core.command.tool.brush.ScatterCommand) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 28 with BlockType

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

the class EditSession method thaw.

/**
 * Thaw blocks in a cylinder.
 *
 * @param position the position
 * @param radius   the radius
 * @param height   the height (upwards and downwards)
 * @return number of blocks affected
 * @throws MaxChangedBlocksException thrown if too many blocks are changed
 */
public int thaw(BlockVector3 position, double radius, int height) throws MaxChangedBlocksException {
    int affected = 0;
    double radiusSq = radius * radius;
    int ox = position.getBlockX();
    int oy = position.getBlockY();
    int oz = position.getBlockZ();
    BlockState air = BlockTypes.AIR.getDefaultState();
    BlockState water = BlockTypes.WATER.getDefaultState();
    int centerY = Math.max(minY, Math.min(maxY, oy));
    int minY = Math.max(this.minY, centerY - height);
    int maxY = Math.min(this.maxY, centerY + height);
    // FAWE start - mutable
    MutableBlockVector3 mutable = new MutableBlockVector3();
    MutableBlockVector3 mutable2 = new MutableBlockVector3();
    // FAWE end
    int ceilRadius = (int) Math.ceil(radius);
    for (int x = ox - ceilRadius; x <= ox + ceilRadius; ++x) {
        for (int z = oz - ceilRadius; z <= oz + ceilRadius; ++z) {
            // FAWE start - mutable
            if ((mutable.setComponents(x, oy, z)).distanceSq(position) > radiusSq) {
                // FAWE end
                continue;
            }
            for (int y = maxY; y > minY; --y) {
                // FAWE start - mutable
                mutable.setComponents(x, y, z);
                mutable2.setComponents(x, y - 1, z);
                BlockType id = getBlock(mutable).getBlockType();
                if (id == BlockTypes.ICE) {
                    if (setBlock(mutable, water)) {
                        ++affected;
                    }
                } else if (id == BlockTypes.SNOW) {
                    // FAWE start
                    if (setBlock(mutable, air)) {
                        if (y > getMinY()) {
                            BlockState block = getBlock(mutable2);
                            if (block.getStates().containsKey(snowy)) {
                                if (setBlock(mutable2, block.with(snowy, false))) {
                                    affected++;
                                }
                            }
                        }
                        // FAWE end
                        ++affected;
                    }
                } else if (id.getMaterial().isAir()) {
                    continue;
                }
                break;
            }
        }
    }
    return affected;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) BlockType(com.sk89q.worldedit.world.block.BlockType)

Example 29 with BlockType

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

the class WorldEdit method flushBlockBag.

/**
 * Flush a block bag's changes to a player.
 *
 * @param actor       the actor
 * @param editSession the edit session
 */
public void flushBlockBag(Actor actor, EditSession editSession) {
    BlockBag blockBag = editSession.getBlockBag();
    if (blockBag != null) {
        blockBag.flushChanges();
    }
    Map<BlockType, Integer> missingBlocks = editSession.popMissingBlocks();
    if (!missingBlocks.isEmpty()) {
        TextComponent.Builder str = TextComponent.builder();
        str.append("Missing these blocks: ");
        int size = missingBlocks.size();
        int i = 0;
        for (Map.Entry<BlockType, Integer> blockTypeIntegerEntry : missingBlocks.entrySet()) {
            str.append((blockTypeIntegerEntry.getKey()).getRichName());
            str.append(" [Amt: ").append(String.valueOf(blockTypeIntegerEntry.getValue())).append("]");
            ++i;
            if (i != size) {
                str.append(", ");
            }
        }
        actor.printError(str.build());
    }
}
Also used : TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) BlockType(com.sk89q.worldedit.world.block.BlockType) BlockBag(com.sk89q.worldedit.extent.inventory.BlockBag) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 30 with BlockType

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

the class WorldConfiguration method convertLegacyBlock.

public String convertLegacyBlock(String legacy) {
    String[] splitter = legacy.split(":", 2);
    try {
        int id;
        byte data;
        if (splitter.length == 1) {
            id = Integer.parseInt(splitter[0]);
            data = 0;
        } else {
            id = Integer.parseInt(splitter[0]);
            data = Byte.parseByte(splitter[1]);
        }
        BlockState legacyBlock = LegacyMapper.getInstance().getBlockFromLegacy(id, data);
        if (legacyBlock != null) {
            return legacyBlock.getBlockType().getId();
        }
    } catch (NumberFormatException ignored) {
    }
    final BlockType blockType = BlockTypes.get(legacy);
    if (blockType != null) {
        return blockType.getId();
    }
    return null;
}
Also used : BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType)

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