# Source code
source: package: co.aikar.commands, class: BukkitCommandIssuer

    001/*
    002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
    003 *
    004 *  Permission is hereby granted, free of charge, to any person obtaining
    005 *  a copy of this software and associated documentation files (the
    006 *  "Software"), to deal in the Software without restriction, including
    007 *  without limitation the rights to use, copy, modify, merge, publish,
    008 *  distribute, sublicense, and/or sell copies of the Software, and to
    009 *  permit persons to whom the Software is furnished to do so, subject to
    010 *  the following conditions:
    011 *
    012 *  The above copyright notice and this permission notice shall be
    013 *  included in all copies or substantial portions of the Software.
    014 *
    015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    022 */
    023
    024package co.aikar.commands;
    025
    026import org.bukkit.command.CommandSender;
    027import org.bukkit.entity.Player;
    028import org.jetbrains.annotations.NotNull;
    029
    030import java.nio.charset.StandardCharsets;
    031import java.util.Objects;
    032import java.util.UUID;
    033
    034public class BukkitCommandIssuer implements CommandIssuer {
    035    private final BukkitCommandManager manager;
    036    private final CommandSender sender;
    037
    038    BukkitCommandIssuer(BukkitCommandManager manager, CommandSender sender) {
    039        this.manager = manager;
    040        this.sender = sender;
    041    }
    042
    043    @Override
    044    public boolean isPlayer() {
    045        return sender instanceof Player;
    046    }
    047
    048    @Override
    049    public CommandSender getIssuer() {
    050        return sender;
    051    }
    052
    053    public Player getPlayer() {
    054        return isPlayer() ? (Player) sender : null;
    055    }
    056
    057    @Override
    058    public @NotNull UUID getUniqueId() {
    059        if (isPlayer()) {
    060            return ((Player) sender).getUniqueId();
    061        }
    062
    063        //generate a unique id based of the name (like for the console command sender)
    064        return UUID.nameUUIDFromBytes(sender.getName().getBytes(StandardCharsets.UTF_8));
    065    }
    066
    067    @Override
    068    public CommandManager getManager() {
    069        return manager;
    070    }
    071
    072    @Override
    073    public void sendMessageInternal(String message) {
    074        sender.sendMessage(ACFBukkitUtil.color(message));
    075    }
    076
    077    @Override
    078    public boolean hasPermission(String name) {
    079        return sender.hasPermission(name);
    080    }
    081
    082    @Override
    083    public boolean equals(Object o) {
    084        if (this == o) return true;
    085        if (o == null || getClass() != o.getClass()) return false;
    086        BukkitCommandIssuer that = (BukkitCommandIssuer) o;
    087        return Objects.equals(sender, that.sender);
    088    }
    089
    090    @Override
    091    public int hashCode() {
    092        return Objects.hash(sender);
    093    }
    094}