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;
020
021import com.plotsquared.core.configuration.caption.Caption;
022import com.plotsquared.core.configuration.caption.StaticCaption;
023import com.plotsquared.core.location.Location;
024import com.plotsquared.core.player.ConsolePlayer;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.plot.Plot;
027import com.plotsquared.core.plot.PlotArea;
028import com.plotsquared.core.queue.GlobalBlockQueue;
029import com.plotsquared.core.util.ChunkManager;
030import com.plotsquared.core.util.EventDispatcher;
031import com.plotsquared.core.util.SchematicHandler;
032import com.plotsquared.core.util.query.PlotQuery;
033import net.kyori.adventure.text.minimessage.Template;
034import org.checkerframework.checker.nullness.qual.NonNull;
035import org.checkerframework.checker.nullness.qual.Nullable;
036
037import java.util.Collections;
038import java.util.Set;
039import java.util.UUID;
040
041/**
042 * PlotSquared API.
043 *
044 * <p>Useful classes:
045 * <ul>
046 * <li>{@link PlotPlayer}</li>
047 * <li>{@link Plot}</li>
048 * <li>{@link Location}</li>
049 * <li>{@link PlotArea}</li>
050 * <li>{@link PlotSquared}</li>
051 * </ul>
052 *
053 * @version 6
054 */
055@SuppressWarnings({"unused", "WeakerAccess"})
056public class PlotAPI {
057
058    public PlotAPI() {
059    }
060
061    /**
062     * Gets all plots.
063     *
064     * @return all plots
065     */
066    public @NonNull Set<@NonNull Plot> getAllPlots() {
067        return PlotQuery.newQuery().allPlots().asSet();
068    }
069
070    /**
071     * Gets all plots for a player.
072     *
073     * @param player Player, whose plots to search for
074     * @return all plots that a player owns
075     */
076    public @NonNull Set<@NonNull Plot> getPlayerPlots(final @NonNull PlotPlayer<?> player) {
077        return PlotQuery.newQuery().ownedBy(player).asSet();
078    }
079
080    /**
081     * Adds a plot world.
082     *
083     * @param plotArea Plot World Object
084     * @see PlotSquared#addPlotArea(PlotArea)
085     */
086    public void addPlotArea(final @NonNull PlotArea plotArea) {
087        PlotSquared.get().addPlotArea(plotArea);
088    }
089
090    /**
091     * ChunkManager class contains several useful methods.
092     * <ul>
093     * <li>Chunk deletion</li>
094     * <li>Moving or copying regions</li>
095     * <li>Plot swapping</li>
096     * <li>Entity Tracking</li>
097     * <li>Region Regeneration</li>
098     * </ul>
099     *
100     * @return ChunkManager
101     * @see ChunkManager
102     */
103    public @NonNull ChunkManager getChunkManager() {
104        return PlotSquared.platform().injector().getInstance(ChunkManager.class);
105    }
106
107    /**
108     * Gets the block/biome set queue
109     *
110     * @return GlobalBlockQueue.IMP
111     */
112    public @NonNull GlobalBlockQueue getBlockQueue() {
113        return PlotSquared.platform().globalBlockQueue();
114    }
115
116    /**
117     * SchematicHandler class contains methods related to pasting, reading
118     * and writing schematics.
119     *
120     * @return SchematicHandler
121     * @see SchematicHandler
122     */
123    public @NonNull SchematicHandler getSchematicHandler() {
124        return PlotSquared.platform().injector().getInstance(SchematicHandler.class);
125    }
126
127    /**
128     * Gets a list of PlotAreas in the world.
129     *
130     * @param world The world to check for plot areas
131     * @return A set of PlotAreas
132     */
133    public @NonNull Set<@NonNull PlotArea> getPlotAreas(final @Nullable String world) {
134        if (world == null) {
135            return Collections.emptySet();
136        }
137        return PlotSquared.get().getPlotAreaManager().getPlotAreasSet(world);
138    }
139
140    /**
141     * Send a message to the console. The message supports color codes.
142     *
143     * @param message      the message
144     * @param replacements Variable replacements
145     */
146    public void sendConsoleMessage(
147            final @NonNull String message,
148            final @NonNull Template @NonNull ... replacements
149    ) {
150        ConsolePlayer.getConsole().sendMessage(StaticCaption.of(message), replacements);
151    }
152
153    /**
154     * Sends a message to the console.
155     *
156     * @param caption      the message
157     * @param replacements Variable replacements
158     */
159    public void sendConsoleMessage(
160            final @NonNull Caption caption,
161            final @NonNull Template @NonNull ... replacements
162    ) {
163        ConsolePlayer.getConsole().sendMessage(caption, replacements);
164    }
165
166    /**
167     * Gets the PlotSquared class.
168     *
169     * @return PlotSquared Class
170     * @see PlotSquared
171     */
172    public @NonNull PlotSquared getPlotSquared() {
173        return PlotSquared.get();
174    }
175
176    /**
177     * Gets the PlotPlayer for a UUID.
178     *
179     * <p><i>Please note that PlotSquared can be configured to provide
180     * different UUIDs than Bukkit</i>
181     *
182     * @param uuid the uuid of the player to wrap
183     * @return a {@link PlotPlayer}
184     */
185    public @Nullable PlotPlayer<?> wrapPlayer(final @NonNull UUID uuid) {
186        return PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
187    }
188
189    /**
190     * Gets the PlotPlayer for a username.
191     *
192     * @param player the player to wrap
193     * @return a {@link PlotPlayer}
194     */
195    public @Nullable PlotPlayer<?> wrapPlayer(final @NonNull String player) {
196        return PlotSquared.platform().playerManager().getPlayerIfExists(player);
197    }
198
199    /**
200     * Registers a listener for PlotSquared Events
201     *
202     * @param listener the listener class to register
203     * @see EventDispatcher#registerListener(Object)
204     */
205    public void registerListener(final @NonNull Object listener) {
206        PlotSquared.get().getEventDispatcher().registerListener(listener);
207    }
208
209}