RoughlyEnoughItemsCore.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package me.shedaniel.rei;
  2. import com.google.common.collect.Maps;
  3. import me.shedaniel.rei.api.ItemRegistry;
  4. import me.shedaniel.rei.api.PluginDisabler;
  5. import me.shedaniel.rei.api.REIPlugin;
  6. import me.shedaniel.rei.api.RecipeHelper;
  7. import me.shedaniel.rei.client.ConfigManager;
  8. import me.shedaniel.rei.client.ItemRegistryImpl;
  9. import me.shedaniel.rei.client.PluginDisablerImpl;
  10. import me.shedaniel.rei.client.RecipeHelperImpl;
  11. import me.shedaniel.rei.gui.widget.ItemListOverlay;
  12. import me.shedaniel.rei.plugin.DefaultPlugin;
  13. import net.fabricmc.api.ClientModInitializer;
  14. import net.fabricmc.api.ModInitializer;
  15. import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
  16. import net.fabricmc.loader.api.FabricLoader;
  17. import net.minecraft.client.resource.language.I18n;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.server.network.ServerPlayerEntity;
  20. import net.minecraft.text.StringTextComponent;
  21. import net.minecraft.text.TranslatableTextComponent;
  22. import net.minecraft.util.Identifier;
  23. import org.apache.logging.log4j.LogManager;
  24. import org.apache.logging.log4j.Logger;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Optional;
  30. public class RoughlyEnoughItemsCore implements ClientModInitializer, ModInitializer {
  31. public static final Logger LOGGER = LogManager.getFormatterLogger("REI");
  32. public static final Identifier DELETE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "delete_item");
  33. public static final Identifier CREATE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "create_item");
  34. private static final RecipeHelper RECIPE_HELPER = new RecipeHelperImpl();
  35. private static final PluginDisabler PLUGIN_DISABLER = new PluginDisablerImpl();
  36. private static final ItemRegistry ITEM_REGISTRY = new ItemRegistryImpl();
  37. private static final Map<Identifier, REIPlugin> plugins = Maps.newHashMap();
  38. private static ConfigManager configManager;
  39. public static RecipeHelper getRecipeHelper() {
  40. return RECIPE_HELPER;
  41. }
  42. public static me.shedaniel.rei.api.ConfigManager getConfigManager() {
  43. return configManager;
  44. }
  45. public static ItemRegistry getItemRegisterer() {
  46. return ITEM_REGISTRY;
  47. }
  48. public static PluginDisabler getPluginDisabler() {
  49. return PLUGIN_DISABLER;
  50. }
  51. public static REIPlugin registerPlugin(Identifier identifier, REIPlugin plugin) {
  52. plugins.put(identifier, plugin);
  53. RoughlyEnoughItemsCore.LOGGER.info("REI: Registered plugin %s from %s", identifier.toString(), plugin.getClass().getSimpleName());
  54. plugin.onFirstLoad(getPluginDisabler());
  55. return plugin;
  56. }
  57. public static List<REIPlugin> getPlugins() {
  58. return new LinkedList<>(plugins.values());
  59. }
  60. public static Optional<Identifier> getPluginIdentifier(REIPlugin plugin) {
  61. for(Identifier identifier : plugins.keySet())
  62. if (identifier != null && plugins.get(identifier).equals(plugin))
  63. return Optional.of(identifier);
  64. return Optional.empty();
  65. }
  66. @Override
  67. public void onInitializeClient() {
  68. configManager = new ConfigManager();
  69. // If pluginloader is not installed, base functionality should still remain
  70. if (!FabricLoader.getInstance().isModLoaded("pluginloader")) {
  71. RoughlyEnoughItemsCore.LOGGER.warn("REI: Plugin Loader is not loaded! Please consider installing https://minecraft.curseforge.com/projects/pluginloader for REI plugin compatibility!");
  72. registerPlugin(new Identifier("roughlyenoughitems", "default_plugin"), new DefaultPlugin());
  73. }
  74. if (FabricLoader.getInstance().isModLoaded("cloth")) {
  75. try {
  76. Class.forName("me.shedaniel.rei.cloth.ClothRegistry").getDeclaredMethod("register").invoke(null);
  77. } catch (IllegalAccessException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82. @Override
  83. public void onInitialize() {
  84. registerFabricPackets();
  85. }
  86. private void registerFabricPackets() {
  87. ServerSidePacketRegistry.INSTANCE.register(DELETE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  88. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  89. if (!player.inventory.getCursorStack().isEmpty())
  90. player.inventory.setCursorStack(ItemStack.EMPTY);
  91. });
  92. ServerSidePacketRegistry.INSTANCE.register(CREATE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  93. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  94. ItemStack stack = packetByteBuf.readItemStack();
  95. if (player.inventory.insertStack(stack.copy()))
  96. player.addChatMessage(new StringTextComponent(I18n.translate("text.rei.cheat_items").replaceAll("\\{item_name}", ItemListOverlay.tryGetItemStackName(stack.copy())).replaceAll("\\{item_count}", stack.copy().getAmount() + "").replaceAll("\\{player_name}", player.getEntityName())), false);
  97. else
  98. player.addChatMessage(new TranslatableTextComponent("text.rei.failed_cheat_items"), false);
  99. });
  100. }
  101. }