-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
592 additions
and
5 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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/me/vaperion/blade/bindings/impl/VelocityBindings.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,19 @@ | ||
package me.vaperion.blade.bindings.impl; | ||
|
||
import com.velocitypowered.api.proxy.Player; | ||
import me.vaperion.blade.bindings.Binding; | ||
import me.vaperion.blade.bindings.impl.provider.VelocityPlayerBladeProvider; | ||
import me.vaperion.blade.service.BladeCommandService; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class VelocityBindings implements Binding { | ||
|
||
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}"); | ||
|
||
@Override | ||
public void bind(@NotNull BladeCommandService commandService) { | ||
commandService.bindProvider(Player.class, new VelocityPlayerBladeProvider()); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/me/vaperion/blade/bindings/impl/provider/VelocityPlayerBladeProvider.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,63 @@ | ||
package me.vaperion.blade.bindings.impl.provider; | ||
|
||
import com.velocitypowered.api.proxy.Player; | ||
import com.velocitypowered.api.proxy.ProxyServer; | ||
import me.vaperion.blade.argument.BladeArgument; | ||
import me.vaperion.blade.argument.BladeProvider; | ||
import me.vaperion.blade.bindings.impl.VelocityBindings; | ||
import me.vaperion.blade.context.BladeContext; | ||
import me.vaperion.blade.exception.BladeExitMessage; | ||
import me.vaperion.blade.exception.BladeUsageMessage; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public class VelocityPlayerBladeProvider implements BladeProvider<Player> { | ||
|
||
@Override | ||
public @Nullable Player provide(@NotNull BladeContext ctx, @NotNull BladeArgument arg) throws BladeExitMessage { | ||
ProxyServer proxyServer = (ProxyServer) ctx.commandService().getVelocityProxyServer(); | ||
|
||
Player player = ctx.sender().parseAs(Player.class); | ||
|
||
if (arg.getType() == BladeArgument.Type.OPTIONAL && "self".equals(arg.getString())) { | ||
if (player != null) return player; | ||
else | ||
throw new BladeUsageMessage(); // show usage to console if we have 'self' as a default value (only works on players) | ||
} | ||
|
||
Player onlinePlayer = getPlayer(proxyServer, arg.getString()); | ||
if (onlinePlayer == null && !arg.getParameter().ignoreFailedArgumentParse()) | ||
throw new BladeExitMessage("Error: No online player with name or UUID '" + arg.getString() + "' found."); | ||
|
||
return onlinePlayer; | ||
} | ||
|
||
@Override | ||
public @NotNull List<String> suggest(@NotNull BladeContext ctx, @NotNull BladeArgument arg) throws BladeExitMessage { | ||
ProxyServer proxyServer = (ProxyServer) ctx.commandService().getVelocityProxyServer(); | ||
|
||
List<String> completions = new ArrayList<>(); | ||
String input = arg.getString(); | ||
|
||
for (Player player : proxyServer.getAllPlayers()) { | ||
if (player.getUsername().toLowerCase().startsWith(input.toLowerCase())) | ||
completions.add(player.getUsername()); | ||
} | ||
|
||
return completions; | ||
} | ||
|
||
private boolean isUUID(@NotNull String input) { | ||
return VelocityBindings.UUID_PATTERN.matcher(input).matches(); | ||
} | ||
|
||
@Nullable | ||
private Player getPlayer(@NotNull ProxyServer proxyServer, @NotNull String input) { | ||
if (isUUID(input)) return proxyServer.getPlayer(UUID.fromString(input)).orElse(null); | ||
return proxyServer.getPlayer(input).orElse(null); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/me/vaperion/blade/command/impl/VelocityUsageMessage.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,106 @@ | ||
package me.vaperion.blade.command.impl; | ||
|
||
import com.velocitypowered.api.command.CommandSource; | ||
import me.vaperion.blade.annotation.Flag; | ||
import me.vaperion.blade.command.BladeCommand; | ||
import me.vaperion.blade.command.BladeParameter; | ||
import me.vaperion.blade.command.BladeParameter.CommandParameter; | ||
import me.vaperion.blade.command.UsageMessage; | ||
import me.vaperion.blade.context.BladeContext; | ||
import net.kyori.adventure.text.Component; | ||
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; | ||
|
||
public class VelocityUsageMessage implements UsageMessage { | ||
|
||
private final Component component; | ||
|
||
public VelocityUsageMessage(BladeCommand command) { | ||
Component component = Component.text("Usage: /").color(NamedTextColor.RED) | ||
.hoverEvent(HoverEvent.showText( | ||
Component.text(command.getDescription()).color(NamedTextColor.GRAY)) | ||
).append( | ||
Component.text(command.getUsageAlias().isEmpty() ? command.getAliases()[0] : command.getUsageAlias()) | ||
); | ||
|
||
if (!command.getCustomUsage().isEmpty()) { | ||
this.component = component.append( | ||
Component.text(command.getCustomUsage()) | ||
); | ||
return; | ||
} | ||
|
||
// Add flag parameters | ||
boolean first = true; | ||
for (BladeParameter.FlagParameter flagParameter : command.getFlagParameters()) { | ||
Flag flag = flagParameter.getFlag(); | ||
|
||
if (first) { | ||
component = component.append( | ||
Component.text(" (").color(NamedTextColor.RED) | ||
); | ||
first = false; | ||
} else { | ||
component = component.append( | ||
Component.text(" | ").color(NamedTextColor.RED) | ||
).hoverEvent(HoverEvent.showText( | ||
Component.text(command.getDescription()).color(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)) | ||
); | ||
} | ||
if (!first) component = component.append( | ||
Component.text(")").color(NamedTextColor.RED) | ||
).hoverEvent(HoverEvent.showText( | ||
Component.text(command.getDescription()).color(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.isCombined()) component = component.append( | ||
Component.text("...") | ||
); | ||
component = component.append( | ||
Component.text(commandParameter.isOptional() ? ")" : ">") | ||
); | ||
} | ||
|
||
// Add extra usage | ||
if (!command.getExtraUsageData().isEmpty()) { | ||
component = component.append( | ||
Component.text(" " + command.getExtraUsageData().trim()) | ||
); | ||
} | ||
|
||
this.component = component; | ||
} | ||
|
||
@Override | ||
public void sendTo(@NotNull BladeContext context) { | ||
((CommandSource) context.sender().getBackingSender()).sendMessage(this.component); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String toString() { | ||
return LegacyComponentSerializer.legacyAmpersand().serialize(this.component); | ||
} | ||
} |
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.