Skip to content

Commit

Permalink
Add net.md_5.bungee.api.ChatColor resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAhmedYT committed Nov 27, 2024
1 parent eae0953 commit d21cc4e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import dev.velix.imperat.exception.UnknownPlayerException;
import dev.velix.imperat.exception.UnknownWorldException;
import dev.velix.imperat.selector.TargetSelector;
import dev.velix.imperat.type.ParameterOfflinePlayer;
import dev.velix.imperat.type.ParameterPlayer;
import dev.velix.imperat.type.ParameterTargetSelector;
import dev.velix.imperat.type.ParameterWorld;
import dev.velix.imperat.type.*;
import dev.velix.imperat.util.reflection.Reflections;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -70,6 +68,7 @@ private void registerValueResolvers() {
config.registerParamType(TargetSelector.class, new ParameterTargetSelector());
var worldClass = Reflections.getClass("org.bukkit.World");
config.registerParamType(worldClass, new ParameterWorld(worldClass));
config.registerParamType(ChatColor.class, new ParameterColorBungee());
}

public void setAdventureProvider(AdventureProvider<CommandSender> adventureProvider) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.velix.imperat.type;

import dev.velix.imperat.BukkitSource;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.BaseParameterType;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.SuggestionContext;
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import net.md_5.bungee.api.ChatColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;

public class ParameterColorBungee extends BaseParameterType<BukkitSource, ChatColor> {

private final ColorSuggestionResolver SUGGESTION_RESOLVER = new ColorSuggestionResolver();

public ParameterColorBungee() {
super(TypeWrap.of(ChatColor.class));
}

@Override
public @Nullable ChatColor resolve(ExecutionContext<BukkitSource> context, @NotNull CommandInputStream<BukkitSource> commandInputStream) {
final String raw = commandInputStream.currentRaw().orElse(null);
return raw == null ? null : ChatColor.valueOf(raw);
}

@Override
public SuggestionResolver<BukkitSource> getSuggestionResolver() {
return SUGGESTION_RESOLVER;
}

private final static class ColorSuggestionResolver implements SuggestionResolver<BukkitSource> {

private final Collection<String> colors = Arrays.stream(ChatColor.values()).map(ChatColor::name).toList();

/**
* @param context the context for suggestions
* @param parameter the parameter of the value to complete
* @return the auto-completed suggestions of the current argument
*/
@Override
public Collection<String> autoComplete(SuggestionContext<BukkitSource> context, CommandParameter<BukkitSource> parameter) {
return colors;
}
}

}

0 comments on commit d21cc4e

Please sign in to comment.