GuiWEHelpPanel.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.zjinja.mcmod.zry_client_utils_mod.gui;
  2. import com.google.gson.Gson;
  3. import com.google.gson.JsonArray;
  4. import com.google.gson.JsonElement;
  5. import com.mojang.blaze3d.vertex.PoseStack;
  6. import com.zjinja.mcmod.zry_client_utils_mod.ZRYClientUtilsMod;
  7. import net.minecraft.client.KeyMapping;
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.client.gui.components.Button;
  10. import net.minecraft.client.gui.screens.Screen;
  11. import net.minecraft.network.chat.Component;
  12. import net.minecraft.resources.ResourceLocation;
  13. import org.lwjgl.glfw.GLFW;
  14. import java.io.InputStreamReader;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. public class GuiWEHelpPanel extends Screen {
  19. public static class WEPanelFunctionItem {
  20. public String Name;
  21. public String Command;
  22. public int KeyCode;
  23. public WEPanelFunctionItem(String name, String command, int keycode) {
  24. this.Name = name;
  25. this.Command = command;
  26. this.KeyCode = keycode;
  27. }
  28. }
  29. private static Gson GSON = new Gson();
  30. private ArrayList<WEPanelFunctionItem> functionList = new ArrayList<>();
  31. private HashMap<Integer, String> functionShortcutKeys = new HashMap<>();
  32. private int eachFunctionButtonWidth = 54;
  33. private int functionAreaMaxX;
  34. private int functionAreaMaxY;
  35. private final String funcListJsonUri = "data/we_panel_func.json";
  36. private int exitKeyCode = GLFW.GLFW_KEY_V;
  37. public GuiWEHelpPanel() {
  38. super(Component.translatable("gui.zry_client_utils.we_panel.title"));
  39. }
  40. @Override
  41. public void init() {
  42. super.init();
  43. for (KeyMapping kb : Minecraft.getInstance().options.keyMappings) {
  44. if (kb.getName().equals("key.zry_client_utils_mod.we_panel")) {
  45. this.exitKeyCode = kb.getKey().getValue();
  46. ZRYClientUtilsMod.LOGGER.info("WE_PANEL ExitKey: keyname={}, keycode={}", kb.getKey().getName(), kb.getKey().getValue());
  47. }
  48. }
  49. this.loadFunctionListFromJson();
  50. this.functionAreaMaxX = this.width - 24;
  51. this.functionAreaMaxY = this.height - 22;
  52. ZRYClientUtilsMod.LOGGER.info("Size Of WE_PANEL Screen: w={}, h={}", this.width, this.height);
  53. {
  54. Component backText = Component.translatable("gui.zry_client_utils.we_panel.back");
  55. addRenderableWidget(new Button(
  56. 4,
  57. 4,
  58. 36,
  59. 20,
  60. backText,
  61. button -> {
  62. this.closePanel();
  63. },
  64. (button, poseStack, n1, n2) -> {
  65. }
  66. )
  67. {});
  68. }
  69. int wXPos = 24, wYPos = 32;
  70. AddFunctionButtonsLoop:
  71. for(WEPanelFunctionItem i : this.functionList) {
  72. if(wXPos + this.eachFunctionButtonWidth > this.functionAreaMaxX) {
  73. wYPos += 20;
  74. wXPos = 24;
  75. }
  76. if (wYPos + 20 > this.functionAreaMaxY) {
  77. ZRYClientUtilsMod.LOGGER.warn("[{}] Too many function items for WE_PANEL.", ZRYClientUtilsMod.MODID);
  78. break AddFunctionButtonsLoop;
  79. }
  80. Component backText = Component.translatable(i.Name);
  81. addRenderableWidget(new Button(
  82. wXPos,
  83. wYPos,
  84. this.eachFunctionButtonWidth,
  85. 20,
  86. backText,
  87. button -> {
  88. issueCmd(i.Command);
  89. },
  90. (button, poseStack, n1, n2) -> {
  91. }
  92. )
  93. {});
  94. if(i.KeyCode > 0) {
  95. this.functionShortcutKeys.put(i.KeyCode, i.Command);
  96. }
  97. wXPos += this.eachFunctionButtonWidth;
  98. }
  99. }
  100. public void loadFunctionListFromJson() {
  101. var resLoc = new ResourceLocation(ZRYClientUtilsMod.MODID, this.funcListJsonUri);
  102. var fljson = Minecraft.getInstance().getResourceManager().getResource(resLoc);
  103. if(fljson.isPresent()) {
  104. try(var file = fljson.get().open()){
  105. try(var isr = new InputStreamReader(file, StandardCharsets.UTF_8)){
  106. JsonArray jsonArray = GSON.fromJson(isr, JsonArray.class);
  107. AddElemLoop:
  108. for(JsonElement ji: jsonArray) {
  109. var jo = ji.getAsJsonObject();
  110. if(jo != null) {
  111. String name = "";
  112. String command = "";
  113. int keyCode = 0;
  114. var rawName = jo.getAsJsonPrimitive("name");
  115. if(rawName != null) {
  116. name = rawName.getAsString();
  117. if(name.equals("")){
  118. continue AddElemLoop;
  119. }
  120. }else{
  121. continue AddElemLoop;
  122. }
  123. var cmd = jo.getAsJsonPrimitive("command");
  124. if (cmd != null) {
  125. command = cmd.getAsString();
  126. if(command.equals("")){
  127. continue AddElemLoop;
  128. }
  129. }else {
  130. continue AddElemLoop;
  131. }
  132. var rawKeycode = jo.getAsJsonPrimitive("keybind");
  133. if(rawKeycode != null){
  134. keyCode = rawKeycode.getAsInt();
  135. if(keyCode < 0) {
  136. keyCode = 0;
  137. }
  138. }
  139. WEPanelFunctionItem ni = new WEPanelFunctionItem(name, command, keyCode);
  140. this.functionList.add(ni);
  141. }else{
  142. ZRYClientUtilsMod.LOGGER.warn("WE_PANEL failed load function: unexpected type in json.");
  143. }
  144. }
  145. }
  146. }catch (Exception e) {
  147. ZRYClientUtilsMod.LOGGER.error("WE_PANEL failed load resource {}: {}", this.functionList, e);
  148. }
  149. }else {
  150. ZRYClientUtilsMod.LOGGER.error("WE_PANEL failed load resource {}: not found.", this.functionList);
  151. }
  152. }
  153. public void issueCmd(String cmd) {
  154. if (Minecraft.getInstance().player != null) {
  155. Minecraft.getInstance().player.commandUnsigned(cmd);
  156. }
  157. }
  158. @Override
  159. public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
  160. super.render(matrices, mouseX, mouseY, delta);
  161. }
  162. public void closePanel() {
  163. KeyMapping.releaseAll();
  164. Minecraft.getInstance().setScreen(null);
  165. //Minecraft.getInstance().cursorEntered();
  166. }
  167. @Override
  168. public boolean keyPressed(int keycode, int param1, int modkey){
  169. ZRYClientUtilsMod.LOGGER.debug("KeyPressed on WE_PANEL: keycode={}, p1={}, p2={}", keycode, param1, modkey);
  170. if( modkey == 0) {
  171. if (keycode == this.exitKeyCode) {
  172. this.closePanel();
  173. return true;
  174. }
  175. if (this.functionShortcutKeys.containsKey(keycode)) {
  176. this.issueCmd(this.functionShortcutKeys.get(keycode));
  177. }
  178. }
  179. return super.keyPressed(keycode, param1, modkey);
  180. }
  181. }