Search in sources :

Example 36 with CommandSender

use of net.md_5.bungee.api.CommandSender in project Bukkit_Bungee_PluginLib by GeorgH93.

the class MessageTest method testBroadcast.

@PrepareForTest({ ProxyServer.class })
@Test
public void testBroadcast() throws NoSuchFieldException, IllegalAccessException {
    int sendMessageCalls = 0;
    int doBroadcastCalls = 0;
    Message message = new Message("");
    Field method = Message.class.getDeclaredField("method");
    method.setAccessible(true);
    ChatSender mockedSender = mock(ChatSender.class);
    doNothing().when(mockedSender).doBroadcast(anyString(), anyObject());
    SendMethod sendMethod = (SendMethod) method.get(message);
    Field defaultSender = sendMethod.getClass().getDeclaredField("defaultSender");
    defaultSender.setAccessible(true);
    defaultSender.set(sendMethod, mockedSender);
    CommandSender mockedCommandSender = mock(CommandSender.class);
    // noinspection deprecation
    doNothing().when(mockedCommandSender).sendMessage(anyString());
    ProxyServer mockedProxyServer = mock(ProxyServer.class);
    when(mockedProxyServer.getConsole()).thenReturn(mockedCommandSender);
    Field proxyServer = ProxyServer.class.getDeclaredField("instance");
    proxyServer.setAccessible(true);
    proxyServer.set(mockedProxyServer, mockedProxyServer);
    message.broadcast("Test");
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.broadcast((Object[]) null);
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.broadcast();
    // noinspection deprecation
    verify(mockedCommandSender, times(++sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(++doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    message.setSendMethod(null);
    message.broadcast();
    // noinspection deprecation
    verify(mockedCommandSender, times(sendMessageCalls)).sendMessage(anyString());
    verify(mockedSender, times(doBroadcastCalls)).doBroadcast(anyString(), anyObject());
    defaultSender.setAccessible(false);
    proxyServer.setAccessible(false);
    method.setAccessible(false);
}
Also used : Field(java.lang.reflect.Field) ChatSender(at.pcgamingfreaks.Bungee.Message.Sender.ChatSender) SendMethod(at.pcgamingfreaks.Bungee.Message.Sender.SendMethod) CommandSender(net.md_5.bungee.api.CommandSender) ProxyServer(net.md_5.bungee.api.ProxyServer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 37 with CommandSender

use of net.md_5.bungee.api.CommandSender in project Ublisk by Derkades.

the class GuildCommand method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!(sender instanceof Player)) {
        sender.sendMessage(Message.NOT_A_PLAYER.toString());
    }
    UPlayer player = new UPlayer(sender);
    if (args.length == 1) {
        if (args[0].equalsIgnoreCase("accept")) {
            if (Guild.GUILD_INVITES.containsKey(player.getName())) {
                // Player has got an invite
                GuildInvite invite = Guild.GUILD_INVITES.get(player.getName());
                Guild guild = invite.getGuild();
                player.setGuild(guild);
                player.sendPrefixedMessage("Guilds", "You joined " + guild.getName() + "!");
                for (UPlayer guildMember : guild.getMembers()) {
                    if (guildMember.isOnline() && guildMember.getUniqueId() != player.getUniqueId()) {
                        guildMember.sendMessage(ChatColor.DARK_AQUA + "" + ChatColor.BOLD + player.getName() + " has joined your guild!");
                    }
                }
                Guild.GUILD_INVITES.remove(player.getName());
                return true;
            } else {
                // Player does not have an invite
                player.sendPrefixedMessage("Guilds", "You don't have any pending invites.");
                return true;
            }
        } else if (args[0].equalsIgnoreCase("leave")) {
            if (player.getGuild() == null) {
                player.sendPrefixedMessage("Guilds", "You are not in a guild");
                return true;
            }
            Guild guild = player.getGuild();
            if (guild.getMembers().size() <= 1) {
                // If player is last person in guild (player can be owner if they're the only one left)
                player.sendPrefixedMessage("Guilds", "You left " + guild.getName() + ". Because you were the last player in the guild, your guild has been queued for deletion. If you regret leaving this guild, ask a staff member to add you back.");
                player.leaveGuild();
                return true;
            } else if (guild.getOwner().getUniqueId().equals(player.getUniqueId())) {
                // If player is owner - don't allow them to leave
                player.sendMessage("You are owner of this guild. You can only leave this guild if you transfer ownership to another guild member.");
                return true;
            } else {
                // Otherwise it's just a normal player - allow leaving
                player.sendPrefixedMessage("Guilds", "You have left " + guild.getName());
                player.leaveGuild();
                return true;
            }
        } else if (args[0].equalsIgnoreCase("list")) {
            player.sendMessage(ChatColor.AQUA + "----------={" + ChatColor.DARK_AQUA + " Guilds " + ChatColor.AQUA + "}=----------");
            for (Guild guild : Guild.getGuildsList(10)) {
                BaseComponent[] members = new BaseComponent[] {};
                for (UPlayer member : guild.getMembers()) {
                    BaseComponent[] components = member.getDisplayName(ChatColor.BLUE, false);
                    members = ArrayUtils.addAll(members, components);
                }
                player.sendMessage(ArrayUtils.addAll(TextComponent.fromLegacyText(ChatColor.YELLOW + guild.getName() + ChatColor.DARK_GRAY + " | " + ChatColor.RED + guild.getPoints() + " Points" + ChatColor.DARK_GRAY + " | "), members));
            }
            return true;
        } else if (args[0].equalsIgnoreCase("help")) {
            return false;
        } else if (args[0].equalsIgnoreCase("info")) {
            Guild guild = player.getGuild();
            if (guild == null) {
                player.sendPrefixedMessage("Guilds", "You are not in a guild. Use /guild info [guild] to check information about another guild.");
                return true;
            }
            player.executeCommand("guild info " + guild.getName());
            return true;
        } else {
            return false;
        }
    } else if (args.length == 2) {
        if (args[0].equals("create")) {
            Ublisk.runAsync(() -> {
                if (args[1].length() > 20) {
                    Ublisk.runSync(() -> player.sendPrefixedMessage("Guilds", "This guild name is too long."));
                    return;
                }
                if (!StringUtils.validateString(args[1])) {
                    Ublisk.runSync(() -> player.sendPrefixedMessage("Guilds", "Guild names can only contain alphanumerical characters and underscores (a-z, A-Z, 0-9, _)"));
                    return;
                }
                if (player.getGuild() != null) {
                    Ublisk.runSync(() -> player.sendPrefixedMessage("Guilds", "You are already in a guild. Please leave your current guild before creating a new one."));
                    return;
                }
                Guild guild = new Guild(args[1]);
                if (guild.exists()) {
                    Ublisk.runSync(() -> player.sendPrefixedMessage("Guilds", "A guild with this name already exists."));
                    return;
                }
                guild.create(player);
                Ublisk.runSync(() -> player.sendPrefixedMessage("Guilds", "The guild has been created."));
            });
            return true;
        } else if (args[0].equals("info")) {
            Guild guild = new Guild(args[1]);
            if (!guild.exists()) {
                player.sendPrefixedMessage("Guilds", "This guild does not exist.");
                return true;
            }
            player.sendMessage(new ComponentBuilder("Name: ").color(ChatColor.DARK_AQUA).create(), new ComponentBuilder(guild.getName()).color(ChatColor.AQUA).create());
            player.sendMessage(new ComponentBuilder("Points: ").color(ChatColor.DARK_AQUA).create(), new ComponentBuilder(guild.getPoints() + "").color(ChatColor.AQUA).create());
            player.sendMessage(new ComponentBuilder("Description: ").color(ChatColor.DARK_AQUA).create(), new ComponentBuilder(guild.getDescription()).color(ChatColor.AQUA).create());
            player.sendMessage(new ComponentBuilder("Members: ").color(ChatColor.DARK_AQUA).create());
            for (UPlayer member : guild.getMembers()) {
                player.sendMessage(new ComponentBuilder("- ").color(ChatColor.DARK_GRAY).bold(true).create(), member.getDisplayName(ChatColor.AQUA, false));
            }
            return true;
        } else if (args[0].equalsIgnoreCase("invite")) {
            if (player.getGuild() == null) {
                player.sendPrefixedMessage("Guilds", "You are not in a guild.");
                return true;
            }
            UPlayer target = null;
            try {
                target = new UPlayer(args[1]);
            } catch (PlayerNotFoundException e) {
                player.sendMessage(Message.PLAYER_NOT_FOUND);
                return true;
            }
            Guild guild = player.getGuild();
            guild.invitePlayer(player, target);
            player.sendPrefixedMessage("Guilds", "You have invited " + target.getName() + " to your guild.");
            return true;
        } else if (args[0].equalsIgnoreCase("rename")) {
            if (player.getGuild() == null) {
                player.sendPrefixedMessage("Guilds", "You are not in a guild.");
                return true;
            }
            if (!StringUtils.validateString(args[1])) {
                player.sendPrefixedMessage("Guilds", "Guild names can only contain alphanumerical characters and underscores (a-z, A-Z, 0-9, _).");
                return true;
            }
            Guild guild = new Guild(args[1]);
            if (guild.exists()) {
                player.sendPrefixedMessage("Guilds", "A guild with this name already exists.");
                return true;
            }
            player.sendPrefixedMessage("Guilds", "Your guild has been renamed to " + args[1]);
            Ublisk.runAsync(() -> {
                guild.rename(args[1]);
            });
            return true;
        } else if (args[0].equalsIgnoreCase("owner")) {
            if (player.getGuild() == null) {
                player.sendPrefixedMessage("Guilds", "You are not in a guild.");
                return true;
            }
            UPlayer target;
            try {
                target = new UPlayer(args[1]);
            } catch (PlayerNotFoundException e) {
                player.sendPrefixedMessage("Guilds", "This player could not be found.");
                return true;
            }
            Guild guild = player.getGuild();
            if (!player.getGuild().getName().equals(target.getGuild().getName())) {
                player.sendPrefixedMessage("Guilds", "You cannot transfer ownership to a player who is not in your guild.");
                return true;
            }
            player.sendPrefixedMessage("Guilds", target.getName() + " is now the owner of " + guild.getName());
            Ublisk.runAsync(() -> {
                guild.setOwner(target);
            });
            return true;
        } else {
            return false;
        }
    } else if (args[0].equalsIgnoreCase("description") || args[0].equalsIgnoreCase("desc")) {
        String description = String.join(" ", ListUtils.removeFirstStringFromArray(args));
        if (description.length() > 100) {
            player.sendPrefixedMessage("Guilds", "This description is too long.");
            return true;
        }
        if (description.length() < 6) {
            player.sendPrefixedMessage("Guilds", "This description is too short.");
            return true;
        }
        Guild guild = player.getGuild();
        if (guild == null) {
            player.sendPrefixedMessage("Guilds", "You are not in a guild.");
            return true;
        }
        player.sendPrefixedMessage("Guilds", "You have changed your guild's description to " + StringUtils.addDotIfNecessary(description));
        Ublisk.runAsync(() -> {
            guild.setDescription(description);
        });
        return true;
    } else {
        return false;
    }
}
Also used : UPlayer(xyz.derkades.ublisk.utils.UPlayer) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) UPlayer(xyz.derkades.ublisk.utils.UPlayer) GuildInvite(xyz.derkades.ublisk.utils.Guild.GuildInvite) PlayerNotFoundException(xyz.derkades.ublisk.utils.exception.PlayerNotFoundException) Guild(xyz.derkades.ublisk.utils.Guild) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder)

Example 38 with CommandSender

use of net.md_5.bungee.api.CommandSender in project Ublisk by Derkades.

the class StatsCommand method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    UPlayer player = new UPlayer(sender);
    if (args.length == 1) {
        UPlayer target = null;
        try {
            target = new UPlayer(args[1]);
        } catch (PlayerNotFoundException e) {
            player.sendMessage(Message.PLAYER_NOT_FOUND);
            return true;
        }
        BaseComponent[] hoverText = new ComponentBuilder("yes click me pls").create();
        BaseComponent[] text = new ComponentBuilder("Click here to open " + target.getName() + "'s statistics page in your browser.").color(ChatColor.AQUA).bold(true).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)).event(new ClickEvent(ClickEvent.Action.OPEN_URL, target.getStatsURL())).create();
        player.sendMessage(text);
        return true;
    } else if (args.length == 0) {
        BaseComponent[] hoverText = new ComponentBuilder("yes click me pls").create();
        BaseComponent[] text = new ComponentBuilder("Click here to open your statistics page in your browser.").color(ChatColor.AQUA).bold(true).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)).event(new ClickEvent(ClickEvent.Action.OPEN_URL, player.getStatsURL())).create();
        player.sendMessage(text);
        return true;
    } else {
        return false;
    }
}
Also used : HoverEvent(net.md_5.bungee.api.chat.HoverEvent) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) UPlayer(xyz.derkades.ublisk.utils.UPlayer) ClickEvent(net.md_5.bungee.api.chat.ClickEvent) PlayerNotFoundException(xyz.derkades.ublisk.utils.exception.PlayerNotFoundException) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder)

Example 39 with CommandSender

use of net.md_5.bungee.api.CommandSender in project Statz by Staartvin.

the class CommandsManager method onCommand.

/* (non-Javadoc)
	 * @see org.bukkit.command.CommandExecutor#onCommand(org.bukkit.command.CommandSender, org.bukkit.command.Command, java.lang.String, java.lang.String[])
	 */
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
    if (args.length == 0) {
        // If admin has predefined a list of stats, show that instead of the list view of all stats.
        if (plugin.getConfigHandler().useCustomList() && sender instanceof Player) {
            Player player = (Player) sender;
            // Show custom stats that admin has provided in the config.
            plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {

                private String playerName;

                private UUID uuid;

                private int pageNumber;

                private Runnable init(String playerName, UUID uuid, int pageNumber) {
                    this.playerName = playerName;
                    this.uuid = uuid;
                    this.pageNumber = pageNumber - 1;
                    return this;
                }

                public void run() {
                    plugin.getDataManager().sendStatisticsList(sender, playerName, uuid, pageNumber, plugin.getConfigHandler().getCustomList());
                }
            }.init(sender.getName(), player.getUniqueId(), 0));
            return true;
        } else {
            // Just show information about the plugin.
            sender.sendMessage(ChatColor.BLUE + "-----------------------------------------------------");
            sender.sendMessage(ChatColor.GOLD + "Developed by: " + ChatColor.GRAY + plugin.getDescription().getAuthors());
            sender.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.GRAY + plugin.getDescription().getVersion());
            sender.sendMessage(Lang.STATZ_HELP_COMMAND.getConfigValue());
        }
        return true;
    }
    final String action = args[0];
    List<String> suggestions = new ArrayList<>();
    List<String> bestSuggestions = new ArrayList<>();
    // Go through every list and check if that action is in there.
    for (final Entry<List<String>, StatzCommand> entry : registeredCommands.entrySet()) {
        String suggestion = StatzUtil.findClosestSuggestion(action, entry.getKey());
        if (suggestion != null) {
            suggestions.add(suggestion);
        }
        for (final String actionString : entry.getKey()) {
            if (actionString.equalsIgnoreCase(action)) {
                // Check if player has proper permission
                if (!this.hasPermission(entry.getValue().getPermission(), sender)) {
                    return true;
                }
                return entry.getValue().onCommand(sender, cmd, label, args);
            }
        }
    }
    // Search for suggestions if argument was not found
    for (String suggestion : suggestions) {
        String[] split = suggestion.split(";");
        int editDistance = Integer.parseInt(split[1]);
        // Only give suggestion if edit distance is small
        if (editDistance <= 2) {
            bestSuggestions.add(split[0]);
        }
    }
    sender.sendMessage(Lang.COMMAND_NOT_RECOGNIZED.getConfigValue());
    if (!bestSuggestions.isEmpty()) {
        BaseComponent[] builder = new ComponentBuilder("Did you perhaps mean ").color(net.md_5.bungee.api.ChatColor.DARK_AQUA).append("/statz ").color(net.md_5.bungee.api.ChatColor.GREEN).append(StatzUtil.seperateList(bestSuggestions, "or")).event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("These are suggestions based on your input.").create())).append("?").color(net.md_5.bungee.api.ChatColor.DARK_AQUA).create();
        if (sender instanceof Player) {
            Player p = (Player) sender;
            p.spigot().sendMessage(builder);
        } else {
            sender.sendMessage(ChatColor.DARK_AQUA + "Did you perhaps mean " + ChatColor.GREEN + "/statz " + StatzUtil.seperateList(bestSuggestions, "or") + ChatColor.DARK_AQUA + "?");
        }
    }
    sender.sendMessage(Lang.STATZ_HELP_COMMAND.getConfigValue());
    return true;
}
Also used : HoverEvent(net.md_5.bungee.api.chat.HoverEvent) Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder)

Aggregations

TextComponent (net.md_5.bungee.api.chat.TextComponent)22 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)15 ComponentBuilder (net.md_5.bungee.api.chat.ComponentBuilder)11 CommandSender (net.md_5.bungee.api.CommandSender)9 HoverEvent (net.md_5.bungee.api.chat.HoverEvent)8 Player (org.bukkit.entity.Player)8 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)6 ClickEvent (net.md_5.bungee.api.chat.ClickEvent)6 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)5 ArrayList (java.util.ArrayList)4 ServerInfo (de.dytanic.cloudnet.lib.server.info.ServerInfo)3 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)3 ChatSender (at.pcgamingfreaks.Bungee.Message.Sender.ChatSender)2 SendMethod (at.pcgamingfreaks.Bungee.Message.Sender.SendMethod)2 CommandHandler (com.Ben12345rocks.AdvancedCore.Objects.CommandHandler)2 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)2 ISoliniaSpell (com.solinia.solinia.Interfaces.ISoliniaSpell)2 SoliniaActiveSpell (com.solinia.solinia.Models.SoliniaActiveSpell)2 SoliniaEntitySpells (com.solinia.solinia.Models.SoliniaEntitySpells)2 CloudPlayer (de.dytanic.cloudnet.lib.player.CloudPlayer)2