Skip to content

Commit

Permalink
feat(kotlin): more extension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 18, 2024
1 parent 841b824 commit 6a319ed
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cloud-core/src/main/java/cloud/commandframework/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ private Builder(
);
}

/**
* Returns the current command description of this command builder.
*
* @return the current description
*/
public @NonNull CommandDescription commandDescription() {
return this.commandDescription;
}

/**
* Returns the result of invoking {@link #commandDescription(CommandDescription)} with the result of
* {@link CommandDescription#commandDescription(Description)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Collections;
import java.util.Objects;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.common.returnsreceiver.qual.This;
Expand Down Expand Up @@ -375,6 +376,15 @@ public static class Builder<C, T> {
return this.name(cloudKey.name()).valueType(cloudKey.type());
}

/**
* Returns the current name, if it has been set.
*
* @return current name
*/
public @MonotonicNonNull String name() {
return this.name;
}

/**
* Sets the {@code name} of the component.
*
Expand Down Expand Up @@ -421,6 +431,18 @@ public static class Builder<C, T> {
return this.valueType(TypeToken.get(valueType));
}

/**
* Returns the current parser, if it has been set.
*
* @return current parser
*/
public @MonotonicNonNull ParserDescriptor<C, T> parser() {
if (this.valueType == null || this.parser == null) {
return null;
}
return ParserDescriptor.of(this.parser, this.valueType);
}

/**
* Sets the {@code parser} and {@code valueType}.
*
Expand All @@ -431,6 +453,18 @@ public static class Builder<C, T> {
return this.parser(parserDescriptor.parser()).valueType(parserDescriptor.valueType());
}

/**
* Returns the current default value, if it has been set.
*
* @return current default value
*/
public @Nullable DefaultValue<C, T> defaultValue() {
if (this.defaultValue == null) {
return null;
}
return (DefaultValue<C, T>) this.defaultValue;
}

/**
* Sets the {@code defaultValue}.
*
Expand Down Expand Up @@ -485,6 +519,15 @@ public static class Builder<C, T> {
return this.optional().defaultValue(defaultValue);
}

/**
* Returns the current description.
*
* @return current description
*/
public @MonotonicNonNull Description description() {
return this.description;
}

/**
* Sets the {@code description}.
*
Expand All @@ -498,6 +541,15 @@ public static class Builder<C, T> {
return this;
}

/**
* Returns the current suggestion provider, if it has been set.
*
* @return current suggestion provider
*/
public @MonotonicNonNull SuggestionProvider<C> suggestionProvider() {
return this.suggestionProvider;
}

/**
* Sets the {@code suggestionProvider}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ public class MutableCommandBuilder<C : Any>(
public infix fun <T : Any> CloudKey<T>.to(value: T): MutableCommandBuilder<C> =
meta(this, value)

/**
* Field to get and set the command description for this command builder
*/
public var commandDescription: CommandDescription
get() = this.commandBuilder.commandDescription()
set(commandDescription) {
onlyMutate {
it.commandDescription(commandDescription)
}
}

/**
* Sets the command description meta for this command
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.kotlin.extension

import cloud.commandframework.CommandComponent
import cloud.commandframework.arguments.aggregate.AggregateCommandParserBuilder

/**
* Creates a new command component using the given [builderDecorator].
*
* @param builderDecorator decorator that prepares the command component
* @return the updated builder
*/
public fun <C, T> AggregateCommandParserBuilder<C>.withComponent(
builderDecorator: CommandComponent.Builder<C, T>.() -> Unit
): AggregateCommandParserBuilder<C> = withComponent(CommandComponent.builder<C, T>().apply(builderDecorator).build())
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.kotlin.extension

import cloud.commandframework.CommandComponent
import cloud.commandframework.Description
import cloud.commandframework.arguments.DefaultValue
import cloud.commandframework.arguments.parser.ParserDescriptor
import cloud.commandframework.arguments.suggestion.SuggestionProvider

/**
* The description of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.description: Description
get() = this.description()
set(description) {
this.description(description)
}

/**
* The description of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.textDescription: String
get() = this.description().textDescription()
set(description) {
this.description(Description.of(description))
}

/**
* The suggestion provider of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.suggestionProvider: SuggestionProvider<C>
get() = this.suggestionProvider()
set(suggestionProvider) {
this.suggestionProvider(suggestionProvider)
}

/**
* The name of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.name: String
get() = this.name()
set(name) {
this.name(name)
}

/**
* The default value of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.defaultValue: DefaultValue<C, T>?
get() = this.defaultValue()
set(defaultValue) {
this.defaultValue(defaultValue)
}

/**
* The parser of the command component.
*/
public var <C, T> CommandComponent.Builder<C, T>.parser: ParserDescriptor<C, T>
get() = this.parser()
set(parser) {
this.parser(parser)
}

0 comments on commit 6a319ed

Please sign in to comment.