/* * This file is part of HuskHomes, licensed under the Apache License 2.0. * * Copyright (c) William278 * Copyright (c) contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.william278.huskhomes.util; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.World; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.spongepowered.api.ResourceKey; import org.spongepowered.api.Sponge; import org.spongepowered.api.world.WorldTypeTemplate; import org.spongepowered.api.world.server.ServerLocation; import org.spongepowered.api.world.server.ServerWorld; import org.spongepowered.api.world.server.WorldManager; import java.util.Optional; public final class SpongeAdapter { /** * Adapt a Sponge {@link ServerLocation} to a HuskHomes {@link Location}. * * @param location the Sponge {@link ServerLocation} to adapt * @return the adapted {@link Location} */ public static Optional adaptLocation(@NotNull Location location) { final WorldManager worldManager = Sponge.server().worldManager(); return worldManager .world(ResourceKey.resolve(location.getWorld().getName())) .map(world -> ServerLocation.of(world, location.getX(), location.getY(), location.getZ())) .or(() -> { final Optional worldKey = worldManager.worldKey(location.getWorld().getUuid()); return worldKey.flatMap(resourceKey -> worldManager.world(resourceKey) .map(world -> ServerLocation.of(world, location.getX(), location.getY(), location.getZ()))); }); } /** * Adapt a HuskHomes {@link Location} to a Sponge {@link ServerLocation}. * * @param location the HuskHomes {@link Location} to adapt * @return the adapted {@link ServerLocation} */ public static Optional adaptLocation(@NotNull ServerLocation location) { return Optional.of(Location.at( location.x(), location.y(), location.z(), adaptWorld(location.world()).orElse(new World())) ); } /** * Adapt a Sponge {@link ServerWorld} to a HuskHomes {@link World}. * * @param world the Sponge {@link ServerWorld} to adapt * @return the adapted {@link World} */ public static Optional adaptWorld(@Nullable ServerWorld world) { if (world == null) { return Optional.empty(); } final String worldType = world.properties().worldType().asTemplate().key().asString(); return Optional.of(World.from( world.key().toString(), world.uniqueId(), (worldType.equals(WorldTypeTemplate.theNether().key().toString()) ? World.Environment.NETHER : worldType.equals(WorldTypeTemplate.theEnd().key().toString()) ? World.Environment.THE_END : worldType.equals(WorldTypeTemplate.overworld().key().toString()) ? World.Environment.OVERWORLD : World.Environment.CUSTOM) )); } }