Skip to content

Commit 2f8084b

Browse files
committed
Add create_instant command
1 parent 4fd986c commit 2f8084b

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

src/main/java/net/modfest/scatteredshards/client/command/ClientShardCommand.java

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import com.mojang.brigadier.tree.CommandNode;
1414
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
1515
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
16-
import net.fabricmc.loader.api.FabricLoader;
17-
import net.fabricmc.loader.api.ModContainer;
1816
import net.minecraft.command.argument.IdentifierArgumentType;
1917
import net.minecraft.text.Text;
2018
import net.minecraft.util.Identifier;
@@ -28,6 +26,7 @@
2826
import net.modfest.scatteredshards.client.screen.ShardCreatorGuiDescription;
2927
import net.modfest.scatteredshards.client.screen.ShardTabletGuiDescription;
3028
import net.modfest.scatteredshards.command.ShardCommand;
29+
import net.modfest.scatteredshards.command.ShardCommandNodeHelper;
3130

3231
import java.util.Optional;
3332
import java.util.Set;
@@ -95,19 +94,19 @@ public static int shards(CommandContext<FabricClientCommandSource> context) thro
9594
return Command.SINGLE_SUCCESS;
9695
}
9796

98-
public static CompletableFuture<Suggestions> suggestShardSets(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
97+
private static CompletableFuture<Suggestions> suggestShardSets(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
9998
for (Identifier id : ScatteredShardsAPI.getClientLibrary().shardSets().keySet()) {
10099
builder.suggest(id.toString());
101100
}
102101
return builder.buildFuture();
103102
}
104103

105-
public static CompletableFuture<Suggestions> suggestShards(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
104+
private static CompletableFuture<Suggestions> suggestShards(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
106105
ScatteredShardsAPI.getClientLibrary().shards().forEach((id, shard) -> builder.suggest(id.toString()));
107106
return builder.buildFuture();
108107
}
109108

110-
public static CompletableFuture<Suggestions> suggestShardTypes(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
109+
static CompletableFuture<Suggestions> suggestShardTypes(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
111110
ScatteredShardsAPI.getClientLibrary().shardTypes().forEach((id, shardSet) -> {
112111
if (!id.equals(ShardType.MISSING_ID)) {
113112
builder.suggest(id.toString());
@@ -116,22 +115,15 @@ public static CompletableFuture<Suggestions> suggestShardTypes(CommandContext<Fa
116115
return builder.buildFuture();
117116
}
118117

119-
public static CompletableFuture<Suggestions> suggestModIds(CommandContext<FabricClientCommandSource> context, SuggestionsBuilder builder) {
120-
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
121-
builder.suggest(mod.getMetadata().getId());
122-
}
123-
return builder.buildFuture();
124-
}
125-
126-
private static LiteralArgumentBuilder<FabricClientCommandSource> literal(String name) {
118+
static LiteralArgumentBuilder<FabricClientCommandSource> literal(String name) {
127119
return LiteralArgumentBuilder.literal(name);
128120
}
129121

130-
private static RequiredArgumentBuilder<FabricClientCommandSource, Identifier> identifierArgument(String name) {
122+
static RequiredArgumentBuilder<FabricClientCommandSource, Identifier> identifierArgument(String name) {
131123
return RequiredArgumentBuilder.argument(name, IdentifierArgumentType.identifier());
132124
}
133125

134-
private static RequiredArgumentBuilder<FabricClientCommandSource, String> stringArgument(String name) {
126+
static RequiredArgumentBuilder<FabricClientCommandSource, String> stringArgument(String name) {
135127
return RequiredArgumentBuilder.argument(name, StringArgumentType.string());
136128
}
137129

@@ -151,7 +143,7 @@ public static void register() {
151143
CommandNode<FabricClientCommandSource> creator = literal("creator").requires((source) -> source.hasPermissionLevel(2)).build();
152144
CommandNode<FabricClientCommandSource> creatorNew = literal("new").build();
153145
CommandNode<FabricClientCommandSource> modId = stringArgument("mod_id")
154-
.suggests(ClientShardCommand::suggestModIds)
146+
.suggests(ShardCommandNodeHelper::suggestModIds)
155147
.build();
156148
CommandNode<FabricClientCommandSource> shardType = identifierArgument("shard_type")
157149
.suggests(ClientShardCommand::suggestShardTypes)
@@ -176,6 +168,8 @@ public static void register() {
176168
creator.addChild(creatorEdit);
177169
creatorEdit.addChild(shardId);
178170

171+
CreateInstantCommand.register(creator);
172+
179173
dispatcher.getRoot().addChild(shardsCommand);
180174
});
181175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package net.modfest.scatteredshards.client.command;
2+
3+
import com.mojang.brigadier.arguments.StringArgumentType;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.CommandNode;
7+
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
8+
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
9+
import net.minecraft.text.Text;
10+
import net.minecraft.util.Identifier;
11+
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
12+
import net.modfest.scatteredshards.api.shard.Shard;
13+
import net.modfest.scatteredshards.api.shard.ShardType;
14+
import net.modfest.scatteredshards.command.ShardCommand;
15+
import net.modfest.scatteredshards.command.ShardCommandNodeHelper;
16+
import net.modfest.scatteredshards.networking.C2SModifyShard;
17+
import net.modfest.scatteredshards.util.ModMetaUtil;
18+
19+
import static net.modfest.scatteredshards.client.command.ClientShardCommand.identifierArgument;
20+
import static net.modfest.scatteredshards.client.command.ClientShardCommand.literal;
21+
import static net.modfest.scatteredshards.client.command.ClientShardCommand.stringArgument;
22+
23+
public class CreateInstantCommand {
24+
public static int create(CommandContext<FabricClientCommandSource> ctx) throws CommandSyntaxException {
25+
var source = ctx.getSource();
26+
27+
var modId = StringArgumentType.getString(ctx, "mod_id");
28+
var shardTypeId = ctx.getArgument("shard_type", Identifier.class);
29+
var name = StringArgumentType.getString(ctx, "shard_name");
30+
var lore = StringArgumentType.getString(ctx, "shard_lore");
31+
var hint = StringArgumentType.getString(ctx, "shard_hint");
32+
33+
var library = ScatteredShardsAPI.getClientLibrary();
34+
35+
// Make sure shard type exists
36+
library.shardTypes().get(shardTypeId).orElseThrow(() -> ShardCommand.INVALID_SHARD_TYPE.create(shardTypeId.toString()));
37+
38+
39+
var shardId = ShardType.createModId(shardTypeId, modId);
40+
var shard = Shard.emptyOfType(shardTypeId);
41+
var modIcon = ModMetaUtil.touchModIcon(modId);
42+
43+
if (library.shards().get(shardId).isPresent()) {
44+
source.sendError(Text.translatable("error.scattered_shards.duplicate_id", shardId.toString()));
45+
return 0;
46+
}
47+
48+
shard.setName(Text.literal(name));
49+
shard.setLore(Text.literal(lore));
50+
shard.setHint(Text.literal(hint));
51+
shard.setIcon(modIcon);
52+
shard.setSourceId(Identifier.of(modId, "shard_pack"));
53+
54+
ClientPlayNetworking.send(new C2SModifyShard(shardId, shard));
55+
56+
source.sendFeedback(Text.translatable("commands.scattered_shards.shard.create_instant", shardId.toString()));
57+
58+
return 1;
59+
}
60+
61+
public static void register(CommandNode<FabricClientCommandSource> parent) {
62+
var createInstantCommand = literal("create_instant").build();
63+
var modId = stringArgument("mod_id").suggests(ShardCommandNodeHelper::suggestModIds).build();
64+
var type = identifierArgument("shard_type").suggests(ClientShardCommand::suggestShardTypes).build();
65+
var name = stringArgument("shard_name").build();
66+
var lore = stringArgument("shard_lore").build();
67+
var hint = stringArgument("shard_hint").executes(CreateInstantCommand::create).build();
68+
69+
parent.addChild(createInstantCommand);
70+
createInstantCommand.addChild(modId);
71+
modId.addChild(type);
72+
type.addChild(name);
73+
name.addChild(lore);
74+
lore.addChild(hint);
75+
}
76+
}

src/main/java/net/modfest/scatteredshards/command/ShardCommandNodeHelper.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public static RequiredArgumentBuilder<ServerCommandSource, String> stringArgumen
8383
return RequiredArgumentBuilder.argument(name, StringArgumentType.string());
8484
}
8585

86+
public static RequiredArgumentBuilder<ServerCommandSource, Identifier> identifierArgument(String name) {
87+
return RequiredArgumentBuilder.argument(name, IdentifierArgumentType.identifier());
88+
}
89+
8690
/**
8791
* Creates literal nodes as necessary to extend a command path to include the desired command node, and returns the
8892
* node. If the node already exists, just find and return it.
@@ -106,7 +110,7 @@ public CommandNode<ServerCommandSource> getOrCreate(CommandNode<ServerCommandSou
106110
return cur;
107111
}
108112

109-
public static CompletableFuture<Suggestions> suggestModIds(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
113+
public static CompletableFuture<Suggestions> suggestModIds(CommandContext<?> context, SuggestionsBuilder builder) {
110114
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
111115
builder.suggest(mod.getMetadata().getId());
112116
}

src/main/resources/assets/scattered_shards/lang/en_us.json

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"item.scattered_shards.shard_item.description": "Awards shard when used",
77

88
"commands.scattered_shards.shard.collect": "Collected shard '%s'",
9+
"commands.scattered_shards.shard.create_instant": "Created shard '%s'",
910
"commands.scattered_shards.shard.award": "Awarded shard '%s' to %s players",
1011
"commands.scattered_shards.shard.award.none": "Tried to award shard '%s' but no players are eligible",
1112
"commands.scattered_shards.shard.block": "Shard-block '%s' added to inventory",
@@ -16,6 +17,7 @@
1617
"commands.scattered_shards.shard.library.delete.all": "Deleted %s shards from the library",
1718
"commands.scattered_shards.shard.library.migrate": "Migrated shard %s to %s",
1819

20+
"error.scattered_shards.duplicate_id": "Shard with id '%s' already exists",
1921
"error.scattered_shards.invalid_set_id": "Unknown shard set '%s'",
2022
"error.scattered_shards.invalid_shard_type": "Unknown shard type '%s'",
2123
"error.scattered_shards.invalid_shard_id": "Unknown shard '%s'",

0 commit comments

Comments
 (0)