Java Code Examples for org.bukkit.Bukkit#getPluginCommand()

The following examples show how to use org.bukkit.Bukkit#getPluginCommand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCommandHandler.java    From TabooLib with MIT License 5 votes vote down vote up
/**
 * 向服务端注册 BaseMainCommand 类
 *
 * @param command         命令全称(需在 plugin.yml 内注册)
 * @param baseMainCommand 命令对象
 * @return {@link BaseMainCommand}
 */
public static BaseMainCommand registerCommand(BaseCommand tCommand, String command, BaseMainCommand baseMainCommand, Plugin plugin) {
    if (Bukkit.getPluginCommand(command) == null) {
        String permission = tCommand.permission();
        if (tCommand.permissionDefault() == PermissionDefault.TRUE || tCommand.permissionDefault() == PermissionDefault.NOT_OP) {
            if (permission == null || permission.isEmpty()) {
                permission = plugin.getName().toLowerCase() + ".command.use";
            }
            if (Bukkit.getPluginManager().getPermission(permission) != null) {
                try {
                    Permission p = new Permission(permission, tCommand.permissionDefault());
                    Bukkit.getPluginManager().addPermission(p);
                    Bukkit.getPluginManager().recalculatePermissionDefaults(p);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
        registerPluginCommand(
                plugin,
                command,
                ArrayUtil.skipEmpty(tCommand.description(), "Registered by TabooLib."),
                ArrayUtil.skipEmpty(tCommand.usage(), "/" + command),
                ArrayUtil.skipEmpty(ArrayUtil.asList(tCommand.aliases()), new ArrayList<>()),
                ArrayUtil.skipEmpty(tCommand.permission()),
                ArrayUtil.skipEmpty(tCommand.permissionMessage()),
                baseMainCommand,
                baseMainCommand);
    }
    return BaseMainCommand.createCommandExecutor(command, baseMainCommand);
}
 
Example 2
Source File: UCListener.java    From UltimateChat with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onCommand(PlayerCommandPreprocessEvent e) {
    Player p = e.getPlayer();
    Command cmd = Bukkit.getPluginCommand(e.getMessage().split(" ")[0].substring(1));
    if (UChat.get().getUCJDA() != null && cmd != null && !UChat.get().getUCConfig().getString("discord.log-ignored-commands").contains(cmd.getName())) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Bukkit.getScheduler().runTaskAsynchronously(UChat.get(), () ->
                UChat.get().getUCJDA().sendCommandsToDiscord(UChat.get().getLang().get("discord.command")
                .replace("{player}", p.getName())
                .replace("{cmd}", e.getMessage())
                .replace("{time-now}", sdf.format(cal.getTime()))));
    }
}
 
Example 3
Source File: ChatEventListener.java    From ChatItem with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.LOWEST)
public void onCommand(final PlayerCommandPreprocessEvent e){
    if(e.getMessage().indexOf(SEPARATOR)!=-1){  //If the BELL character is found, we have to remove it
        String msg = e.getMessage().replace(Character.toString(SEPARATOR), "");
        e.setMessage(msg);
    }
    String commandString = e.getMessage().split(" ")[0].replaceAll("^/+", ""); //First part of the command, without leading slashes and without arguments
    Command cmd = Bukkit.getPluginCommand(commandString);
    if(cmd==null){ //not a plugin command
        if(!c.ALLOWED_DEFAULT_COMMANDS.contains(commandString)){
            return;
        }
    }else{
        if(!c.ALLOWED_PLUGIN_COMMANDS.contains(cmd)){
            return;
        }
    }

    Player p = e.getPlayer();
    boolean found = false;

    for (String rep : c.PLACEHOLDERS) {
        if (e.getMessage().contains(rep)) {
            found = true;
            break;
        }
    }

    if (!found) {
        return;
    }

    if (!p.hasPermission("chatitem.use")) {
        if(!c.NO_PERMISSION_MESSAGE.isEmpty() && c.SHOW_NO_PERM_COMMAND){
            p.sendMessage(c.NO_PERMISSION_MESSAGE);
        }
        e.setCancelled(true);
        return;
    }

    if (e.getPlayer().getItemInHand().getType().equals(Material.AIR)) {
        if (c.DENY_IF_NO_ITEM) {
            e.setCancelled(true);
            if (!c.DENY_MESSAGE.isEmpty()) {
                e.getPlayer().sendMessage(c.DENY_MESSAGE);
            }
        }
        if(c.HAND_DISABLED) {
            return;
        }
    }

    if(c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")){
        if(COOLDOWNS.containsKey(p.getName())){
            long start = COOLDOWNS.get(p.getName());
            long current = System.currentTimeMillis()/1000;
            long elapsed = current - start;
            if(elapsed >= c.COOLDOWN){
                COOLDOWNS.remove(p.getName());
            }else{
                if(!c.LET_MESSAGE_THROUGH) {
                    e.setCancelled(true);
                }
                if(!c.COOLDOWN_MESSAGE.isEmpty()){
                    long left = (start + c.COOLDOWN) - current;
                    p.sendMessage(c.COOLDOWN_MESSAGE.replace(LEFT, calculateTime(left)));
                }
                return;
            }
        }
    }

    String s = e.getMessage();
    for(String placeholder : c.PLACEHOLDERS){
        s = s.replace(placeholder, c.PLACEHOLDERS.get(0));
    }
    int occurrences = countOccurrences(c.PLACEHOLDERS.get(0), s);

    if(occurrences>c.LIMIT){
        e.setCancelled(true);
        if(c.LIMIT_MESSAGE.isEmpty()){
           return;
        }
        e.getPlayer().sendMessage(c.LIMIT_MESSAGE);
        return;
    }

    StringBuilder sb = new StringBuilder(e.getMessage());
    sb.append(SEPARATOR).append(e.getPlayer().getName());
    e.setMessage(sb.toString());
    if(!p.hasPermission("chatitem.ignore-cooldown")) {
        COOLDOWNS.put(p.getName(), System.currentTimeMillis() / 1000);
    }
}