001package net.tnemc.plugincore.sponge.impl;
002/*
003 * The New Plugin Core
004 * Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
015 *
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020import net.tnemc.plugincore.core.compatibility.CmdSource;
021import net.tnemc.plugincore.core.compatibility.PlayerProvider;
022import net.tnemc.plugincore.core.io.message.MessageData;
023import net.tnemc.plugincore.core.io.message.MessageHandler;
024import net.tnemc.plugincore.sponge.SpongePluginCore;
025import revxrsal.commands.sponge.actor.SpongeCommandActor;
026
027import java.util.Optional;
028
029/**
030 * SpongeCMDSource
031 *
032 * @author creatorfromhell
033 * @since 0.1.2.0
034 */
035public class SpongeCMDSource extends CmdSource<SpongeCommandActor> {
036
037  private final SpongePlayerProvider provider;
038
039  public SpongeCMDSource(final SpongeCommandActor actor) {
040
041    super(actor);
042
043    if(actor.isPlayer() && actor.asPlayer() != null) {
044      provider = new SpongePlayerProvider(actor.asPlayer().user(),
045                                          SpongePluginCore.instance().getContainer());
046    } else {
047      provider = null;
048    }
049  }
050
051  /**
052   * Determines if this {@link CmdSource} is an instance of a player.
053   *
054   * @return True if this represents a player, otherwise false if it's a non-player such as the
055   * console.
056   */
057  @Override
058  public boolean isPlayer() {
059
060    return actor.isPlayer();
061  }
062
063  /**
064   * Used to get the related {@link PlayerProvider} for this command source.
065   *
066   * @return An optional containing the related {@link PlayerProvider} if this command source is a
067   * player, otherwise an empty {@link Optional}.
068   */
069  @Override
070  public Optional<PlayerProvider> player() {
071
072    return Optional.ofNullable(provider);
073  }
074
075  /**
076   * Used to send a message to this command source.
077   *
078   * @param messageData The message data to utilize for this translation.
079   */
080  @Override
081  public void message(final MessageData messageData) {
082
083    if(identifier().isEmpty()) {
084      MessageHandler.translate(messageData, null, actor.cause().audience());
085      return;
086    }
087    MessageHandler.translate(messageData, identifier().get(), actor.cause().audience());
088  }
089}