-
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 #38 from Paul2708/development
Added new arguments and fixed some bugs
- Loading branch information
Showing
16 changed files
with
329 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Contributing guidelines | ||
> First of all if you want to contribute, I am really thankful and I appreciate it a lot! | ||
The following topics will guide you through a _good_ contribution. | ||
|
||
## Find an issue you want to work on | ||
The [issues page](https://github.com/Paul2708/simple-commands/issues) gives an overview about all open ideas, bugs and other issues. | ||
If you found an interesting one, just comment the issue and I will assign you to the issue (to avoid duplicated contributions). | ||
|
||
If this is done, you can start to work on the issue (see **Getting started**). | ||
Otherwise you can **Create an issue**. | ||
|
||
### Create an issue | ||
If you have an idea, feature request or other things you want to mention, you can open a new issue. | ||
(Issue templates: [Report a bug](https://github.com/Paul2708/simple-commands/issues/new?assignees=&labels=bug&template=bug-report.md&title=) or [Request a feature](https://github.com/Paul2708/simple-commands/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=)) | ||
|
||
## 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**. | ||
|
||
### Checkstyle | ||
The only restriction is a provided checkstyle file under `src/test/resources/checkstyle.xml`. | ||
Please make sure that `mvn checkstyle:check` won't fail. | ||
Otherwise the pull request will be rejected. | ||
|
||
### New command arguments | ||
If you want to add a new argument, please do the following: | ||
1. Create a class in `de.paul2708.commands.arguments.impl` in `arguments` that implements `CommandArgument<?>`. | ||
2. Add relevant messages in `messages_XX.properties`. | ||
3. Register it in `ArgumentHolder`. | ||
4. Test it locally by creating a sample command, that can be removed afterwards. | ||
|
||
Note: Enums are already covered. You have to register it without creating a new class. | ||
|
||
## Open a pull request | ||
If you are finally done, you should open a pull request. | ||
You have to merge your `feature branch` into `development`. | ||
Please use the pull request template to provide all relevant information. | ||
|
||
> Thanks for reading (and contributing), Paul. :heart: |
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
72 changes: 72 additions & 0 deletions
72
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/EntityArgument.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,72 @@ | ||
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.Bukkit; | ||
import org.bukkit.entity.Entity; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This command arguments represents a {@link Entity} argument. | ||
* | ||
* @author Cerus | ||
*/ | ||
public final class EntityArgument implements CommandArgument<Entity> { | ||
|
||
/** | ||
* Validate the object by a given command argument. | ||
* The command argument is trimmed, doesn't contain any spaces and is not empty. | ||
* | ||
* @param argument command argument | ||
* @return a valid or invalid validation | ||
*/ | ||
@Override | ||
public Validation<Entity> validate(String argument) { | ||
Entity entity = Bukkit.getWorlds() | ||
.stream() | ||
.flatMap(world -> world.getEntities().stream()) | ||
.filter(anEntity -> anEntity.getUniqueId().toString().equalsIgnoreCase(argument) | ||
|| argument.equalsIgnoreCase(anEntity.getCustomName())) | ||
.findAny() | ||
.orElse(null); | ||
|
||
if (entity == null) { | ||
return Validation.invalid(MessageResource.of("argument.entity.invalid", argument)); | ||
} | ||
|
||
return Validation.valid(entity); | ||
} | ||
|
||
/** | ||
* Get the correct usage for this type. | ||
* | ||
* @return usage | ||
*/ | ||
@Override | ||
public MessageResource usage() { | ||
return MessageResource.of("argument.entity.usage"); | ||
} | ||
|
||
/** | ||
* Get a list of strings that are used for the auto completion. | ||
* The argument can be empty. | ||
* | ||
* @param argument command argument (might be empty) | ||
* @return unmodifiable list of string | ||
*/ | ||
@Override | ||
public List<String> autoComplete(String argument) { | ||
List<String> autoComplete = Bukkit.getWorlds().stream() | ||
.flatMap(world -> world.getEntities().stream()) | ||
.map(entity -> (entity.getCustomName() == null | ||
? entity.getUniqueId().toString() : entity.getCustomName())) | ||
.filter(s -> s.startsWith(argument.toLowerCase())) | ||
.collect(Collectors.toList()); | ||
|
||
return Collections.unmodifiableList(autoComplete); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/LocationArgument.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,95 @@ | ||
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.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* This command arguments represents a {@link Location} argument. | ||
* | ||
* @author Cerus | ||
*/ | ||
public final class LocationArgument implements CommandArgument<Location> { | ||
|
||
private final Predicate<String> locationValidator = s -> s.matches(".*,(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)"); | ||
|
||
/** | ||
* Validate the object by a given command argument. | ||
* The command argument is trimmed, doesn't contain any spaces and is not empty. | ||
* | ||
* @param argument command argument | ||
* @return a valid or invalid validation | ||
*/ | ||
@Override | ||
public Validation<Location> validate(String argument) { | ||
if (!locationValidator.test(argument)) { | ||
return Validation.invalid(MessageResource.of("argument.location.invalid", argument)); | ||
} | ||
|
||
String[] splitted = argument.split(","); | ||
String worldName = splitted[0]; | ||
String xAsString = splitted[1]; | ||
String yAsString = splitted[2]; | ||
String zAsString = splitted[3]; | ||
|
||
World world = Bukkit.getWorld(worldName); | ||
if (world == null) { | ||
return Validation.invalid(MessageResource.of("argument.location.invalid.world", worldName)); | ||
} | ||
|
||
int x; | ||
try { | ||
x = Integer.parseInt(xAsString); | ||
} catch (NumberFormatException ignored) { | ||
// Should only be thrown if given number exceeds Integer range | ||
return Validation.invalid(MessageResource.of("argument.location.invalid.x", xAsString)); | ||
} | ||
|
||
int y; | ||
try { | ||
y = Integer.parseInt(yAsString); | ||
} catch (NumberFormatException ignored) { | ||
// Should only be thrown if given number exceeds Integer range | ||
return Validation.invalid(MessageResource.of("argument.location.invalid.y", yAsString)); | ||
} | ||
|
||
int z; | ||
try { | ||
z = Integer.parseInt(zAsString); | ||
} catch (NumberFormatException ignored) { | ||
// Should only be thrown if given number exceeds Integer range | ||
return Validation.invalid(MessageResource.of("argument.location.invalid.z", zAsString)); | ||
} | ||
|
||
return Validation.valid(new Location(world, x, y, z)); | ||
} | ||
|
||
/** | ||
* Get the correct usage for this type. | ||
* | ||
* @return usage | ||
*/ | ||
@Override | ||
public MessageResource usage() { | ||
return MessageResource.of("argument.location.usage"); | ||
} | ||
|
||
/** | ||
* Get a list of strings that are used for the auto completion. | ||
* The argument can be empty. | ||
* | ||
* @param argument command argument (might be empty) | ||
* @return unmodifiable list of string | ||
*/ | ||
@Override | ||
public List<String> autoComplete(String argument) { | ||
return Collections.emptyList(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
arguments/src/main/java/de/paul2708/commands/arguments/impl/spigot/WorldArgument.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,64 @@ | ||
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.Bukkit; | ||
import org.bukkit.World; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This command arguments represents a {@link World} argument. | ||
* | ||
* @author Cerus | ||
*/ | ||
public final class WorldArgument implements CommandArgument<World> { | ||
|
||
/** | ||
* Validate the object by a given command argument. | ||
* The command argument is trimmed, doesn't contain any spaces and is not empty. | ||
* | ||
* @param argument command argument | ||
* @return a valid or invalid validation | ||
*/ | ||
@Override | ||
public Validation<World> validate(String argument) { | ||
World world = Bukkit.getWorld(argument); | ||
|
||
if (world == null) { | ||
return Validation.invalid(MessageResource.of("argument.world.invalid", argument)); | ||
} | ||
|
||
return Validation.valid(world); | ||
} | ||
|
||
/** | ||
* Get the correct usage for this type. | ||
* | ||
* @return usage | ||
*/ | ||
@Override | ||
public MessageResource usage() { | ||
return MessageResource.of("argument.world.usage"); | ||
} | ||
|
||
/** | ||
* Get a list of strings that are used for the auto completion. | ||
* The argument can be empty. | ||
* | ||
* @param argument command argument (might be empty) | ||
* @return unmodifiable list of string | ||
*/ | ||
@Override | ||
public List<String> autoComplete(String argument) { | ||
List<String> autoComplete = Bukkit.getWorlds().stream() | ||
.map(world -> world.getName().toLowerCase()) | ||
.filter(name -> name.startsWith(argument.toLowerCase())) | ||
.collect(Collectors.toList()); | ||
|
||
return Collections.unmodifiableList(autoComplete); | ||
} | ||
} |
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
Oops, something went wrong.