Skip to content

Commit 4cf8431

Browse files
committed
fix(commands): update command registration and improve error handling
Signed-off-by: TimMayr <[email protected]>
1 parent 8edefad commit 4cf8431

File tree

15 files changed

+211
-187
lines changed

15 files changed

+211
-187
lines changed

common/src/main/java/com/github/lunatrius/schematica/Schematica.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ public class Schematica {
2626
public static void init() {
2727
if (Platform.getEnv() == EnvType.CLIENT) {
2828
Schematica.clientInit();
29+
} else {
30+
Schematica.serverInit();
2931
}
3032

3133
CommandRegistrationEvent.EVENT.register(
3234
(dispatcher, context, selection) -> CommandSchematicaBase.register(dispatcher));
3335

34-
if (Platform.getEnv() == EnvType.SERVER) {
35-
LifecycleEvent.SERVER_STARTING.register(
36-
(server) -> ServerProxy.serverWeakReference = new WeakReference<>(server));
37-
}
38-
3936
Reference.proxy = EnvExecutor.getEnvSpecific(() -> ClientProxy::new, () -> ServerProxy::new);
4037
Reference.proxy.init();
4138
PacketHandler.init();
@@ -48,7 +45,11 @@ public static void init() {
4845
}
4946

5047
private static void clientInit() {
51-
ClientCommandRegistrationEvent.EVENT.register(
52-
((dispatcher, context) -> CommandSchematicaBase.registerClient(context)));
48+
ClientCommandRegistrationEvent.EVENT.register(CommandSchematicaBase::registerClient);
49+
}
50+
51+
private static void serverInit() {
52+
LifecycleEvent.SERVER_STARTING.register(
53+
(server) -> ServerProxy.serverWeakReference = new WeakReference<>(server));
5354
}
5455
}

common/src/main/java/com/github/lunatrius/schematica/command/CommandSchematicaBase.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.github.lunatrius.schematica.command;
22

33
import com.github.lunatrius.schematica.command.client.CommandSchematicaReplace;
4+
import com.github.lunatrius.schematica.reference.Names;
45
import com.github.lunatrius.schematica.reference.Reference;
56
import com.mojang.brigadier.CommandDispatcher;
67
import com.mojang.brigadier.arguments.StringArgumentType;
78
import com.mojang.brigadier.context.CommandContext;
89
import com.mojang.brigadier.exceptions.CommandSyntaxException;
910
import com.mojang.brigadier.suggestion.Suggestions;
1011
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
11-
import com.mojang.brigadier.tree.LiteralCommandNode;
12+
import com.mojang.brigadier.tree.CommandNode;
13+
import dev.architectury.event.events.client.ClientCommandRegistrationEvent;
1214
import net.minecraft.ChatFormatting;
1315
import net.minecraft.commands.CommandBuildContext;
1416
import net.minecraft.commands.CommandSourceStack;
@@ -27,7 +29,7 @@
2729
import java.util.concurrent.CompletableFuture;
2830

2931
public abstract class CommandSchematicaBase {
30-
private static LiteralCommandNode<CommandSourceStack> mainNode;
32+
private static CommandNode<CommandSourceStack> rootNode;
3133

3234
protected static @NotNull MutableComponent withStyle(MutableComponent component, ChatFormatting formatting,
3335
@Nullable String command) {
@@ -41,15 +43,15 @@ public abstract class CommandSchematicaBase {
4143
}
4244

4345
public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispatcher) {
44-
mainNode = dispatcher.register(Commands.literal("schematica")
45-
.then(CommandSchematicaDownload.register())
46-
.then(CommandSchematicaList.register())
47-
.then(CommandSchematicaSave.register())
48-
.then(CommandSchematicaRemove.register()));
46+
rootNode = dispatcher.register(Commands.literal(Names.Command.BASE)
47+
.then(CommandSchematicaDownload.register())
48+
.then(CommandSchematicaList.register())
49+
.then(CommandSchematicaSave.register())
50+
.then(CommandSchematicaRemove.register()));
4951
}
5052

51-
public static void registerClient(CommandBuildContext context) {
52-
mainNode.addChild(CommandSchematicaReplace.register(context).build());
53+
public static void registerClient(@NotNull CommandDispatcher<ClientCommandRegistrationEvent.ClientCommandSourceStack> dispatcher, CommandBuildContext context) {
54+
dispatcher.register(CommandSchematicaReplace.register(context));
5355
}
5456

5557
public static CompletableFuture<Suggestions> getSchematicNamesSuggestions(

common/src/main/java/com/github/lunatrius/schematica/command/CommandSchematicaDownload.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@ public class CommandSchematicaDownload extends CommandSchematicaBase {
2323

2424
public static ArgumentBuilder<CommandSourceStack, ?> register() {
2525
return Commands.literal(Names.Command.Download.NAME)
26-
.then(Commands.argument("filename", StringArgumentType.string())
27-
.suggests(
28-
((context, builder) -> CommandSchematicaBase.getSchematicNamesSuggestions(
29-
context, builder, FILE_FILTER_SCHEMATIC)))
30-
.executes((commandContext) -> {
31-
CommandSourceStack source = commandContext.getSource();
32-
ServerPlayer player = source.getPlayerOrException();
26+
.then(Commands.argument("filename", StringArgumentType.string())
27+
.suggests(
28+
((context, builder) -> CommandSchematicaBase.getSchematicNamesSuggestions(
29+
context, builder, FILE_FILTER_SCHEMATIC)))
30+
.executes((commandContext) -> {
31+
CommandSourceStack source = commandContext.getSource();
32+
ServerPlayer player = source.getPlayerOrException();
3333

34-
String filename = StringArgumentType.getString(commandContext, "filename");
35-
File directory = Reference.proxy.getPlayerSchematicDirectory(player, true);
34+
String filename = StringArgumentType.getString(commandContext, "filename");
35+
File directory = Reference.proxy.getPlayerSchematicDirectory(player, true);
3636

37-
if (!FileUtils.contains(directory, filename)) {
38-
Reference.logger.error("{} has tried to download" + " the file " + "{}",
39-
player.getName(), filename);
37+
if (!FileUtils.contains(directory, filename)) {
38+
Reference.logger.error("{} has tried to download" + " the file " + "{}",
39+
player.getName(), filename);
4040

41-
source.sendFailure(Component.translatable(
42-
Names.Command.Download.Message.DOWNLOAD_FAILED));
43-
return -1;
44-
}
41+
source.sendFailure(Component.translatable(
42+
Names.Command.Download.Message.DOWNLOAD_FAILED));
43+
return -1;
44+
}
4545

46-
ISchematic schematic = SchematicFormat.readFromFile(directory, filename,
47-
Reference.proxy.getLevel(
48-
player));
46+
ISchematic schematic = SchematicFormat.readFromFile(directory, filename,
47+
Reference.proxy.getLevel(
48+
player));
4949

50-
if (schematic != null) {
51-
DownloadHandler.INSTANCE.transferMap.put(player.getScoreboardName(),
52-
new SchematicTransfer(schematic,
53-
filename));
54-
source.sendSuccess(() -> Component.translatable(
55-
Names.Command.Download.Message.DOWNLOAD_STARTED, filename), true);
56-
} else {
57-
source.sendFailure(Component.translatable(
58-
Names.Command.Download.Message.DOWNLOAD_FAILED));
59-
return -1;
60-
}
50+
if (schematic != null) {
51+
DownloadHandler.INSTANCE.transferMap.put(player.getScoreboardName(),
52+
new SchematicTransfer(schematic,
53+
filename));
54+
source.sendSuccess(() -> Component.translatable(
55+
Names.Command.Download.Message.DOWNLOAD_STARTED, filename), true);
56+
} else {
57+
source.sendFailure(Component.translatable(
58+
Names.Command.Download.Message.DOWNLOAD_FAILED));
59+
return -1;
60+
}
6161

62-
return 0;
63-
}));
62+
return 0;
63+
}));
6464
}
6565
}

common/src/main/java/com/github/lunatrius/schematica/command/CommandSchematicaList.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class CommandSchematicaList extends CommandSchematicaBase {
2525

2626
public static ArgumentBuilder<CommandSourceStack, ?> register() {
2727
return Commands.literal(Names.Command.List.NAME)
28-
.executes(CommandSchematicaList::printList)
29-
.then(Commands.argument("page", IntegerArgumentType.integer(1))
30-
.executes(CommandSchematicaList::printList));
28+
.executes(CommandSchematicaList::printList)
29+
.then(Commands.argument("page", IntegerArgumentType.integer(1))
30+
.executes(CommandSchematicaList::printList));
3131
}
3232

3333
private static int printList(@NotNull CommandContext<CommandSourceStack> commandContext)
@@ -60,7 +60,7 @@ private static int printList(@NotNull CommandContext<CommandSourceStack> command
6060
if (!schematicDirectory.exists()) {
6161
if (!schematicDirectory.mkdirs()) {
6262
Reference.logger.warn("Could not create player schematic directory {}",
63-
schematicDirectory.getAbsolutePath());
63+
schematicDirectory.getAbsolutePath());
6464

6565
source.sendFailure(Component.translatable(Names.Command.Save.Message.PLAYER_SCHEMATIC_DIR_UNAVAILABLE));
6666
return -1;
@@ -74,23 +74,23 @@ private static int printList(@NotNull CommandContext<CommandSourceStack> command
7474
String fileName = path.getName();
7575

7676
Component chatComponent = Component.literal(String.format("%2d (%s): %s [", currentFile + 1,
77-
FileUtils.humanReadableByteCount(
78-
path.length()),
79-
FilenameUtils.removeExtension(fileName)));
77+
FileUtils.humanReadableByteCount(
78+
path.length()),
79+
FilenameUtils.removeExtension(fileName)));
8080

8181
String removeCommand =
8282
String.format("/%s %s", Reference.MOD_ID + " " + Names.Command.Remove.NAME, fileName);
8383
Component removeLink =
8484
withStyle(Component.translatable(Names.Command.List.Message.REMOVE), ChatFormatting.RED,
85-
removeCommand);
85+
removeCommand);
8686
chatComponent = chatComponent.copy().append(removeLink).append("][");
8787

8888
String downloadCommand =
8989
String.format("/%s %s", Reference.MOD_ID + " " + Names.Command.Download.NAME, fileName);
9090
Component downloadLink =
9191
withStyle(Component.translatable(Names.Command.List.Message.DOWNLOAD),
92-
ChatFormatting.GREEN,
93-
downloadCommand);
92+
ChatFormatting.GREEN,
93+
downloadCommand);
9494
chatComponent = chatComponent.copy().append(downloadLink).append("]");
9595

9696
componentsToSend.add(chatComponent);
@@ -112,7 +112,7 @@ private static int printList(@NotNull CommandContext<CommandSourceStack> command
112112

113113
source.sendSystemMessage(
114114
withStyle(Component.translatable(Names.Command.List.Message.PAGE_HEADER, page + 1, totalPages + 1),
115-
ChatFormatting.DARK_GREEN, null));
115+
ChatFormatting.DARK_GREEN, null));
116116
for (Component chatComponent : componentsToSend) {
117117
source.sendSystemMessage(chatComponent);
118118
}

common/src/main/java/com/github/lunatrius/schematica/command/CommandSchematicaSave.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@
2121
public class CommandSchematicaSave extends CommandSchematicaBase {
2222
public static ArgumentBuilder<CommandSourceStack, ?> register() {
2323
return Commands.literal(Names.Command.Save.NAME)
24-
.then(Commands.argument("from", BlockPosArgument.blockPos())
25-
.then(Commands.argument("to", BlockPosArgument.blockPos())
26-
.then(Commands.argument("name", StringArgumentType.string())
27-
.executes(CommandSchematicaSave::execute)
28-
.then(Commands.argument("format",
29-
StringArgumentType.string())
30-
.suggests(((context, builder) -> {
31-
for (String s :
32-
SchematicFormat.FORMATS.keySet()) {
33-
builder.suggest(s);
34-
}
35-
return builder.buildFuture();
36-
}))
37-
.executes(
38-
CommandSchematicaSave::execute)))));
24+
.then(Commands.argument("from", BlockPosArgument.blockPos())
25+
.then(Commands.argument("to", BlockPosArgument.blockPos())
26+
.then(Commands.argument("name", StringArgumentType.string())
27+
.executes(CommandSchematicaSave::execute)
28+
.then(Commands.argument("format",
29+
StringArgumentType.string())
30+
.suggests(((context, builder) -> {
31+
for (String s :
32+
SchematicFormat.FORMATS.keySet()) {
33+
builder.suggest(s);
34+
}
35+
return builder.buildFuture();
36+
}))
37+
.executes(CommandSchematicaSave::execute)))));
3938
}
4039

4140
private static int execute(@NotNull CommandContext<CommandSourceStack> commandContext)
@@ -67,7 +66,7 @@ private static int execute(@NotNull CommandContext<CommandSourceStack> commandCo
6766

6867
String filename = name + SchematicFormat.getExtension(format);
6968

70-
Reference.logger.debug("Saving " + "schematic from {} to {} to {}", from, to, filename);
69+
Reference.logger.debug("Saving schematic from {} to {} to {}", from, to, filename);
7170
File schematicDirectory = Reference.proxy.getPlayerSchematicDirectory(player, true);
7271
if (schematicDirectory == null) {
7372
//Chances are that if this is null, we could not retrieve their UUID.
@@ -79,15 +78,15 @@ private static int execute(@NotNull CommandContext<CommandSourceStack> commandCo
7978
if (!schematicDirectory.exists()) {
8079
if (!schematicDirectory.mkdirs()) {
8180
Reference.logger.warn("Could not create " + "player " + "schematic " + "directory " + "{}",
82-
schematicDirectory.getAbsolutePath());
81+
schematicDirectory.getAbsolutePath());
8382
source.sendFailure(Component.translatable(Names.Command.Save.Message.PLAYER_SCHEMATIC_DIR_UNAVAILABLE));
8483
return -1;
8584
}
8685
}
8786

8887
try {
8988
Reference.proxy.saveSchematic(player, schematicDirectory, filename, player.getCommandSenderWorld(), format,
90-
from, to);
89+
from, to);
9190
} catch (Exception e) {
9291
source.sendFailure(Component.translatable(Names.Command.Save.Message.SAVE_FAILED));
9392
return -1;

common/src/main/java/com/github/lunatrius/schematica/command/client/CommandSchematicaReplace.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,53 @@
66
import com.github.lunatrius.schematica.reference.Names;
77
import com.github.lunatrius.schematica.reference.Reference;
88
import com.github.lunatrius.schematica.world.FakeLevel;
9-
import com.mojang.brigadier.builder.ArgumentBuilder;
9+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
10+
import dev.architectury.event.events.client.ClientCommandRegistrationEvent;
1011
import net.minecraft.commands.CommandBuildContext;
11-
import net.minecraft.commands.CommandSourceStack;
12-
import net.minecraft.commands.Commands;
12+
import net.minecraft.commands.arguments.blocks.BlockInput;
1313
import net.minecraft.commands.arguments.blocks.BlockStateArgument;
1414
import net.minecraft.network.chat.Component;
1515
import net.minecraft.world.level.block.state.BlockState;
1616
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockStateMatchTest;
1717

1818
public class CommandSchematicaReplace extends CommandSchematicaBase {
19-
public static ArgumentBuilder<CommandSourceStack, ?> register(CommandBuildContext context) {
20-
return Commands.literal(Names.Command.Replace.NAME)
21-
.then(Commands.argument("toReplace", BlockStateArgument.block(context))
22-
.then(Commands.argument("with", BlockStateArgument.block(context)))
23-
.executes((commandContext) -> {
24-
CommandSourceStack source = commandContext.getSource();
25-
BlockState toReplace =
26-
BlockStateArgument.getBlock(commandContext, "toReplace").getState();
27-
BlockState with =
28-
BlockStateArgument.getBlock(commandContext, "with").getState();
19+
public static LiteralArgumentBuilder<ClientCommandRegistrationEvent.ClientCommandSourceStack> register(CommandBuildContext context) {
20+
return ClientCommandRegistrationEvent.literal(Names.Command.BASE).then(
21+
ClientCommandRegistrationEvent.literal(Names.Command.Replace.NAME)
22+
.then(ClientCommandRegistrationEvent.argument("toReplace", BlockStateArgument.block(context))
23+
.then(ClientCommandRegistrationEvent.argument("with",
24+
BlockStateArgument.block(context))
25+
.executes((commandContext) -> {
26+
ClientCommandRegistrationEvent.ClientCommandSourceStack source =
27+
commandContext.getSource();
2928

30-
FakeLevel schematic = ClientProxy.schematic;
29+
BlockState toReplace = commandContext.getArgument("toReplace",
30+
BlockInput.class).getState();
31+
BlockState with =
32+
commandContext.getArgument("with", BlockInput.class).getState();
3133

32-
if (schematic == null) {
33-
source.sendFailure(Component.translatable(
34-
Names.Command.Replace.Message.NO_SCHEMATIC));
35-
return -1;
36-
}
34+
FakeLevel schematic = ClientProxy.schematic;
3735

38-
try {
39-
BlockStateMatchTest matcher = new BlockStateMatchTest(toReplace);
40-
BlockStateReplacer replacer = BlockStateReplacer.forBlockState(with);
41-
int count = schematic.replaceBlock(matcher, replacer);
36+
if (schematic == null) {
37+
source.arch$sendFailure(Component.translatable(
38+
Names.Command.Replace.Message.NO_SCHEMATIC));
39+
return -1;
40+
}
4241

43-
source.sendSuccess(
44-
() -> Component.translatable(Names.Command.Replace.Message.SUCCESS,
45-
count), true);
46-
} catch (Exception e) {
47-
Reference.logger.error("Something went wrong!", e);
48-
source.sendFailure(Component.literal(e.getMessage()));
49-
return -1;
50-
}
51-
return 0;
52-
}));
42+
try {
43+
BlockStateMatchTest matcher = new BlockStateMatchTest(toReplace);
44+
BlockStateReplacer replacer = BlockStateReplacer.forBlockState(with);
45+
int count = schematic.replaceBlock(matcher, replacer);
46+
47+
source.arch$sendSuccess(
48+
() -> Component.translatable(Names.Command.Replace.Message.SUCCESS,
49+
count), true);
50+
} catch (Exception e) {
51+
Reference.logger.error("Something went wrong!", e);
52+
source.arch$sendFailure(Component.literal(e.getMessage()));
53+
return -1;
54+
}
55+
return 0;
56+
}))));
5357
}
5458
}

0 commit comments

Comments
 (0)