Skip to content

Commit

Permalink
Setup target selector for testing as a new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 19, 2024
1 parent 2dae663 commit fb9656e
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "dev.velix"
version = "1.0.3"
version = "1.0.4"

ext {
def kyoriVersion = "4.17.0"
Expand Down
3 changes: 3 additions & 0 deletions bukkit/src/main/java/dev/velix/imperat/BukkitImperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import dev.velix.imperat.exception.UnknownPlayerException;
import dev.velix.imperat.exception.UnknownWorldException;
import dev.velix.imperat.resolvers.PermissionResolver;
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.util.BukkitUtil;
import dev.velix.imperat.util.ImperatDebugger;
Expand Down Expand Up @@ -227,6 +229,7 @@ public void unregisterAllCommands() {
private void registerValueResolvers() {
this.registerParamType(Player.class, new ParameterPlayer());
this.registerParamType(OfflinePlayer.class, new ParameterOfflinePlayer());
this.registerParamType(TargetSelector.class, new ParameterTargetSelector());
this.registerParamType(Reflections.getClass("org.bukkit.World"), new ParameterWorld());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import dev.velix.imperat.ArgumentTypeResolver;
import dev.velix.imperat.command.parameters.NumericRange;
import dev.velix.imperat.command.parameters.type.ParameterWord;
import dev.velix.imperat.util.BukkitUtil;
import dev.velix.imperat.util.TypeUtility;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Type;
Expand All @@ -34,16 +31,10 @@ class DefaultArgTypeResolvers {

private static final ArgumentType<?> SINGLE_PLAYER = entity(true, true);
public static final ArgumentTypeResolver PLAYER = parameter -> SINGLE_PLAYER;
private static final ArgumentType<?> MULTI_PLAYER = entity(false, true);
private static final ArgumentType<?> MULTI_ENTITY = entity(false, false);

//TODO add entity selector
public static final ArgumentTypeResolver ENTITY_SELECTOR = parameter -> {
Class<? extends Entity> valueType = BukkitUtil.getSelectedEntity(parameter.type().type());
if (Player.class.isAssignableFrom(valueType)) // TargetSelector<Player>
return MULTI_PLAYER;
return MULTI_ENTITY;
};
public static final ArgumentTypeResolver ENTITY_SELECTOR = parameter -> MULTI_ENTITY;

private static ArgumentType<? extends Number> numeric(
Type type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@

public final class TargetSelector implements Iterable<Entity> {

private final SelectionType type;
private final List<Entity> selectedEntities;

private TargetSelector(List<Entity> selectedEntities) {
private TargetSelector(SelectionType type, List<Entity> selectedEntities) {
this.type = type;
this.selectedEntities = selectedEntities;
}

public static TargetSelector of(List<Entity> list) {
return new TargetSelector(list);
public static TargetSelector of(SelectionType type, List<Entity> list) {
return new TargetSelector(type, list);
}

public static TargetSelector of(Entity... list) {
return of(List.of(list));
public static TargetSelector of(SelectionType type, Entity... list) {
return of(type, List.of(list));
}

public static TargetSelector empty() {
return of(SelectionType.UNKNOWN, List.of());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import dev.velix.imperat.util.TypeWrap;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
* AbstractField is an abstract base class that implements the SelectionField
Expand All @@ -24,6 +26,13 @@ public abstract class AbstractField<V> implements SelectionField<V> {
*/
protected final Type type;


/**
* A list that holds suggestion strings related to the selection field's value.
* This list can be used to provide autocomplete or assistive suggestions for the user.
*/
protected final List<String> suggestions = new ArrayList<>();

/**
* Constructs an AbstractField instance with the specified name and type.
*
Expand Down Expand Up @@ -58,4 +67,14 @@ public String getName() {
public Type getValueType() {
return type;
}

/**
* Retrieves a list of suggestions related to this selection field's value
*
* @return A list of suggestion strings.
*/
@Override
public List<String> getSuggestions() {
return suggestions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.velix.imperat.exception.ImperatException;

import java.lang.reflect.Type;
import java.util.List;

//selection field for mojang entity/target-selectors in minecraft

Expand Down Expand Up @@ -40,4 +41,11 @@ public interface SelectionField<V> extends SelectionFields {
*/
V parseFieldValue(String value) throws ImperatException;


/**
* Retrieves a list of suggestions related to this selection field's value
*
* @return A list of suggestion strings.
*/
List<String> getSuggestions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface SelectionFields {

//TODO add constants for each type of field

Set<SelectionField<?>> ALL = BukkitUtil.mergedSet(PredicateField.ALL, OperatorField.ALL, HashSet::new);
Set<SelectionField<?>> ALL = BukkitUtil.mergedSet(
new HashSet<>(PredicateField.ALL_PREDICATES),
new HashSet<>(OperatorField.ALL_OPERATORS),
HashSet::new
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
import org.bukkit.entity.HumanEntity;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

final class GamemodeField extends PredicateField<GameMode> {

GamemodeField(String name) {
super(name, TypeWrap.of(GameMode.class));
Arrays.stream(GameMode.values())
.map(GameMode::name)
.map(String::toLowerCase)
.forEach(suggestions::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public final class LevelField extends PredicateField<Range<Integer>> {
final class LevelField extends PredicateField<Range<Integer>> {

private final RangedNumericField<Integer> numericField;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

final class TypeField extends PredicateField<EntityType> {
/**
* Constructs an AbstractField instance with the specified name and type.
Expand All @@ -17,6 +19,10 @@ final class TypeField extends PredicateField<EntityType> {
*/
TypeField(String name) {
super(name, TypeWrap.of(EntityType.class));
Arrays.stream(EntityType.values())
.map(EntityType::name)
.map(String::toLowerCase)
.forEach(suggestions::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @param <V> The type of the value that this field handles.
*/
public abstract class OperatorField<V> extends AbstractField<V> {
public abstract class OperatorField<V> extends AbstractField<V> implements OperatorFields {

protected OperatorField(String name, TypeWrap<V> type) {
super(name, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import dev.velix.imperat.util.TypeWrap;
import org.bukkit.entity.Entity;

import java.util.Arrays;
import java.util.List;

final class SortOperatorField extends OperatorField<SortOption> {

SortOperatorField(String name) {
super(name, TypeWrap.of(SortOption.class));
Arrays.stream(SortOption.values())
.map(SortOption::name)
.map(String::toLowerCase)
.forEach(suggestions::add);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public final class ParameterTargetSelector extends BaseParameterType<BukkitSourc
private final static char PARAMETER_END = ']';

private final TypeSuggestionResolver<BukkitSource, TargetSelector> suggestionResolver;
private ParameterTargetSelector() {

public ParameterTargetSelector() {
super(TypeWrap.of(TargetSelector.class));
SelectionType.TYPES.stream()
.filter(type -> type != SelectionType.UNKNOWN)
Expand All @@ -52,10 +53,13 @@ private ParameterTargetSelector() {

String raw = commandInputStream.currentRaw().orElse(null);
if (raw == null)
return TargetSelector.of();
return TargetSelector.empty();

if (Version.isOrOver(13)) {
SelectionType type = commandInputStream.popLetter()
.map(SelectionType::from).orElse(SelectionType.UNKNOWN);
return TargetSelector.of(
type,
Bukkit.selectEntities(context.source().origin(), raw)
);
}
Expand All @@ -65,10 +69,11 @@ private ParameterTargetSelector() {
if (commandInputStream.currentLetter().filter((c) -> c != SelectionType.MENTION_CHARACTER).isPresent()) {
Player target = Bukkit.getPlayer(raw);
if (target == null)
return TargetSelector.of();
return TargetSelector.empty();

return TargetSelector.of(target);
return TargetSelector.of(SelectionType.UNKNOWN, target);
}

if (context.source().isConsole()) {
throw new SourceException("Only players can use this");
}
Expand Down Expand Up @@ -100,7 +105,7 @@ private ParameterTargetSelector() {
}
operateFields(inputParameters, selected);

return TargetSelector.of(selected);
return TargetSelector.of(type, selected);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -190,8 +195,7 @@ public Collection<String> autoComplete(
.filter((selectionField) -> selectionField.getName().equalsIgnoreCase(name))
.findFirst().orElse(null);

//TODO get values from the type of this field

return field == null ? Collections.emptyList() : field.getSuggestions();
}
return Collections.singleton(String.valueOf(PARAMETER_END));
}
Expand Down

0 comments on commit fb9656e

Please sign in to comment.