|
| 1 | +package dev.xpple.seedmapper.command.commands; |
| 2 | + |
| 3 | +import com.github.cubiomes.Cubiomes; |
| 4 | +import com.mojang.brigadier.Command; |
| 5 | +import com.mojang.brigadier.StringReader; |
| 6 | +import com.mojang.brigadier.arguments.BoolArgumentType; |
| 7 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 8 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 9 | +import com.mojang.brigadier.context.CommandContext; |
| 10 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 11 | +import dev.xpple.seedmapper.command.CustomClientCommandSource; |
| 12 | +import dev.xpple.seedmapper.config.Configs; |
| 13 | +import dev.xpple.seedmapper.datapack.DatapackStructureManager; |
| 14 | +import dev.xpple.seedmapper.util.SeedIdentifier; |
| 15 | +import dev.xpple.seedmapper.util.WorldIdentifier; |
| 16 | +import dev.xpple.seedmapper.command.arguments.DimensionArgument; |
| 17 | +import dev.xpple.seedmapper.seedmap.SeedMapScreen; |
| 18 | +import dev.xpple.seedmapper.seedmap.SeedMapMinimapManager; |
| 19 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 20 | +import net.minecraft.client.Minecraft; |
| 21 | +import net.minecraft.network.chat.Component; |
| 22 | +import net.minecraft.server.permissions.PermissionSet; |
| 23 | + |
| 24 | +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; |
| 25 | +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; |
| 26 | + |
| 27 | +public class DatapackImportCommand { |
| 28 | + private static final String COMMAND = "sm:datapack"; |
| 29 | + |
| 30 | + public static void register(com.mojang.brigadier.CommandDispatcher<FabricClientCommandSource> dispatcher) { |
| 31 | + dispatcher.register(literal(COMMAND) |
| 32 | + .then(literal("import") |
| 33 | + .then(argument("url", StringArgumentType.greedyString()) |
| 34 | + .executes(DatapackImportCommand::execute))) |
| 35 | + .then(literal("autoload") |
| 36 | + .then(argument("enabled", BoolArgumentType.bool()) |
| 37 | + .executes(DatapackImportCommand::toggleAutoload))) |
| 38 | + .then(literal("save") |
| 39 | + .executes(DatapackImportCommand::save)) |
| 40 | + .then(literal("load") |
| 41 | + .executes(DatapackImportCommand::load)) |
| 42 | + .then(literal("read") |
| 43 | + .executes(DatapackImportCommand::read)) |
| 44 | + .then(literal("colorscheme") |
| 45 | + .then(argument("scheme", IntegerArgumentType.integer(1, 3)) |
| 46 | + .executes(DatapackImportCommand::setColorScheme)))); |
| 47 | + } |
| 48 | + |
| 49 | + @SuppressWarnings("unused") |
| 50 | + private static int execute(CommandContext<FabricClientCommandSource> context) { |
| 51 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 52 | + String url = StringArgumentType.getString(context, "url").trim(); |
| 53 | + return importUrl(source, url); |
| 54 | + } |
| 55 | + |
| 56 | + private static int toggleAutoload(CommandContext<FabricClientCommandSource> context) { |
| 57 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 58 | + boolean enabled = BoolArgumentType.getBool(context, "enabled"); |
| 59 | + Configs.DatapackAutoload = enabled; |
| 60 | + Configs.save(); |
| 61 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.autoloadSet", enabled)); |
| 62 | + return Command.SINGLE_SUCCESS; |
| 63 | + } |
| 64 | + |
| 65 | + private static int save(CommandContext<FabricClientCommandSource> context) { |
| 66 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 67 | + String url = DatapackStructureManager.getLastImportedUrl(); |
| 68 | + if (url == null || url.isBlank()) { |
| 69 | + source.sendError(Component.translatable("seedMap.datapackImport.save.noImported")); |
| 70 | + return Command.SINGLE_SUCCESS; |
| 71 | + } |
| 72 | + java.nio.file.Path cachePath = DatapackStructureManager.getLastImportedCachePath(); |
| 73 | + if (!Configs.saveDatapackForCurrentServer(url, cachePath)) { |
| 74 | + source.sendError(Component.translatable("seedMap.datapackImport.save.noServer")); |
| 75 | + return Command.SINGLE_SUCCESS; |
| 76 | + } |
| 77 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.save.success")); |
| 78 | + return Command.SINGLE_SUCCESS; |
| 79 | + } |
| 80 | + |
| 81 | + private static int load(CommandContext<FabricClientCommandSource> context) { |
| 82 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 83 | + String url = Configs.getSavedDatapackUrlForCurrentServer(); |
| 84 | + java.nio.file.Path cachePath = Configs.getSavedDatapackCachePathForCurrentServer(); |
| 85 | + if ((url == null || url.isBlank()) && cachePath == null) { |
| 86 | + source.sendError(Component.translatable("seedMap.datapackImport.load.none")); |
| 87 | + return Command.SINGLE_SUCCESS; |
| 88 | + } |
| 89 | + return importSavedWithFallback(source, url, cachePath); |
| 90 | + } |
| 91 | + |
| 92 | + private static int read(CommandContext<FabricClientCommandSource> context) { |
| 93 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 94 | + String url = DatapackStructureManager.getLastImportedUrl(); |
| 95 | + if (url == null || url.isBlank()) { |
| 96 | + url = Configs.getSavedDatapackUrlForCurrentServer(); |
| 97 | + } |
| 98 | + if (url == null || url.isBlank()) { |
| 99 | + source.sendError(Component.translatable("seedMap.datapackImport.read.none")); |
| 100 | + return Command.SINGLE_SUCCESS; |
| 101 | + } |
| 102 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.read.current", url)); |
| 103 | + return Command.SINGLE_SUCCESS; |
| 104 | + } |
| 105 | + |
| 106 | + private static int setColorScheme(CommandContext<FabricClientCommandSource> context) { |
| 107 | + CustomClientCommandSource source = CustomClientCommandSource.of(context.getSource()); |
| 108 | + int scheme = IntegerArgumentType.getInteger(context, "scheme"); |
| 109 | + Configs.DatapackColorScheme = scheme; |
| 110 | + Configs.save(); |
| 111 | + DatapackStructureManager.clearColorSchemeCache(); |
| 112 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.colorschemeSet", scheme)); |
| 113 | + try { |
| 114 | + int generatorFlags = source.getGeneratorFlags(); |
| 115 | + SeedMapScreen.reopenIfOpen(generatorFlags); |
| 116 | + SeedMapMinimapManager.refreshIfOpenWithGeneratorFlags(generatorFlags); |
| 117 | + } catch (CommandSyntaxException ignored) { |
| 118 | + SeedMapScreen.reopenIfOpen(0); |
| 119 | + SeedMapMinimapManager.refreshIfOpenWithGeneratorFlags(0); |
| 120 | + } |
| 121 | + return Command.SINGLE_SUCCESS; |
| 122 | + } |
| 123 | + |
| 124 | + public static int importUrl(CustomClientCommandSource source, String url) { |
| 125 | + if (url.isBlank()) { |
| 126 | + source.sendError(Component.translatable("seedMap.datapackImport.status.invalidUrl")); |
| 127 | + return Command.SINGLE_SUCCESS; |
| 128 | + } |
| 129 | + |
| 130 | + SeedIdentifier seed = Configs.Seed; |
| 131 | + if (seed == null) { |
| 132 | + source.sendError(Component.translatable("seedMap.datapackImport.noSeed")); |
| 133 | + return Command.SINGLE_SUCCESS; |
| 134 | + } |
| 135 | + |
| 136 | + int dimension; |
| 137 | + try { |
| 138 | + String dimensionPath = source.getWorld().dimension().identifier().getPath(); |
| 139 | + dimension = DimensionArgument.dimension().parse(new StringReader(dimensionPath)); |
| 140 | + } catch (CommandSyntaxException e) { |
| 141 | + source.sendError(Component.translatable("seedMap.datapackImport.dimensionError")); |
| 142 | + return Command.SINGLE_SUCCESS; |
| 143 | + } |
| 144 | + |
| 145 | + WorldIdentifier identifier = new WorldIdentifier(seed.seed(), dimension, seed.version(), seed.generatorFlags()); |
| 146 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.started")); |
| 147 | + DatapackStructureManager.importDatapack(identifier, url, source::sendFeedback, source::sendError); |
| 148 | + return Command.SINGLE_SUCCESS; |
| 149 | + } |
| 150 | + |
| 151 | + private static int importSavedWithFallback(CustomClientCommandSource source, String url, java.nio.file.Path cachePath) { |
| 152 | + SeedIdentifier seed = Configs.Seed; |
| 153 | + if (seed == null) { |
| 154 | + source.sendError(Component.translatable("seedMap.datapackImport.noSeed")); |
| 155 | + return Command.SINGLE_SUCCESS; |
| 156 | + } |
| 157 | + |
| 158 | + int dimension; |
| 159 | + try { |
| 160 | + String dimensionPath = source.getWorld().dimension().identifier().getPath(); |
| 161 | + dimension = DimensionArgument.dimension().parse(new StringReader(dimensionPath)); |
| 162 | + } catch (CommandSyntaxException e) { |
| 163 | + source.sendError(Component.translatable("seedMap.datapackImport.dimensionError")); |
| 164 | + return Command.SINGLE_SUCCESS; |
| 165 | + } |
| 166 | + |
| 167 | + WorldIdentifier identifier = new WorldIdentifier(seed.seed(), dimension, seed.version(), seed.generatorFlags()); |
| 168 | + source.sendFeedback(Component.translatable("seedMap.datapackImport.started")); |
| 169 | + DatapackStructureManager.importDatapackWithFallback(identifier, cachePath, url, source::sendFeedback, source::sendError); |
| 170 | + return Command.SINGLE_SUCCESS; |
| 171 | + } |
| 172 | + |
| 173 | + public static void importUrlForCurrentServer(String url) { |
| 174 | + Minecraft minecraft = Minecraft.getInstance(); |
| 175 | + if (minecraft == null || minecraft.player == null || minecraft.level == null) { |
| 176 | + return; |
| 177 | + } |
| 178 | + CustomClientCommandSource source = new CustomClientCommandSource( |
| 179 | + minecraft.getConnection(), |
| 180 | + minecraft, |
| 181 | + minecraft.player, |
| 182 | + minecraft.player.position(), |
| 183 | + minecraft.player.getRotationVector(), |
| 184 | + minecraft.level, |
| 185 | + PermissionSet.NO_PERMISSIONS, |
| 186 | + new java.util.HashMap<>() |
| 187 | + ); |
| 188 | + java.nio.file.Path cachePath = Configs.getSavedDatapackCachePathForCurrentServer(); |
| 189 | + importSavedWithFallback(source, url, cachePath); |
| 190 | + } |
| 191 | +} |
0 commit comments