|
| 1 | +/* |
| 2 | + * This file is part of Apollo, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Moonsworth |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package com.lunarclient.apollo; |
| 25 | + |
| 26 | +import com.lunarclient.apollo.common.ApolloEntity; |
| 27 | +import com.lunarclient.apollo.common.location.ApolloBlockLocation; |
| 28 | +import com.lunarclient.apollo.common.location.ApolloLocation; |
| 29 | +import com.lunarclient.apollo.common.location.ApolloPlayerLocation; |
| 30 | +import com.lunarclient.apollo.player.ApolloPlayer; |
| 31 | +import com.lunarclient.apollo.player.ApolloPlayerManager; |
| 32 | +import com.lunarclient.apollo.recipients.Recipients; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Optional; |
| 36 | +import java.util.UUID; |
| 37 | +import java.util.function.Consumer; |
| 38 | +import java.util.stream.Collectors; |
| 39 | +import lombok.NonNull; |
| 40 | +import org.bukkit.Bukkit; |
| 41 | +import org.bukkit.Location; |
| 42 | +import org.bukkit.entity.Entity; |
| 43 | +import org.bukkit.entity.Player; |
| 44 | + |
| 45 | +/** |
| 46 | + * Utility class for converting objects to and from their corresponding Bukkit |
| 47 | + * representations with additional helper methods for easier integration. |
| 48 | + * |
| 49 | + * @since 1.1.8 |
| 50 | + */ |
| 51 | +public final class FoliaApollo { |
| 52 | + |
| 53 | + /** |
| 54 | + * Runs a specified operation for a {@link Player}. |
| 55 | + * |
| 56 | + * @param player the player |
| 57 | + * @param playerConsumer the operation to be performed |
| 58 | + * @since 1.1.8 |
| 59 | + */ |
| 60 | + public static void runForPlayer(@NonNull Player player, @NonNull Consumer<ApolloPlayer> playerConsumer) { |
| 61 | + runForPlayer(player.getUniqueId(), playerConsumer); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Runs a specified operation for a {@link ApolloPlayer} from the provided {@link UUID}. |
| 66 | + * |
| 67 | + * @param playerUuid the player |
| 68 | + * @param playerConsumer the operation to be performed |
| 69 | + * @since 1.1.8 |
| 70 | + */ |
| 71 | + public static void runForPlayer(@NonNull UUID playerUuid, @NonNull Consumer<ApolloPlayer> playerConsumer) { |
| 72 | + Apollo.getPlayerManager().getPlayer(playerUuid).ifPresent(playerConsumer); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Converts a {@link Collection} of {@link Player}s to an {@link Recipients}. |
| 77 | + * |
| 78 | + * @param players the players |
| 79 | + * @return the recipients object containing the converted ApolloPlayer objects |
| 80 | + * @since 1.1.8 |
| 81 | + */ |
| 82 | + public static Recipients getRecipientsFrom(@NonNull Collection<Player> players) { |
| 83 | + ApolloPlayerManager playerManager = Apollo.getPlayerManager(); |
| 84 | + List<ApolloPlayer> apolloPlayers = players.stream() |
| 85 | + .map(player -> playerManager.getPlayer(player.getUniqueId())) |
| 86 | + .filter(Optional::isPresent) |
| 87 | + .map(Optional::get) |
| 88 | + .collect(Collectors.toList()); |
| 89 | + |
| 90 | + return Recipients.of(apolloPlayers); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Converts a {@link Location} to an {@link ApolloLocation} object. |
| 95 | + * |
| 96 | + * @param location the location |
| 97 | + * @return the converted apollo location object |
| 98 | + * @since 1.1.8 |
| 99 | + */ |
| 100 | + public static ApolloLocation toApolloLocation(@NonNull Location location) { |
| 101 | + return ApolloLocation.builder() |
| 102 | + .world(location.getWorld().getName()) |
| 103 | + .x(location.getX()) |
| 104 | + .y(location.getY()) |
| 105 | + .z(location.getZ()) |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Converts a {@link Location} to an {@link ApolloBlockLocation} object. |
| 111 | + * |
| 112 | + * @param location the location |
| 113 | + * @return the converted apollo block location object |
| 114 | + * @since 1.1.8 |
| 115 | + */ |
| 116 | + public static ApolloBlockLocation toApolloBlockLocation(@NonNull Location location) { |
| 117 | + return ApolloBlockLocation.builder() |
| 118 | + .world(location.getWorld().getName()) |
| 119 | + .x(location.getBlockX()) |
| 120 | + .y(location.getBlockY()) |
| 121 | + .z(location.getBlockZ()) |
| 122 | + .build(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Converts a {@link Location} to an {@link ApolloPlayerLocation} object. |
| 127 | + * |
| 128 | + * @param location the location |
| 129 | + * @return the converted apollo player location object |
| 130 | + * @since 1.1.8 |
| 131 | + */ |
| 132 | + public static ApolloPlayerLocation toApolloPlayerLocation(@NonNull Location location) { |
| 133 | + return ApolloPlayerLocation.builder() |
| 134 | + .location(FoliaApollo.toApolloLocation(location)) |
| 135 | + .yaw(location.getYaw()) |
| 136 | + .pitch(location.getPitch()) |
| 137 | + .build(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Converts a {@link Entity} to an {@link ApolloEntity} object. |
| 142 | + * |
| 143 | + * @param entity the entity |
| 144 | + * @return the converted apollo entity object |
| 145 | + * @since 1.1.8 |
| 146 | + */ |
| 147 | + public static ApolloEntity toApolloEntity(@NonNull Entity entity) { |
| 148 | + return new ApolloEntity(entity.getEntityId(), entity.getUniqueId()); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Converts a {@link ApolloLocation} to an {@link Location} object. |
| 153 | + * |
| 154 | + * @param location the apollo location |
| 155 | + * @return the converted location object |
| 156 | + * @since 1.1.8 |
| 157 | + */ |
| 158 | + public static Location toBukkitLocation(@NonNull ApolloLocation location) { |
| 159 | + return new Location( |
| 160 | + Bukkit.getWorld(location.getWorld()), |
| 161 | + location.getX(), |
| 162 | + location.getY(), |
| 163 | + location.getZ() |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Converts a {@link ApolloBlockLocation} to an {@link Location} object. |
| 169 | + * |
| 170 | + * @param location the apollo block location |
| 171 | + * @return the converted location object |
| 172 | + * @since 1.1.8 |
| 173 | + */ |
| 174 | + public static Location toBukkitLocation(@NonNull ApolloBlockLocation location) { |
| 175 | + return new Location( |
| 176 | + Bukkit.getWorld(location.getWorld()), |
| 177 | + location.getX(), |
| 178 | + location.getY(), |
| 179 | + location.getZ() |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Converts a {@link ApolloPlayerLocation} to an {@link Location} object. |
| 185 | + * |
| 186 | + * @param location the apollo location |
| 187 | + * @return the converted location object |
| 188 | + * @since 1.1.8 |
| 189 | + */ |
| 190 | + public static Location toBukkitLocation(@NonNull ApolloPlayerLocation location) { |
| 191 | + ApolloLocation apolloLocation = location.getLocation(); |
| 192 | + |
| 193 | + return new Location( |
| 194 | + Bukkit.getWorld(apolloLocation.getWorld()), |
| 195 | + apolloLocation.getX(), |
| 196 | + apolloLocation.getY(), |
| 197 | + apolloLocation.getZ(), |
| 198 | + location.getYaw(), |
| 199 | + location.getPitch() |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + private FoliaApollo() { |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments