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.core.queue;
020
021import com.plotsquared.core.PlotSquared;
022import com.sk89q.worldedit.world.World;
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027public abstract class QueueProvider {
028
029    private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + PlotSquared.class.getSimpleName());
030
031    public static QueueProvider of(final @NonNull Class<? extends QueueCoordinator> primary) {
032        return new QueueProvider() {
033
034            @Override
035            public QueueCoordinator getNewQueue(@NonNull World world) {
036                try {
037                    return (QueueCoordinator) primary.getConstructors()[0].newInstance(world);
038                } catch (Throwable e) {
039                    LOGGER.error("Error creating Queue: {} - Does it have the correct constructor(s)?", primary.getName());
040                    if (!primary.getName().contains("com.plotsquared")) {
041                        LOGGER.error(
042                                "It looks like {} is a custom queue. Please look for a plugin in its classpath and report to them.",
043                                primary.getSimpleName()
044                        );
045                    }
046                    e.printStackTrace();
047                }
048                return null;
049            }
050        };
051    }
052
053    /**
054     * Get a queue for the given world
055     *
056     * @param world world
057     * @return new QueueCoordinator
058     */
059    public abstract QueueCoordinator getNewQueue(@NonNull World world);
060
061}