Skip to content

Commit

Permalink
core: Add CommandFlag-accepting getters to FlagContext
Browse files Browse the repository at this point in the history
This allows for type-safe access to the collection of available flags.
  • Loading branch information
zml2008 authored and Citymonstret committed Jan 12, 2021
1 parent dc640f6 commit 1b581a5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public boolean isPresent(final @NonNull String flag) {
return FLAG_PRESENCE_VALUE.equals(value);
}

/**
* Check whether a presence flag is present. This will return {@code false}
* for all value flags.
*
* @param flag A presence flag instance
* @return {@code true} if the flag is a presence flag and is present,
* else {@code false}
* @since 1.4.0
*/
public boolean isPresent(final @NonNull CommandFlag<Void> flag) {
return this.isPresent(flag.getName());
}

/**
* Get a flag value as an optional. Will be empty if the value is not present.
*
Expand All @@ -110,6 +123,20 @@ public boolean isPresent(final @NonNull String flag) {
return Optional.of(casted);
}

/**
* Get a flag value as an optional. Will be empty if the value is not present.
*
* @param flag Flag type
* @param <T> Value type
* @return Optional containing stored value if present
* @since 1.4.0
*/
public <T> @NonNull Optional<T> getValue(
final @NonNull CommandFlag<T> flag
) {
return this.getValue(flag.getName());
}

/**
* Get a flag value
*
Expand All @@ -125,6 +152,22 @@ public boolean isPresent(final @NonNull String flag) {
return this.<T>getValue(name).orElse(defaultValue);
}

/**
* Get a flag value
*
* @param name Flag value
* @param defaultValue Default value
* @param <T> Value type
* @return Stored value, or the supplied default value
* @since 1.4.0
*/
public <T> @Nullable T getValue(
final @NonNull CommandFlag<T> name,
final @Nullable T defaultValue
) {
return this.getValue(name).orElse(defaultValue);
}

/**
* Check whether a flag is present. This will return {@code true} if the flag
* is a presence flag and is present, or if the flag is a value flag and has
Expand All @@ -140,6 +183,21 @@ public boolean hasFlag(
return this.getValue(name).isPresent();
}

/**
* Check whether a flag is present. This will return {@code true} if the flag
* is a presence flag and is present, or if the flag is a value flag and has
* a value provided.
*
* @param flag The flag instance
* @return whether the flag is present
* @since 1.4.0
*/
public boolean hasFlag(
final @NonNull CommandFlag<?> flag
) {
return this.getValue(flag).isPresent();
}

/**
* Check whether a flag is present. This will return {@code true} if the flag
* is a presence flag and is present, or if the flag is a value flag and has
Expand All @@ -153,6 +211,19 @@ public boolean contains(final @NonNull String name) {
return this.hasFlag(name);
}

/**
* Check whether a flag is present. This will return {@code true} if the flag
* is a presence flag and is present, or if the flag is a value flag and has
* a value provided.
*
* @param flag Flag instance
* @return whether the flag is present
* @since 1.4.0
*/
public boolean contains(final @NonNull CommandFlag<?> flag) {
return this.hasFlag(flag);
}

/**
* Get a flag value
*
Expand All @@ -168,4 +239,18 @@ public boolean contains(final @NonNull String name) {
return this.<T>getValue(name).orElse(null);
}

/**
* Get a flag value
*
* @param flag Flag name
* @param <T> Value type
* @return Stored value if present, else {@code null}
* @since 1.4.0
*/
public <T> @Nullable T get(
final @NonNull CommandFlag<T> flag
) {
return this.getValue(flag).orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.compound.ArgumentPair;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.arguments.preprocessor.RegexPreprocessor;
import cloud.commandframework.arguments.standard.EnumArgument;
import cloud.commandframework.arguments.standard.FloatArgument;
Expand Down Expand Up @@ -116,21 +117,28 @@ static void newTree() {
}));

/* Build command for testing flags */
final CommandFlag<Void> test = manager.flagBuilder("test")
.withAliases("t")
.build();

final CommandFlag<Integer> num = manager.flagBuilder("num")
.withArgument(IntegerArgument.of("num"))
.build();

manager.command(manager.commandBuilder("flags")
.flag(manager.flagBuilder("test")
.withAliases("t")
.build())
.flag(manager.flagBuilder("test2")
.withAliases("f")
.build())
.flag(manager.flagBuilder("num")
.withArgument(IntegerArgument.of("num")).build())
.flag(num)
.flag(manager.flagBuilder("enum")
.withArgument(EnumArgument.of(FlagEnum.class, "enum")))
.handler(c -> {
System.out.println("Flag present? " + c.flags().isPresent("test"));
System.out.println("Flag present? " + c.flags().isPresent(test));
System.out.println("Second flag present? " + c.flags().isPresent("test2"));
System.out.println("Numerical flag: " + c.flags().getValue("num", -10));
System.out.println("Numerical flag: " + c.flags().getValue(num, -10));
System.out.println("Enum: " + c.flags().getValue("enum", FlagEnum.PROXI));
})
.build());
Expand Down

0 comments on commit 1b581a5

Please sign in to comment.