Skip to content

Commit

Permalink
Detect recursive use of calias. Closes #320
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed May 19, 2024
1 parent 1411003 commit 59199d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.logging.LogUtils;
import net.earthcomputer.clientcommands.features.BrigadierRemover;
import net.earthcomputer.clientcommands.interfaces.IClientSuggestionsProvider_Alias;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class AliasCommand {
private static final DynamicCommandExceptionType ALIAS_EXISTS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.calias.addAlias.aliasAlreadyExists", arg));
private static final DynamicCommandExceptionType COMMAND_EXISTS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.calias.addAlias.commandAlreadyExists", arg));
private static final DynamicCommandExceptionType NOT_FOUND_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.calias.notFound", arg));
private static final DynamicCommandExceptionType RECURSIVE_ALIAS_EXCEPTION = new DynamicCommandExceptionType(arg -> Component.translatable("commands.calias.recursive", arg));

private static final HashMap<String, String> aliasMap = loadAliases();

Expand Down Expand Up @@ -76,6 +78,12 @@ private static int executeAliasCommand(FabricClientCommandSource source, String
if (cmd == null) {
throw NOT_FOUND_EXCEPTION.create(aliasKey);
}

if (((IClientSuggestionsProvider_Alias) source).clientcommands_isAliasSeen(aliasKey)) {
throw RECURSIVE_ALIAS_EXCEPTION.create(aliasKey);
}
((IClientSuggestionsProvider_Alias) source).clientcommands_addSeenAlias(aliasKey);

int inlineArgumentCount = (int) Pattern.compile("(?<!%)%(?:%%)*(?!%)").matcher(cmd).results().count();
if (inlineArgumentCount > 0) {
String[] argumentArray = arguments.split(" ", inlineArgumentCount + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.earthcomputer.clientcommands.interfaces;

public interface IClientSuggestionsProvider_Alias {
void clientcommands_addSeenAlias(String alias);

boolean clientcommands_isAliasSeen(String alias);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.earthcomputer.clientcommands.mixin.commands.alias;

import net.earthcomputer.clientcommands.interfaces.IClientSuggestionsProvider_Alias;
import net.minecraft.client.multiplayer.ClientSuggestionProvider;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import java.util.HashSet;
import java.util.Set;

@Mixin(ClientSuggestionProvider.class)
public class ClientSuggestionProviderMixin implements IClientSuggestionsProvider_Alias {
@Unique
private final Set<String> seenAliases = new HashSet<>();

@Override
public void clientcommands_addSeenAlias(String alias) {
seenAliases.add(alias);
}

@Override
public boolean clientcommands_isAliasSeen(String alias) {
return seenAliases.contains(alias);
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/clientcommands/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"commands.calias.addAlias.success": "Successfully added alias \"%s\"",
"commands.calias.listAliases.noAliasesRegistered": "No aliases registered",
"commands.calias.listAliases.success": "%s aliases registered:",
"commands.calias.recursive": "Recursive alias \"%s\"",
"commands.calias.removeAlias.success": "Successfully removed alias \"%s\"",

"commands.careastats.notLoaded": "A chunk in this area is not loaded",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.clientcommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"requireAnnotations": true
},
"client": [
"commands.alias.ClientSuggestionProviderMixin",
"commands.enchant.MultiPlayerGameModeMixin",
"commands.generic.CommandSuggestionsMixin",
"dataqueryhandler.ClientPacketListenerMixin",
Expand Down

0 comments on commit 59199d0

Please sign in to comment.