001package net.tnemc.plugincore.core.compatibility;
002
003/*
004 * The New Plugin Core
005 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Affero General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU Affero General Public License for more details.
016 *
017 * You should have received a copy of the GNU Affero General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020
021import net.tnemc.plugincore.core.io.message.MessageData;
022import revxrsal.commands.command.CommandActor;
023
024import java.util.Optional;
025import java.util.UUID;
026
027/**
028 * CmdSource
029 *
030 * @author creatorfromhell
031 * @see PlayerProvider
032 * @since 0.1.2.0
033 */
034public abstract class CmdSource<T extends CommandActor> {
035
036  protected final T actor;
037
038  public CmdSource(final T actor) {
039
040    this.actor = actor;
041  }
042
043  /**
044   * The UUID of this command source.
045   *
046   * @return The UUID of this command source.
047   */
048  public Optional<UUID> identifier() {
049
050    if(!isPlayer()) {
051      return Optional.empty();
052    }
053    return Optional.of(actor.uniqueId());
054  }
055
056  /**
057   * The name of this command source.
058   *
059   * @return The name of this command source.
060   */
061  public String name() {
062
063    return actor.name();
064  }
065
066  /**
067   * Determines if this {@link CmdSource} is an instance of a player.
068   *
069   * @return True if this represents a player, otherwise false if it's a non-player such as the
070   * console.
071   */
072  public abstract boolean isPlayer();
073
074  /**
075   * Used to get the related {@link PlayerProvider} for this command source.
076   *
077   * @return An optional containing the related {@link PlayerProvider} if this command source is a
078   * player, otherwise an empty {@link Optional}.
079   */
080  public abstract Optional<PlayerProvider> player();
081
082  /**
083   * Used to send a message to this command source.
084   *
085   * @param messageData The message data to utilize for this translation.
086   */
087  public abstract void message(final MessageData messageData);
088
089  public T getActor() {
090
091    return actor;
092  }
093}