Skip to content

Commit

Permalink
Fix velocity argument parsing issue
Browse files Browse the repository at this point in the history
Due to faulty parsing, if no arguments were provided Blade still picked up a singular empty String argument, which caused parsing issues.
  • Loading branch information
vaperion committed Jan 2, 2024
1 parent 611ca87 commit 61f6c7c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 68 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ allprojects {
}

group = 'me.vaperion.blade'
version = '3.0.8'
version = '3.0.9'

// workaround for gradle issue: https://github.com/gradle/gradle/issues/17236#issuecomment-894385386
tasks.withType(Copy).all {
tasks.withType(Copy).configureEach {
duplicatesStrategy DuplicatesStrategy.INCLUDE
outputs.upToDateWhen { false }
}
Expand All @@ -31,7 +31,7 @@ subprojects {
}
}

tasks.withType(JavaCompile) {
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}

Expand All @@ -43,7 +43,9 @@ subprojects {

publishing {
publications {
//noinspection all
maven(MavenPublication) {
//noinspection all
artifact shadowJar
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/me/vaperion/blade/argument/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked"})
Expand All @@ -27,7 +28,7 @@ public static Provider<?> unsafe(@NotNull Class<?> type, @NotNull ArgumentProvid
public boolean doAnnotationsMatch(@Nullable List<Class<? extends Annotation>> annotations) {
if (annotations == null || requiredAnnotations == null) return false;
if (annotations.size() != requiredAnnotations.size()) return false;
return requiredAnnotations.containsAll(annotations);
return new HashSet<>(requiredAnnotations).containsAll(annotations);
}

}
3 changes: 2 additions & 1 deletion core/src/main/java/me/vaperion/blade/command/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public CommandParameter(String name, Class<?> type, List<String> data, Optional
}
}

@Getter
public static final class FlagParameter extends Parameter {
@Getter private final Flag flag;
private final Flag flag;

public FlagParameter(String name, Class<?> type, Optional optional, AnnotatedElement element, Flag flag) {
super(name, type, Collections.emptyList(), optional, null, null, false, element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

@RequiredArgsConstructor
Expand Down Expand Up @@ -68,7 +67,7 @@ public void suggest(@NotNull List<String> suggestions, @NotNull Context context,
if (!isFlag || !"true".equals(entry.getValue())) arguments.remove(entry.getValue());
}

if (arguments.size() == 0) return;
if (arguments.isEmpty()) return;
if (command.getParameterProviders().size() < arguments.size()) return;

int index = Math.max(0, arguments.size() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static List<String> combineQuotedArguments(@NotNull List<String> args) {
}

if (c == ' ' && boundary == '\0') {
if (building.length() > 0) {
if (!building.isEmpty()) {
arguments.add(building.toString());
building.setLength(0);
}
Expand All @@ -155,7 +155,7 @@ public static List<String> combineQuotedArguments(@NotNull List<String> args) {
building.append(c);
}

if (building.length() > 0) {
if (!building.isEmpty()) {
arguments.add(building.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class PaginatedOutput<T> {
public abstract String formatLine(T result, int index);

public final List<String> generatePage(List<T> results, int page) {
if (results.size() == 0) {
if (results.isEmpty()) {
return Collections.singletonList(formatErrorMessage(Error.NO_RESULTS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import me.vaperion.blade.command.UsageMessage;
import me.vaperion.blade.context.Context;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.NotNull;

import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;

public final class VelocityUsageMessage implements UsageMessage {

private final Component component;
Expand All @@ -23,17 +27,15 @@ public VelocityUsageMessage(Command command) {
}

public VelocityUsageMessage(Command command, boolean addPrefix) {
Component component = Component.text((addPrefix ? "Usage: " : "") + "/").color(NamedTextColor.RED)
.hoverEvent(HoverEvent.showText(
Component.text(command.getDescription()).color(NamedTextColor.GRAY))
).append(
Component.text(command.getUsageAlias())
);
TextComponent.Builder builder = text();

builder.append(text((addPrefix ? "Usage: " : "") + "/", NamedTextColor.RED)
.hoverEvent(HoverEvent.showText(text(command.getDescription(), NamedTextColor.GRAY))))
.append(text(command.getUsageAlias(), NamedTextColor.RED));

if (!command.getCustomUsage().isEmpty()) {
this.component = component.append(
Component.text(command.getCustomUsage())
);
builder.append(text(command.getCustomUsage(), NamedTextColor.RED));
this.component = builder.build();
return;
}

Expand All @@ -43,59 +45,35 @@ public VelocityUsageMessage(Command command, boolean addPrefix) {
Flag flag = flagParameter.getFlag();

if (first) {
component = component.append(
Component.text(" (").color(NamedTextColor.RED)
);
builder.append(text(" (", NamedTextColor.RED));
first = false;
} else {
component = component.append(
Component.text(" | ").color(NamedTextColor.RED)
).hoverEvent(HoverEvent.showText(
Component.text(command.getDescription()).color(NamedTextColor.GRAY))
);
builder.append(text(" | ", NamedTextColor.RED))
.hoverEvent(HoverEvent.showText(text(command.getDescription(), NamedTextColor.GRAY)));
}

component = component.append(
Component.text("-" + flag.value() + (flagParameter.isBooleanFlag() ? "" : " <" + flagParameter.getName() + ">"))
).color(NamedTextColor.AQUA)
.hoverEvent(HoverEvent.showText(
Component.text(flag.description()).color(NamedTextColor.GRAY))
);
builder
.append(text("-" + flag.value() + (flagParameter.isBooleanFlag() ? "" : " <" + flagParameter.getName() + ">"), NamedTextColor.AQUA))
.hoverEvent(HoverEvent.showText(text(flag.description(), NamedTextColor.GRAY)));
}
if (!first) component = component.append(
Component.text(")").color(NamedTextColor.RED)
).hoverEvent(HoverEvent.showText(
Component.text(command.getDescription()).color(NamedTextColor.GRAY))
);
if (!first) builder.append(text(")", NamedTextColor.RED))
.hoverEvent(HoverEvent.showText(text(command.getDescription(), NamedTextColor.GRAY)));

// Add real parameters
for (CommandParameter commandParameter : command.getCommandParameters()) {
component = component.append(
Component.text(" ")
);

component = component.append(
Component.text(commandParameter.isOptional() ? "(" : "<")
);
component = component.append(
Component.text(commandParameter.getName())
);
if (commandParameter.isText()) component = component.append(
Component.text("...")
);
component = component.append(
Component.text(commandParameter.isOptional() ? ")" : ">")
);
builder.append(text(" ", NamedTextColor.RED))
.append(text(commandParameter.isOptional() ? "(" : "<", NamedTextColor.RED))
.append(text(commandParameter.getName(), NamedTextColor.RED))
.append(commandParameter.isText() ? text("...", NamedTextColor.RED) : empty())
.append(text(commandParameter.isOptional() ? ")" : ">", NamedTextColor.RED));
}

// Add extra usage
if (!command.getExtraUsageData().isEmpty()) {
component = component.append(
Component.text(" " + command.getExtraUsageData().trim())
);
builder.append(text(" " + command.getExtraUsageData().trim(), NamedTextColor.RED));
}

this.component = component;
this.component = builder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import me.vaperion.blade.util.Tuple;
import me.vaperion.blade.velocity.command.VelocityUsageMessage;
import me.vaperion.blade.velocity.context.VelocitySender;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,6 +25,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static net.kyori.adventure.text.Component.text;

@Getter
public class VelocityContainer implements RawCommand, Container {

Expand Down Expand Up @@ -101,7 +102,7 @@ public boolean hasPermission(Invocation invocation) {
@Override
public void execute(Invocation invocation) {
CommandSource sender = invocation.source();
String[] args = invocation.arguments().split(" ");
String[] args = invocation.arguments().isEmpty() ? new String[0] : invocation.arguments().split(" ");
String alias = invocation.alias();

Command command = null;
Expand Down Expand Up @@ -155,23 +156,23 @@ public void execute(Invocation invocation) {
} catch (BladeUsageMessage ex) {
sendUsageMessage(context, finalCommand);
} catch (BladeExitMessage ex) {
sender.sendMessage(Component.text(ex.getMessage()).color(NamedTextColor.RED));
sender.sendMessage(text(ex.getMessage()).color(NamedTextColor.RED));
} catch (InvocationTargetException ex) {
if (ex.getTargetException() != null) {
if (ex.getTargetException() instanceof BladeUsageMessage) {
sendUsageMessage(context, finalCommand);
return;
} else if (ex.getTargetException() instanceof BladeExitMessage) {
sender.sendMessage(Component.text(ex.getTargetException().getMessage()).color(NamedTextColor.RED));
sender.sendMessage(text(ex.getTargetException().getMessage()).color(NamedTextColor.RED));
return;
}
}

ex.printStackTrace();
sender.sendMessage(Component.text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
sender.sendMessage(text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
} catch (Throwable t) {
t.printStackTrace();
sender.sendMessage(Component.text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
sender.sendMessage(text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
}
};

Expand All @@ -195,10 +196,10 @@ public void execute(Invocation invocation) {
} catch (BladeUsageMessage ex) {
sendUsageMessage(context, command);
} catch (BladeExitMessage ex) {
sender.sendMessage(Component.text(ex.getMessage()).color(NamedTextColor.RED));
sender.sendMessage(text(ex.getMessage()).color(NamedTextColor.RED));
} catch (Throwable t) {
t.printStackTrace();
sender.sendMessage(Component.text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
sender.sendMessage(text("An exception was thrown while executing this command.").color(NamedTextColor.RED));
}
}

Expand Down Expand Up @@ -233,10 +234,10 @@ public List<String> suggest(Invocation invocation) {
blade.getCompleter().suggest(suggestions, context, command, actualArguments);
return suggestions;
} catch (BladeExitMessage ex) {
sender.sendMessage(Component.text(ex.getMessage()).color(NamedTextColor.RED));
sender.sendMessage(text(ex.getMessage()).color(NamedTextColor.RED));
} catch (Exception ex) {
ex.printStackTrace();
sender.sendMessage(Component.text("An exception was thrown while completing this command.").color(NamedTextColor.RED));
sender.sendMessage(text("An exception was thrown while completing this command.").color(NamedTextColor.RED));
}

return Collections.emptyList();
Expand Down

0 comments on commit 61f6c7c

Please sign in to comment.