001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api;
019
020import baritone.api.cache.IWorldScanner;
021import baritone.api.command.ICommand;
022import baritone.api.command.ICommandSystem;
023import baritone.api.schematic.ISchematicSystem;
024import net.minecraft.client.Minecraft;
025import net.minecraft.client.multiplayer.ClientPacketListener;
026import net.minecraft.client.player.LocalPlayer;
027
028import java.util.List;
029import java.util.Objects;
030
031/**
032 * Provides the present {@link IBaritone} instances, as well as non-baritone instance related APIs.
033 *
034 * @author leijurv
035 */
036public interface IBaritoneProvider {
037
038    /**
039     * Returns the primary {@link IBaritone} instance. This instance is persistent, and
040     * is represented by the local player that is created by the game itself, not a "bot"
041     * player through Baritone.
042     *
043     * @return The primary {@link IBaritone} instance.
044     */
045    IBaritone getPrimaryBaritone();
046
047    /**
048     * Returns all of the active {@link IBaritone} instances. This includes the local one
049     * returned by {@link #getPrimaryBaritone()}.
050     *
051     * @return All active {@link IBaritone} instances.
052     * @see #getBaritoneForPlayer(LocalPlayer)
053     */
054    List<IBaritone> getAllBaritones();
055
056    /**
057     * Provides the {@link IBaritone} instance for a given {@link LocalPlayer}.
058     *
059     * @param player The player
060     * @return The {@link IBaritone} instance.
061     */
062    default IBaritone getBaritoneForPlayer(LocalPlayer player) {
063        for (IBaritone baritone : this.getAllBaritones()) {
064            if (Objects.equals(player, baritone.getPlayerContext().player())) {
065                return baritone;
066            }
067        }
068        return null;
069    }
070
071    /**
072     * Provides the {@link IBaritone} instance for a given {@link Minecraft}.
073     *
074     * @param minecraft The minecraft
075     * @return The {@link IBaritone} instance.
076     */
077    default IBaritone getBaritoneForMinecraft(Minecraft minecraft) {
078        for (IBaritone baritone : this.getAllBaritones()) {
079            if (Objects.equals(minecraft, baritone.getPlayerContext().minecraft())) {
080                return baritone;
081            }
082        }
083        return null;
084    }
085
086    /**
087     * Provides the {@link IBaritone} instance for the player with the specified connection.
088     *
089     * @param connection The connection
090     * @return The {@link IBaritone} instance.
091     */
092    default IBaritone getBaritoneForConnection(ClientPacketListener connection) {
093        for (IBaritone baritone : this.getAllBaritones()) {
094            final LocalPlayer player = baritone.getPlayerContext().player();
095            if (player != null && player.connection == connection) {
096                return baritone;
097            }
098        }
099        return null;
100    }
101
102    /**
103     * Creates and registers a new {@link IBaritone} instance using the specified {@link Minecraft}. The existing
104     * instance is returned if already registered.
105     *
106     * @param minecraft The minecraft
107     * @return The {@link IBaritone} instance
108     */
109    IBaritone createBaritone(Minecraft minecraft);
110
111    /**
112     * Destroys and removes the specified {@link IBaritone} instance. If the specified instance is the
113     * {@link #getPrimaryBaritone() primary baritone}, this operation has no effect and will return {@code false}.
114     *
115     * @param baritone The baritone instance to remove
116     * @return Whether the baritone instance was removed
117     */
118    boolean destroyBaritone(IBaritone baritone);
119
120    /**
121     * Returns the {@link IWorldScanner} instance. This is not a type returned by
122     * {@link IBaritone} implementation, because it is not linked with {@link IBaritone}.
123     *
124     * @return The {@link IWorldScanner} instance.
125     */
126    IWorldScanner getWorldScanner();
127
128    /**
129     * Returns the {@link ICommandSystem} instance. This is not bound to a specific {@link IBaritone}
130     * instance because {@link ICommandSystem} itself controls global behavior for {@link ICommand}s.
131     *
132     * @return The {@link ICommandSystem} instance.
133     */
134    ICommandSystem getCommandSystem();
135
136    /**
137     * @return The {@link ISchematicSystem} instance.
138     */
139    ISchematicSystem getSchematicSystem();
140}