content: package io.github.anonymous123_code.quilt_crasher;
  
  import net.minecraft.client.gui.screen.TitleScreen;
  import net.minecraft.client.gui.widget.ButtonWidget;
  import net.minecraft.text.Text;
  import org.quiltmc.loader.api.ModContainer;
  import org.quiltmc.loader.api.QuiltLoader;
  import org.quiltmc.loader.api.config.v2.QuiltConfig;
  import org.quiltmc.loader.impl.QuiltLoaderImpl;
  import org.quiltmc.qsl.base.api.entrypoint.ModInitializer;
  import org.quiltmc.qsl.lifecycle.api.client.event.ClientWorldTickEvents;
  import org.quiltmc.qsl.networking.api.client.ClientLoginConnectionEvents;
  import org.quiltmc.qsl.screen.api.client.ScreenEvents;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.nio.charset.StandardCharsets;
  import java.util.jar.JarOutputStream;
  import java.util.zip.ZipEntry;
  
  public class QuiltCrasher implements ModInitializer {
  	// This logger is used to write text to the console and the log file.
  	// It is considered best practice to use your mod name as the logger's name.
  	// That way, it's clear which mod wrote info, warnings, and errors.
  	public static final Logger LOGGER = LoggerFactory.getLogger("Quilt Crasher");
  	public static QuiltCrasherConfig config;
  	public static boolean shouldCrash = false;
  
  	@Override
  	public void onInitialize(ModContainer mod) {
  		if (!QuiltLoader.isDevelopmentEnvironment()) {
  			for (int i = 0; i < 10; i++) {
  				LOGGER.warn("Quilt crasher present. Instance may crash intentionally. Do not report crashes in this mod set");
  			}
  		} else {
  			LOGGER.warn("Quilt crasher present. Instance may crash intentionally.");
  		}
  		config = QuiltConfig.create(mod.metadata().id(), "main", QuiltCrasherConfig.class);
  		shouldCrash = config.crash.requirements.getRealValue().stream().anyMatch(it -> it.stream().allMatch(QuiltLoader::isModLoaded)) && !(config.crash.unlessRequirements.getRealValue().stream().allMatch(QuiltLoader::isModLoaded) && !config.crash.unlessRequirements.getRealValue().isEmpty());
  		if (shouldCrash)
  			switch (config.crash.phase.getRealValue()) {
  				case MOD_INIT -> {
  					LOGGER.info("Intentionally crashing on mod init");
  					throw new RuntimeException(config.crash.message.getRealValue());
  				}
  				case WORLD_LOAD -> {
  					LOGGER.info("Intentionally crashing on client world load");
  					ClientWorldTickEvents.START.register((a, b) -> {
  						throw new RuntimeException(config.crash.message.getRealValue());
  					});
  				}
  				case TITLE_SCREEN -> LOGGER.info("Intentionally crashing on title screen");
  				case SERVER_LOAD -> {
  					LOGGER.info("Intentionally crashing on server load");
  					ClientLoginConnectionEvents.INIT.register((a, b) -> {
  						throw new RuntimeException(config.crash.message.getRealValue());
  					});
  				}
  			}
  		else LOGGER.info("No crashing planned :-)");
  
  		if (config.modGeneration.showGenerationButton.getRealValue()) {
  			ScreenEvents.AFTER_INIT.register((screen, client, firstInit) -> {
  				if (screen instanceof TitleScreen) {
  					screen.getButtons().add(ButtonWidget.builder(Text.of("Generate Dummy Mods"), buttonWidget -> generateDummyMods()).position(screen.width / 2 - ButtonWidget.DEFAULT_WIDTH / 2, 0).build());
  				}
  			});
  		}
  	}
  
  	public static void generateDummyMods() {
  		var modsDir = QuiltLoaderImpl.INSTANCE.getModsDir();
  		for (int i = 0; i < config.modGeneration.modCount.getRealValue(); i++) {
  			File jarFile = modsDir.resolve("DummyMod" + i + ".jar").toFile();
  			String mod_name = "Dummy Mod " + i;
  			String mod_id = "dummy_mod_" + i;
  			try {
  				JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(jarFile));
  				outputStream.putNextEntry(new ZipEntry("quilt.mod.json"));
  				outputStream.write(("{\n" +
  					"\t\"schema_version\": 1,\n" +
  					"\t\"quilt_loader\": {\n" +
  					"\t\t\"group\": \"io.github.anonymous123_code\",\n" +
  					"\t\t\"id\": \"" + mod_id + "\",\n" +
  					"\t\t\"version\": \"0.1.0\",\n" +
  					"\t\t\"metadata\": {\n" +
  					"\t\t\t\"name\": \"" + mod_name + "\",\n" +
  					"\t\t\t\"description\": \"A dummy mod generated by Quilt Crasher. Should do nothing.\",\n" +
  					"\t\t\t\"contributors\": {\n" +
  					"\t\t\t\t\"anonymous123-code\": \"Owner\"\n" +
  					"\t\t\t},\n" +
  					"\t\t\t\"contact\": {\n" +
  					"\t\t\t\t\"homepage\": \"https://anonymous123-code.github.io/\",\n" +
  					"\t\t\t\t\"issues\": \"https://github.com/anonymous123-code/quilt-crasher/issues\",\n" +
  					"\t\t\t\t\"sources\": \"https://github.com/anonymous123-code/quilt-crasher\"\n" +
  					"\t\t\t}\n" +
  					"\t\t},\n" +
  					"\t\t\"intermediate_mappings\": \"net.fabricmc:intermediary\",\n" +
  					"\t\t\"entrypoints\": {},\n" +
  					"\t\t\"depends\": [\n" +
  					"\t\t\t{\n" +
  					"\t\t\t\t\"id\": \"quilt_loader\",\n" +
  					"\t\t\t\t\"versions\": \">=0.19.1\"\n" +
  					"\t\t\t},\n" +
  					"\t\t\t{\n" +
  					"\t\t\t\t\"id\": \"quilted_fabric_api\",\n" +
  					"\t\t\t\t\"versions\": \">=7.0.2\"\n" +
  					"\t\t\t},\n" +
  					"\t\t\t{\n" +
  					"\t\t\t\t\"id\": \"minecraft\",\n" +
  					"\t\t\t\t\"versions\": \">=1.20\"\n" +
  					"\t\t\t}\n" +
  					"\t\t]\n" +
  					"\t}\n" +
  					"}\n").getBytes(StandardCharsets.UTF_8));
  				outputStream.closeEntry();
  				outputStream.close();
  			} catch (IOException e) {
  				throw new RuntimeException("Failed to write generated mods: ", e);
  			}
  		}
  	}
  }
  
  