Add EssentiaCommandArgument
This commit is contained in:
parent
2ef960b189
commit
d786ebcff5
|
|
@ -0,0 +1,41 @@
|
|||
package com.alttd.essentia.commands;
|
||||
|
||||
import com.alttd.essentia.util.Pair;
|
||||
import com.mojang.brigadier.Message;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface EssentiaArgument {
|
||||
|
||||
default <S> CompletableFuture<Suggestions> completedFuture(SuggestionsBuilder builder, Collection<String> possibleValues) {
|
||||
String remaining = builder.getRemaining().toLowerCase();
|
||||
for (String str : possibleValues) {
|
||||
if (str.toLowerCase().startsWith(remaining)) {
|
||||
builder.suggest(StringArgumentType.escapeIfRequired(str));
|
||||
}
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(
|
||||
builder.build()
|
||||
);
|
||||
}
|
||||
|
||||
default <S> CompletableFuture<Suggestions> completedFuturePair(SuggestionsBuilder builder, Collection<Pair<String, Message>> possibleValues) {
|
||||
String remaining = builder.getRemaining().toLowerCase();
|
||||
for (Pair<String, Message> pair : possibleValues) {
|
||||
String str = pair.x();
|
||||
if (str.toLowerCase().startsWith(remaining)) {
|
||||
builder.suggest(StringArgumentType.escapeIfRequired(str), pair.y());
|
||||
}
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(
|
||||
builder.build()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.essentia.commands.argumement;
|
||||
|
||||
import com.alttd.essentia.commands.EssentiaArgument;
|
||||
import com.mojang.brigadier.Message;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
|
|
@ -15,9 +16,11 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class EquipmentArgumentType implements CustomArgumentType.Converted<EquipmentSlot, String> {
|
||||
public class EquipmentArgumentType implements CustomArgumentType.Converted<EquipmentSlot, String>, EssentiaArgument {
|
||||
|
||||
@Override
|
||||
public @NotNull EquipmentSlot convert(String nativeType) throws CommandSyntaxException {
|
||||
|
|
@ -37,13 +40,12 @@ public class EquipmentArgumentType implements CustomArgumentType.Converted<Equip
|
|||
|
||||
@Override
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
Collection<String> possibleValues = new ArrayList<>();
|
||||
for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) {
|
||||
builder.suggest(equipmentSlot.name().toLowerCase());
|
||||
possibleValues.add(equipmentSlot.name().toLowerCase());
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(
|
||||
builder.build()
|
||||
);
|
||||
return completedFuture(builder, possibleValues);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
package com.alttd.essentia.commands.argumement;
|
||||
|
||||
import com.alttd.essentia.commands.EssentiaArgument;
|
||||
import com.alttd.essentia.util.Pair;
|
||||
import com.mojang.brigadier.Message;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
import com.mojang.brigadier.suggestion.Suggestion;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
|
||||
|
|
@ -17,9 +20,11 @@ import org.bukkit.OfflinePlayer;
|
|||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class OfflinePlayerCompletingArgument implements CustomArgumentType.Converted<OfflinePlayer, String> {
|
||||
public class OfflinePlayerCompletingArgument implements CustomArgumentType.Converted<OfflinePlayer, String>, EssentiaArgument {
|
||||
|
||||
@Override
|
||||
public @NotNull OfflinePlayer convert(String nativeType) throws CommandSyntaxException {
|
||||
|
|
@ -39,12 +44,11 @@ public class OfflinePlayerCompletingArgument implements CustomArgumentType.Conve
|
|||
|
||||
@Override
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
Collection<Pair<String, Message>> possibleValues = new ArrayList<>();
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
builder.suggest(player.getName(), MessageComponentSerializer.message().serialize(player.displayName()));
|
||||
possibleValues.add(new Pair<>(player.getName(), MessageComponentSerializer.message().serialize(player.displayName())));
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(
|
||||
builder.build()
|
||||
);
|
||||
return completedFuturePair(builder, possibleValues);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.essentia.commands.argumement;
|
||||
|
||||
import com.alttd.essentia.commands.EssentiaArgument;
|
||||
import com.mojang.brigadier.Message;
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
|
|
@ -15,9 +16,11 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||
import org.bukkit.WeatherType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class WeatherArgument implements CustomArgumentType.Converted<WeatherType, String> {
|
||||
public class WeatherArgument implements CustomArgumentType.Converted<WeatherType, String>, EssentiaArgument {
|
||||
|
||||
@Override
|
||||
public @NotNull WeatherType convert(String nativeType) throws CommandSyntaxException {
|
||||
|
|
@ -37,12 +40,11 @@ public class WeatherArgument implements CustomArgumentType.Converted<WeatherType
|
|||
|
||||
@Override
|
||||
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
|
||||
Collection<String> possibleValues = new ArrayList<>();
|
||||
for (WeatherType weatherType : WeatherType.values()) {
|
||||
builder.suggest(weatherType.name().toLowerCase());
|
||||
possibleValues.add(weatherType.name().toLowerCase());
|
||||
}
|
||||
|
||||
return CompletableFuture.completedFuture(
|
||||
builder.build()
|
||||
);
|
||||
return completedFuture(builder, possibleValues);
|
||||
}
|
||||
}
|
||||
5
plugin/src/main/java/com/alttd/essentia/util/Pair.java
Normal file
5
plugin/src/main/java/com/alttd/essentia/util/Pair.java
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package com.alttd.essentia.util;
|
||||
|
||||
public record Pair<X, Y>(X x, Y y) {
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user