diff options
| author | inyourwalls <inyourwalls@tutanota.com> | 2024-01-26 23:31:48 -0500 |
|---|---|---|
| committer | inyourwalls <inyourwalls@tutanota.com> | 2024-01-26 23:31:48 -0500 |
| commit | 9590d69e89c2d966fe80501ce4f754da85bc1ac5 (patch) | |
| tree | d1d16a6dd3f96d7d76eb3f4df5804412cbd6b9ac | |
| parent | c14a63c8d0ce0e95bb072028026f59138f31f5d8 (diff) | |
reformat, initial config test
| -rw-r--r-- | build.gradle.kts | 2 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/Frank.java | 128 | ||||
| -rw-r--r-- | src/main/java/net/inyourwalls/frank/config/FrankConfig.java | 86 |
3 files changed, 157 insertions, 59 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index b97f98a..de9662c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } val minecraftVersion = "1.20.2" -version = "0.1.1+$minecraftVersion" +version = "1.0.0+$minecraftVersion" group = "net.inyourwalls" repositories { diff --git a/src/main/java/net/inyourwalls/frank/Frank.java b/src/main/java/net/inyourwalls/frank/Frank.java index 4b8fc08..7583978 100644 --- a/src/main/java/net/inyourwalls/frank/Frank.java +++ b/src/main/java/net/inyourwalls/frank/Frank.java @@ -4,6 +4,7 @@ package net.inyourwalls.frank; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.registry.FuelRegistry; +import net.inyourwalls.frank.config.FrankConfig; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.ingame.AbstractFurnaceScreen; @@ -16,69 +17,80 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Frank implements ClientModInitializer { - public static final Logger LOGGER = LoggerFactory.getLogger(Frank.class); + 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 int WHITE = 0xffffff; + private static final int CRAFTING_SLOT_INDEX = 0; + private static final int FUEL_SLOT_INDEX = 1; - @Override - public void onInitializeClient() { - ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { - if (screen instanceof AbstractFurnaceScreen furnace) { - ScreenEvents.afterRender(screen).register((_s, drawContext, mouseX, mouseY, tickDelta) -> { - AbstractFurnaceScreenHandler handler = (AbstractFurnaceScreenHandler) furnace.getScreenHandler(); - int speedMultiplier = (handler instanceof BlastFurnaceScreenHandler || handler instanceof SmokerScreenHandler) ? 2 : 1; - renderInfoLines( - furnace, - drawContext, - client.textRenderer, - handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), - handler.getCookProgress(), - handler.getSlot(FUEL_SLOT_INDEX).getStack(), - handler.getFuelProgress(), - speedMultiplier - ); - }); - } - }); - LOGGER.info("Screen events registered."); - } + @Override + public void onInitializeClient() { + FrankConfig config = new FrankConfig(); + + ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { + if (screen instanceof AbstractFurnaceScreen furnace) { + AbstractFurnaceScreenHandler handler = (AbstractFurnaceScreenHandler) furnace.getScreenHandler(); + + // Add our text lines. + for (FrankConfig.InfoLine infoLine : config.getActiveInfoLines()) { + + } + + int speedMultiplier = (handler instanceof BlastFurnaceScreenHandler || handler instanceof SmokerScreenHandler) ? 2 : 1; + ScreenEvents.afterRender(screen).register((_s, drawContext, mouseX, mouseY, tickDelta) -> { + renderInfoLines( + furnace, + drawContext, + client.textRenderer, + handler.getSlot(CRAFTING_SLOT_INDEX).getStack().getCount(), + handler.getCookProgress(), + handler.getSlot(FUEL_SLOT_INDEX).getStack(), + handler.getFuelProgress(), + speedMultiplier, + config.getTextColour() + ); + }); - /** - * Renders the info lines on the given screen. - */ - private void renderInfoLines( - AbstractFurnaceScreen<?> screen, - DrawContext context, - TextRenderer renderer, - int itemsInProgress, - float cookProgress, - ItemStack fuel, - float fuelProgress, - int speedMultiplier - ) { - // Determine where to start drawing the information. - int infoX = screen.width / 2 + 90; - int infoY = screen.height / 2 - 80; - if (screen.recipeBook.isOpen()) { - infoX += 77; + ScreenEvents.afterTick(screen).register(_s -> {}); + } + }); + LOGGER.info("Screen events registered."); } - - int smeltingSpeed = 10 / speedMultiplier; // this code sucks, but i don't care. - int secondsLeft = itemsInProgress * smeltingSpeed - (int)(cookProgress * smeltingSpeed); - int minutesLeft = secondsLeft / 60; - secondsLeft = secondsLeft % 60; - int fuelCount = fuel.getCount(); - Integer fuelTicks = FuelRegistry.INSTANCE.get(fuel.getItem()); - // Avoid null pointer exception because Java is terrible. - if (fuelTicks == null) fuelTicks = 0; - int burningFuel = (int)((float)(fuelTicks / 10) * fuelProgress); - int itemsSmeltable = (((fuelTicks / 10) * fuelCount) / 20) + (burningFuel / 20); + /** + * Renders the info lines on the given screen. + */ + private void renderInfoLines( + AbstractFurnaceScreen<?> screen, + DrawContext context, + TextRenderer renderer, + int itemsInProgress, + float cookProgress, + ItemStack fuel, + float fuelProgress, + int speedMultiplier, + int textColour + ) { + // Determine where to start drawing the information. + int infoX = screen.width / 2 + 90; + int infoY = screen.height / 2 - 80; + if (screen.recipeBook.isOpen()) { + infoX += 77; + } + + int smeltingSpeed = 10 / speedMultiplier; // this code sucks, but i don't care. + int secondsLeft = itemsInProgress * smeltingSpeed - (int)(cookProgress * smeltingSpeed); + int minutesLeft = secondsLeft / 60; + secondsLeft = secondsLeft % 60; - context.drawTextWithShadow(renderer, Text.translatable("ui.frank.time_left", minutesLeft, secondsLeft), infoX, infoY, WHITE); - context.drawTextWithShadow(renderer, Text.translatable("ui.frank.smeltable", itemsSmeltable), infoX, infoY + 10, WHITE); - } + int fuelCount = fuel.getCount(); + Integer fuelTicks = FuelRegistry.INSTANCE.get(fuel.getItem()); + // Avoid null pointer exception because Java is terrible. + if (fuelTicks == null) fuelTicks = 0; + int burningFuel = (int)((float)(fuelTicks / 10) * fuelProgress); + int itemsSmeltable = (((fuelTicks / 10) * fuelCount) / 20) + (burningFuel / 20); + + context.drawTextWithShadow(renderer, Text.translatable("ui.frank.time_left", minutesLeft, secondsLeft), infoX, infoY, textColour); + context.drawTextWithShadow(renderer, Text.translatable("ui.frank.smeltable", itemsSmeltable), infoX, infoY + 10, textColour); + } } diff --git a/src/main/java/net/inyourwalls/frank/config/FrankConfig.java b/src/main/java/net/inyourwalls/frank/config/FrankConfig.java new file mode 100644 index 0000000..b17af67 --- /dev/null +++ b/src/main/java/net/inyourwalls/frank/config/FrankConfig.java @@ -0,0 +1,86 @@ +// FrankConfig: Config class for Frank. +package net.inyourwalls.frank.config; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +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 int textColour = 0xffffff; + + /** + * Gets which info lines should be displayed. The order should be treated as + * the order in which they are displayed. + */ + public InfoLine[] getActiveInfoLines() { + return this.linesToDisplay; + } + + /** + * Sets which info lines should be displayed. The order should be treated as + * the order in which they are displayed. + */ + public void setActiveInfoLines(InfoLine... infoLines) { + this.linesToDisplay = infoLines; + } + + /** + * Gets the current text colour. + */ + public int getTextColour() { + return this.textColour; + } + + /** + * Sets a new text colour. + */ + public void setTextColour(int textColour) { + this.textColour = textColour; + } + + /** + * 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. + * @param configPath The path to try loading the configuration from. + * @throws IOException If any 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); + } + + /** + * Attempts to save the given configuration to the given path. + * @param config The configuration to save. + * @param configPath The path to try saving the configuration to. + * @throws IOException If any errors occur while saving the config. + */ + public static void save(FrankConfig config, Path configPath) throws IOException { + FileWriter writer = new FileWriter(configPath.toFile()); + GSON.toJson(config, writer); + } +} |
