-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some internal documentation #170
Changes from all commits
e1ff159
0baa085
218b5df
ce73163
ccd0a17
737bdce
4849239
10131cb
8faefb5
b003210
564f143
3b800a6
2f4236a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,21 @@ | |
import tools.redstone.redstonetools.features.arguments.serializers.TypeSerializer; | ||
import com.mojang.brigadier.context.CommandContext; | ||
|
||
/** | ||
* An argument to a command or other process | ||
* as a configuration option. | ||
* | ||
* @param <T> The value type. | ||
*/ | ||
public class Argument<T> { | ||
|
||
private String name; | ||
private final TypeSerializer<T, ?> type; | ||
private boolean optional = false; | ||
private T value; | ||
private T defaultValue; | ||
// TODO: maybe add an isSet flag and unset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for null values There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The values shouldnt be used outside of the execute method anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the serializer accepts null or if the default is null then null is a valid value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes i know but how do we know if the value is set or not when null is a valid value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you dont need to? if the value is null its either explicitly set to null or defaulted to null, whether its set by the user or if it has that value by default shouldnt matter |
||
// the value after executing the command | ||
|
||
private Argument(TypeSerializer<T, ?> type) { | ||
this.type = type; | ||
|
@@ -18,6 +27,13 @@ public static <T> Argument<T> ofType(TypeSerializer<T, ?> type) { | |
return new Argument<>(type); | ||
} | ||
|
||
/** | ||
* Set the default value on this argument. | ||
* This forces it to be optional. | ||
* | ||
* @param defaultValue The value. | ||
* @return This. | ||
*/ | ||
public Argument<T> withDefault(T defaultValue) { | ||
optional = true; | ||
this.defaultValue = defaultValue; | ||
|
@@ -31,14 +47,23 @@ public Argument<T> named(String name) { | |
return this; | ||
} | ||
|
||
public Argument<T> ensureNamed(String fieldName) { | ||
if (name == null) { | ||
name = fieldName; | ||
/** | ||
* Set the name of this argument to the given | ||
* value if unset. | ||
* | ||
* @param name The name to set. | ||
* @return This. | ||
*/ | ||
public Argument<T> ensureNamed(String name) { | ||
if (this.name == null) { | ||
this.name = name; | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/* Getters */ | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
@@ -51,8 +76,14 @@ public boolean isOptional() { | |
return optional; | ||
} | ||
|
||
/** | ||
* Update the value of this argument using | ||
* the given command context. | ||
* | ||
* @param context The command context. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public void setValue(CommandContext<?> context) { | ||
public void updateValue(CommandContext<?> context) { | ||
try { | ||
value = (T) context.getArgument(name, Object.class); | ||
} catch (IllegalArgumentException e) { | ||
|
@@ -64,6 +95,11 @@ public void setValue(CommandContext<?> context) { | |
} | ||
} | ||
|
||
/** | ||
* Get the current value set. | ||
* | ||
* @return The value or null if unset. | ||
*/ | ||
public T getValue() { | ||
return value; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda wanna refactor most of the code for features so I think its easiest if we leave the changes to CONTRIBUTING.md out and add them back after we're sure what the code is gonna look like. The other changes all seem fine to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok