Skip to content

Commit

Permalink
Simplify default exception handler registration
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 27, 2024
1 parent 88f7b8f commit 26acbf9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ public FabricServerCommandManager(
}

ModdedParserMappings.registerServer(this);
ModdedExceptionHandler.registerDefaults(
this,
CommandSourceStack::sendFailure,
CommandSourceStack::getTextName
);
ModdedExceptionHandler.registerDefaults(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.BiConsumer;
import java.util.function.Function;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
Expand All @@ -40,6 +41,7 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.brigadier.BrigadierManagerHolder;
import org.incendo.cloud.exception.ArgumentParseException;
import org.incendo.cloud.exception.CommandExecutionException;
import org.incendo.cloud.exception.InvalidCommandSenderException;
Expand Down Expand Up @@ -81,17 +83,32 @@ default void handle(final ExceptionContext<C, T> context) throws Throwable {
*/
void handle(S source, C sender, T throwable) throws Throwable;

/**
* Registers the default handlers.
*
* @param commandManager the command manager
* @param <M> command manager type
* @param <C> command sender type
*/
static <C, M extends CommandManager<C> & BrigadierManagerHolder<C, CommandSourceStack>> void registerDefaults(
final M commandManager
) {
registerDefaults(commandManager, CommandSourceStack::sendFailure, CommandSourceStack::getTextName);
}

/**
* Registers the default handlers.
*
* @param commandManager the command manager
* @param sendError error message sender
* @param getName name getter
* @param <M> command manager type
* @param <C> command sender type
* @param <S> command source type
*/
static <C, S extends SharedSuggestionProvider> void registerDefaults(
final CommandManager<C> commandManager,
static <C, S extends SharedSuggestionProvider,
M extends CommandManager<C> & BrigadierManagerHolder<C, S>> void registerDefaults(
final M commandManager,
final BiConsumer<S, Component> sendError,
final Function<S, String> getName
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected NeoForgeCommandManager(
this.senderMapper.map(dummyCommandSourceProvider.get()),
this
), senderMapper);
ModdedExceptionHandler.registerDefaults(this, CommandSourceStack::sendFailure, CommandSourceStack::getTextName);
ModdedExceptionHandler.registerDefaults(this);
registrationHandler.initialize(this);
this.captionRegistry().registerProvider(new ModdedDefaultCaptionsProvider<>());
this.registerCommandPreProcessor(new ModdedPreprocessor<>(senderMapper));
Expand Down

0 comments on commit 26acbf9

Please sign in to comment.