Skip to content

Commit

Permalink
feat: CreativeModeTabArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Mar 28, 2022
1 parent a0aea48 commit 911aaf1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
Expand Up @@ -2,6 +2,7 @@

import com.mojang.brigadier.arguments.ArgumentType;
import org.auioc.mods.arnicalib.ArnicaLib;
import org.auioc.mods.arnicalib.common.command.argument.CreativeModeTabArgument;
import org.auioc.mods.arnicalib.common.command.argument.DamageSourceArgument;
import org.auioc.mods.arnicalib.common.command.argument.EntityDamageSourceArgument;
import org.auioc.mods.arnicalib.common.command.argument.IndirectEntityDamageSourceArgument;
Expand All @@ -19,5 +20,6 @@ public static void init() {
register("damage_source", DamageSourceArgument.class, new EmptyArgumentSerializer<>(DamageSourceArgument::damageSource));
register("entity_damage_source", EntityDamageSourceArgument.class, new EmptyArgumentSerializer<>(EntityDamageSourceArgument::damageSource));
register("indirect_entity_damage_source", IndirectEntityDamageSourceArgument.class, new EmptyArgumentSerializer<>(IndirectEntityDamageSourceArgument::damageSource));
register("creative_mode_tab", CreativeModeTabArgument.class, new EmptyArgumentSerializer<>(CreativeModeTabArgument::creativeModeTab));
}
}
@@ -0,0 +1,56 @@
package org.auioc.mods.arnicalib.common.command.argument;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import org.auioc.mods.arnicalib.ArnicaLib;
import org.auioc.mods.arnicalib.api.mixin.common.IMixinCreativeModeTab;
import org.auioc.mods.arnicalib.utils.game.TextUtils;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.world.item.CreativeModeTab;

public class CreativeModeTabArgument implements ArgumentType<CreativeModeTab> {

private static final DynamicCommandExceptionType UNKNOWN_CREATIVE_MOD_TAB = new DynamicCommandExceptionType(
(langId) -> TextUtils.I18nText(ArnicaLib.i18n("argument.creative_mod_tab.unknown"), langId)
);

public static CreativeModeTabArgument creativeModeTab() {
return new CreativeModeTabArgument();
}

@Override
public CreativeModeTab parse(StringReader reader) throws CommandSyntaxException {
String langId = reader.readString();
return getAllTabs()
.stream()
.filter((tab) -> getLangId(tab).equals(langId))
.findAny()
.orElseThrow(() -> UNKNOWN_CREATIVE_MOD_TAB.create(langId));
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return SharedSuggestionProvider.suggest(
getAllTabs().stream().map(CreativeModeTabArgument::getLangId),
builder
);
}

public static List<CreativeModeTab> getAllTabs() {
return Arrays.asList(CreativeModeTab.TABS);
}

public static String getLangId(CreativeModeTab tab) {
// return ((TranslatableComponent) tab.getDisplayName()).getKey().replaceFirst("^itemGroup\\.", "");
return ((IMixinCreativeModeTab) tab).getLangId();
}

}
@@ -1,23 +1,28 @@
package org.auioc.mods.arnicalib.server.command;

import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;
import java.util.List;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode;
import org.auioc.mods.arnicalib.ArnicaLib;
import org.auioc.mods.arnicalib.common.command.argument.CreativeModeTabArgument;
import org.auioc.mods.arnicalib.common.command.impl.VersionCommand;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.world.item.CreativeModeTab;

public final class AHServerCommands {

public static final CommandNode<CommandSourceStack> NODE = literal(ArnicaLib.MOD_ID).build();

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
NODE.addChild(literal("version").executes((ctx) -> VersionCommand.getModVersion(ctx, ArnicaLib.MAIN_VERSION, ArnicaLib.FULL_VERSION, ArnicaLib.MOD_NAME)).build());
NODE.addChild(literal("test").executes((ctx) -> {
NODE.addChild(literal("test").then(argument("tab", CreativeModeTabArgument.creativeModeTab()).executes((ctx) -> {
System.err.println(ctx.getArgument("tab", CreativeModeTab.class));

return Command.SINGLE_SUCCESS;
}).build());
})).build());

getRootNode(dispatcher).addChild(NODE);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/arnicalib/lang/en_us.json
Expand Up @@ -9,7 +9,8 @@
"arnicalib.command.failure.not_dedicated_server": "This command must be executed by the dedicated server",
"arnicalib.command.failure.reflection": "Failed while using reflection to get private source",

"arnicalib.argument.damage_source.invalid": "Invalid damage source %s",
"arnicalib.argument.damage_source.invalid": "Invalid damage source \"%s\"",
"arnicalib.argument.creative_mod_tab.unknown": "Unknown creative mod tab \"%s\"",

"arnicalib.command.version.success": "Version: %s (%s)",
"arnicalib.command.version.failure": "§eCould not read the mod version. §7If this is a development environment you can ignore this message."
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/arnicalib/lang/zh_cn.json
Expand Up @@ -9,7 +9,8 @@
"arnicalib.command.failure.not_dedicated_server": "此命令必须由专用服务器执行",
"arnicalib.command.failure.reflection": "使用反射获取命令源失败",

"arnicalib.argument.damage_source.invalid": "无效的伤害类型%s",
"arnicalib.argument.damage_source.invalid": "无效的伤害类型\"%s\"",
"arnicalib.argument.creative_mod_tab.unknown": "未知的创造模式物品栏标签\"%s\"",

"arnicalib.command.version.success": "版本: %s (%s)",
"arnicalib.command.version.failure": "§e无法读取模组版本。§7若处在开发环境中则您可以忽略此消息"
Expand Down

0 comments on commit 911aaf1

Please sign in to comment.