001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.bukkit.util;
020
021import org.bukkit.Chunk;
022import org.bukkit.Location;
023import org.bukkit.World;
024import org.bukkit.entity.Entity;
025import org.bukkit.event.player.PlayerTeleportEvent;
026import org.jetbrains.annotations.ApiStatus;
027
028import java.util.concurrent.CompletableFuture;
029
030/**
031 * Not for external use. May change at any point.
032 */
033@ApiStatus.Internal
034public enum PaperSupport {
035    ;
036    public static final boolean PAPER;
037
038    static {
039        PAPER = hasClass("com.destroystokyo.paper.PaperConfig")
040            || hasClass("io.papermc.paper.configuration.Configuration");
041    }
042
043    public static boolean isPaper() {
044        return PAPER;
045    }
046
047    public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location) {
048        if (isPaper()) {
049            return entity.teleportAsync(location);
050        }
051        return CompletableFuture.completedFuture(entity.teleport(location));
052    }
053
054    public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location location, PlayerTeleportEvent.TeleportCause cause) {
055        if (isPaper()) {
056            return entity.teleportAsync(location, cause);
057        }
058        return CompletableFuture.completedFuture(entity.teleport(location, cause));
059    }
060
061    public static CompletableFuture<Chunk> getChunkAtAsync(Location location) {
062        return getChunkAtAsync(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, true);
063    }
064
065    public static CompletableFuture<Chunk> getChunkAtAsync(World world, int cx, int cz, boolean gen) {
066        return getChunkAtAsync(world, cx, cz, gen, false);
067    }
068
069    public static CompletableFuture<Chunk> getChunkAtAsync(World world, int cx, int cz, boolean gen, boolean urgent) {
070        if (isPaper()) {
071            return world.getChunkAtAsync(cx, cz, gen, urgent);
072        } else {
073            return CompletableFuture.completedFuture(world.getChunkAt(cx, cz));
074        }
075    }
076
077    private static boolean hasClass(String className) {
078        try {
079            Class.forName(className);
080            return true;
081        } catch (ClassNotFoundException e) {
082            return false;
083        }
084    }
085}