001package net.tnemc.plugincore.paper.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.core.compatibility.CmdSource; 024import net.tnemc.plugincore.core.compatibility.PlayerProvider; 025import net.tnemc.plugincore.core.io.message.MessageData; 026import net.tnemc.plugincore.core.io.message.MessageHandler; 027import net.tnemc.plugincore.paper.PaperPluginCore; 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 PaperCMDSource extends CmdSource<BukkitCommandActor> { 039 040 private final PlayerProvider provider; 041 042 public PaperCMDSource(final BukkitCommandActor actor) { 043 044 super(actor); 045 046 if(actor.isPlayer() && actor.asPlayer() != null) { 047 provider = PluginCore.server().initializePlayer(actor.asPlayer()); 048 } else { 049 provider = null; 050 } 051 } 052 053 /** 054 * Determines if this {@link CmdSource} is an instance of a player. 055 * 056 * @return True if this represents a player, otherwise false if it's a non-player such as the 057 * console. 058 */ 059 @Override 060 public boolean isPlayer() { 061 062 return actor.isPlayer(); 063 } 064 065 /** 066 * Used to get the related {@link PlayerProvider} for this command source. 067 * 068 * @return An optional containing the related {@link PlayerProvider} if this command source is a 069 * player, otherwise an empty {@link Optional}. 070 */ 071 @Override 072 public Optional<PlayerProvider> player() { 073 074 return Optional.ofNullable(provider); 075 } 076 077 /** 078 * Used to send a message to this command source. 079 * 080 * @param messageData The message data to utilize for this translation. 081 */ 082 @Override 083 public void message(final MessageData messageData) { 084 085 try(final BukkitAudiences provider = BukkitAudiences.create(PaperPluginCore.instance().getPlugin())) { 086 if(identifier().isEmpty()) { 087 MessageHandler.translate(messageData, null, provider.sender(actor.sender())); 088 return; 089 } 090 MessageHandler.translate(messageData, identifier().get(), provider.sender(actor.sender())); 091 } 092 } 093}