Skip to content

Commit

Permalink
fix NumberLimitations somehow being nested under Item, update README …
Browse files Browse the repository at this point in the history
…& version
  • Loading branch information
efekos committed May 20, 2024
1 parent 0cc8205 commit 5a02f02
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 48 deletions.
52 changes: 34 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ implementation 'dev.efekos:Arn:0.1.0'
## Usage

Arn uses Java Reflection API to scan through your plugin, detect needed classes and use them. Because of this, you have
to add a [`Container`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/Container.html) annotation to
**every** class that must be scanned by [`Arn`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/Arn.html).
to add a [`Container`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/Container.html) annotation to
**every** class that must be scanned by [`Arn`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/Arn.html).

### Creating commands

Expand Down Expand Up @@ -89,14 +89,14 @@ public int helloWorld(CommandSender sender, @CommandArgument String name /*strin
````

All we have to do is add a parameter with
[`CommandArgument`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/CommandArgument.html) annotation. This
[`CommandArgument`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/CommandArgument.html) annotation. This
method is now the equivalent of `/helloworld <name>`, `<name>` being a `String` that can have whitespaces using quoted
strings. You can use following combinations of annotations and types by default (all of them requires
[`CommandArgument`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/CommandArgument.html).):
[`CommandArgument`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/CommandArgument.html).):

* [`Advancement`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/advancement/Advancement.html)
* [`Attribute`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html)
* [`@Block`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/modifier/Block.html) [`Material`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html)
* [`@Block`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/Block.html) [`Material`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html)
* [`BlockData`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/block/data/BlockData.html)
* `Boolean`
* `boolean`
Expand All @@ -111,9 +111,9 @@ strings. You can use following combinations of annotations and types by default
* [`GameMode`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/GameMode.html)
* `Integer`
* `int`
* [`@InventorySlot`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/modifier/NumberLimitations.html) `Integer`
* [`@InventorySlot`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/modifier/NumberLimitations.html) `int`
* [`@Item`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/modifier/Item.html) [`Material`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html)
* [`@InventorySlot`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/NumberLimitations.html) `Integer`
* [`@InventorySlot`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/NumberLimitations.html) `int`
* [`@Item`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/Item.html) [`Material`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html)
* [`ItemStack`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/ItemStack.html)
* [`Location`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Location.html)
* `Long`
Expand All @@ -123,9 +123,9 @@ strings. You can use following combinations of annotations and types by default
* [`Player`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html)
* `String`
* [`BaseComponent`](https://javadoc.io/doc/net.md-5/bungeecord-chat/latest/net/md_5/bungee/api/chat/BaseComponent.html)
* [`@Vector`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/modifier/Vector.html) [`Location`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Location.html)
* [`@Vector`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/Vector.html) [`Location`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Location.html)

These don't require a [`CommandArgument`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/CommandArgument.html)
These don't require a [`CommandArgument`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/CommandArgument.html)
annotation.

* [`BlockCommandSender`](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/command/BlockCommandSender.html)
Expand Down Expand Up @@ -162,7 +162,7 @@ with a bit of research.
### Advanced literals

I only showed you how to make base commands. There is more than base commands in Arn. If you want to use two or more
literals, you can simply separate them with `.` in your [`CommandArgument`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/CommandArgument.html)
literals, you can simply separate them with `.` in your [`CommandArgument`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/CommandArgument.html)
annotation. But in this way, arguments will be placed after all the literals. If you want to place literals between
arguments, you can follow this syntax: `[ab]:[0-9]+:[a-z]+`. First group of a letter determines will this literal be placed before
or after the argument with the given index. Second group of a number is the index of an argument. Finally, last group of
Expand All @@ -177,7 +177,7 @@ the literal would be placed after the 1st argument.
### Custom arguments

If you want to make a custom argument, you can, using Enum classes! All you have to do is annotate an enum class with both
[`Container`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/Container.html) and [`CustomArgument`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/CustomArgument.html),
[`Container`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/Container.html) and [`CustomArgument`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/CustomArgument.html),
and you'll be able to use that enum class as a command argument.

````java
Expand Down Expand Up @@ -216,7 +216,7 @@ public class Commands {
### Blocking access

You can block access by using a permission or annotations. All you have to do is add the permission node to the
[`Command`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/Command.html) like this:
[`Command`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/Command.html) like this:

````java
import dev.efekos.arn.annotation.Command;
Expand All @@ -231,20 +231,36 @@ import dev.efekos.arn.annotation.block.BlockPlayer;
public int method(/*...*/);
````

### Limits

You can apply [`NumberLimitations`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/modifier/NumberLimitations.html)
to any command argument with a number type.

````java
import dev.efekos.arn.annotation.Command;
import dev.efekos.arn.annotation.CommandArgument;
import dev.efekos.arn.annotation.block.BlockCommandBlock;
import dev.efekos.arn.annotation.modifier.NumberLimitations;

@Command("settimeout")
@BlockCommandBlock
public int setTimeout(@CommandArgument @NumberLimitations(min = 0,max = 30) Integer argument);
````

### Configurations

If you want to go even more crazy, you can implement your own command parameter types. You have to make an implementation
of [`CommandHandlerMethodArgumentResolver`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/resolver/CommandHandlerMethodArgumentResolver.html)
of [`CommandHandlerMethodArgumentResolver`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/resolver/CommandHandlerMethodArgumentResolver.html)
first. If you want to add an argument to the command structure to resolver a parameter, you might also need to make an
implementation of a [`CommandArgumentResolver`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/resolver/CommandArgumentResolver.html).
After that, simply make a [`Container`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/annotation/Container.html) that
implements [`ArnConfigurer`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/config/ArnConfigurer.html), and add your
implementation of a [`CommandArgumentResolver`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/resolver/CommandArgumentResolver.html).
After that, simply make a [`Container`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/annotation/Container.html) that
implements [`ArnConfigurer`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/config/ArnConfigurer.html), and add your
resolvers using that configurer.

### Running

After all of your command and configurations are done, call
[`Arn`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/Arn.html)[`#run`](https://efekos.dev/javadoc/arn/0.1/dev/efekos/arn/Arn.html#run(java.lang.Class))
[`Arn`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/Arn.html)[`#run`](https://efekos.dev/javadoc/arn/0.1.1/dev/efekos/arn/Arn.html#run(java.lang.Class))
from your plugin.

````java
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.efekos</groupId>
<artifactId>Arn</artifactId>
<version>0.1.0</version>
<version>0.1.1 </version>
<packaging>jar</packaging>

<name>Arn</name>
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/dev/efekos/arn/annotation/modifier/Item.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.efekos.arn.annotation.modifier;


import dev.efekos.arn.annotation.CommandArgument;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -19,27 +17,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Item {
/**
* Applies limitations to number arguments annotated with {@link CommandArgument}. When this annotation is present on any
* number argument ({@code long},{@code int},{@code float} and {@code double}), given minimum and maximum limits will be
* applied to the command argument node which will be on final command structure.
* @since 0.1
* @author efekos
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@interface NumberLimitations {

/**
* Minimum value of the number argument.
* @return Minimum value.
*/
long min() default Integer.MIN_VALUE;

/**
* Maximum value of the number argument.
* @return Maximum value.
*/
long max() default Integer.MAX_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.brigadier.builder.ArgumentBuilder;
import dev.efekos.arn.annotation.CommandArgument;
import dev.efekos.arn.annotation.modifier.Item;
import dev.efekos.arn.annotation.modifier.NumberLimitations;
import dev.efekos.arn.resolver.CommandArgumentResolver;
import net.minecraft.commands.CommandDispatcher;

Expand All @@ -30,7 +31,7 @@ public boolean isApplicable(Parameter parameter) {
@Override
public ArgumentBuilder<?,?> apply(Parameter parameter) {
String s = parameter.getAnnotation(CommandArgument.class).value();
Item.NumberLimitations limitations = parameter.getAnnotation(Item.NumberLimitations.class);
NumberLimitations limitations = parameter.getAnnotation(NumberLimitations.class);
boolean b = limitations != null;
return CommandDispatcher.a(s.isEmpty() ?parameter.getName():s, b? DoubleArgumentType.doubleArg(limitations.min(),limitations.max()):DoubleArgumentType.doubleArg());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.brigadier.builder.ArgumentBuilder;
import dev.efekos.arn.annotation.CommandArgument;
import dev.efekos.arn.annotation.modifier.Item;
import dev.efekos.arn.annotation.modifier.NumberLimitations;
import dev.efekos.arn.resolver.CommandArgumentResolver;
import net.minecraft.commands.CommandDispatcher;

Expand All @@ -30,7 +31,7 @@ public boolean isApplicable(Parameter parameter) {
@Override
public ArgumentBuilder<?,?> apply(Parameter parameter) {
String s = parameter.getAnnotation(CommandArgument.class).value();
Item.NumberLimitations limitations = parameter.getAnnotation(Item.NumberLimitations.class);
NumberLimitations limitations = parameter.getAnnotation(NumberLimitations.class);
boolean b = limitations != null;
return CommandDispatcher.a(s.isEmpty() ?parameter.getName():s, b?FloatArgumentType.floatArg(limitations.min(),limitations.max()):FloatArgumentType.floatArg());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.efekos.arn.annotation.CommandArgument;
import dev.efekos.arn.annotation.modifier.InventorySlot;
import dev.efekos.arn.annotation.modifier.Item;
import dev.efekos.arn.annotation.modifier.NumberLimitations;
import dev.efekos.arn.resolver.CommandArgumentResolver;
import net.minecraft.commands.CommandDispatcher;

Expand All @@ -31,7 +32,7 @@ public boolean isApplicable(Parameter parameter) {
@Override
public ArgumentBuilder<?,?> apply(Parameter parameter) {
String s = parameter.getAnnotation(CommandArgument.class).value();
Item.NumberLimitations limitations = parameter.getAnnotation(Item.NumberLimitations.class);
NumberLimitations limitations = parameter.getAnnotation(NumberLimitations.class);
boolean b = limitations != null;
return CommandDispatcher.a(s.isEmpty() ?parameter.getName():s, b? IntegerArgumentType.integer((int)limitations.min(),(int)limitations.max()):IntegerArgumentType.integer());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import dev.efekos.arn.annotation.CommandArgument;
import dev.efekos.arn.annotation.modifier.Item;
import dev.efekos.arn.annotation.modifier.NumberLimitations;
import dev.efekos.arn.resolver.CommandArgumentResolver;
import net.minecraft.commands.CommandDispatcher;

Expand All @@ -30,7 +30,7 @@ public boolean isApplicable(Parameter parameter) {
@Override
public ArgumentBuilder<?,?> apply(Parameter parameter) {
String s = parameter.getAnnotation(CommandArgument.class).value();
Item.NumberLimitations limitations = parameter.getAnnotation(Item.NumberLimitations.class);
NumberLimitations limitations = parameter.getAnnotation(NumberLimitations.class);
boolean b = limitations != null;
return CommandDispatcher.a(s.isEmpty() ?parameter.getName():s, b? LongArgumentType.longArg(limitations.min(),limitations.max()):LongArgumentType.longArg());
}
Expand Down

0 comments on commit 5a02f02

Please sign in to comment.