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    super(actor);
041
042    if(actor.isPlayer() && actor.asPlayer() != null) {
043      provider = new SpongePlayerProvider(actor.asPlayer().user(),
044                                          SpongePluginCore.instance().getContainer());
045    } else {
046      provider = null;
047    }
048  }
049
050  /**
051   * Determines if this {@link CmdSource} is an instance of a player.
052   *
053   * @return True if this represents a player, otherwise false if it's a non-player such as the console.
054   */
055  @Override
056  public boolean isPlayer() {
057    return actor.isPlayer();
058  }
059
060  /**
061   * Used to get the related {@link PlayerProvider} for this command source.
062   *
063   * @return An optional containing the related {@link PlayerProvider} if this command source is a
064   * player, otherwise an empty {@link Optional}.
065   */
066  @Override
067  public Optional<PlayerProvider> player() {
068    return Optional.ofNullable(provider);
069  }
070
071  /**
072   * Used to send a message to this command source.
073   *
074   * @param messageData The message data to utilize for this translation.
075   */
076  @Override
077  public void message(final MessageData messageData) {
078    if(identifier().isEmpty()) {
079      MessageHandler.translate(messageData, null, actor.cause().audience());
080      return;
081    }
082    MessageHandler.translate(messageData, identifier().get(), actor.cause().audience());
083  }
084}