Search in sources :

Example 21 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class SpongeAdapter method adapt.

/**
 * Create a WorldEdit location from a Sponge location.
 *
 * @param location the Sponge location
 * @return a WorldEdit location
 */
public static Location adapt(org.spongepowered.api.world.Location<org.spongepowered.api.world.World> location, Vector3d rotation) {
    checkNotNull(location);
    Vector3 position = asVector(location);
    return new Location(adapt(location.getExtent()), position, (float) rotation.getX(), (float) rotation.getY());
}
Also used : BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) Location(com.sk89q.worldedit.util.Location)

Example 22 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class AsyncPlayer method ascendToCeiling.

@Override
public boolean ascendToCeiling(int clearance, boolean alwaysGlass) {
    Location pos = getBlockLocation();
    int x = pos.getBlockX();
    int initialY = Math.max(getWorld().getMinY(), pos.getBlockY());
    int y = Math.max(getWorld().getMinY(), pos.getBlockY() + 2);
    int z = pos.getBlockZ();
    Extent world = getLocation().getExtent();
    MutableBlockVector3 mutable = new MutableBlockVector3();
    // No free space above
    if (!world.getBlock(mutable.setComponents(x, y, z)).getBlockType().getMaterial().isAir()) {
        return false;
    }
    while (y <= world.getMaximumPoint().getY()) {
        // Found a ceiling!
        if (world.getBlock(mutable.mutY(y)).getBlockType().getMaterial().isMovementBlocker()) {
            int platformY = Math.max(initialY, y - 3 - clearance);
            floatAt(x, platformY + 1, z, alwaysGlass);
            return true;
        }
        ++y;
    }
    return false;
}
Also used : MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) Extent(com.sk89q.worldedit.extent.Extent) Location(com.sk89q.worldedit.util.Location)

Example 23 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class ExtentEntityCopy method apply.

@Override
public boolean apply(Entity entity) throws WorldEditException {
    BaseEntity state = entity.getState();
    // FAWE start - Don't copy players
    if (state != null && state.getType() != EntityTypes.PLAYER) {
        // FAWE end
        Location newLocation;
        Location location = entity.getLocation();
        // If the entity has stored the location in the NBT data, we use that location
        CompoundTag tag = state.getNbtData();
        boolean hasTilePosition = tag != null && tag.containsKey("TileX") && tag.containsKey("TileY") && tag.containsKey("TileZ");
        if (hasTilePosition) {
            location = location.setPosition(Vector3.at(tag.asInt("TileX"), tag.asInt("TileY"), tag.asInt("TileZ")).add(0.5, 0.5, 0.5));
        }
        Vector3 pivot = from.round().add(0.5, 0.5, 0.5);
        Vector3 newPosition = transform.apply(location.toVector().subtract(pivot));
        if (hasTilePosition) {
            newPosition = newPosition.subtract(0.5, 0.5, 0.5);
        }
        Vector3 newDirection;
        newDirection = transform.isIdentity() ? entity.getLocation().getDirection() : transform.apply(location.getDirection()).subtract(transform.apply(Vector3.ZERO)).normalize();
        newLocation = new Location(destination, newPosition.add(to.round().add(0.5, 0.5, 0.5)), newDirection);
        // Some entities store their position data in NBT
        state = transformNbtData(state);
        boolean success = destination.createEntity(newLocation, state) != null;
        // Remove
        if (isRemoving() && success) {
            // FAWE start
            UUID uuid = null;
            if (tag.containsKey("UUID")) {
                int[] arr = tag.getIntArray("UUID");
                uuid = new UUID((long) arr[0] << 32 | (arr[1] & 0xFFFFFFFFL), (long) arr[2] << 32 | (arr[3] & 0xFFFFFFFFL));
            } else if (tag.containsKey("UUIDMost")) {
                uuid = new UUID(tag.getLong("UUIDMost"), tag.getLong("UUIDLeast"));
            } else if (tag.containsKey("PersistentIDMSB")) {
                uuid = new UUID(tag.getLong("PersistentIDMSB"), tag.getLong("PersistentIDLSB"));
            }
            if (uuid != null) {
                if (source != null) {
                    source.removeEntity(entity.getLocation().getBlockX(), entity.getLocation().getBlockY(), entity.getLocation().getBlockZ(), uuid);
                } else {
                    // FAWE end
                    entity.remove();
                }
            }
        }
        return success;
    } else {
        return false;
    }
}
Also used : BaseEntity(com.sk89q.worldedit.entity.BaseEntity) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) UUID(java.util.UUID) CompoundTag(com.sk89q.jnbt.CompoundTag) Location(com.sk89q.worldedit.util.Location)

Example 24 with Location

use of com.sk89q.worldedit.util.Location in project FastAsyncWorldEdit by IntellectualSites.

the class MinecraftStructure method read.

@Override
public Clipboard read(UUID clipboardId) throws IOException {
    NamedTag rootTag = inputStream.readNamedTag();
    // MC structures are all unnamed, but this doesn't seem to be necessary? might remove this later
    if (!rootTag.getName().isEmpty()) {
        throw new IOException("Root tag has name - are you sure this is a structure?");
    }
    Map<String, Tag> tags = ((CompoundTag) rootTag.getTag()).getValue();
    ListTag size = (ListTag) tags.get("size");
    int width = size.getInt(0);
    int height = size.getInt(1);
    int length = size.getInt(2);
    // Init clipboard
    BlockVector3 origin = BlockVector3.at(0, 0, 0);
    CuboidRegion region = new CuboidRegion(origin, origin.add(width, height, length).subtract(BlockVector3.ONE));
    Clipboard clipboard = new BlockArrayClipboard(region, clipboardId);
    // Blocks
    ListTag blocks = (ListTag) tags.get("blocks");
    if (blocks != null) {
        // Palette
        List<CompoundTag> palette = (List<CompoundTag>) tags.get("palette").getValue();
        BlockState[] combinedArray = new BlockState[palette.size()];
        for (int i = 0; i < palette.size(); i++) {
            CompoundTag compound = palette.get(i);
            Map<String, Tag> map = compound.getValue();
            String name = ((StringTag) map.get("Name")).getValue();
            BlockType type = BlockTypes.get(name);
            BlockState state = type.getDefaultState();
            CompoundTag properties = (CompoundTag) map.get("Properties");
            if (properties != null) {
                for (Map.Entry<String, Tag> entry : properties.getValue().entrySet()) {
                    String key = entry.getKey();
                    String value = ((StringTag) entry.getValue()).getValue();
                    Property<Object> property = type.getProperty(key);
                    state = state.with(property, property.getValueFor(value));
                }
            }
            combinedArray[i] = state;
        }
        // Populate blocks
        List<CompoundTag> blocksList = (List<CompoundTag>) tags.get("blocks").getValue();
        try {
            for (CompoundTag compound : blocksList) {
                Map<String, Tag> blockMap = compound.getValue();
                IntTag stateTag = (IntTag) blockMap.get("state");
                ListTag posTag = (ListTag) blockMap.get("pos");
                BlockState state = combinedArray[stateTag.getValue()];
                int x = posTag.getInt(0);
                int y = posTag.getInt(1);
                int z = posTag.getInt(2);
                if (state.getBlockType().getMaterial().hasContainer()) {
                    CompoundTag nbt = (CompoundTag) blockMap.get("nbt");
                    if (nbt != null) {
                        BaseBlock block = state.toBaseBlock(nbt);
                        clipboard.setBlock(x, y, z, block);
                        continue;
                    }
                }
                clipboard.setBlock(x, y, z, state);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Entities
    ListTag entities = (ListTag) tags.get("entities");
    if (entities != null) {
        List<CompoundTag> entityList = (List<CompoundTag>) (List<?>) entities.getValue();
        for (CompoundTag entityEntry : entityList) {
            Map<String, Tag> entityEntryMap = entityEntry.getValue();
            ListTag posTag = (ListTag) entityEntryMap.get("pos");
            CompoundTag nbtTag = (CompoundTag) entityEntryMap.get("nbt");
            String id = nbtTag.getString("Id");
            Location location = NBTConversions.toLocation(clipboard, posTag, nbtTag.getListTag("Rotation"));
            if (!id.isEmpty()) {
                BaseEntity state = new BaseEntity(EntityTypes.get(id), nbtTag);
                clipboard.createEntity(location, state);
            }
        }
    }
    return clipboard;
}
Also used : StringTag(com.sk89q.jnbt.StringTag) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) NamedTag(com.sk89q.jnbt.NamedTag) ArrayList(java.util.ArrayList) List(java.util.List) CompoundTag(com.sk89q.jnbt.CompoundTag) IntTag(com.sk89q.jnbt.IntTag) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ListTag(com.sk89q.jnbt.ListTag) IOException(java.io.IOException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) 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) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) BlockArrayClipboard(com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard) HashMap(java.util.HashMap) Map(java.util.Map) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap) Location(com.sk89q.worldedit.util.Location)

Example 25 with Location

use of com.sk89q.worldedit.util.Location 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)

Aggregations

Location (com.sk89q.worldedit.util.Location)65 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)26 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)12 CompoundTag (com.sk89q.jnbt.CompoundTag)11 World (com.sk89q.worldedit.world.World)11 Region (com.sk89q.worldedit.regions.Region)10 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)9 Command (org.enginehub.piston.annotation.Command)9 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)8 Tag (com.sk89q.jnbt.Tag)8 List (java.util.List)8 ListTag (com.sk89q.jnbt.ListTag)7 Vector3 (com.sk89q.worldedit.math.Vector3)7 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)7 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)7 StringTag (com.sk89q.jnbt.StringTag)6 Player (com.sk89q.worldedit.entity.Player)6 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)6 BlockState (com.sk89q.worldedit.world.block.BlockState)6 IntTag (com.sk89q.jnbt.IntTag)5