RoughlyEnoughItemsInitializer.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 java.lang.reflect.InvocationTargetException;
  30. public class RoughlyEnoughItemsInitializer implements ModInitializer {
  31. @Override
  32. public void onInitialize() {
  33. checkRequiredFabricModules();
  34. checkClothConfig();
  35. if (checkRequiredFabricModules() && checkRequiredFabricModules()) {
  36. initializeEntryPoint("me.shedaniel.rei.RoughlyEnoughItemsNetwork");
  37. if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
  38. initializeEntryPoint("me.shedaniel.rei.RoughlyEnoughItemsCore");
  39. initializeEntryPoint("me.shedaniel.rei.impl.ClientHelperImpl");
  40. initializeEntryPoint("me.shedaniel.rei.impl.ScreenHelper");
  41. }
  42. }
  43. initializeEntryPoint("me.shedaniel.rei.impl.ErrorDisplayer");
  44. }
  45. public void initializeEntryPoint(String className) {
  46. try {
  47. Object instance = Class.forName(className).getConstructor().newInstance();
  48. if (instance instanceof ModInitializer) {
  49. ((ModInitializer) instance).onInitialize();
  50. } else if (instance instanceof ClientModInitializer) {
  51. ((ClientModInitializer) instance).onInitializeClient();
  52. }
  53. } catch (InstantiationException | InvocationTargetException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. public static boolean checkRequiredFabricModules() {
  58. ImmutableSet<String> requiredModules = FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT ?
  59. ImmutableSet.<String>builder()
  60. .add("fabric-api-base")
  61. .add("fabric-resource-loader-v0")
  62. .add("fabric-networking-v0")
  63. .add("fabric-lifecycle-events-v1")
  64. .add("fabric-rendering-fluids-v1")
  65. .build() :
  66. ImmutableSet.<String>builder()
  67. .add("fabric-api-base")
  68. .add("fabric-resource-loader-v0")
  69. .add("fabric-networking-v0")
  70. .add("fabric-lifecycle-events-v1")
  71. .build();
  72. for (String module : requiredModules) {
  73. boolean moduleLoaded = FabricLoader.getInstance().isModLoaded(module);
  74. if (!moduleLoaded) {
  75. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. public static boolean checkClothConfig() {
  82. if (!FabricLoader.getInstance().isModLoaded("cloth-config2")) {
  83. RoughlyEnoughItemsState.error("Cloth Config is not installed!", "https://www.curseforge.com/minecraft/mc-mods/cloth-config/files/all");
  84. return false;
  85. }
  86. return true;
  87. }
  88. }