Search in sources :

Example 36 with Location

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

the class SnapshotRestore method restore.

/**
 * Restores to world.
 *
 * @throws MaxChangedBlocksException if the max block change limit is exceeded
 */
public void restore() throws MaxChangedBlocksException {
    missingChunks = new ArrayList<>();
    errorChunks = new ArrayList<>();
    // Now let's start restoring!
    for (Map.Entry<BlockVector2, Set<BlockVector3>> entry : neededChunks.entrySet()) {
        BlockVector2 chunkPos = entry.getKey();
        Chunk chunk;
        try {
            chunk = chunkStore.getChunk(chunkPos, editSession.getWorld());
            // Now just copy blocks!
            for (BlockVector3 pos : entry.getValue()) {
                try {
                    editSession.setBlock(pos, chunk.getBlock(pos));
                    // FAWE start - biome and entity restore
                    if (restoreBiomes && (pos.getX() & 3) == 0 && (pos.getY() & 3) == 0 && (pos.getZ() & 3) == 0) {
                        editSession.setBiome(pos, chunk.getBiome(pos));
                    }
                // FAWE end
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
            // FAWE start - biome and entity restore
            if (restoreEntities) {
                try {
                    for (BaseEntity entity : chunk.getEntities()) {
                        CompoundBinaryTag tag = entity.getNbtReference().getValue();
                        ListBinaryTag pos = tag.getList("Pos");
                        ListBinaryTag rotation = tag.getList("Rotation");
                        double x = pos.getDouble(0);
                        double y = pos.getDouble(1);
                        double z = pos.getDouble(2);
                        float yRot = rotation.getFloat(0);
                        float xRot = rotation.getFloat(1);
                        Location location = new Location(editSession.getWorld(), x, y, z, yRot, xRot);
                        editSession.createEntity(location, entity);
                    }
                } catch (DataException e) {
                // this is a workaround: just ignore for now
                }
            }
        // FAWE end
        } catch (MissingChunkException me) {
            missingChunks.add(chunkPos);
        } catch (IOException | DataException me) {
            errorChunks.add(chunkPos);
            lastErrorMessage = me.getMessage();
        }
    }
}
Also used : Set(java.util.Set) LocalBlockVectorSet(com.fastasyncworldedit.core.math.LocalBlockVectorSet) ListBinaryTag(com.sk89q.worldedit.util.nbt.ListBinaryTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IOException(java.io.IOException) Chunk(com.sk89q.worldedit.world.chunk.Chunk) BlockVector3(com.sk89q.worldedit.math.BlockVector3) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MissingChunkException(com.sk89q.worldedit.world.storage.MissingChunkException) DataException(com.sk89q.worldedit.world.DataException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Location(com.sk89q.worldedit.util.Location)

Example 37 with Location

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

the class ServerCUIHandler method createStructureBlock.

/**
 * Creates a structure block that shows the region.
 *
 * <p>
 * Null symbolises removal of the CUI.
 * </p>
 *
 * @param player The player to create the structure block for.
 * @return The structure block, or null
 */
@Nullable
public static BaseBlock createStructureBlock(Player player) {
    LocalSession session = WorldEdit.getInstance().getSessionManager().get(player);
    RegionSelector regionSelector = session.getRegionSelector(player.getWorld());
    int posX;
    int posY;
    int posZ;
    int width;
    int height;
    int length;
    if (regionSelector instanceof CuboidRegionSelector) {
        if (regionSelector.isDefined()) {
            try {
                CuboidRegion region = ((CuboidRegionSelector) regionSelector).getRegion();
                posX = region.getMinimumPoint().getBlockX();
                posY = region.getMinimumPoint().getBlockY();
                posZ = region.getMinimumPoint().getBlockZ();
                width = region.getWidth();
                height = region.getHeight();
                length = region.getLength();
            } catch (IncompleteRegionException e) {
                // This will never happen.
                e.printStackTrace();
                return null;
            }
        } else {
            CuboidRegion region = ((CuboidRegionSelector) regionSelector).getIncompleteRegion();
            BlockVector3 point;
            if (region.getPos1() != null) {
                point = region.getPos1();
            } else if (region.getPos2() != null) {
                point = region.getPos2();
            } else {
                // No more selection
                return null;
            }
            // Just select the point.
            posX = point.getBlockX();
            posY = point.getBlockY();
            posZ = point.getBlockZ();
            width = 1;
            height = 1;
            length = 1;
        }
    } else {
        // We only support cuboid regions right now.
        return null;
    }
    int maxSize = getMaxServerCuiSize();
    if (width > maxSize || length > maxSize || height > maxSize) {
        // Structure blocks have a limit of maxSize^3
        return null;
    }
    // Borrowed this math from FAWE
    final Location location = player.getLocation();
    double rotX = location.getYaw();
    double rotY = location.getPitch();
    double xz = Math.cos(Math.toRadians(rotY));
    int x = (int) (location.getX() - (-xz * Math.sin(Math.toRadians(rotX))) * 12);
    int z = (int) (location.getZ() - (xz * Math.cos(Math.toRadians(rotX))) * 12);
    int y = Math.max(player.getWorld().getMinY(), Math.min(Math.min(player.getWorld().getMaxY(), posY + MAX_DISTANCE), posY + 3));
    // FAWE start - CBT > Map<String, Tag>
    CompoundBinaryTag.Builder structureTag = CompoundBinaryTag.builder();
    posX -= x;
    posY -= y;
    posZ -= z;
    if (Math.abs(posX) > MAX_DISTANCE || Math.abs(posY) > MAX_DISTANCE || Math.abs(posZ) > MAX_DISTANCE) {
        // Structure blocks have a limit
        return null;
    }
    // FAWE start - see comment of CBT
    structureTag.putString("name", "worldedit:" + player.getName());
    structureTag.putString("author", player.getName());
    structureTag.putString("metadata", "");
    structureTag.putInt("x", x);
    structureTag.putInt("y", y);
    structureTag.putInt("z", z);
    structureTag.putInt("posX", posX);
    structureTag.putInt("posY", posY);
    structureTag.putInt("posZ", posZ);
    structureTag.putInt("sizeX", width);
    structureTag.putInt("sizeY", height);
    structureTag.putInt("sizeZ", length);
    structureTag.putString("rotation", "NONE");
    structureTag.putString("mirror", "NONE");
    structureTag.putString("mode", "SAVE");
    structureTag.putByte("ignoreEntities", (byte) 1);
    structureTag.putByte("showboundingbox", (byte) 1);
    structureTag.putString("id", BlockTypes.STRUCTURE_BLOCK.getId());
    return BlockTypes.STRUCTURE_BLOCK.getDefaultState().toBaseBlock(structureTag.build());
// FAWE end
}
Also used : CuboidRegionSelector(com.sk89q.worldedit.regions.selector.CuboidRegionSelector) RegionSelector(com.sk89q.worldedit.regions.RegionSelector) LocalSession(com.sk89q.worldedit.LocalSession) IncompleteRegionException(com.sk89q.worldedit.IncompleteRegionException) CuboidRegionSelector(com.sk89q.worldedit.regions.selector.CuboidRegionSelector) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) BlockVector3(com.sk89q.worldedit.math.BlockVector3) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Location(com.sk89q.worldedit.util.Location) Nullable(javax.annotation.Nullable)

Example 38 with Location

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

the class SpongeEntity method getLocation.

@Override
public Location getLocation() {
    org.spongepowered.api.entity.Entity entity = entityRef.get();
    if (entity != null) {
        org.spongepowered.api.world.Location<World> entityLoc = entity.getLocation();
        Vector3d entityRot = entity.getRotation();
        return SpongeWorldEdit.inst().getAdapter().adapt(entityLoc, entityRot);
    } else {
        return new Location(NullWorld.getInstance());
    }
}
Also used : Vector3d(com.flowpowered.math.vector.Vector3d) NullWorld(com.sk89q.worldedit.world.NullWorld) World(org.spongepowered.api.world.World) Location(com.sk89q.worldedit.util.Location)

Example 39 with Location

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

the class ForgeEntity method getLocation.

@Override
public Location getLocation() {
    net.minecraft.entity.Entity entity = entityRef.get();
    if (entity != null) {
        Vector3 position = Vector3.at(entity.posX, entity.posY, entity.posZ);
        float yaw = entity.rotationYaw;
        float pitch = entity.rotationPitch;
        return new Location(ForgeAdapter.adapt(entity.world), position, yaw, pitch);
    } else {
        return new Location(NullWorld.getInstance());
    }
}
Also used : Vector3(com.sk89q.worldedit.math.Vector3) ResourceLocation(net.minecraft.util.ResourceLocation) Location(com.sk89q.worldedit.util.Location)

Example 40 with Location

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

the class ForgeWorldEdit method onPlayerInteract.

@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event) {
    if (platform == null) {
        return;
    }
    if (!platform.isHookingEvents())
        // We have to be told to catch these events
        return;
    if (event.getWorld().isRemote && event instanceof LeftClickEmpty) {
        // catch LCE, pass it to server
        InternalPacketHandler.getHandler().sendToServer(new LeftClickAirEventMessage());
        return;
    }
    boolean isLeftDeny = event instanceof PlayerInteractEvent.LeftClickBlock && ((PlayerInteractEvent.LeftClickBlock) event).getUseItem() == Event.Result.DENY;
    boolean isRightDeny = event instanceof PlayerInteractEvent.RightClickBlock && ((PlayerInteractEvent.RightClickBlock) event).getUseItem() == Event.Result.DENY;
    if (isLeftDeny || isRightDeny || event.getEntity().world.isRemote || event.getHand() == Hand.OFF_HAND) {
        return;
    }
    WorldEdit we = WorldEdit.getInstance();
    ForgePlayer player = adaptPlayer((ServerPlayerEntity) event.getPlayer());
    ForgeWorld world = getWorld(event.getPlayer().world);
    if (event instanceof PlayerInteractEvent.LeftClickEmpty) {
        // this event cannot be canceled
        we.handleArmSwing(player);
    } else if (event instanceof PlayerInteractEvent.LeftClickBlock) {
        Location pos = new Location(world, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
        if (we.handleBlockLeftClick(player, pos)) {
            event.setCanceled(true);
        }
        if (we.handleArmSwing(player)) {
            event.setCanceled(true);
        }
    } else if (event instanceof PlayerInteractEvent.RightClickBlock) {
        Location pos = new Location(world, event.getPos().getX(), event.getPos().getY(), event.getPos().getZ());
        if (we.handleBlockRightClick(player, pos)) {
            event.setCanceled(true);
        }
        if (we.handleRightClick(player)) {
            event.setCanceled(true);
        }
    } else if (event instanceof PlayerInteractEvent.RightClickItem) {
        if (we.handleRightClick(player)) {
            event.setCanceled(true);
        }
    }
}
Also used : WorldEdit(com.sk89q.worldedit.WorldEdit) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) LeftClickEmpty(net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickEmpty) LeftClickAirEventMessage(com.sk89q.worldedit.forge.net.packet.LeftClickAirEventMessage) Location(com.sk89q.worldedit.util.Location) ResourceLocation(net.minecraft.util.ResourceLocation) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

Location (com.sk89q.worldedit.util.Location)75 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)26 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)12 World (com.sk89q.worldedit.world.World)12 CompoundTag (com.sk89q.jnbt.CompoundTag)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 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)8 RegionContainer (com.sk89q.worldguard.protection.regions.RegionContainer)8 List (java.util.List)8 ListTag (com.sk89q.jnbt.ListTag)7 Player (com.sk89q.worldedit.entity.Player)7 Vector3 (com.sk89q.worldedit.math.Vector3)7 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)7 LocalPlayer (com.sk89q.worldguard.LocalPlayer)7 StringTag (com.sk89q.jnbt.StringTag)6 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)6