From f985c1b0ab2bf6e6503dedb6ee05e959d9b64c55 Mon Sep 17 00:00:00 2001 From: Marcel Konrad Date: Sat, 22 Jun 2019 15:04:36 +0200 Subject: [PATCH] Change child mod registration from IMC to proper registry events --- .../exopandora/worldhandler/WorldHandler.java | 33 ++------ .../worldhandler/gui/category/Category.java | 78 +++++++++-------- .../worldhandler/gui/content/Content.java | 84 +++++++++---------- .../impl/ContentScoreboardObjectives.java | 4 +- ...tryTranslator.java => RegistryHelper.java} | 32 ++++--- 5 files changed, 117 insertions(+), 114 deletions(-) rename src/main/java/exopandora/worldhandler/helper/{RegistryTranslator.java => RegistryHelper.java} (56%) diff --git a/src/main/java/exopandora/worldhandler/WorldHandler.java b/src/main/java/exopandora/worldhandler/WorldHandler.java index 3b7dc8c..d19fe0d 100644 --- a/src/main/java/exopandora/worldhandler/WorldHandler.java +++ b/src/main/java/exopandora/worldhandler/WorldHandler.java @@ -1,13 +1,9 @@ package exopandora.worldhandler; -import java.util.Objects; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.lwjgl.glfw.GLFW; -import com.google.common.base.Predicates; - import exopandora.worldhandler.config.Config; import exopandora.worldhandler.gui.category.Category; import exopandora.worldhandler.gui.content.Content; @@ -21,14 +17,12 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DistExecutor; -import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.config.ModConfig.Type; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; -import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent; import net.minecraftforge.fml.event.server.FMLServerStartingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -37,9 +31,9 @@ public class WorldHandler { public static final Logger LOGGER = LogManager.getLogger(); - public static final KeyBinding KEY_WORLD_HANDLER = new KeyBinding(Main.NAME, GLFW.GLFW_KEY_V, "key.categories.misc"); - public static final KeyBinding KEY_WORLD_HANDLER_POS1 = new KeyBinding(Main.NAME + " Pos1", GLFW.GLFW_KEY_O, "key.categories.misc"); - public static final KeyBinding KEY_WORLD_HANDLER_POS2 = new KeyBinding(Main.NAME + " Pos2", GLFW.GLFW_KEY_P, "key.categories.misc"); + public static final KeyBinding KEY_WORLD_HANDLER = new KeyBinding(Main.NAME, GLFW.GLFW_KEY_V, Main.NAME); + public static final KeyBinding KEY_WORLD_HANDLER_POS1 = new KeyBinding(Main.NAME + " Pos1", GLFW.GLFW_KEY_O, Main.NAME); + public static final KeyBinding KEY_WORLD_HANDLER_POS2 = new KeyBinding(Main.NAME + " Pos2", GLFW.GLFW_KEY_P, Main.NAME); public static String USERNAME = null; @@ -51,10 +45,13 @@ public class WorldHandler IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); modEventBus.addListener(this::commonSetup); modEventBus.addListener(this::clientSetup); - modEventBus.addListener(this::loadComplete); MinecraftForge.EVENT_BUS.addListener(this::serverStarting); ModLoadingContext.get().registerConfig(Type.CLIENT, Config.CLIENT_SPEC, Main.MODID + ".toml"); modEventBus.register(Config.class); + modEventBus.addListener(Content::createRegistry); + modEventBus.addListener(Category::createRegistry); + modEventBus.addGenericListener(Content.class, Content::register); + modEventBus.addGenericListener(Category.class, Category::register); } @SubscribeEvent @@ -71,25 +68,9 @@ public class WorldHandler UtilKeyBinding.updatePosKeys(); } - @SubscribeEvent - public void loadComplete(FMLLoadCompleteEvent event) - { - Content.registerContents(); - Category.register(); - InterModComms.getMessages(Main.MODID, Predicates.equalTo("register")) - .map(imc -> (Runnable) imc.getMessageSupplier().get()) - .forEach(Runnable::run); - } - @SubscribeEvent public void serverStarting(FMLServerStartingEvent event) { CommandHelper.registerCommands(event.getCommandDispatcher()); } - - public static void registerIMC(Runnable registrationEvent) - { - Objects.requireNonNull(registrationEvent); - InterModComms.sendTo(Main.MODID, "register", () -> registrationEvent); - } } \ No newline at end of file diff --git a/src/main/java/exopandora/worldhandler/gui/category/Category.java b/src/main/java/exopandora/worldhandler/gui/category/Category.java index 500d817..274326d 100644 --- a/src/main/java/exopandora/worldhandler/gui/category/Category.java +++ b/src/main/java/exopandora/worldhandler/gui/category/Category.java @@ -1,7 +1,8 @@ package exopandora.worldhandler.gui.category; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -9,10 +10,13 @@ import com.google.common.collect.Lists; import exopandora.worldhandler.Main; import exopandora.worldhandler.gui.content.Content; -import exopandora.worldhandler.gui.content.Contents; +import exopandora.worldhandler.helper.RegistryHelper; import net.minecraft.util.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.event.RegistryEvent.NewRegistry; +import net.minecraftforge.event.RegistryEvent.Register; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.RegistryBuilder; @@ -20,37 +24,42 @@ import net.minecraftforge.registries.RegistryBuilder; @OnlyIn(Dist.CLIENT) public class Category extends ForgeRegistryEntry { - public static final IForgeRegistry REGISTRY = new RegistryBuilder() - .setType(Category.class) - .setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "category"}))) - .disableSync() - .disableSaving() - .create(); + public static IForgeRegistry REGISTRY; - private final List contents; + private final List contents; public Category() { - this.contents = new ArrayList(); + this.contents = Lists.newArrayList(); } - public Category(List contents) + public Category(List contents) { this.contents = contents; } - public Category(Content... contents) + public Category(ResourceLocation... contents) { - this.contents = Lists.newArrayList(contents); + this(Lists.newArrayList(contents)); } - public Category add(Content content) + public Category(String... keys) + { + this(Arrays.stream(keys).map(key -> new ResourceLocation(Main.MODID, key)).collect(Collectors.toList())); + } + + public Category add(ResourceLocation content) { this.contents.add(content); return this; } - public List getContents() + public Category add(String key) + { + return this.add(new ResourceLocation(Main.MODID, key)); + } + + public List getContents() { return this.contents; } @@ -63,28 +72,29 @@ public class Category extends ForgeRegistryEntry @Nullable public Content getContent(int index) { - return this.contents.get(index); + return Content.REGISTRY.getValue(this.contents.get(index)); } - public static void register() + @SubscribeEvent + public static void createRegistry(NewRegistry event) { - Category.register("main", new Category(Contents.MAIN, Contents.CONTAINERS, Contents.MULTIPLAYER)); - Category.register("entities", new Category(Contents.SUMMON)); - Category.register("items", new Category(Contents.CUSTOM_ITEM, Contents.ENCHANTMENT, Contents.RECIPES)); - Category.register("blocks", new Category(Contents.EDIT_BLOCKS, Contents.SIGN_EDITOR, Contents.NOTE_EDITOR)); - Category.register("world", new Category(Contents.WORLD_INFO, Contents.GAMERULES)); - Category.register("player", new Category(Contents.PLAYER, Contents.EXPERIENCE, Contents.ADVANCEMENTS)); - Category.register("scoreboard", new Category(Contents.SCOREBOARD_OBJECTIVES, Contents.SCOREBOARD_TEAMS, Contents.SCOREBOARD_PLAYERS)); + REGISTRY = new RegistryBuilder() + .setType(Category.class) + .setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "category"}))) + .disableSaving() + .disableSync() + .create(); } - private static void register(String name, Category category) - { - Category.register(new ResourceLocation(Main.MODID, name), category); - } - - private static void register(ResourceLocation name, Category category) - { - category.setRegistryName(name); - REGISTRY.register(category); - } + @SubscribeEvent + public static void register(Register event) + { + RegistryHelper.register(event.getRegistry(), "main", new Category("main", "containers", "multiplayer")); + RegistryHelper.register(event.getRegistry(), "entities", new Category("summon")); + RegistryHelper.register(event.getRegistry(), "items", new Category("custom_item", "enchantment", "recipes")); + RegistryHelper.register(event.getRegistry(), "blocks", new Category("edit_blocks", "sign_editor", "note_editor")); + RegistryHelper.register(event.getRegistry(), "world", new Category("world", "gamerules")); + RegistryHelper.register(event.getRegistry(), "player", new Category("player", "experience", "advancements")); + RegistryHelper.register(event.getRegistry(), "scoreboard", new Category("scoreboard_objectives", "scoreboard_teams", "scoreboard_players")); + } } diff --git a/src/main/java/exopandora/worldhandler/gui/content/Content.java b/src/main/java/exopandora/worldhandler/gui/content/Content.java index cc4ded9..aef5455 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/Content.java +++ b/src/main/java/exopandora/worldhandler/gui/content/Content.java @@ -29,9 +29,13 @@ import exopandora.worldhandler.gui.content.impl.ContentSettings; import exopandora.worldhandler.gui.content.impl.ContentSignEditor; import exopandora.worldhandler.gui.content.impl.ContentSummon; import exopandora.worldhandler.gui.content.impl.ContentWorldInfo; +import exopandora.worldhandler.helper.RegistryHelper; import net.minecraft.util.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.event.RegistryEvent.NewRegistry; +import net.minecraftforge.event.RegistryEvent.Register; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.RegistryBuilder; @@ -39,67 +43,63 @@ import net.minecraftforge.registries.RegistryBuilder; @OnlyIn(Dist.CLIENT) public abstract class Content extends ForgeRegistryEntry implements IContent { - public static final IForgeRegistry REGISTRY = new RegistryBuilder() - .setType(Content.class) - .setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "content"}))) - .disableSync() - .disableSaving() - .create(); + public static IForgeRegistry REGISTRY; - public static void registerContents() + @SubscribeEvent + public static void createRegistry(NewRegistry event) + { + REGISTRY = new RegistryBuilder() + .setType(Content.class) + .setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "content"}))) + .disableSaving() + .disableSync() + .create(); + } + + @SubscribeEvent + public static void register(Register event) { //MAIN - Content.register("main", new ContentMain()); - Content.register("containers", new ContentContainers()); - Content.register("multiplayer", new ContentMultiplayer()); + RegistryHelper.register(event.getRegistry(), "main", new ContentMain()); + RegistryHelper.register(event.getRegistry(), "containers", new ContentContainers()); + RegistryHelper.register(event.getRegistry(), "multiplayer", new ContentMultiplayer()); //ENTITIES - Content.register("summon", new ContentSummon()); + RegistryHelper.register(event.getRegistry(), "summon", new ContentSummon()); //ITEMS - Content.register("custom_item", new ContentCustomItem()); - Content.register("enchantment", new ContentEnchantment()); + RegistryHelper.register(event.getRegistry(), "custom_item", new ContentCustomItem()); + RegistryHelper.register(event.getRegistry(), "enchantment", new ContentEnchantment()); + RegistryHelper.register(event.getRegistry(), "recipes", new ContentRecipes()); //BLOCKS - Content.register("edit_blocks", new ContentEditBlocks()); - Content.register("sign_editor", new ContentSignEditor()); - Content.register("note_editor", new ContentNoteEditor()); + RegistryHelper.register(event.getRegistry(), "edit_blocks", new ContentEditBlocks()); + RegistryHelper.register(event.getRegistry(), "sign_editor", new ContentSignEditor()); + RegistryHelper.register(event.getRegistry(), "note_editor", new ContentNoteEditor()); //WORLD - Content.register("world", new ContentWorldInfo()); - Content.register("gamerules", new ContentGamerules()); - Content.register("recipes", new ContentRecipes()); + RegistryHelper.register(event.getRegistry(), "world", new ContentWorldInfo()); + RegistryHelper.register(event.getRegistry(), "gamerules", new ContentGamerules()); //PLAYER - Content.register("player", new ContentPlayer()); - Content.register("experience", new ContentExperience()); - Content.register("advancements", new ContentAdvancements()); + RegistryHelper.register(event.getRegistry(), "player", new ContentPlayer()); + RegistryHelper.register(event.getRegistry(), "experience", new ContentExperience()); + RegistryHelper.register(event.getRegistry(), "advancements", new ContentAdvancements()); //SCOREBOARD - Content.register("scoreboard_objectives", new ContentScoreboardObjectives()); - Content.register("scoreboard_teams", new ContentScoreboardTeams()); - Content.register("scoreboard_players", new ContentScoreboardPlayers()); + RegistryHelper.register(event.getRegistry(), "scoreboard_objectives", new ContentScoreboardObjectives()); + RegistryHelper.register(event.getRegistry(), "scoreboard_teams", new ContentScoreboardTeams()); + RegistryHelper.register(event.getRegistry(), "scoreboard_players", new ContentScoreboardPlayers()); //MISC - Content.register("change_world", new ContentChangeWorld()); - Content.register("continue", new ContentContinue()); + RegistryHelper.register(event.getRegistry(), "change_world", new ContentChangeWorld()); + RegistryHelper.register(event.getRegistry(), "continue", new ContentContinue()); //NO CATEGORY - Content.register("potions", new ContentPotions()); - Content.register("butcher", new ContentButcher()); - Content.register("butcher_settings", new ContentButcherSettings()); - Content.register("settings", new ContentSettings()); - } - - private static void register(String name, Content content) - { - Content.registerContent(new ResourceLocation(Main.MODID, name), content); - } - - private static void registerContent(ResourceLocation name, Content content) - { - content.setRegistryName(name); - REGISTRY.register(content); + RegistryHelper.register(event.getRegistry(), "potions", new ContentPotions()); + RegistryHelper.register(event.getRegistry(), "butcher", new ContentButcher()); + RegistryHelper.register(event.getRegistry(), "butcher_settings", new ContentButcherSettings()); + RegistryHelper.register(event.getRegistry(), "settings", new ContentSettings()); } private Map persistence; diff --git a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java index e8ec36d..0bf250f 100644 --- a/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java +++ b/src/main/java/exopandora/worldhandler/gui/content/impl/ContentScoreboardObjectives.java @@ -21,7 +21,7 @@ import exopandora.worldhandler.gui.content.impl.abstr.ContentScoreboard; import exopandora.worldhandler.gui.logic.ILogicClickList; import exopandora.worldhandler.helper.ActionHelper; import exopandora.worldhandler.helper.CommandHelper; -import exopandora.worldhandler.helper.RegistryTranslator; +import exopandora.worldhandler.helper.RegistryHelper; import net.minecraft.client.resources.I18n; import net.minecraft.stats.StatType; import net.minecraft.stats.Stats; @@ -89,7 +89,7 @@ public class ContentScoreboardObjectives extends ContentScoreboard return I18n.format(type.getTranslationKey()); } - String translation = RegistryTranslator.translate(resource); + String translation = RegistryHelper.translate(resource); if(translation != null) { diff --git a/src/main/java/exopandora/worldhandler/helper/RegistryTranslator.java b/src/main/java/exopandora/worldhandler/helper/RegistryHelper.java similarity index 56% rename from src/main/java/exopandora/worldhandler/helper/RegistryTranslator.java rename to src/main/java/exopandora/worldhandler/helper/RegistryHelper.java index 4053b62..c8b10ab 100644 --- a/src/main/java/exopandora/worldhandler/helper/RegistryTranslator.java +++ b/src/main/java/exopandora/worldhandler/helper/RegistryHelper.java @@ -6,6 +6,7 @@ import java.util.function.Function; import javax.annotation.Nullable; +import exopandora.worldhandler.Main; import net.minecraft.block.Block; import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.EntityType; @@ -19,30 +20,31 @@ import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistry; +import net.minecraftforge.registries.IForgeRegistryEntry; @OnlyIn(Dist.CLIENT) -public class RegistryTranslator +public class RegistryHelper { private static final Map, Function> FORGE = new HashMap, Function>(); private static final Map, Function> VANILLA = new HashMap, Function>(); static { - register(ForgeRegistries.BLOCKS, Block::getTranslationKey); - register(ForgeRegistries.ITEMS, Item::getTranslationKey); - register(ForgeRegistries.POTIONS, Effect::getName); - register(ForgeRegistries.BIOMES, Biome::getTranslationKey); - register(ForgeRegistries.ENCHANTMENTS, Enchantment::getName); - register(ForgeRegistries.ENTITIES, EntityType::getTranslationKey); - register(Registry.field_212623_l, stat -> "stat." + stat.toString().replace(':', '.')); + registerRegistry(ForgeRegistries.BLOCKS, Block::getTranslationKey); + registerRegistry(ForgeRegistries.ITEMS, Item::getTranslationKey); + registerRegistry(ForgeRegistries.POTIONS, Effect::getName); + registerRegistry(ForgeRegistries.BIOMES, Biome::getTranslationKey); + registerRegistry(ForgeRegistries.ENCHANTMENTS, Enchantment::getName); + registerRegistry(ForgeRegistries.ENTITIES, EntityType::getTranslationKey); + registerRegistry(Registry.field_212623_l, stat -> "stat." + stat.toString().replace(':', '.')); } - private static > void register(IForgeRegistry registry, Function mapper) + private static > void registerRegistry(IForgeRegistry registry, Function mapper) { FORGE.put(registry, mapper); } - private static void register(Registry registry, Function mapper) + private static void registerRegistry(Registry registry, Function mapper) { VANILLA.put(registry, mapper); } @@ -69,4 +71,14 @@ public class RegistryTranslator return null; } + + public static > void register(IForgeRegistry registry, String name, T entry) + { + register(registry, Main.MODID, name, entry); + } + + public static > void register(IForgeRegistry registry, String modid, String name, T entry) + { + registry.register(entry.setRegistryName(new ResourceLocation(modid, name))); + } }