Skip to content

Commit

Permalink
Some minor fixes and cleanups to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 9, 2024
1 parent 80f6451 commit a60df65
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public final class ChunkGeneratorArgument {
public static final DynamicCommandExceptionType GENERATOR_NOT_FOUND = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.chunk_generator.generator_not_found", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.chunk_generator.generator_not_found", arg)
);

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public final class DimensionOptionsArgument {
public static final DynamicCommandExceptionType DIMENSION_NOT_FOUND = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.dimension_options.dimension_not_found", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.dimension_options.dimension_not_found", arg)
);

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public final class MapManageCommand {
);

public static final DynamicCommandExceptionType MAP_ALREADY_EXISTS = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.map.open.map_already_exists", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.map.open.map_already_exists", arg)
);

public static final SimpleCommandExceptionType MAP_MISMATCH = new SimpleCommandExceptionType(
Text.translatable("text.nucleoid_creator_tools.map.delete.map_mismatch")
);

public static final DynamicCommandExceptionType INVALID_GENERATOR_CONFIG = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.map.open.invalid_generator_config", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.map.open.invalid_generator_config", arg)
);

// @formatter:off
Expand Down Expand Up @@ -142,25 +142,23 @@ private static int openWorkspace(CommandContext<ServerCommandSource> context, Ru
throw MAP_ALREADY_EXISTS.create(identifier);
}

source.getServer().submit(() -> {
try {
if (worldConfig != null) {
workspaceManager.open(identifier, worldConfig);
} else {
workspaceManager.open(identifier);
}

source.sendFeedback(
() -> Text.translatable("text.nucleoid_creator_tools.map.open.success",
identifier,
Text.translatable("text.nucleoid_creator_tools.map.open.join_command", identifier).formatted(Formatting.GRAY)),
false
);
} catch (Throwable throwable) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.open.error"));
CreatorTools.LOGGER.error("Failed to open workspace", throwable);
try {
if (worldConfig != null) {
workspaceManager.open(identifier, worldConfig);
} else {
workspaceManager.open(identifier);
}
});

source.sendFeedback(
() -> Text.translatable("text.nucleoid_creator_tools.map.open.success",
Text.of(identifier),
Text.translatable("text.nucleoid_creator_tools.map.open.join_command", Text.of(identifier)).formatted(Formatting.GRAY)),
false
);
} catch (Throwable throwable) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.open.error"));
CreatorTools.LOGGER.error("Failed to open workspace", throwable);
}

return Command.SINGLE_SUCCESS;
}
Expand All @@ -185,14 +183,7 @@ private static int openWorkspaceByGenerator(CommandContext<ServerCommandSource>
server.getRegistryManager()
);

var result = generatorCodec.parse(ops, config);

var error = result.error();
if (error.isPresent()) {
throw INVALID_GENERATOR_CONFIG.create(error.get());
}

var chunkGenerator = result.result().get();
var chunkGenerator = Util.getResult(generatorCodec.parse(ops, config), INVALID_GENERATOR_CONFIG::create);

var worldConfig = new RuntimeWorldConfig()
.setDimensionType(DimensionTypes.OVERWORLD)
Expand Down Expand Up @@ -240,7 +231,7 @@ private static int setWorkspaceBounds(CommandContext<ServerCommandSource> contex

private static int joinWorkspace(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();
var player = source.getPlayer();
var player = source.getPlayerOrThrow();

var workspace = MapWorkspaceArgument.get(context, "workspace");

Expand All @@ -260,7 +251,7 @@ private static int joinWorkspace(CommandContext<ServerCommandSource> context) th

source.sendFeedback(
() -> Text.translatable("text.nucleoid_creator_tools.map.join.success",
workspace.getIdentifier(),
Text.of(workspace.getIdentifier()),
Text.translatable("text.nucleoid_creator_tools.map.join.leave_command").formatted(Formatting.GRAY)),
false
);
Expand All @@ -270,7 +261,7 @@ private static int joinWorkspace(CommandContext<ServerCommandSource> context) th

private static int leaveMap(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();
var player = source.getPlayer();
var player = source.getPlayerOrThrow();

var workspaceManager = MapWorkspaceManager.get(source.getServer());
var workspace = workspaceManager.byDimension(player.getWorld().getRegistryKey());
Expand All @@ -289,7 +280,7 @@ private static int leaveMap(CommandContext<ServerCommandSource> context) throws
}

source.sendFeedback(
() -> Text.translatable("text.nucleoid_creator_tools.map.leave.success", workspace.getIdentifier()),
() -> Text.translatable("text.nucleoid_creator_tools.map.leave.success", Text.of(workspace.getIdentifier())),
false
);

Expand Down Expand Up @@ -318,7 +309,7 @@ private static int exportMap(CommandContext<ServerCommandSource> context, boolea

future.handle((v, throwable) -> {
if (throwable == null) {
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.export.success", workspace.getIdentifier()), false);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.export.success", Text.of(workspace.getIdentifier())), false);
} else {
CreatorTools.LOGGER.error("Failed to export map to '{}'", workspace.getIdentifier(), throwable);
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.export.error"));
Expand All @@ -344,9 +335,9 @@ private static int deleteWorkspace(CommandContext<ServerCommandSource> context)
source.sendFeedback(() -> {
MutableText message;
if (deleted) {
message = Text.translatable("text.nucleoid_creator_tools.map.delete.success", workspace.getIdentifier());
message = Text.translatable("text.nucleoid_creator_tools.map.delete.success", Text.of(workspace.getIdentifier()));
} else {
message = Text.translatable("text.nucleoid_creator_tools.map.delete.error", workspace.getIdentifier());
message = Text.translatable("text.nucleoid_creator_tools.map.delete.error", Text.of(workspace.getIdentifier()));
}

return message.formatted(Formatting.RED);
Expand Down Expand Up @@ -387,12 +378,12 @@ private static int importWorkspace(CommandContext<ServerCommandSource> context,
try {
var placer = new MapTemplatePlacer(template);
placer.placeAt(workspace.getWorld(), origin);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.import.success", toWorkspaceId), false);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.import.success", Text.of(toWorkspaceId)), false);
} catch (Exception e) {
CreatorTools.LOGGER.error("Failed to place template into world!", e);
}
} else {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.import.no_template_found", location));
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.import.no_template_found", Text.of(location)));
}
}, server);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

public final class MapMetadataCommand {
public static final DynamicCommandExceptionType ENTITY_TYPE_NOT_FOUND = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.entity_type_not_found", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.map.region.entity.filter.entity_type_not_found", arg)
);

public static final SimpleCommandExceptionType MAP_NOT_HERE = MapManageCommand.MAP_NOT_HERE;
Expand All @@ -61,11 +61,9 @@ public final class MapMetadataCommand {
);

private static final DynamicCommandExceptionType MODIFY_EXPECTED_OBJECT_EXCEPTION = new DynamicCommandExceptionType(
arg -> Text.translatable("commands.data.modify.expected_object", arg)
arg -> Text.stringifiedTranslatable("commands.data.modify.expected_object", arg)
);

private static final NbtTextFormatter NBT_FORMATTER = new NbtTextFormatter(" ", 0);

// @formatter:off
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(
Expand Down Expand Up @@ -210,7 +208,7 @@ private static int addRegion(CommandContext<ServerCommandSource> context, NbtCom

private static int renameRegions(CommandContext<ServerCommandSource> context, RegionPredicate predicate) throws CommandSyntaxException {
var source = context.getSource();
var pos = source.getPlayer().getBlockPos();
var pos = source.getPlayerOrThrow().getBlockPos();

var oldMarker = StringArgumentType.getString(context, "old");
var newMarker = StringArgumentType.getString(context, "new");
Expand All @@ -219,7 +217,7 @@ private static int renameRegions(CommandContext<ServerCommandSource> context, Re

var regions = map.getRegions().stream()
.filter(region -> predicate.test(region, oldMarker, pos))
.collect(Collectors.toList());
.toList();

for (var region : regions) {
map.removeRegion(region);
Expand All @@ -239,7 +237,7 @@ private static int getRegionBounds(CommandContext<ServerCommandSource> context)

var regions = map.getRegions().stream()
.filter(region -> region.marker().equals(marker))
.collect(Collectors.toList());
.toList();

source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.bounds.get.header", regions.size()).formatted(Formatting.BOLD), false);

Expand All @@ -255,10 +253,14 @@ private static int getRegionBounds(CommandContext<ServerCommandSource> context)
return Command.SINGLE_SUCCESS;
}

private static Text formatNbt(final NbtElement data) {
return new NbtTextFormatter(" ", 0).apply(data);
}

private static boolean executeRegionDataGet(CommandContext<ServerCommandSource> context, MapWorkspace map, WorkspaceRegion region) {
context.getSource().sendFeedback(() -> {
return withMapPrefix(map,
Text.translatable("text.nucleoid_creator_tools.map.region.data.get", region.marker(), NBT_FORMATTER.apply(region.data()))
Text.translatable("text.nucleoid_creator_tools.map.region.data.get", region.marker(), formatNbt(region.data()))
);
}, false);
return false;
Expand Down Expand Up @@ -295,7 +297,7 @@ private static int removeRegion(CommandContext<ServerCommandSource> context, Blo

var regions = map.getRegions().stream()
.filter(region -> region.bounds().contains(pos))
.collect(Collectors.toList());
.toList();

for (var region : regions) {
map.removeRegion(region);
Expand Down Expand Up @@ -348,9 +350,9 @@ private static int addEntities(CommandContext<ServerCommandSource> context) thro
.count();

if (result == 0) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.add.error", map.getIdentifier()));
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.add.error", Text.of(map.getIdentifier())));
} else {
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.add.success", result, map.getIdentifier()),
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.add.success", result, Text.of(map.getIdentifier())),
false);
}

Expand All @@ -369,9 +371,9 @@ private static int removeEntities(CommandContext<ServerCommandSource> context) t
.count();

if (result == 0) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.remove.error", map.getIdentifier()));
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.remove.error", Text.of(map.getIdentifier())));
} else {
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.remove.success", result, map.getIdentifier()),
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.remove.success", result, Text.of(map.getIdentifier())),
false);
}

Expand All @@ -385,9 +387,9 @@ private static int addEntityType(CommandContext<ServerCommandSource> context) th
var type = getEntityType(context);

if (!map.addEntityType(type.getRight())) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.add.already_present", type.getLeft(), map.getIdentifier()));
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.add.already_present", Text.of(type.getLeft()), Text.of(map.getIdentifier())));
} else {
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.add.success", type.getLeft(), map.getIdentifier()), false);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.add.success", Text.of(type.getLeft()), Text.of(map.getIdentifier())), false);
}
return Command.SINGLE_SUCCESS;
}
Expand All @@ -399,9 +401,9 @@ private static int removeEntityType(CommandContext<ServerCommandSource> context)
var type = getEntityType(context);

if (!map.removeEntityType(type.getRight())) {
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.remove.not_present", type.getLeft(), map.getIdentifier()));
source.sendError(Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.remove.not_present", Text.of(type.getLeft()), Text.of(map.getIdentifier())));
} else {
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.remove.success", type.getLeft(), map.getIdentifier()), false);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.region.entity.filter.type.remove.success", Text.of(type.getLeft()), Text.of(map.getIdentifier())), false);
}
return Command.SINGLE_SUCCESS;
}
Expand Down Expand Up @@ -462,7 +464,7 @@ private static int executeDataGet(CommandContext<ServerCommandSource> context) t
var source = context.getSource();
var map = getWorkspaceForSource(context.getSource());
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.data.get",
getMapPrefix(map), NBT_FORMATTER.apply(map.getData())),
getMapPrefix(map), formatNbt(map.getData())),
false);
return Command.SINGLE_SUCCESS;
}
Expand All @@ -473,8 +475,8 @@ private static int executeDataGetAt(CommandContext<ServerCommandSource> context)
var path = NbtPathArgumentType.getNbtPath(context, "path");
var element = getTagAt(map.getData(), path);
source.sendFeedback(() -> Text.translatable("text.nucleoid_creator_tools.map.data.get.at",
map.getIdentifier().toString(), path.toString(),
NBT_FORMATTER.apply(element)),
Text.of(map.getIdentifier()), path.toString(),
formatNbt(element)),
false);
return Command.SINGLE_SUCCESS;
}
Expand Down Expand Up @@ -550,7 +552,7 @@ private static SuggestionProvider<ServerCommandSource> regionSuggestions() {
return (context, builder) -> {
var map = getWorkspaceForSource(context.getSource());
return CommandSource.suggestMatching(
map.getRegions().stream().map(region -> region.marker()),
map.getRegions().stream().map(WorkspaceRegion::marker),
builder
);
};
Expand All @@ -559,7 +561,7 @@ private static SuggestionProvider<ServerCommandSource> regionSuggestions() {
private static SuggestionProvider<ServerCommandSource> localRegionSuggestions() {
return (context, builder) -> {
var map = getWorkspaceForSource(context.getSource());
var sourcePos = context.getSource().getPlayer().getBlockPos();
var sourcePos = context.getSource().getPlayerOrThrow().getBlockPos();
return CommandSource.suggestMatching(
map.getRegions().stream().filter(region -> region.bounds().contains(sourcePos))
.map(WorkspaceRegion::marker),
Expand All @@ -581,14 +583,14 @@ private static SuggestionProvider<ServerCommandSource> localRegionSuggestions()
private static Command<ServerCommandSource> executeInRegions(String message, RegionExecutor executor) {
return context -> {
var source = context.getSource();
var pos = source.getPlayer().getBlockPos();
var pos = source.getPlayerOrThrow().getBlockPos();

var marker = StringArgumentType.getString(context, "marker");

var map = getWorkspaceForSource(context.getSource());
var regions = map.getRegions().stream()
.filter(region -> region.bounds().contains(pos) && region.marker().equals(marker))
.collect(Collectors.toList());
.toList();

int count = 0;
for (var region : regions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public final class MapWorkspaceArgument {
public static final DynamicCommandExceptionType WORKSPACE_NOT_FOUND = new DynamicCommandExceptionType(arg ->
Text.translatable("text.nucleoid_creator_tools.map_workspace.workspace_not_found", arg)
Text.stringifiedTranslatable("text.nucleoid_creator_tools.map_workspace.workspace_not_found", arg)
);

public static RequiredArgumentBuilder<ServerCommandSource, Identifier> argument(String name) {
Expand Down
Loading

0 comments on commit a60df65

Please sign in to comment.