-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removed `CommandAPIHandler#previewableArguments` and related methods - Added `PreviewableCommandNode` for storing `Previewable` information directly in Brigadier's tree - Tweak `NMS_1_19_Common_ChatPreviewHandler` to build previews from the node tree rather than by the node path TODO: Should probably test these changes on a real server to verify. Also, another example of Mojang/brigadier#144 being annoying.
- Loading branch information
1 parent
5d6e054
commit b7adc7c
Showing
6 changed files
with
228 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...dapi-core/src/main/java/dev/jorel/commandapi/commandnodes/PreviewableArgumentBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package dev.jorel.commandapi.commandnodes; | ||
|
||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.builder.ArgumentBuilder; | ||
import com.mojang.brigadier.suggestion.SuggestionProvider; | ||
import com.mojang.brigadier.tree.CommandNode; | ||
|
||
import dev.jorel.commandapi.arguments.Previewable; | ||
import dev.jorel.commandapi.wrappers.PreviewableFunction; | ||
|
||
/** | ||
* A special type of {@link RequiredArgumentBuilder} for {@link Previewable} Arguments. Compared to the | ||
* {@link RequiredArgumentBuilder}, this class builds a {@link PreviewableCommandNode} | ||
* | ||
* @param <Source> The Brigadier Source object for running commands. | ||
* @param <T> The type returned when this argument is parsed. | ||
*/ | ||
// We can't actually extend RequiredArgumentBuilder since its only constructor is private :( | ||
// See https://github.com/Mojang/brigadier/pull/144 | ||
public class PreviewableArgumentBuilder<Source, T> extends ArgumentBuilder<Source, PreviewableArgumentBuilder<Source, T>> { | ||
// Everything here is copied from RequiredArgumentBuilder, which is why it would be nice to extend that directly | ||
private final String name; | ||
private final ArgumentType<T> type; | ||
private SuggestionProvider<Source> suggestionsProvider = null; | ||
|
||
// `Previewable` information | ||
private final PreviewableFunction<?> previewableFunction; | ||
private final boolean legacy; | ||
private final boolean isListed; | ||
|
||
private PreviewableArgumentBuilder(String name, ArgumentType<T> type, PreviewableFunction<?> previewableFunction, boolean legacy, boolean isListed) { | ||
this.name = name; | ||
this.type = type; | ||
|
||
this.previewableFunction = previewableFunction; | ||
this.legacy = legacy; | ||
this.isListed = isListed; | ||
} | ||
|
||
public static <Source, T> PreviewableArgumentBuilder<Source, T> previewableArgument(String name, ArgumentType<T> type, PreviewableFunction<?> previewableFunction, boolean legacy, boolean isListed) { | ||
return new PreviewableArgumentBuilder<>(name, type, previewableFunction, legacy, isListed); | ||
} | ||
|
||
public PreviewableArgumentBuilder<Source, T> suggests(final SuggestionProvider<Source> provider) { | ||
this.suggestionsProvider = provider; | ||
return getThis(); | ||
} | ||
|
||
public SuggestionProvider<Source> getSuggestionsProvider() { | ||
return suggestionsProvider; | ||
} | ||
|
||
@Override | ||
protected PreviewableArgumentBuilder<Source, T> getThis() { | ||
return this; | ||
} | ||
|
||
public ArgumentType<T> getType() { | ||
return type; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public PreviewableCommandNode<Source, T> build() { | ||
final PreviewableCommandNode<Source, T> result = new PreviewableCommandNode<Source, T>( | ||
previewableFunction, legacy, isListed, | ||
getName(), getType(), | ||
getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider() | ||
); | ||
|
||
for (final CommandNode<Source> argument : getArguments()) { | ||
result.addChild(argument); | ||
} | ||
|
||
return result; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
commandapi-core/src/main/java/dev/jorel/commandapi/commandnodes/PreviewableCommandNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package dev.jorel.commandapi.commandnodes; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
import com.mojang.brigadier.Command; | ||
import com.mojang.brigadier.RedirectModifier; | ||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.arguments.ArgumentType; | ||
import com.mojang.brigadier.context.CommandContextBuilder; | ||
import com.mojang.brigadier.context.ParsedArgument; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.suggestion.SuggestionProvider; | ||
import com.mojang.brigadier.tree.ArgumentCommandNode; | ||
import com.mojang.brigadier.tree.CommandNode; | ||
|
||
import dev.jorel.commandapi.arguments.Previewable; | ||
import dev.jorel.commandapi.wrappers.PreviewableFunction; | ||
|
||
/** | ||
* A special type of {@link ArgumentCommandNode} for {@link Previewable} arguments. Compared to the | ||
* {@link ArgumentCommandNode}, this class also has the methods {@link #getPreview()} and {@link #isLegacy()}, | ||
* which are used when players try to use the chat preview feature. | ||
* | ||
* @param <Source> The Brigadier Source object for running commands. | ||
* @param <T> The type returned when this argument is parsed. | ||
*/ | ||
public class PreviewableCommandNode<Source, T> extends ArgumentCommandNode<Source, T> { | ||
private final PreviewableFunction<?> preview; | ||
private final boolean legacy; | ||
|
||
// Instead of having a listed and unlisted copy of this class, we can just handle this with this boolean | ||
private final boolean isListed; | ||
|
||
public PreviewableCommandNode( | ||
PreviewableFunction<?> preview, boolean legacy, boolean isListed, | ||
String name, ArgumentType<T> type, | ||
Command<Source> command, Predicate<Source> requirement, CommandNode<Source> redirect, RedirectModifier<Source> modifier, boolean forks, SuggestionProvider<Source> customSuggestions | ||
) { | ||
super(name, type, command, requirement, redirect, modifier, forks, customSuggestions); | ||
this.preview = preview; | ||
this.legacy = legacy; | ||
this.isListed = isListed; | ||
} | ||
|
||
// Methods needed to generate a preview | ||
public Optional<PreviewableFunction<?>> getPreview() { | ||
return Optional.ofNullable(preview); | ||
} | ||
|
||
public boolean isLegacy() { | ||
return legacy; | ||
} | ||
|
||
// If we are unlisted, then when parsed, don't add the argument result to the CommandContext | ||
public boolean isListed() { | ||
return isListed; | ||
} | ||
|
||
@Override | ||
public void parse(StringReader reader, CommandContextBuilder<Source> contextBuilder) throws CommandSyntaxException { | ||
// Copied from `super#parse`, but with listability added | ||
int start = reader.getCursor(); | ||
|
||
T result = this.getType().parse(reader); | ||
ParsedArgument<Source, T> parsed = new ParsedArgument<>(start, reader.getCursor(), result); | ||
|
||
if(isListed) contextBuilder.withArgument(this.getName(), parsed); | ||
|
||
contextBuilder.withNode(this, parsed.getRange()); | ||
} | ||
|
||
// Typical ArgumentCommandNode methods, but make it our classes | ||
// Mostly copied and inspired by the implementations for these methods in ArgumentCommandNode | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (!(obj instanceof PreviewableCommandNode<?, ?> other)) return false; | ||
|
||
if (!Objects.equals(this.preview, other.preview)) return false; | ||
if (this.legacy != other.legacy) return false; | ||
if (this.isListed != other.isListed) return false; | ||
return super.equals(other); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hash(this.preview, this.legacy, this.isListed); | ||
result = 31*result + super.hashCode(); | ||
return result; | ||
} | ||
|
||
// TODO: Um, this currently doesn't work since PreviewableArgumentBuilder does not extend RequiredArgumentBuilder | ||
// See PreviewableArgumentBuilder for why | ||
// I hope no one tries to use this method! | ||
// @Override | ||
// public PreviewableArgumentBuilder<Source, T> createBuilder() { | ||
// PreviewableArgumentBuilder<Source, T> builder = PreviewableArgumentBuilder.previewableArgument(getName(), getType(), preview, legacy, isListed); | ||
|
||
// builder.requires(getRequirement()); | ||
// builder.forward(getRedirect(), getRedirectModifier(), isFork()); | ||
// if (getCommand() != null) { | ||
// builder.executes(getCommand()); | ||
// } | ||
// return builder; | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.