LightOverlay.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package me.shedaniel.lightoverlay.common;
  2. import com.mojang.blaze3d.platform.InputConstants;
  3. import com.mojang.blaze3d.vertex.PoseStack;
  4. import dev.architectury.event.events.client.ClientGuiEvent;
  5. import dev.architectury.event.events.client.ClientTickEvent;
  6. import dev.architectury.injectables.targets.ArchitecturyTarget;
  7. import dev.architectury.platform.Platform;
  8. import dev.architectury.registry.client.keymappings.KeyMappingRegistry;
  9. import net.minecraft.client.KeyMapping;
  10. import net.minecraft.resources.ResourceLocation;
  11. import net.minecraft.util.Mth;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.text.DecimalFormat;
  17. import java.util.Properties;
  18. import java.util.function.Consumer;
  19. public class LightOverlay {
  20. public static final DecimalFormat FORMAT = new DecimalFormat("#.#");
  21. public static int reach = 12;
  22. public static int crossLevel = 7;
  23. public static int secondaryLevel = -1;
  24. public static int lowerCrossLevel = -1;
  25. public static int higherCrossLevel = -1;
  26. public static boolean caching = false;
  27. public static boolean showNumber = false;
  28. public static boolean underwater = false;
  29. public static boolean mushroom = false;
  30. public static float lineWidth = 1.0F;
  31. public static int yellowColor = 0xFFFF00, redColor = 0xFF0000, secondaryColor = 0x0000FF;
  32. public static File configFile;
  33. public static KeyMapping enableOverlay;
  34. public static boolean enabled = false;
  35. public static LightOverlayTicker ticker = new LightOverlayTicker();
  36. public static LightOverlayRenderer renderer = new LightOverlayRenderer(ticker);
  37. public static void register() {
  38. // Load Config
  39. configFile = new File(Platform.getConfigFolder().toFile(), "lightoverlay.properties");
  40. loadConfig(configFile);
  41. enableOverlay = createKeyBinding(new ResourceLocation("lightoverlay", "enable_overlay"), InputConstants.Type.KEYSYM, 296, "key.lightoverlay.category");
  42. KeyMappingRegistry.register(enableOverlay);
  43. registerDebugRenderer(renderer);
  44. ClientGuiEvent.DEBUG_TEXT_LEFT.register(list -> {
  45. if (enabled) {
  46. if (caching) {
  47. list.add(String.format("[Light Overlay] Chunks to queue: %02d", ticker.POS.size()));
  48. } else {
  49. list.add("[Light Overlay] Enabled");
  50. }
  51. } else {
  52. list.add("[Light Overlay] Disabled");
  53. }
  54. });
  55. ClientTickEvent.CLIENT_POST.register(ticker::tick);
  56. }
  57. public static void queueChunkAndNear(CubicChunkPos pos) {
  58. for (int xOffset = -1; xOffset <= 1; xOffset++) {
  59. for (int yOffset = -1; yOffset <= 1; yOffset++) {
  60. for (int zOffset = -1; zOffset <= 1; zOffset++) {
  61. queueChunk(new CubicChunkPos(pos.x + xOffset, pos.y + yOffset, pos.z + zOffset));
  62. }
  63. }
  64. }
  65. }
  66. public static void queueChunk(CubicChunkPos pos) {
  67. ticker.queueChunk(pos);
  68. }
  69. public static int getChunkRange() {
  70. return Math.max(Mth.ceil(reach / 16f), 1);
  71. }
  72. public static void loadConfig(File file) {
  73. try {
  74. redColor = 0xFF0000;
  75. yellowColor = 0xFFFF00;
  76. secondaryColor = 0x0000FF;
  77. if (!file.exists() || !file.canRead())
  78. saveConfig(file);
  79. FileInputStream fis = new FileInputStream(file);
  80. Properties properties = new Properties();
  81. properties.load(fis);
  82. fis.close();
  83. reach = Integer.parseInt((String) properties.computeIfAbsent("reach", a -> "12"));
  84. crossLevel = Integer.parseInt((String) properties.computeIfAbsent("crossLevel", a -> "7"));
  85. secondaryLevel = Integer.parseInt((String) properties.computeIfAbsent("secondaryLevel", a -> "-1"));
  86. caching = ((String) properties.computeIfAbsent("caching", a -> "false")).equalsIgnoreCase("true");
  87. showNumber = ((String) properties.computeIfAbsent("showNumber", a -> "false")).equalsIgnoreCase("true");
  88. underwater = ((String) properties.computeIfAbsent("underwater", a -> "false")).equalsIgnoreCase("true");
  89. mushroom = ((String) properties.computeIfAbsent("mushroom", a -> "false")).equalsIgnoreCase("true");
  90. lineWidth = Float.parseFloat((String) properties.computeIfAbsent("lineWidth", a -> "1"));
  91. {
  92. int r, g, b;
  93. r = Integer.parseInt((String) properties.computeIfAbsent("yellowColorRed", a -> "255"));
  94. g = Integer.parseInt((String) properties.computeIfAbsent("yellowColorGreen", a -> "255"));
  95. b = Integer.parseInt((String) properties.computeIfAbsent("yellowColorBlue", a -> "0"));
  96. yellowColor = (r << 16) + (g << 8) + b;
  97. }
  98. {
  99. int r, g, b;
  100. r = Integer.parseInt((String) properties.computeIfAbsent("redColorRed", a -> "255"));
  101. g = Integer.parseInt((String) properties.computeIfAbsent("redColorGreen", a -> "0"));
  102. b = Integer.parseInt((String) properties.computeIfAbsent("redColorBlue", a -> "0"));
  103. redColor = (r << 16) + (g << 8) + b;
  104. }
  105. {
  106. int r, g, b;
  107. r = Integer.parseInt((String) properties.computeIfAbsent("secondaryColorRed", a -> "0"));
  108. g = Integer.parseInt((String) properties.computeIfAbsent("secondaryColorGreen", a -> "0"));
  109. b = Integer.parseInt((String) properties.computeIfAbsent("secondaryColorBlue", a -> "255"));
  110. secondaryColor = (r << 16) + (g << 8) + b;
  111. }
  112. saveConfig(file);
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. reach = 12;
  116. crossLevel = 7;
  117. secondaryLevel = -1;
  118. lineWidth = 1.0F;
  119. redColor = 0xFF0000;
  120. yellowColor = 0xFFFF00;
  121. secondaryColor = 0x0000FF;
  122. caching = false;
  123. showNumber = false;
  124. underwater = false;
  125. mushroom = false;
  126. try {
  127. saveConfig(file);
  128. } catch (IOException ex) {
  129. ex.printStackTrace();
  130. }
  131. }
  132. if (secondaryLevel >= crossLevel) System.err.println("[Light Overlay] Secondary Level is higher than Cross Level");
  133. lowerCrossLevel = Math.min(crossLevel, secondaryLevel);
  134. higherCrossLevel = Math.max(crossLevel, secondaryLevel);
  135. ticker.CHUNK_MAP.clear();
  136. ticker.POS.clear();
  137. }
  138. public static void saveConfig(File file) throws IOException {
  139. FileOutputStream fos = new FileOutputStream(file, false);
  140. fos.write("# Light Overlay Config".getBytes());
  141. fos.write("\n".getBytes());
  142. fos.write(("reach=" + reach).getBytes());
  143. fos.write("\n".getBytes());
  144. fos.write(("crossLevel=" + crossLevel).getBytes());
  145. fos.write("\n".getBytes());
  146. fos.write(("secondaryLevel=" + secondaryLevel).getBytes());
  147. fos.write("\n".getBytes());
  148. fos.write(("caching=" + caching).getBytes());
  149. fos.write("\n".getBytes());
  150. fos.write(("showNumber=" + showNumber).getBytes());
  151. fos.write("\n".getBytes());
  152. fos.write(("underwater=" + underwater).getBytes());
  153. fos.write("\n".getBytes());
  154. fos.write(("mushroom=" + mushroom).getBytes());
  155. fos.write("\n".getBytes());
  156. fos.write(("lineWidth=" + FORMAT.format(lineWidth)).getBytes());
  157. fos.write("\n".getBytes());
  158. fos.write(("yellowColorRed=" + ((yellowColor >> 16) & 255)).getBytes());
  159. fos.write("\n".getBytes());
  160. fos.write(("yellowColorGreen=" + ((yellowColor >> 8) & 255)).getBytes());
  161. fos.write("\n".getBytes());
  162. fos.write(("yellowColorBlue=" + (yellowColor & 255)).getBytes());
  163. fos.write("\n".getBytes());
  164. fos.write(("redColorRed=" + ((redColor >> 16) & 255)).getBytes());
  165. fos.write("\n".getBytes());
  166. fos.write(("redColorGreen=" + ((redColor >> 8) & 255)).getBytes());
  167. fos.write("\n".getBytes());
  168. fos.write(("redColorBlue=" + (redColor & 255)).getBytes());
  169. fos.write("\n".getBytes());
  170. fos.write(("secondaryColorRed=" + ((secondaryColor >> 16) & 255)).getBytes());
  171. fos.write("\n".getBytes());
  172. fos.write(("secondaryColorGreen=" + ((secondaryColor >> 8) & 255)).getBytes());
  173. fos.write("\n".getBytes());
  174. fos.write(("secondaryColorBlue=" + (secondaryColor & 255)).getBytes());
  175. fos.close();
  176. }
  177. private static KeyMapping createKeyBinding(ResourceLocation id, InputConstants.Type type, int code, String category) {
  178. return new KeyMapping("key." + id.getNamespace() + "." + id.getPath(), type, code, category);
  179. }
  180. private static void registerDebugRenderer(Consumer<PoseStack> runnable) {
  181. try {
  182. Class.forName("me.shedaniel.lightoverlay." + ArchitecturyTarget.getCurrentTarget() + ".LightOverlayImpl").getDeclaredField("debugRenderer").set(null, runnable);
  183. } catch (Throwable throwable) {
  184. throw new RuntimeException(throwable);
  185. }
  186. }
  187. public static final byte CROSS_YELLOW = 0;
  188. public static final byte CROSS_RED = 1;
  189. public static final byte CROSS_SECONDARY = 2;
  190. public static final byte CROSS_NONE = 2;
  191. }