Skip to content

Commit

Permalink
Merge pull request #48 from Paul2708/development
Browse files Browse the repository at this point in the history
Updated version to 0.2.4
  • Loading branch information
Paul2708 committed Oct 31, 2019
2 parents e5b97fe + 37e845b commit d1c8829
Show file tree
Hide file tree
Showing 24 changed files with 458 additions and 35 deletions.
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ If you have an idea, feature request or other things you want to mention, you ca
## Getting started
1. Be sure you have an assigned issue you can refer to.
2. Fork the project.
3. Create a new branch based on the issue, e.g. `fix/null-argument`, `feature/uuid-argument`, `doc/extendend-example`.
4. Commit your changes to the branch. Make more simple commits instead of one commit that resolves the issue at once.
5. Push the branch to your forked project.
6. **Open a pull request**.
3. Switch to `development` branch by `git checkout development`.
4. Create a new branch based on the issue, e.g. `fix/null-argument`, `feature/uuid-argument`, `doc/extendend-example`.
5. Commit your changes to the branch. Make more simple commits instead of one commit that resolves the issue at once.
6. Push the branch to your forked project.
7. **Open a pull request**.

### Checkstyle
The only restriction is a provided checkstyle file under `src/test/resources/checkstyle.xml`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Just add the following repository and dependency to your Maven project.
<dependency>
<groupId>com.github.Paul2708</groupId>
<artifactId>simple-commands</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
</dependency>
```

Expand All @@ -51,7 +51,7 @@ If you don't use a build tool like Maven or Gradle, you can download the latest
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-core</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions arguments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>simple-commands</artifactId>
<groupId>de.paul2708</groupId>
<version>0.2.3</version>
<version>0.2.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-language</artifactId>
<version>0.2.3</version>
<version>0.2.4</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import de.paul2708.commands.arguments.impl.primitive.FloatArgument;
import de.paul2708.commands.arguments.impl.primitive.IntegerArgument;
import de.paul2708.commands.arguments.impl.primitive.ShortArgument;
import de.paul2708.commands.arguments.impl.spigot.EnchantmentArgument;
import de.paul2708.commands.arguments.impl.spigot.EntityArgument;
import de.paul2708.commands.arguments.impl.spigot.GameRuleArgument;
import de.paul2708.commands.arguments.impl.spigot.LocationArgument;
import de.paul2708.commands.arguments.impl.spigot.OnlinePlayerArgument;
import de.paul2708.commands.arguments.impl.spigot.UuidArgument;
import de.paul2708.commands.arguments.impl.spigot.WorldArgument;
import org.bukkit.GameMode;
import org.bukkit.Material;
Expand Down Expand Up @@ -46,6 +49,9 @@ public interface ArgumentHolder {
.add(new WorldArgument())
.add(new EntityArgument())
.add(new LocationArgument())
.add(new GameRuleArgument())
.add(new UuidArgument())
.add(new EnchantmentArgument())
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,21 @@ static void condition(boolean condition, String description) {
throw new NotFulfilledConditionException(description);
}
}

/**
* @return this argument as an optional argument
* @see CommandArgument#isOptional()
*/
default CommandArgument<T> asOptional() {
return new OptionalArgument<>(this);
}

/**
* An optional argument may be skipped by the argument parser
*
* @return whether this argument is optional
*/
default boolean isOptional() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.paul2708.commands.arguments;

import de.paul2708.commands.language.MessageResource;

import java.util.List;

/**
* An argument which is marked as Optional to the argument parser. This can be received via
* {@link CommandArgument#asOptional()}
* @param <T> The type of this argument
*/
public class OptionalArgument<T> implements CommandArgument<T> {
private CommandArgument<T> internal;

/**
* For internal use only. Use {@link CommandArgument#asOptional()} instead.
* @param internal the internal argument which will be called for any parsing
*/
OptionalArgument(CommandArgument<T> internal) {
this.internal = internal;
}

@Override
public Validation<T> validate(String argument) {
return internal.validate(argument);
}

@Override
public MessageResource usage() {
// TODO: Append hint about optionality to usage?
return internal.usage();
}

@Override
public List<String> autoComplete(String argument) {
return internal.autoComplete(argument);
}

@Override
public CommandArgument<T> asOptional() {
return this;
}

@Override
public boolean isOptional() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EnumArgument(Class<T> enumClass) {
@Override
public Validation<T> validate(String argument) {
for (T enumValue : enumClass.getEnumConstants()) {
if (enumValue.toString().equals(argument.toUpperCase())) {
if (enumValue.toString().toUpperCase().equals(argument.toUpperCase())) {
return Validation.valid(enumValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package de.paul2708.commands.arguments.impl.spigot;

import de.paul2708.commands.arguments.CommandArgument;
import de.paul2708.commands.arguments.Validation;
import de.paul2708.commands.language.MessageResource;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

/**
* @author Stu (https://github.com/Stupremee)
* @since 19.10.19
*/
public final class EnchantmentArgument implements CommandArgument<Enchantment> {

@Override
public Validation<Enchantment> validate(String argument) {
// TODO: Add try-catch with invalidation as none-existing key throws an exception

String newArgument = argument.startsWith("minecraft:") ? argument.substring(10) : argument;

NamespacedKey key = NamespacedKey.minecraft(newArgument);
return Stream.of(Enchantment.values())
.filter(e -> e.getKey().equals(key))
.findFirst()
.map(Validation::valid)
.orElse(Validation.invalid(MessageResource.of("argument.enchantment.invalid", newArgument)));
}

@Override
public MessageResource usage() {
return MessageResource.of("argument.enchantment.usage");
}

@Override
public List<String> autoComplete(String argument) {
return Stream.of(Enchantment.values())
.filter(enchantment -> {
NamespacedKey key = enchantment.getKey();
return argument.startsWith(key.toString())
|| argument.startsWith(key.getNamespace())
|| argument.startsWith(key.getKey());
})
.map(enchantment -> enchantment.getKey().toString())
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.paul2708.commands.arguments.impl.spigot;

import de.paul2708.commands.arguments.CommandArgument;
import de.paul2708.commands.arguments.Validation;
import de.paul2708.commands.language.MessageResource;
import org.bukkit.GameRule;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

/**
* @author Stu (https://github.com/Stupremee)
* @since 19.10.19
*/
public final class GameRuleArgument implements CommandArgument<GameRule<?>> {
@Override
public Validation<GameRule<?>> validate(String argument) {
GameRule<?> gameRule = GameRule.getByName(argument);
if (gameRule == null) {
return Validation.invalid(MessageResource.of("argument.gamerule.invalid", argument));
} else {
return Validation.valid(gameRule);
}
}

@Override
public MessageResource usage() {
return MessageResource.of("argument.gamerule.usage");
}

@Override
public List<String> autoComplete(String argument) {
return Stream.of(GameRule.values())
.filter(rule -> rule.getName().startsWith(argument))
.map(GameRule::getName)
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.paul2708.commands.arguments.impl.spigot;

import de.paul2708.commands.arguments.CommandArgument;
import de.paul2708.commands.arguments.Validation;
import de.paul2708.commands.language.MessageResource;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* This command arguments represents a {@link UUID} argument.
*
* @author Stu (https://github.com/Stupremee)
* @since 19.10.19
*/
public final class UuidArgument implements CommandArgument<UUID> {

@Override
public Validation<UUID> validate(String argument) {
try {
return Validation.valid(UUID.fromString(argument));
} catch (IllegalArgumentException error) {
return Validation.invalid(MessageResource.of("argument.uuid.invalid"));
}
}

@Override
public MessageResource usage() {
return MessageResource.of("argument.uuid.usage");
}

@Override
public List<String> autoComplete(String argument) {
return Collections.emptyList();
}
}
42 changes: 42 additions & 0 deletions arguments/src/test/java/arguments/other/GameRuleArgumentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package arguments.other;

import arguments.AbstractArgumentTest;
import de.paul2708.commands.arguments.CommandArgument;
import de.paul2708.commands.arguments.impl.spigot.GameRuleArgument;
import de.paul2708.commands.arguments.impl.spigot.UuidArgument;
import de.paul2708.commands.arguments.util.Pair;
import org.bukkit.GameRule;

import java.util.UUID;
import java.util.function.Function;

/**
* @author Stu (https://github.com/Stupremee)
* @since 19.10.19
*/
public final class GameRuleArgumentTest extends AbstractArgumentTest {

@Override
public CommandArgument<?> create() {
return new GameRuleArgument();
}

@Override
public Pair[] validArguments() {
Function<GameRule<?>, Pair<String, GameRule<?>>> makePair = rule -> Pair.of(rule.getName(), rule);

GameRule<?> first = GameRule.ANNOUNCE_ADVANCEMENTS;
GameRule<?> second = GameRule.DO_FIRE_TICK;
GameRule<?> third = GameRule.SEND_COMMAND_FEEDBACK;
return new Pair[]{
makePair.apply(first),
makePair.apply(second),
makePair.apply(third),
};
}

@Override
public String[] invalidArguments() {
return new String[]{"totally not a valid gamerule", "do fire tick"};
}
}
40 changes: 40 additions & 0 deletions arguments/src/test/java/arguments/other/UuidArgumentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package arguments.other;

import arguments.AbstractArgumentTest;
import de.paul2708.commands.arguments.CommandArgument;
import de.paul2708.commands.arguments.impl.spigot.UuidArgument;
import de.paul2708.commands.arguments.util.Pair;

import java.util.UUID;
import java.util.function.Function;

/**
* @author Stu (https://github.com/Stupremee)
* @since 19.10.19
*/
public final class UuidArgumentTest extends AbstractArgumentTest {

@Override
public CommandArgument<?> create() {
return new UuidArgument();
}

@Override
public Pair[] validArguments() {
Function<UUID, Pair<String, UUID>> makePair = uuid -> Pair.of(uuid.toString(), uuid);

UUID first = UUID.randomUUID();
UUID second = UUID.randomUUID();
UUID third = UUID.randomUUID();
return new Pair[]{
makePair.apply(first),
makePair.apply(second),
makePair.apply(third),
};
}

@Override
public String[] invalidArguments() {
return new String[]{"totally not a valid uuid", "0011-1232-1234-1234"};
}
}

0 comments on commit d1c8829

Please sign in to comment.