diff options
| author | inyourwalls <inyourwalls@tutanota.com> | 2024-01-28 19:39:06 -0500 |
|---|---|---|
| committer | inyourwalls <inyourwalls@tutanota.com> | 2024-01-28 19:39:06 -0500 |
| commit | f25401cdcecfe67e484173d68653a90eff446ba6 (patch) | |
| tree | 8183d14a1bf6dc37aaef0c334d7b11e4da9c952e | |
| parent | d61bebc891726fec527deda01e26f784057cb383 (diff) | |
modify rendering again, no longer using array in config
| -rw-r--r-- | build.gradle.kts | 3 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/Frank.java | 131 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/ModMenuIntegration.java | 15 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/config/ConfigScreen.java | 110 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/config/FrankConfig.java | 58 | ||||
| -rw-r--r-- | src/main/resources/assets/frank/lang/en_us.json | 4 | ||||
| -rw-r--r-- | src/main/resources/fabric.mod.json | 7 |
7 files changed, 264 insertions, 64 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index de9662c..032649f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,6 +9,7 @@ group = "net.inyourwalls" repositories { // Repositories to fetch additional dependencies from. + maven("https://maven.terraformersmc.com/") } dependencies { @@ -18,6 +19,8 @@ dependencies { mappings("net.fabricmc:yarn:$minecraftVersion+build.$yarnBuild:v2") modImplementation("net.fabricmc:fabric-loader:0.14.23") modImplementation("net.fabricmc.fabric-api:fabric-api:0.90.4+$minecraftVersion") + + modImplementation("com.terraformersmc:modmenu:9.0.0") } tasks { diff --git a/src/main/java/net/inyourwalls/frank/Frank.java b/src/main/java/net/inyourwalls/frank/Frank.java index 45554f3..e402076 100644 --- a/src/main/java/net/inyourwalls/frank/Frank.java +++ b/src/main/java/net/inyourwalls/frank/Frank.java @@ -2,6 +2,7 @@ package net.inyourwalls.frank; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.registry.FuelRegistry; import net.inyourwalls.frank.config.FrankConfig; @@ -16,17 +17,24 @@ import net.minecraft.text.Text; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.nio.file.Path; + public class Frank implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger(Frank.class); - private static final int CRAFTING_SLOT_INDEX = 0; private static final int FUEL_SLOT_INDEX = 1; + private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("frank.json"); + private static FrankConfig config; @Override public void onInitializeClient() { - FrankConfig config = new FrankConfig(); - config.setTextColour(0xff7aff); - config.setActiveInfoLines(FrankConfig.InfoLine.SMELTABLE, FrankConfig.InfoLine.TIME_LEFT); + try { + config = FrankConfig.load(CONFIG_PATH); + } catch (IOException ex) { + LOGGER.warn("Failed to load configuration file, falling back on default values."); + config = new FrankConfig(); + } ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { if (screen instanceof AbstractFurnaceScreen furnace) { @@ -37,37 +45,81 @@ public class Frank implements ClientModInitializer { int x = screen.width / 2 + 90; // just after the right edge. int y = screen.height / 2 - 80; // more or less in line with the top edge. if (furnace.recipeBook.isOpen()) x += 77; - for (FrankConfig.InfoLine infoLine : config.getActiveInfoLines()) { - switch (infoLine) { - case TIME_LEFT -> { - renderTimeLeft( - drawContext, - client.textRenderer, - x, - y, - config.getTextColour(), - handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), - speedMultiplier, - handler.getCookProgress() - ); - break; - } + switch (config.getDisplayMode()) { + case TIME_LEFT_FIRST -> { + renderTimeLeft( + drawContext, + client.textRenderer, + x, + y, + config.getTextColour(), + handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), + speedMultiplier, + handler.getCookProgress() + ); + renderItemsSmeltable( + drawContext, + client.textRenderer, + x, + y + 10, + config.getTextColour(), + handler.getSlot(FUEL_SLOT_INDEX).getStack(), + handler.getFuelProgress() + ); + break; + } - case SMELTABLE -> { - renderItemsSmeltable( - drawContext, - client.textRenderer, - x, - y, - config.getTextColour(), - handler.getSlot(FUEL_SLOT_INDEX).getStack(), - handler.getFuelProgress() - ); - break; - } + case TIME_LEFT_ONLY -> { + renderTimeLeft( + drawContext, + client.textRenderer, + x, + y, + config.getTextColour(), + handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), + speedMultiplier, + handler.getCookProgress() + ); + break; } - y += 10; + case ITEMS_SMELTABLE_FIRST -> { + renderItemsSmeltable( + drawContext, + client.textRenderer, + x, + y + 10, + config.getTextColour(), + handler.getSlot(FUEL_SLOT_INDEX).getStack(), + handler.getFuelProgress() + ); + renderTimeLeft( + drawContext, + client.textRenderer, + x, + y + 10, + config.getTextColour(), + handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), + speedMultiplier, + handler.getCookProgress() + ); + break; + } + + case ITEMS_SMELTABLE_ONLY -> { + renderItemsSmeltable( + drawContext, + client.textRenderer, + x, + y, + config.getTextColour(), + handler.getSlot(FUEL_SLOT_INDEX).getStack(), + handler.getFuelProgress() + ); + break; + } + + case DISABLED -> {} } }); } @@ -110,5 +162,20 @@ public class Frank implements ClientModInitializer { context.drawTextWithShadow(renderer, Text.translatable("ui.frank.smeltable", itemsSmeltable), x, y, textColour); } + + /** + * Gets the current active configuration. + */ + public static FrankConfig getConfig() { + return config; + } + + /** + * Attempts to save the provided configuration. + */ + public static void saveConfig(FrankConfig configToSave) throws IOException { + config = configToSave; + FrankConfig.save(config, CONFIG_PATH); + } } diff --git a/src/main/java/net/inyourwalls/frank/ModMenuIntegration.java b/src/main/java/net/inyourwalls/frank/ModMenuIntegration.java new file mode 100644 index 0000000..e28fd5f --- /dev/null +++ b/src/main/java/net/inyourwalls/frank/ModMenuIntegration.java @@ -0,0 +1,15 @@ +// ModMenuIntegration: Entrypoint for integrating with Mod Menu. +package net.inyourwalls.frank; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.inyourwalls.frank.config.ConfigScreen; + +public class ModMenuIntegration implements ModMenuApi { + @Override + public ConfigScreenFactory<?> getModConfigScreenFactory() { + return parent -> { + return new ConfigScreen(parent); + }; + } +} diff --git a/src/main/java/net/inyourwalls/frank/config/ConfigScreen.java b/src/main/java/net/inyourwalls/frank/config/ConfigScreen.java new file mode 100644 index 0000000..3217f6e --- /dev/null +++ b/src/main/java/net/inyourwalls/frank/config/ConfigScreen.java @@ -0,0 +1,110 @@ +// ConfigScreen: Frank's configuration screen. +package net.inyourwalls.frank.config; + +import net.inyourwalls.frank.Frank; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +import java.io.IOException; + +import static net.minecraft.client.gui.widget.ButtonWidget.DEFAULT_WIDTH_SMALL; +import static net.minecraft.client.gui.widget.ButtonWidget.DEFAULT_HEIGHT; + +public class ConfigScreen extends Screen { + private static final Identifier FURNACE_UI_TEXTURE = new Identifier("textures/gui/container/furnace.png"); + private static final int FURNACE_TEXTURE_WIDTH = 176; + private static final int FURNACE_TEXTURE_HEIGHT = 166; + private final FrankConfig modifiedConfig; + private final Screen previousScreen; + + public ConfigScreen(Screen previous) { + super(Text.translatable("ui.frank.config")); + this.previousScreen = previous; + this.modifiedConfig = Frank.getConfig(); + } + + @Override + public void init() { + int exitButtonY = this.height / 2 + FURNACE_TEXTURE_HEIGHT + 10; + ButtonWidget backButton = ButtonWidget.builder(Text.translatable("gui.back"), thisButton -> { + this.close(); + }) + .size(DEFAULT_WIDTH_SMALL, DEFAULT_HEIGHT) + .position(this.width / 2 - DEFAULT_WIDTH_SMALL, exitButtonY) + .build(); + + ButtonWidget saveButton = ButtonWidget.builder(Text.translatable("selectWorld.edit.save"), thisButton -> { + try { + Frank.saveConfig(modifiedConfig); + Frank.LOGGER.info("Configuration file was successfully saved."); + } catch (IOException ex) { + Frank.LOGGER.error("Failed to save configuration file", ex); + } + }) + .size(DEFAULT_WIDTH_SMALL, DEFAULT_HEIGHT) + .position(this.width / 2, exitButtonY) + .build(); + + this.addDrawableChild(backButton); + this.addDrawableChild(saveButton); + } + + @Override + public void close() { + this.client.setScreen(this.previousScreen); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + super.render(context, mouseX, mouseY, delta); + context.drawCenteredTextWithShadow(this.textRenderer, Text.translatable("ui.frank.config"), this.width / 2, 5, 0xffffff); + + int x = (this.width / 2) - (FURNACE_TEXTURE_WIDTH / 2); + int y = this.height / 2; + renderPreview(context, x, y); + } + + private void renderPreview(DrawContext context, int x, int y) { + context.drawCenteredTextWithShadow( + this.textRenderer, + Text.translatable("ui.frank.config.preview"), + x + (FURNACE_TEXTURE_WIDTH / 2), + y - 10, + 0xffffff + ); + context.drawTexture(FURNACE_UI_TEXTURE, x, y, 0, 0, FURNACE_TEXTURE_WIDTH, FURNACE_TEXTURE_HEIGHT); + + int infoX = x + FURNACE_TEXTURE_WIDTH + 2; + int infoY = y + 3; + for (FrankConfig.InfoLine infoLine : modifiedConfig.getActiveInfoLines()) { + switch (infoLine) { + case TIME_LEFT -> { + context.drawTextWithShadow( + this.textRenderer, + Text.translatable("ui.frank.time_left", 0, 0), + infoX, + infoY, + modifiedConfig.getTextColour() + ); + break; + } + + case SMELTABLE -> { + context.drawTextWithShadow( + this.textRenderer, + Text.translatable("ui.frank.smeltable", 0), + infoX, + infoY, + modifiedConfig.getTextColour() + ); + break; + } + } + + infoY += 10; + } + } +} diff --git a/src/main/java/net/inyourwalls/frank/config/FrankConfig.java b/src/main/java/net/inyourwalls/frank/config/FrankConfig.java index b17af67..4619bb7 100644 --- a/src/main/java/net/inyourwalls/frank/config/FrankConfig.java +++ b/src/main/java/net/inyourwalls/frank/config/FrankConfig.java @@ -12,23 +12,21 @@ import java.nio.file.Path; public class FrankConfig { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - private InfoLine[] linesToDisplay = {InfoLine.TIME_LEFT, InfoLine.SMELTABLE}; + private InfoDisplayMode infoDisplayMode = InfoDisplayMode.TIME_LEFT_FIRST; private int textColour = 0xffffff; /** - * Gets which info lines should be displayed. The order should be treated as - * the order in which they are displayed. + * Gets the "mode" Frank should display its information in. */ - public InfoLine[] getActiveInfoLines() { - return this.linesToDisplay; + public InfoDisplayMode getDisplayMode() { + return this.infoDisplayMode; } /** - * Sets which info lines should be displayed. The order should be treated as - * the order in which they are displayed. + * Sets the "mode" Frank should display its information in. */ - public void setActiveInfoLines(InfoLine... infoLines) { - this.linesToDisplay = infoLines; + public void setDisplayMode(InfoDisplayMode infoDisplayMode) { + this.infoDisplayMode = infoDisplayMode; } /** @@ -46,31 +44,15 @@ public class FrankConfig { } /** - * An enum that lists the information lines available in Frank. - */ - public static enum InfoLine { - /** - * A variant representing the "time left" line. - */ - TIME_LEFT, - /** - * A variant representing the "items smeltable" line. - */ - SMELTABLE; - - public String translationKey() { - return "ui.frank." + this.toString().toLowerCase(); - } - } - - /** - * Attempts to load the configuration from the given path. + * Attempts to load the configuration from the given path. If the file is + * empty, all values will be set to default. * @param configPath The path to try loading the configuration from. - * @throws IOException If any errors occur while loading the config. + * @throws IOException If any I/O errors occur while loading the config. */ public static FrankConfig load(Path configPath) throws IOException { FileReader reader = new FileReader(configPath.toFile()); - return GSON.fromJson(reader, FrankConfig.class); + FrankConfig loadedConfig = GSON.fromJson(reader, FrankConfig.class); + return loadedConfig == null ? new FrankConfig() : loadedConfig; } /** @@ -82,5 +64,21 @@ public class FrankConfig { public static void save(FrankConfig config, Path configPath) throws IOException { FileWriter writer = new FileWriter(configPath.toFile()); GSON.toJson(config, writer); + writer.close(); + } + + /** + * An enum representing the five ways Frank can display its information. + */ + public static enum InfoDisplayMode { + TIME_LEFT_FIRST, + TIME_LEFT_ONLY, + ITEMS_SMELTABLE_FIRST, + ITEMS_SMELTABLE_ONLY, + DISABLED; + + public String translationKey() { + return "ui.frank.config." + this.toString().toLowerCase(); + } } } diff --git a/src/main/resources/assets/frank/lang/en_us.json b/src/main/resources/assets/frank/lang/en_us.json index ca2862d..bdb1422 100644 --- a/src/main/resources/assets/frank/lang/en_us.json +++ b/src/main/resources/assets/frank/lang/en_us.json @@ -1,5 +1,9 @@ { "ui.frank.time_left": "Time remaining: %sm %ss", "ui.frank.smeltable": "Items smeltable: %s", + + "ui.frank.config": "Configuration - Frank", + "ui.frank.config.preview": "Preview", + "frank.matrix": "Matrix" } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 5c676c5..e02b2b5 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -3,7 +3,7 @@ "id": "frank", "version": "${version}", "name": "Frank", - "description": "Mod that displays additional info about furnaces and fuels.", + "description": "Mod that displays additional info in furnaces.", "authors": [ "InYourWalls" ], @@ -16,11 +16,14 @@ "entrypoints": { "client": [ "net.inyourwalls.frank.Frank" + ], + "modmenu": [ + "net.inyourwalls.frank.ModMenuIntegration" ] }, "depends": { "fabricloader": ">=0.14.22", - "fabric-api": ">=0.90.4+1.20.2", + "fabric-screen-api-v1": ">=0.90.4+1.20.2", "minecraft": "~1.20.2", "java": ">=17" }, |
