Skip to content

Commit

Permalink
Rewrite argument registration (#9)
Browse files Browse the repository at this point in the history
* Rewrite argument registration

* Remove added built-in argument types for now

* Update README

* Put build call on new line
  • Loading branch information
xpple authored Aug 23, 2023
1 parent 5542594 commit 1eccc04
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ arbitrary types. To do this, all you have to do is register the (de)serialiser w
to create a variable with type `Block` you can do
```java
new ModConfigBuilder(<mod id>, Configs.class)
.registerTypeHierarchyWithArgument(Block.class, new BlockAdapter(), new Pair<>(BlockArgumentType::block, BlockArgumentType::getBlock))
.registerTypeHierarchy(Block.class, new BlockAdapter(), BlockArgumentType::block)
.build();
```
where `BlockAdapter` extends `TypeAdapter<Block>` and `BlockArgumentType` implements `ArgumentType<Block>`. See
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public void onInitializeClient() {
}

private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
new ConfigCommandClient().register(dispatcher);
ConfigCommandClient.register(dispatcher, registryAccess);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.xpple.betterconfig.command.ConfigCommandHelper;
import dev.xpple.betterconfig.command.AbstractConfigCommand;
import dev.xpple.betterconfig.impl.ModConfigImpl;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.text.Text;

public class ConfigCommandClient extends ConfigCommandHelper<FabricClientCommandSource> {
public void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(this.create("cconfig"));
public class ConfigCommandClient extends AbstractConfigCommand<FabricClientCommandSource> {
private ConfigCommandClient() {
super("cconfig");
}

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
dispatcher.register(new ConfigCommandClient().create(registryAccess));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/xpple/betterconfig/BetterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public void onInitializeServer() {
}

private static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
new ConfigCommand().register(dispatcher);
ConfigCommand.register(dispatcher, registryAccess);
}
}
80 changes: 59 additions & 21 deletions src/main/java/dev/xpple/betterconfig/api/ModConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import dev.xpple.betterconfig.impl.BetterConfigInternals;
import dev.xpple.betterconfig.impl.ModConfigImpl;
import dev.xpple.betterconfig.util.CheckedBiFunction;
import dev.xpple.betterconfig.util.Pair;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.util.Pair;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class ModConfigBuilder {
Expand All @@ -23,8 +25,8 @@ public class ModConfigBuilder {
final Class<?> configsClass;

final GsonBuilder builder = new GsonBuilder().serializeNulls().enableComplexMapKeySerialization();
final Map<Class<?>, Pair<?, ?>> arguments = new HashMap<>();
final Map<Class<?>, Pair<?, ?>> suggestors = new HashMap<>();
final Map<Class<?>, Function<CommandRegistryAccess, ? extends ArgumentType<?>>> arguments = new HashMap<>();
final Map<Class<?>, Pair<SuggestionProvider<? extends CommandSource>, CheckedBiFunction<CommandContext<? extends CommandSource>, String, ?, CommandSyntaxException>>> suggestors = new HashMap<>();

public ModConfigBuilder(String modId, Class<?> configsClass) {
this.modId = modId;
Expand All @@ -35,65 +37,101 @@ public ModConfigBuilder(String modId, Class<?> configsClass) {
* Register a new type adapter and argument type for the specified type.
* @param type the type's class
* @param adapter the type adapter
* @param argument a brigadier argument pair
* @param argumentTypeSupplier a supplier for the argument type
* @param <T> the type
* @return the current builder instance
* @implNote On servers, consider using {@link ModConfigBuilder#registerTypeHierarchyWithSuggestor}
* @implNote On servers, consider using {@link ModConfigBuilder#registerType(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)}
* instead. To use this method on servers, operators need to register the brigadier argument type
* as well.
* @see ModConfigBuilder#registerTypeHierarchyWithArgument
* @see ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Supplier)
*/
public <T> ModConfigBuilder registerTypeWithArgument(Class<T> type, TypeAdapter<T> adapter, Pair<Supplier<ArgumentType<T>>, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException>> argument) {
public <T> ModConfigBuilder registerType(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
return this.registerType(type, adapter, registryAccess -> argumentTypeSupplier.get());
}

/**
* Register a new type adapter and argument type for the specified type.
* @param type the type's class
* @param adapter the type adapter
* @param argumentTypeFunction a function for the argument type needing registry access
* @param <T> the type
* @return the current builder instance
* @implNote On servers, consider using {@link ModConfigBuilder#registerType(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)}
* instead. To use this method on servers, operators need to register the brigadier argument type
* as well.
* @see ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Function)
*/
public <T> ModConfigBuilder registerType(Class<T> type, TypeAdapter<T> adapter, Function<CommandRegistryAccess, ArgumentType<T>> argumentTypeFunction) {
this.builder.registerTypeAdapter(type, adapter);
this.arguments.put(type, argument);
this.arguments.put(type, argumentTypeFunction);
return this;
}

/**
* Register a new type adapter and argument type for the specified type and all subclasses.
* @param type the type's class
* @param adapter the type adapter
* @param argument a pair of a brigadier argument and parser
* @param argumentTypeSupplier a supplier for the argument type
* @param <T> the type
* @return the current builder instance
* @implNote On servers, consider using {@link ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)}
* instead. To use this method on servers, operators need to register the brigadier argument type
* as well.
* @see ModConfigBuilder#registerType(Class, TypeAdapter, Supplier)
*/
public <T> ModConfigBuilder registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Supplier<ArgumentType<T>> argumentTypeSupplier) {
return this.registerTypeHierarchy(type, adapter, registryAccess -> argumentTypeSupplier.get());
}

/**
* Register a new type adapter and argument type for the specified type and all subclasses.
* @param type the type's class
* @param adapter the type adapter
* @param argumentTypeFunction a function for the argument type needing registry access
* @param <T> the type
* @return the current builder instance
* @implNote On servers, consider using {@link ModConfigBuilder#registerTypeHierarchyWithSuggestor}
* @implNote On servers, consider using {@link ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)}
* instead. To use this method on servers, operators need to register the brigadier argument type
* as well.
* @see ModConfigBuilder#registerType(Class, TypeAdapter, Function)
*/
public <T> ModConfigBuilder registerTypeHierarchyWithArgument(Class<T> type, TypeAdapter<T> adapter, Pair<Supplier<ArgumentType<T>>, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException>> argument) {
public <T> ModConfigBuilder registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, Function<CommandRegistryAccess, ArgumentType<T>> argumentTypeFunction) {
this.builder.registerTypeHierarchyAdapter(type, adapter);
this.arguments.put(type, argument);
this.arguments.put(type, argumentTypeFunction);
return this;
}

/**
* Register a new type adapter and suggestor for the specified type.
* @param type the type's class
* @param adapter the type adapter
* @param suggestor a pair of a custom suggestions provider and parser
* @param suggestionProvider a suggestion provider for the type
* @param argumentParser a parser for the argument
* @param <T> the type
* @return the current builder instance
* @implNote On clients, consider using {@link ModConfigBuilder#registerTypeWithArgument} instead.
* @see ModConfigBuilder#registerTypeHierarchyWithSuggestor
* @implNote On clients, consider using {@link ModConfigBuilder#registerType(Class, TypeAdapter, Supplier)} instead.
* @see ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)
*/
public <T> ModConfigBuilder registerTypeWithSuggestor(Class<T> type, TypeAdapter<T> adapter, Pair<Supplier<SuggestionProvider<? extends CommandSource>>, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException>> suggestor) {
public <T> ModConfigBuilder registerType(Class<T> type, TypeAdapter<T> adapter, SuggestionProvider<? extends CommandSource> suggestionProvider, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException> argumentParser) {
this.builder.registerTypeAdapter(type, adapter);
this.suggestors.put(type, suggestor);
this.suggestors.put(type, new Pair<>(suggestionProvider, argumentParser));
return this;
}

/**
* Register a new type adapter and suggestor for the specified type and all subclasses.
* @param type the type's class
* @param adapter the type adapter
* @param suggestor a pair of a custom suggestions provider and parser
* @param suggestionProvider a suggestion provider for the type
* @param argumentParser a parser for the argument
* @param <T> the type
* @return the current builder instance
* @implNote On clients, consider using {@link ModConfigBuilder#registerTypeHierarchyWithArgument} instead.
* @implNote On clients, consider using {@link ModConfigBuilder#registerTypeHierarchy(Class, TypeAdapter, Supplier)} instead.
* @see ModConfigBuilder#registerType(Class, TypeAdapter, SuggestionProvider, CheckedBiFunction)
*/
public <T> ModConfigBuilder registerTypeHierarchyWithSuggestor(Class<T> type, TypeAdapter<T> adapter, Pair<Supplier<SuggestionProvider<? extends CommandSource>>, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException>> suggestor) {
public <T> ModConfigBuilder registerTypeHierarchy(Class<T> type, TypeAdapter<T> adapter, SuggestionProvider<? extends CommandSource> suggestionProvider, CheckedBiFunction<CommandContext<? extends CommandSource>, String, T, CommandSyntaxException> argumentParser) {
this.builder.registerTypeHierarchyAdapter(type, adapter);
this.suggestors.put(type, suggestor);
this.suggestors.put(type, new Pair<>(suggestionProvider, argumentParser));
return this;
}

Expand Down
Loading

0 comments on commit 1eccc04

Please sign in to comment.