-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from Paul2708/development
Updated version to 0.2.4
- Loading branch information
Showing
24 changed files
with
458 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
arguments/src/main/java/de/paul2708/commands/arguments/OptionalArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/EnchantmentArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/GameRuleArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/UuidArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
arguments/src/test/java/arguments/other/GameRuleArgumentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
arguments/src/test/java/arguments/other/UuidArgumentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}; | ||
} | ||
} |
Oops, something went wrong.