RoughlyEnoughItemsInitializer.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  24. import com.google.common.collect.ImmutableSet;
  25. import net.fabricmc.api.ClientModInitializer;
  26. import net.fabricmc.api.EnvType;
  27. import net.fabricmc.api.ModInitializer;
  28. import net.fabricmc.loader.api.FabricLoader;
  29. import net.fabricmc.loader.api.SemanticVersion;
  30. import net.fabricmc.loader.api.VersionParsingException;
  31. import java.lang.reflect.InvocationTargetException;
  32. public class RoughlyEnoughItemsInitializer implements ModInitializer, ClientModInitializer {
  33. @Override
  34. public void onInitialize() {
  35. checkRequiredFabricModules();
  36. if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
  37. checkClothConfig();
  38. checkModMenu();
  39. }
  40. if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
  41. initializeEntryPoint("me.shedaniel.rei.RoughlyEnoughItemsNetwork");
  42. }
  43. }
  44. @Override
  45. public void onInitializeClient() {
  46. if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
  47. initializeEntryPoint("me.shedaniel.rei.RoughlyEnoughItemsCore");
  48. initializeEntryPoint("me.shedaniel.rei.impl.ClientHelperImpl");
  49. initializeEntryPoint("me.shedaniel.rei.impl.ScreenHelper");
  50. }
  51. initializeEntryPoint("me.shedaniel.rei.impl.ErrorDisplayer");
  52. }
  53. public void initializeEntryPoint(String className) {
  54. try {
  55. Object instance = Class.forName(className).getConstructor().newInstance();
  56. if (instance instanceof ModInitializer) {
  57. ((ModInitializer) instance).onInitialize();
  58. } else if (instance instanceof ClientModInitializer) {
  59. ((ClientModInitializer) instance).onInitializeClient();
  60. }
  61. } catch (InstantiationException | InvocationTargetException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65. public static void checkRequiredFabricModules() {
  66. ImmutableSet<String> requiredModules = FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT ?
  67. ImmutableSet.<String>builder()
  68. .add("fabric-api-base")
  69. .add("fabric-resource-loader-v0")
  70. .add("fabric-networking-v0")
  71. .add("fabric-lifecycle-events-v1")
  72. .add("fabric-rendering-fluids-v1")
  73. .build() :
  74. ImmutableSet.<String>builder()
  75. .add("fabric-api-base")
  76. .add("fabric-resource-loader-v0")
  77. .add("fabric-networking-v0")
  78. .add("fabric-lifecycle-events-v1")
  79. .build();
  80. for (String module : requiredModules) {
  81. boolean moduleLoaded = FabricLoader.getInstance().isModLoaded(module);
  82. if (!moduleLoaded) {
  83. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  84. break;
  85. }
  86. }
  87. }
  88. public static void checkClothConfig() {
  89. try {
  90. if (!FabricLoader.getInstance().isModLoaded("cloth-config2")) {
  91. RoughlyEnoughItemsState.error("Cloth Config is not installed!", "https://www.curseforge.com/minecraft/mc-mods/cloth-config/files/all");
  92. } else if (SemanticVersion.parse(FabricLoader.getInstance().getModContainer("cloth-config2").get().getMetadata().getVersion().getFriendlyString()).compareTo(SemanticVersion.parse("4.11.14")) < 0) {
  93. RoughlyEnoughItemsState.error("Your Cloth Config version is too old!", "https://www.curseforge.com/minecraft/mc-mods/cloth-config/files/all");
  94. }
  95. } catch (VersionParsingException e) {
  96. RoughlyEnoughItemsState.error("Failed to parse Cloth Config version: " + e.getMessage());
  97. e.printStackTrace();
  98. }
  99. }
  100. public static void checkModMenu() {
  101. try {
  102. if (FabricLoader.getInstance().isModLoaded("modmenu")) {
  103. if (SemanticVersion.parse(FabricLoader.getInstance().getModContainer("modmenu").get().getMetadata().getVersion().getFriendlyString()).compareTo(SemanticVersion.parse("1.16.7")) < 0) {
  104. RoughlyEnoughItemsState.error("Your Mod Menu version is too old!", "https://www.curseforge.com/minecraft/mc-mods/modmenu/files/all");
  105. }
  106. }
  107. } catch (VersionParsingException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. }