From 2ac73568884f03d340f13f44552cfe9d5bdce82a Mon Sep 17 00:00:00 2001 From: dags- Date: Fri, 26 Jun 2020 17:31:58 +0100 Subject: [PATCH] more translation keys + generator defaults --- .../assets/terraforged/lang/en_us.json | 20 +++++++ .../chunk/settings/SettingsHelper.java | 37 +++--------- .../com/terraforged/data/LangGenerator.java | 10 ++++ .../java/com/terraforged/gui/GuiKeys.java | 34 +++++++++++ .../com/terraforged/gui/SettingsScreen.java | 20 +++---- .../com/terraforged/gui/page/PresetsPage.java | 33 +++++++++-- .../com/terraforged/gui/page/SimplePage.java | 5 +- .../gui/page/SimplePreviewPage.java | 3 +- .../com/terraforged/gui/page/WorldPage.java | 3 +- .../com/terraforged/gui/preview/Preview.java | 8 +-- .../terraforged/gui/preview/PreviewPage.java | 3 +- .../com/terraforged/util/TranslationKey.java | 59 +++++++++++++++++++ 12 files changed, 182 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/terraforged/gui/GuiKeys.java create mode 100644 src/main/java/com/terraforged/util/TranslationKey.java diff --git a/generated/resources/assets/terraforged/lang/en_us.json b/generated/resources/assets/terraforged/lang/en_us.json index 03853f1..dc49c69 100644 --- a/generated/resources/assets/terraforged/lang/en_us.json +++ b/generated/resources/assets/terraforged/lang/en_us.json @@ -39,6 +39,7 @@ "display.terraforged.climate.temperature.max": "Max", "display.terraforged.climate.temperature.min": "Min", "display.terraforged.climate.temperature.scale": "Scale", + "display.terraforged.climate.title": "Climate Settings", "display.terraforged.dimensions": "Dimensions", "display.terraforged.dimensions.bedrock_layer": "Bedrock Layer", "display.terraforged.dimensions.bedrock_layer.material": "Material", @@ -59,6 +60,9 @@ "display.terraforged.filters.smoothing.iterations": "Iterations", "display.terraforged.filters.smoothing.smoothing_radius": "Smoothing Radius", "display.terraforged.filters.smoothing.smoothing_rate": "Smoothing Rate", + "display.terraforged.filters.title": "Filter Settings", + "display.terraforged.general.cancel": "Cancel", + "display.terraforged.general.done": "Done", "display.terraforged.miscellaneous": "Miscellaneous", "display.terraforged.miscellaneous.custom_biome_features": "Custom Biome Features", "display.terraforged.miscellaneous.erosion_decorator": "Erosion Decorator", @@ -66,10 +70,23 @@ "display.terraforged.miscellaneous.natural_snow_decorator": "Natural Snow Decorator", "display.terraforged.miscellaneous.smooth_layer_decorator": "Smooth Layer Decorator", "display.terraforged.miscellaneous.strata_decorator": "Strata Decorator", + "display.terraforged.miscellaneous.title": "Miscellaneous Settings", "display.terraforged.miscellaneous.vanilla_water_features": "Vanilla Water Features", + "display.terraforged.preset.create": "Create", + "display.terraforged.preset.default": "Make Default", + "display.terraforged.preset.delete": "Delete", + "display.terraforged.preset.load": "Load", + "display.terraforged.preset.reset": "Reset", + "display.terraforged.preset.save": "Save", + "display.terraforged.presets.title": "Presets & Defaults", "display.terraforged.preview": "Preview", + "display.terraforged.preview.area": "Area: ", + "display.terraforged.preview.biome": "Biome: ", "display.terraforged.preview.display": "Display", + "display.terraforged.preview.seed": "New Seed", + "display.terraforged.preview.terrain": "Terrain: ", "display.terraforged.preview.zoom": "Zoom", + "display.terraforged.river.title": "River Settings", "display.terraforged.rivers": "Rivers", "display.terraforged.rivers.primary_rivers": "Primary Rivers", "display.terraforged.rivers.primary_rivers.bank_width": "Bank Width", @@ -112,6 +129,7 @@ "display.terraforged.structures.strongholds": "Strongholds", "display.terraforged.structures.strongholds.distance": "Distance", "display.terraforged.structures.strongholds.separation": "Separation", + "display.terraforged.structures.title": "Structure Settings", "display.terraforged.structures.villages": "Villages", "display.terraforged.structures.villages.distance": "Distance", "display.terraforged.structures.villages.separation": "Separation", @@ -155,6 +173,7 @@ "display.terraforged.terrain.steppe.horizontal_scale": "Horizontal Scale", "display.terraforged.terrain.steppe.vertical_scale": "Vertical Scale", "display.terraforged.terrain.steppe.weight": "Weight", + "display.terraforged.terrain.title": "Terrain Settings", "display.terraforged.terrain.torridonian": "Torridonian", "display.terraforged.terrain.torridonian.base_scale": "Base Scale", "display.terraforged.terrain.torridonian.horizontal_scale": "Horizontal Scale", @@ -175,6 +194,7 @@ "display.terraforged.world.properties.sea_level": "Sea Level", "display.terraforged.world.properties.spawn_type": "Spawn Type", "display.terraforged.world.properties.world_height": "World Height", + "display.terraforged.world.title": "World Settings", "display.terraforged.world.transition_points": "Transition Points", "display.terraforged.world.transition_points.beach": "Beach", "display.terraforged.world.transition_points.coast": "Coast", diff --git a/src/main/java/com/terraforged/chunk/settings/SettingsHelper.java b/src/main/java/com/terraforged/chunk/settings/SettingsHelper.java index be99dfb..d782c3f 100644 --- a/src/main/java/com/terraforged/chunk/settings/SettingsHelper.java +++ b/src/main/java/com/terraforged/chunk/settings/SettingsHelper.java @@ -8,8 +8,6 @@ import com.terraforged.Log; import com.terraforged.util.nbt.NBTHelper; import net.minecraft.nbt.CompoundNBT; import net.minecraft.world.storage.WorldInfo; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.fml.DistExecutor; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -19,17 +17,16 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; -import java.nio.file.Files; public class SettingsHelper { - public static final String SETTINGS_FILE_NAME = "terraforged-generator.json"; public static final File SETTINGS_DIR = new File("config", "terraforged"); - public static final File SETTINGS_FILE= new File(SETTINGS_DIR, SETTINGS_FILE_NAME); + public static final File DEFAULTS_FILE = new File(SETTINGS_DIR, "generator-defaults.json"); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public static void clearDefaults() { - if (SETTINGS_FILE.exists() && SETTINGS_FILE.delete()) { + if (DEFAULTS_FILE.exists() && DEFAULTS_FILE.delete()) { Log.info("Deleted generator defaults"); } } @@ -37,7 +34,7 @@ public class SettingsHelper { public static void exportDefaults(TerraSettings settings) { CompoundNBT tag = NBTHelper.serializeCompact(settings); JsonElement json = NBTHelper.toJson(tag); - try (Writer writer = new BufferedWriter(new FileWriter(SETTINGS_FILE))) { + try (Writer writer = new BufferedWriter(new FileWriter(DEFAULTS_FILE))) { GSON.toJson(json, writer); } catch (IOException e) { e.printStackTrace(); @@ -46,7 +43,7 @@ public class SettingsHelper { public static CompoundNBT applyDefaults(CompoundNBT options, TerraSettings dest) { if (options.isEmpty()) { - try (Reader reader = new BufferedReader(new FileReader(SETTINGS_FILE))) { + try (Reader reader = new BufferedReader(new FileReader(DEFAULTS_FILE))) { JsonElement json = new JsonParser().parse(reader); options = NBTHelper.fromJson(json); } catch (IOException ignored) { @@ -60,8 +57,8 @@ public class SettingsHelper { public static TerraSettings getSettings(WorldInfo info) { TerraSettings settings = new TerraSettings(); if (info.getGeneratorOptions().isEmpty()) { - if (SETTINGS_FILE.exists()) { - try (Reader reader = new BufferedReader(new FileReader(SETTINGS_FILE))) { + if (DEFAULTS_FILE.exists()) { + try (Reader reader = new BufferedReader(new FileReader(DEFAULTS_FILE))) { Log.info("Loading generator settings from json"); JsonElement json = new JsonParser().parse(reader); CompoundNBT root = NBTHelper.fromJson(json); @@ -78,24 +75,8 @@ public class SettingsHelper { } public static void init() { - if (SETTINGS_FILE.exists()) { - return; - } - - File src = new File("config", SETTINGS_FILE_NAME); - if (src.exists()) { - if (SETTINGS_DIR.exists() || SETTINGS_DIR.mkdirs()) { - try { - Files.copy(src.toPath(), SETTINGS_FILE.toPath()); - if (src.delete()) { - Log.info("Moved settings file to new location: {}", SETTINGS_FILE.getAbsoluteFile()); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } else { - DistExecutor.runWhenOn(Dist.DEDICATED_SERVER, () -> () -> exportDefaults(new TerraSettings())); + if (!DEFAULTS_FILE.exists()) { + exportDefaults(new TerraSettings()); } } } diff --git a/src/main/java/com/terraforged/data/LangGenerator.java b/src/main/java/com/terraforged/data/LangGenerator.java index 5fe97ad..0869c11 100644 --- a/src/main/java/com/terraforged/data/LangGenerator.java +++ b/src/main/java/com/terraforged/data/LangGenerator.java @@ -2,7 +2,9 @@ package com.terraforged.data; import com.terraforged.chunk.settings.TerraSettings; import com.terraforged.core.util.NameUtil; +import com.terraforged.gui.GuiKeys; import com.terraforged.gui.preview2.PreviewSettings; +import com.terraforged.util.TranslationKey; import com.terraforged.util.nbt.NBTHelper; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; @@ -21,14 +23,18 @@ public class LangGenerator { @SubscribeEvent public static void gather(GatherDataEvent event) { + GuiKeys.init(); + LanguageProvider langProvider = new LanguageProvider(event.getGenerator(), "terraforged", "en_us") { @Override protected void addTranslations() { worlds(this); biomes(this); + translationKeys(this); settings(this); } }; + event.getGenerator().addProvider(langProvider); } @@ -46,6 +52,10 @@ public class LangGenerator { } } + private static void translationKeys(LanguageProvider provider) { + TranslationKey.each(key -> provider.add(key.getKey(), key.getDefaultValue())); + } + private static void settings(LanguageProvider provider) { visit(NBTHelper.serialize(new DataGenSettings()), provider); } diff --git a/src/main/java/com/terraforged/gui/GuiKeys.java b/src/main/java/com/terraforged/gui/GuiKeys.java new file mode 100644 index 0000000..5803656 --- /dev/null +++ b/src/main/java/com/terraforged/gui/GuiKeys.java @@ -0,0 +1,34 @@ +package com.terraforged.gui; + +import com.terraforged.util.TranslationKey; + +public class GuiKeys { + + public static final TranslationKey DONE = TranslationKey.gui("general.done", "Done"); + public static final TranslationKey CANCEL = TranslationKey.gui("general.cancel", "Cancel"); + + public static final TranslationKey PREVIEW_SEED = TranslationKey.gui("preview.seed", "New Seed"); + public static final TranslationKey PREVIEW_AREA = TranslationKey.gui("preview.area", "Area: "); + public static final TranslationKey PREVIEW_TERRAIN = TranslationKey.gui("preview.terrain", "Terrain: "); + public static final TranslationKey PREVIEW_BIOME = TranslationKey.gui("preview.biome", "Biome: "); + + public static final TranslationKey PRESETS = TranslationKey.gui("presets.title", "Presets & Defaults"); + public static final TranslationKey WORLD_SETTINGS = TranslationKey.gui("world.title", "World Settings"); + public static final TranslationKey CLIMATE_SETTINGS = TranslationKey.gui("climate.title", "Climate Settings"); + public static final TranslationKey TERRAIN_SETTINGS = TranslationKey.gui("terrain.title", "Terrain Settings"); + public static final TranslationKey RIVER_SETTINGS = TranslationKey.gui("river.title", "River Settings"); + public static final TranslationKey FILTER_SETTINGS = TranslationKey.gui("filters.title", "Filter Settings"); + public static final TranslationKey STRUCTURE_SETTINGS = TranslationKey.gui("structures.title", "Structure Settings"); + public static final TranslationKey MISC_SETTINGS = TranslationKey.gui("miscellaneous.title", "Miscellaneous Settings"); + + public static final TranslationKey PRESET_CREATE = TranslationKey.gui("preset.create", "Create"); + public static final TranslationKey PRESET_LOAD = TranslationKey.gui("preset.load", "Load"); + public static final TranslationKey PRESET_SAVE = TranslationKey.gui("preset.save", "Save"); + public static final TranslationKey PRESET_RESET = TranslationKey.gui("preset.reset", "Reset"); + public static final TranslationKey PRESET_DEFAULT = TranslationKey.gui("preset.default", "Make Default"); + public static final TranslationKey PRESET_DELETE = TranslationKey.gui("preset.delete", "Delete"); + + public static void init() { + + } +} diff --git a/src/main/java/com/terraforged/gui/SettingsScreen.java b/src/main/java/com/terraforged/gui/SettingsScreen.java index eded999..cd694dd 100644 --- a/src/main/java/com/terraforged/gui/SettingsScreen.java +++ b/src/main/java/com/terraforged/gui/SettingsScreen.java @@ -63,12 +63,12 @@ public class SettingsScreen extends OverlayScreen { this.pages = new Page[]{ new PresetsPage(instance, preview, preview.getPreviewWidget()), new WorldPage(instance, preview), - new SimplePreviewPage("Climate Settings", "climate", preview, instance, s -> s.climate), - new SimplePreviewPage("Terrain Settings", "terrain", preview, instance, s -> s.terrain), - new SimplePreviewPage("River Settings", "rivers", preview, instance, s -> s.rivers), - new SimplePreviewPage("Filter Settings", "filters", preview, instance, s -> s.filters), - new SimplePage("Structure Settings", "structures", instance, s -> s.structures), - new SimplePage("Feature Settings", "miscellaneous", instance, s -> s.miscellaneous) + new SimplePreviewPage(GuiKeys.CLIMATE_SETTINGS, "climate", preview, instance, s -> s.climate), + new SimplePreviewPage(GuiKeys.TERRAIN_SETTINGS, "terrain", preview, instance, s -> s.terrain), + new SimplePreviewPage(GuiKeys.RIVER_SETTINGS, "rivers", preview, instance, s -> s.rivers), + new SimplePreviewPage(GuiKeys.FILTER_SETTINGS, "filters", preview, instance, s -> s.filters), + new SimplePage(GuiKeys.STRUCTURE_SETTINGS, "structures", instance, s -> s.structures), + new SimplePage(GuiKeys.MISC_SETTINGS, "miscellaneous", instance, s -> s.miscellaneous) }; } @@ -104,10 +104,10 @@ public class SettingsScreen extends OverlayScreen { } // -52 - addButton(new Button(buttonsCenter - buttonWidth - buttonPad, buttonsRow, buttonWidth, buttonHeight, "Cancel", b -> onClose())); + addButton(new Button(buttonsCenter - buttonWidth - buttonPad, buttonsRow, buttonWidth, buttonHeight, GuiKeys.CANCEL.get(), b -> onClose())); // +2 - addButton(new Button(buttonsCenter + buttonPad, buttonsRow, buttonWidth, buttonHeight, "Done", b -> { + addButton(new Button(buttonsCenter + buttonPad, buttonsRow, buttonWidth, buttonHeight, GuiKeys.DONE.get(), b -> { for (Page page : pages) { page.save(); } @@ -179,7 +179,7 @@ public class SettingsScreen extends OverlayScreen { boolean a = pages[pageIndex].action(pane -> pane.mouseClicked(x, y, button)); boolean b = isPresetsPage() || preview.action(pane -> pane.mouseClicked(x, y, button));; boolean c = super.mouseClicked(x, y, button); - return a || b || c || true; + return a || b || c; } @Override @@ -187,7 +187,7 @@ public class SettingsScreen extends OverlayScreen { boolean a = pages[pageIndex].action(pane -> pane.mouseReleased(x, y, button)); boolean b = isPresetsPage() || preview.action(pane -> pane.mouseReleased(x, y, button)); boolean c = super.mouseReleased(x, y, button); - return a || b || c || true; + return a || b || c; } @Override diff --git a/src/main/java/com/terraforged/gui/page/PresetsPage.java b/src/main/java/com/terraforged/gui/page/PresetsPage.java index 5c98692..0f133b6 100644 --- a/src/main/java/com/terraforged/gui/page/PresetsPage.java +++ b/src/main/java/com/terraforged/gui/page/PresetsPage.java @@ -1,8 +1,10 @@ package com.terraforged.gui.page; +import com.terraforged.chunk.settings.SettingsHelper; import com.terraforged.chunk.settings.TerraSettings; import com.terraforged.chunk.settings.preset.Preset; import com.terraforged.chunk.settings.preset.PresetManager; +import com.terraforged.gui.GuiKeys; import com.terraforged.gui.Instance; import com.terraforged.gui.OverlayScreen; import com.terraforged.gui.ScrollPane; @@ -39,7 +41,7 @@ public class PresetsPage extends BasePage { @Override public String getTitle() { - return "Presets & Defaults"; + return GuiKeys.PRESETS.get(); } @Override @@ -63,7 +65,7 @@ public class PresetsPage extends BasePage { Column right = getColumn(1); right.scrollPane.addButton(nameInput); - right.scrollPane.addButton(new TerraButton("Create") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_CREATE.get()) { @Override public void render(int x, int z, float ticks) { @@ -91,7 +93,7 @@ public class PresetsPage extends BasePage { } }); - right.scrollPane.addButton(new TerraButton("Load") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_LOAD.get()) { @Override public void render(int x, int z, float ticks) { @@ -106,7 +108,7 @@ public class PresetsPage extends BasePage { } }); - right.scrollPane.addButton(new TerraButton("Save") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_SAVE.get()) { @Override public void render(int x, int z, float ticks) { @@ -131,7 +133,7 @@ public class PresetsPage extends BasePage { } }); - right.scrollPane.addButton(new TerraButton("Reset") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_RESET.get()) { @Override public void render(int x, int z, float ticks) { @@ -155,7 +157,26 @@ public class PresetsPage extends BasePage { } }); - right.scrollPane.addButton(new TerraButton("Delete") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_DEFAULT.get()) { + + @Override + public void render(int x, int z, float ticks) { + super.active = hasSelectedPreset(); + super.render(x, z, ticks); + } + + @Override + public void onClick(double x, double y) { + super.onClick(x, y); + getSelected().ifPresent(preset -> { + TerraSettings settings = preset.getSettings(); + + SettingsHelper.exportDefaults(settings); + }); + } + }); + + right.scrollPane.addButton(new TerraButton(GuiKeys.PRESET_DELETE.get()) { @Override public void render(int x, int z, float ticks) { diff --git a/src/main/java/com/terraforged/gui/page/SimplePage.java b/src/main/java/com/terraforged/gui/page/SimplePage.java index 7e86dd7..87a4d9b 100644 --- a/src/main/java/com/terraforged/gui/page/SimplePage.java +++ b/src/main/java/com/terraforged/gui/page/SimplePage.java @@ -3,6 +3,7 @@ package com.terraforged.gui.page; import com.terraforged.chunk.settings.TerraSettings; import com.terraforged.gui.Instance; import com.terraforged.gui.OverlayScreen; +import com.terraforged.util.TranslationKey; import net.minecraft.nbt.CompoundNBT; import java.util.function.Function; @@ -16,8 +17,8 @@ public class SimplePage extends BasePage { protected CompoundNBT sectionSettings = null; - public SimplePage(String title, String sectionName, Instance instance, Function section) { - this.title = title; + public SimplePage(TranslationKey title, String sectionName, Instance instance, Function section) { + this.title = title.get(); this.section = section; this.sectionName = sectionName; this.instance = instance; diff --git a/src/main/java/com/terraforged/gui/page/SimplePreviewPage.java b/src/main/java/com/terraforged/gui/page/SimplePreviewPage.java index 61477bb..f413e55 100644 --- a/src/main/java/com/terraforged/gui/page/SimplePreviewPage.java +++ b/src/main/java/com/terraforged/gui/page/SimplePreviewPage.java @@ -2,6 +2,7 @@ package com.terraforged.gui.page; import com.terraforged.chunk.settings.TerraSettings; import com.terraforged.gui.Instance; +import com.terraforged.util.TranslationKey; import com.terraforged.util.nbt.NBTHelper; import java.util.function.Function; @@ -10,7 +11,7 @@ public class SimplePreviewPage extends SimplePage { private final UpdatablePage preview; - public SimplePreviewPage(String title, String sectionName, UpdatablePage preview, Instance instance, Function section) { + public SimplePreviewPage(TranslationKey title, String sectionName, UpdatablePage preview, Instance instance, Function section) { super(title, sectionName, instance, section); this.preview = preview; } diff --git a/src/main/java/com/terraforged/gui/page/WorldPage.java b/src/main/java/com/terraforged/gui/page/WorldPage.java index 320101e..5f369d6 100644 --- a/src/main/java/com/terraforged/gui/page/WorldPage.java +++ b/src/main/java/com/terraforged/gui/page/WorldPage.java @@ -26,6 +26,7 @@ package com.terraforged.gui.page; import com.terraforged.TerraWorld; +import com.terraforged.gui.GuiKeys; import com.terraforged.gui.Instance; import com.terraforged.gui.OverlayScreen; import com.terraforged.gui.element.TerraTextInput; @@ -53,7 +54,7 @@ public class WorldPage extends BasePage { @Override public String getTitle() { - return "World Settings"; + return GuiKeys.WORLD_SETTINGS.get(); } @Override diff --git a/src/main/java/com/terraforged/gui/preview/Preview.java b/src/main/java/com/terraforged/gui/preview/Preview.java index 65097c6..aa24a1b 100644 --- a/src/main/java/com/terraforged/gui/preview/Preview.java +++ b/src/main/java/com/terraforged/gui/preview/Preview.java @@ -34,6 +34,7 @@ import com.terraforged.core.settings.Settings; import com.terraforged.core.tile.Size; import com.terraforged.core.tile.Tile; import com.terraforged.core.tile.gen.TileGenerator; +import com.terraforged.gui.GuiKeys; import com.terraforged.n2d.util.NoiseUtil; import com.terraforged.util.nbt.NBTHelper; import com.terraforged.world.GeneratorContext; @@ -71,7 +72,7 @@ public class Preview extends Button { private CacheEntry task = null; private Tile tile = null; - private String[] labels = {"Area: ", "Terrain: ", "Biome: "}; + private String[] labels = {GuiKeys.PREVIEW_AREA.get(), GuiKeys.PREVIEW_TERRAIN.get(), GuiKeys.PREVIEW_BIOME.get()}; private String[] values = {"", "", ""}; public Preview(int seed) { @@ -276,11 +277,10 @@ public class Preview extends Button { } private static String getTerrainName(Cell cell) { - String terrain = cell.terrain.getName().toLowerCase(); - if (terrain.contains("river")) { + if (cell.terrain.isRiver()) { return "river"; } - return terrain; + return cell.terrain.getName().toLowerCase(); } private static String getBiomeName(Cell cell) { diff --git a/src/main/java/com/terraforged/gui/preview/PreviewPage.java b/src/main/java/com/terraforged/gui/preview/PreviewPage.java index 580dc2b..30d4386 100644 --- a/src/main/java/com/terraforged/gui/preview/PreviewPage.java +++ b/src/main/java/com/terraforged/gui/preview/PreviewPage.java @@ -26,6 +26,7 @@ package com.terraforged.gui.preview; import com.terraforged.chunk.settings.TerraSettings; +import com.terraforged.gui.GuiKeys; import com.terraforged.gui.OverlayScreen; import com.terraforged.gui.element.TerraButton; import com.terraforged.gui.page.UpdatablePage; @@ -73,7 +74,7 @@ public class PreviewPage extends UpdatablePage { preview.setHeight(Preview.HEIGHT); addElements(right.left, right.top, right, previewerSettings, right.scrollPane::addButton, this::update); - right.scrollPane.addButton(new TerraButton("New Seed") { + right.scrollPane.addButton(new TerraButton(GuiKeys.PREVIEW_SEED.get()) { @Override public void onPress() { preview.regenerate(); diff --git a/src/main/java/com/terraforged/util/TranslationKey.java b/src/main/java/com/terraforged/util/TranslationKey.java new file mode 100644 index 0000000..2d7b965 --- /dev/null +++ b/src/main/java/com/terraforged/util/TranslationKey.java @@ -0,0 +1,59 @@ +package com.terraforged.util; + +import com.terraforged.core.util.NameUtil; +import net.minecraft.client.resources.I18n; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +public class TranslationKey { + + private static final Map keys = new HashMap<>(); + + private final String translationKey; + private final String defaultValue; + + public TranslationKey(String key, String display) { + this.translationKey = key; + this.defaultValue = display; + keys.put(translationKey, this); + } + + public String getKey() { + return translationKey; + } + + public String getDefaultValue() { + return defaultValue; + } + + public String get() { + if (I18n.hasKey(translationKey)) { + return I18n.format(translationKey); + } + return defaultValue; + } + + public String get(Object... args) { + if (I18n.hasKey(translationKey)) { + return I18n.format(translationKey, args); + } + return defaultValue; + } + + public static void each(Consumer consumer) { + keys.values().stream().sorted(Comparator.comparing(TranslationKey::getKey)).forEach(consumer); + } + + public static TranslationKey gui(String text) { + String key = NameUtil.toDisplayNameKey(text); + String display = NameUtil.toDisplayName(text.substring(text.lastIndexOf('.') + 1)); + return new TranslationKey(key, display); + } + + public static TranslationKey gui(String key, String display) { + return new TranslationKey(NameUtil.toDisplayNameKey(key), display); + } +}