    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.jetbrains.annotations.NotNull;
    027import org.spongepowered.api.command.CommandSource;
    028import org.spongepowered.api.entity.living.player.Player;
    029import org.spongepowered.api.text.serializer.TextSerializers;
    030import org.spongepowered.api.util.Identifiable;
    031
    032import java.nio.charset.StandardCharsets;
    033import java.util.Objects;
    034import java.util.UUID;
    035
    036public class SpongeCommandIssuer implements CommandIssuer {
    037
    038    private final SpongeCommandManager manager;
    039    private final CommandSource source;
    040
    041    SpongeCommandIssuer(SpongeCommandManager manager, final CommandSource source) {
    042        this.manager = manager;
    043        this.source = source;
    044    }
    045
    046    @Override
    047    public boolean isPlayer() {
    048        return this.source instanceof Player;
    049    }
    050
    051    @Override
    052    public CommandSource getIssuer() {
    053        return this.source;
    054    }
    055
    056    @Override
    057    public @NotNull UUID getUniqueId() {
    058        if (this.source instanceof Identifiable) {
    059            return ((Identifiable) source).getUniqueId();
    060        }
    061
    062        //generate a unique id based of the name (like for the console command sender)
    063        return UUID.nameUUIDFromBytes(source.getName().getBytes(StandardCharsets.UTF_8));
    064    }
    065
    066    public Player getPlayer() {
    067        return isPlayer() ? (Player) source : null;
    068    }
    069
    070    @Override
    071    public CommandManager getManager() {
    072        return manager;
    073    }
    074
    075    @Override
    076    public void sendMessageInternal(String message) {
    077        this.source.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message));
    078    }
    079
    080    @Override
    081    public boolean hasPermission(final String permission) {
    082        return this.source.hasPermission(permission);
    083    }
    084
    085    @Override
    086    public boolean equals(Object o) {
    087        if (this == o) return true;
    088        if (o == null || getClass() != o.getClass()) return false;
    089        SpongeCommandIssuer that = (SpongeCommandIssuer) o;
    090        return Objects.equals(source, that.source);
    091    }
    092
    093    @Override
    094    public int hashCode() {
    095        return Objects.hash(source);
    096    }
    097}