@Test(expected = PlayerOnlyException.class) public void whenConsoleSendsPlayer() { CommandOptionsValidator commandOptionsValidator = new CommandOptionsValidator(); Sender sender = mock(Sender.class); when(sender.getKind()).thenReturn(SenderKind.CONSOLE); when(sender.hasPermission(any())).thenReturn(true); commandOptionsValidator.validate(commandOptions(), new String[]{"a", "b", "c"}, sender); }
public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); }
CommandOptionsValidator { public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); } }
CommandOptionsValidator { public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); }  }
CommandOptionsValidator { public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); }  Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender); }
CommandOptionsValidator { public Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender) { int argumentsLength = arguments.length - 1; if (argumentsLength < options.getMinimumNumberOfArguments() || argumentsLength >= options.getMaximumNumberOfArguments()) { return Optional.of(new Message("command.arguments.insufficient")); } Optional<String> permission = options.getPermission(); if (permission.isPresent() && !sender.hasPermission(permission.get())) { return Optional.of(new Message("permissions.no.permission")); } if (sender.getKind().equals(SenderKind.CONSOLE) && options.forPlayersOnly()) { throw new PlayerOnlyException(); } return Optional.empty(); }  Optional<Message> validate(CommandOptions options, String[] arguments, Sender sender);  }