CreditsScreen.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.gui.credits;
  24. import com.google.common.collect.Lists;
  25. import me.shedaniel.rei.gui.credits.CreditsEntryListWidget.TextCreditsItem;
  26. import me.shedaniel.rei.gui.credits.CreditsEntryListWidget.TranslationCreditsItem;
  27. import me.shedaniel.rei.impl.ScreenHelper;
  28. import net.fabricmc.loader.api.FabricLoader;
  29. import net.fabricmc.loader.api.metadata.CustomValue;
  30. import net.minecraft.client.gui.screen.Screen;
  31. import net.minecraft.client.gui.screen.ingame.HandledScreen;
  32. import net.minecraft.client.gui.widget.AbstractPressableButtonWidget;
  33. import net.minecraft.client.resource.language.I18n;
  34. import net.minecraft.client.util.NarratorManager;
  35. import net.minecraft.client.util.math.MatrixStack;
  36. import net.minecraft.text.LiteralText;
  37. import net.minecraft.text.TranslatableText;
  38. import net.minecraft.util.Pair;
  39. import org.jetbrains.annotations.ApiStatus;
  40. import java.util.Comparator;
  41. import java.util.List;
  42. import java.util.Locale;
  43. import java.util.stream.Collectors;
  44. @ApiStatus.Internal
  45. public class CreditsScreen extends Screen {
  46. private Screen parent;
  47. private AbstractPressableButtonWidget buttonDone;
  48. private CreditsEntryListWidget entryListWidget;
  49. public CreditsScreen(Screen parent) {
  50. super(new LiteralText(""));
  51. this.parent = parent;
  52. }
  53. @Override
  54. public boolean keyPressed(int int_1, int int_2, int int_3) {
  55. if (int_1 == 256 && this.shouldCloseOnEsc()) {
  56. this.client.openScreen(parent);
  57. if (parent instanceof HandledScreen)
  58. ScreenHelper.getLastOverlay().init();
  59. return true;
  60. }
  61. return super.keyPressed(int_1, int_2, int_3);
  62. }
  63. @Override
  64. protected void init() {
  65. children.add(entryListWidget = new CreditsEntryListWidget(client, width, height, 32, height - 32));
  66. entryListWidget.creditsClearEntries();
  67. List<Pair<String, String>> translators = Lists.newArrayList();
  68. Exception[] exception = {null};
  69. FabricLoader.getInstance().getModContainer("roughlyenoughitems").ifPresent(rei -> {
  70. try {
  71. if (rei.getMetadata().containsCustomValue("rei:translators")) {
  72. CustomValue.CvObject jsonObject = rei.getMetadata().getCustomValue("rei:translators").getAsObject();
  73. jsonObject.forEach(entry -> {
  74. CustomValue value = entry.getValue();
  75. String behind = value.getType() == CustomValue.CvType.ARRAY ? Lists.newArrayList(value.getAsArray().iterator()).stream().map(CustomValue::getAsString).sorted(String::compareToIgnoreCase).collect(Collectors.joining(", ")) : value.getAsString();
  76. translators.add(new Pair<>(entry.getKey(), behind));
  77. });
  78. }
  79. translators.sort(Comparator.comparing(Pair::getLeft, String::compareToIgnoreCase));
  80. } catch (Exception e) {
  81. exception[0] = e;
  82. e.printStackTrace();
  83. }
  84. });
  85. List<Pair<String, String>> translatorsMapped = translators.stream().map(pair -> {
  86. return new Pair<>(
  87. " " + (I18n.hasTranslation("language.roughlyenoughitems." + pair.getLeft().toLowerCase(Locale.ROOT).replace(' ', '_')) ? I18n.translate("language.roughlyenoughitems." + pair.getLeft().toLowerCase(Locale.ROOT).replace(' ', '_')) : pair.getLeft()),
  88. pair.getRight()
  89. );
  90. }).collect(Collectors.toList());
  91. int i = width - 80 - 6;
  92. for (String line : I18n.translate("text.rei.credit.text", FabricLoader.getInstance().getModContainer("roughlyenoughitems").map(mod -> mod.getMetadata().getVersion().getFriendlyString()).orElse("Unknown"), "%translators%").split("\n"))
  93. if (line.equalsIgnoreCase("%translators%")) {
  94. if (exception[0] != null) {
  95. entryListWidget.creditsAddEntry(new TextCreditsItem(new LiteralText("Failed to get translators: " + exception[0].toString())));
  96. for (StackTraceElement traceElement : exception[0].getStackTrace())
  97. entryListWidget.creditsAddEntry(new TextCreditsItem(new LiteralText(" at " + traceElement)));
  98. } else {
  99. int maxWidth = translatorsMapped.stream().mapToInt(pair -> textRenderer.getStringWidth(pair.getLeft())).max().orElse(0) + 5;
  100. for (Pair<String, String> pair : translatorsMapped) {
  101. entryListWidget.creditsAddEntry(new TranslationCreditsItem(new TranslatableText(pair.getLeft()), new TranslatableText(pair.getRight()), i - maxWidth - 10, maxWidth));
  102. }
  103. }
  104. } else entryListWidget.creditsAddEntry(new TextCreditsItem(new LiteralText(line)));
  105. entryListWidget.creditsAddEntry(new TextCreditsItem(NarratorManager.EMPTY));
  106. children.add(buttonDone = new AbstractPressableButtonWidget(width / 2 - 100, height - 26, 200, 20, new TranslatableText("gui.done")) {
  107. @Override
  108. public void onPress() {
  109. CreditsScreen.this.client.openScreen(parent);
  110. if (parent instanceof HandledScreen)
  111. ScreenHelper.getLastOverlay().init();
  112. }
  113. });
  114. }
  115. @Override
  116. public boolean mouseScrolled(double double_1, double double_2, double double_3) {
  117. if (entryListWidget.mouseScrolled(double_1, double_2, double_3))
  118. return true;
  119. return super.mouseScrolled(double_1, double_2, double_3);
  120. }
  121. @Override
  122. public void render(MatrixStack matrices, int int_1, int int_2, float float_1) {
  123. this.renderDirtBackground(0);
  124. this.entryListWidget.render(matrices, int_1, int_2, float_1);
  125. this.drawCenteredString(matrices, this.textRenderer, I18n.translate("text.rei.credits"), this.width / 2, 16, 16777215);
  126. super.render(matrices, int_1, int_2, float_1);
  127. buttonDone.render(matrices, int_1, int_2, float_1);
  128. }
  129. }