001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.server.command;
018
019import net.tridentsdk.Console;
020import net.tridentsdk.entity.living.Player;
021import net.tridentsdk.meta.ChatColor;
022import net.tridentsdk.meta.MessageBuilder;
023import net.tridentsdk.plugin.annotation.CommandDesc;
024import net.tridentsdk.plugin.cmd.Command;
025import net.tridentsdk.registry.Registered;
026import net.tridentsdk.service.PermissionOwner;
027
028import java.util.UUID;
029
030@CommandDesc(name = "op", permission = "trident.op")
031public class OpCommand extends Command {
032    @Override
033    public void handlePlayer(Player player, String arguments, String alias) {
034        String[] args = arguments.split(" ");
035        if (args.length != 1) {
036            new MessageBuilder("Those are the wrong arguments").color(ChatColor.RED).sendTo(player);
037            return;
038        }
039
040        UUID uuid = null;
041        for (Player p : Registered.players()) {
042            if (p.name().equals(args[0])) {
043                uuid = p.uniqueId();
044            }
045        }
046
047        if (uuid == null) {
048            new MessageBuilder("There is no player by the name " + args[0]).color(ChatColor.RED).sendTo(player);
049            return;
050        }
051
052        Registered.statuses().op(uuid);
053        new MessageBuilder("[CONSOLE: Opped " + uuid + "]").color(ChatColor.GRAY).sendTo(player);
054    }
055
056    @Override
057    public void handleConsole(Console sender, String arguments, String alias) {
058        String[] args = arguments.split(" ");
059        if (args.length != 1) {
060            sender.sendRaw(ChatColor.RED + "Those are the wrong arguments");
061            return;
062        }
063
064        UUID uuid = null;
065        for (Player p : Registered.players()) {
066            if (p.name().equals(args[0])) {
067                uuid = p.uniqueId();
068            }
069        }
070
071        if (uuid == null) {
072            sender.sendRaw(ChatColor.RED + "There is no player by the name " + args[0]);
073            return;
074        }
075
076        Registered.statuses().op(uuid);
077        sender.sendRaw(ChatColor.GREEN + "Opped " + uuid);
078
079        final UUID finalUuid = uuid;
080        Registered.players().stream().filter(PermissionOwner::opped).forEach(player ->
081                new MessageBuilder("[CONSOLE: Opped " + finalUuid + "]").color(ChatColor.GRAY).sendTo(player));
082    }
083}