001package net.tnemc.plugincore.bukkit.impl;
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.kyori.adventure.platform.bukkit.BukkitAudiences;
022import net.tnemc.plugincore.PluginCore;
023import net.tnemc.plugincore.bukkit.BukkitPluginCore;
024import net.tnemc.plugincore.core.compatibility.CmdSource;
025import net.tnemc.plugincore.core.compatibility.PlayerProvider;
026import net.tnemc.plugincore.core.io.message.MessageData;
027import net.tnemc.plugincore.core.io.message.MessageHandler;
028import revxrsal.commands.bukkit.actor.BukkitCommandActor;
029
030import java.util.Optional;
031
032/**
033 * BukkitCMDSource
034 *
035 * @author creatorfromhell
036 * @since 0.1.2.0
037 */
038public class BukkitCMDSource extends CmdSource<BukkitCommandActor> {
039  private final PlayerProvider provider;
040
041  public BukkitCMDSource(final BukkitCommandActor actor) {
042    super(actor);
043
044    if(actor.isPlayer() && actor.asPlayer() != null) {
045      provider = PluginCore.server().initializePlayer(actor.asPlayer());
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 console.
055   */
056  @Override
057  public boolean isPlayer() {
058    return actor.isPlayer();
059  }
060
061  /**
062   * Used to get the related {@link PlayerProvider} for this command source.
063   *
064   * @return An optional containing the related {@link PlayerProvider} if this command source is a
065   * player, otherwise an empty {@link Optional}.
066   */
067  @Override
068  public Optional<PlayerProvider> player() {
069    return Optional.ofNullable(provider);
070  }
071
072  /**
073   * Used to send a message to this command source.
074   *
075   * @param messageData The message data to utilize for this translation.
076   */
077  @Override
078  public void message(final MessageData messageData) {
079
080    try(final BukkitAudiences provider = BukkitAudiences.create(BukkitPluginCore.instance().getPlugin())) {
081      if(identifier().isEmpty()) {
082        MessageHandler.translate(messageData, null, provider.sender(actor.sender()));
083        return;
084      }
085      MessageHandler.translate(messageData, identifier().get(), provider.sender(actor.sender()));
086    }
087  }
088}