Skip to content

Commit

Permalink
Merge pull request #38 from Paul2708/development
Browse files Browse the repository at this point in the history
Added new arguments and fixed some bugs
  • Loading branch information
Paul2708 committed Oct 12, 2019
2 parents 06267f4 + e9f2e8d commit 710428b
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 29 deletions.
44 changes: 44 additions & 0 deletions CONTRIBUTING.md
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:
12 changes: 4 additions & 8 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.2</version>
<version>0.2.3</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.2</version>
<version>0.2.3</version>
</dependency>
```

Expand Down Expand Up @@ -141,14 +141,10 @@ public void test(Player sender, Player target, int yHeight) {
I think, you can spot the differences and the boilerplate by spigot.

## Contribution
[Here](https://github.com/Paul2708/simple-commands/issues) is a list of TODOs, ideas and planned features.
Maybe you can help and implement a new feature.

If you want to contribute, just clone the project, switch to a feature branch and open a [pull request](https://github.com/Paul2708/simple-commands/pulls).
The only restriction is a provided checkstyle file under `src/test/resources/checkstyle.xml`.
[Here](CONTRIBUTING.md) is a set of instructions that will guide you through your contribution.

## Contact
If you find any issues, please let me know!
Just open a [bug report](https://github.com/Paul2708/simple-commands/issues/new?template=bug-report.md) or an other[issue](https://github.com/Paul2708/simple-commands/issues/new/choose).
Just open a [bug report](https://github.com/Paul2708/simple-commands/issues/new?template=bug-report.md) or another [issue](https://github.com/Paul2708/simple-commands/issues/new/choose).

Paul2708 - [Twitter](https://twitter.com/theplayerpaul) Discord: Paul2708#1098 [Mail](mailto:playerpaul2708@gmx.de)
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.2</version>
<version>0.2.3</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.2</version>
<version>0.2.3</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
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.EntityArgument;
import de.paul2708.commands.arguments.impl.spigot.LocationArgument;
import de.paul2708.commands.arguments.impl.spigot.OnlinePlayerArgument;
import de.paul2708.commands.arguments.impl.spigot.WorldArgument;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;

Expand All @@ -37,7 +41,11 @@ public interface ArgumentHolder {
.add(new BooleanArgument())
.add(new EnumArgument<>(Material.class))
.add(new EnumArgument<>(EntityType.class))
.add(new EnumArgument<>(GameMode.class))
.add(new OnlinePlayerArgument())
.add(new WorldArgument())
.add(new EntityArgument())
.add(new LocationArgument())
.build();

/**
Expand Down
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);
}
}
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();
}
}
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);
}
}
4 changes: 2 additions & 2 deletions core/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.2</version>
<version>0.2.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>de.paul2708</groupId>
<artifactId>simple-commands-arguments</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
<!-- Spigot -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
StringBuilder usage = new StringBuilder("/" + simpleCommand.getInformation().name() + " ");

for (CommandArgument<?> argument : arguments) {
usage.append(languageSelector.translate(sender, argument.usage()));
usage.append(languageSelector.translate(sender, argument.usage())).append(" ");
}

languageSelector.sendMessage(sender, MessageResource.of("command.false_usage", usage.toString()));
Expand Down

0 comments on commit 710428b

Please sign in to comment.