Skip to content

Commit

Permalink
Add support for @s and @r selectors, at a basic core without filters
Browse files Browse the repository at this point in the history
  • Loading branch information
NickImpact committed Mar 18, 2024
1 parent f687ea7 commit 9a3e1a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public final class PlatformSourceParser implements ArgumentParser<CommandSource, PlatformSource>, BlockingSuggestionProvider<CommandSource> {
public final class PlatformSourceParser implements
ArgumentParser<CommandSource, PlatformSource>,
BlockingSuggestionProvider<CommandSource>
{

private static final Pattern SELECTOR = Pattern.compile("@[rs]");
private final PlainTextComponentSerializer plain = PlainTextComponentSerializer.plainText();

@Override
Expand All @@ -58,11 +64,23 @@ public final class PlatformSourceParser implements ArgumentParser<CommandSource,
options.add(PlatformSource.server());
options.addAll(online);

Optional<PlatformSource> match = options.stream()
.filter(player -> this.plain.serialize(player.name()).equalsIgnoreCase(args.peekString()))
.findFirst();
Optional<PlatformSource> target;

return match.map(player -> {
String input = args.peekString();
Matcher selector = SELECTOR.matcher(input);
if(selector.matches()) {
try {
target = Optional.of(this.parseFromSelector(context, input));
} catch (IllegalArgumentException e) {
return ArgumentParseResult.failure(e);
}
} else {
target = options.stream()
.filter(player -> this.plain.serialize(player.name()).equalsIgnoreCase(args.peekString()))
.findFirst();
}

return target.map(player -> {
args.readString();
return player;
})
Expand All @@ -78,11 +96,25 @@ public final class PlatformSourceParser implements ArgumentParser<CommandSource,
.collect(Collectors.toList());

names.add("Server");
names.add("@r");
names.add("@s");

return names.stream()
.filter(name -> name.toLowerCase().startsWith(input.peekString().toLowerCase()))
.map(Suggestion::simple)
.collect(Collectors.toList());
}

private PlatformSource parseFromSelector(CommandContext<CommandSource> context, String input) {
String selector = input.substring(1);
return switch (selector.toLowerCase()) {
case "s" -> context.sender().source();
case "r" -> {
Random rng = new Random();
List<PlatformPlayer> players = Impactor.instance().services().provide(PlatformPlayerService.class).online().stream().toList();
yield players.get(rng.nextInt(players.size()));
}
default -> throw new IllegalArgumentException("Invalid selector: " + selector);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public abstract class BaseImpactorPlugin implements ImpactorPlugin, Configurable
public BaseImpactorPlugin(ImpactorBootstrapper bootstrapper) {
instance = this;
this.bootstrapper = bootstrapper;

this.bootstrapper.logger().info("Initializing API...");
Impactor service = new ImpactorService();
APIRegister.register(service);
}

public static BaseImpactorPlugin instance() {
Expand Down Expand Up @@ -118,14 +122,10 @@ protected ModuleInitializer registerModules() {

@Override
public void construct() {
this.bootstrapper.logger().info("Initializing API...");
Impactor service = new ImpactorService();
APIRegister.register(service);

this.bootstrapper.logger().info("Registering modules...");
this.initializer = this.registerModules();
try {
this.initializer.construct(service);
this.initializer.construct(Impactor.instance());
} catch (Exception e) {
ExceptionPrinter.print(this.logger(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.impactdev.impactor.api.scheduler.Ticks;
import net.impactdev.impactor.api.scheduler.v2.Scheduler;
import net.impactdev.impactor.api.scheduler.v2.Schedulers;
import net.impactdev.impactor.core.commands.parsers.PlatformSourceParser;
import net.impactdev.impactor.core.modules.ModuleInitializer;
import net.impactdev.impactor.core.plugin.BaseImpactorPlugin;
import net.impactdev.impactor.core.plugin.ImpactorBootstrapper;
Expand All @@ -43,6 +44,7 @@
import net.impactdev.impactor.minecraft.scoreboard.ScoreboardModule;
import net.impactdev.impactor.minecraft.ui.UIModule;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import org.incendo.cloud.brigadier.argument.BrigadierMapping;
import org.incendo.cloud.brigadier.argument.BrigadierMappings;
Expand Down Expand Up @@ -71,15 +73,20 @@ protected ModuleInitializer registerModules() {

@Override
public void construct() {
super.construct();
Impactor.instance().events().subscribe(RegisterBrigadierMappingsEvent.class, event -> {
BrigadierMappings<CommandSource, CommandSourceStack> mappings = (BrigadierMappings<CommandSource, CommandSourceStack>) event.mappings();
BrigadierMapping<?, CurrencyParser, CommandSourceStack> currency = this.createMapping(
parser -> ResourceLocationArgument.id()
);

BrigadierMapping<?, PlatformSourceParser, CommandSourceStack> sources = this.createMapping(
parser -> EntityArgument.entity()
);

mappings.registerMapping(PlatformSourceParser.class, sources);
mappings.registerMapping(CurrencyParser.class, currency);
});
super.construct();
}

@Override
Expand Down

0 comments on commit 9a3e1a1

Please sign in to comment.