Change child mod registration from IMC to proper registry events

This commit is contained in:
Marcel Konrad
2019-06-22 15:04:36 +02:00
parent dff636eb34
commit f985c1b0ab
5 changed files with 117 additions and 114 deletions

View File

@@ -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);
}
}

View File

@@ -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<Category>
{
public static final IForgeRegistry<Category> REGISTRY = new RegistryBuilder<Category>()
.setType(Category.class)
.setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "category"})))
.disableSync()
.disableSaving()
.create();
public static IForgeRegistry<Category> REGISTRY;
private final List<Content> contents;
private final List<ResourceLocation> contents;
public Category()
{
this.contents = new ArrayList<Content>();
this.contents = Lists.newArrayList();
}
public Category(List<Content> contents)
public Category(List<ResourceLocation> 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<Content> getContents()
public Category add(String key)
{
return this.add(new ResourceLocation(Main.MODID, key));
}
public List<ResourceLocation> getContents()
{
return this.contents;
}
@@ -63,28 +72,29 @@ public class Category extends ForgeRegistryEntry<Category>
@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<Category>()
.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<Category> 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"));
}
}

View File

@@ -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<Content> implements IContent
{
public static final IForgeRegistry<Content> REGISTRY = new RegistryBuilder<Content>()
.setType(Content.class)
.setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "content"})))
.disableSync()
.disableSaving()
.create();
public static IForgeRegistry<Content> REGISTRY;
public static void registerContents()
@SubscribeEvent
public static void createRegistry(NewRegistry event)
{
REGISTRY = new RegistryBuilder<Content>()
.setType(Content.class)
.setName(new ResourceLocation(String.join("_", new String[] {Main.MODID, "content"})))
.disableSaving()
.disableSync()
.create();
}
@SubscribeEvent
public static void register(Register<Content> 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<String, Object> persistence;

View File

@@ -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)
{

View File

@@ -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<IForgeRegistry<?>, Function<?, String>> FORGE = new HashMap<IForgeRegistry<?>, Function<?, String>>();
private static final Map<Registry<?>, Function<?, String>> VANILLA = new HashMap<Registry<?>, Function<?, String>>();
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 <T extends ForgeRegistryEntry<T>> void register(IForgeRegistry<T> registry, Function<T, String> mapper)
private static <T extends ForgeRegistryEntry<T>> void registerRegistry(IForgeRegistry<T> registry, Function<T, String> mapper)
{
FORGE.put(registry, mapper);
}
private static <T> void register(Registry<T> registry, Function<T, String> mapper)
private static <T> void registerRegistry(Registry<T> registry, Function<T, String> mapper)
{
VANILLA.put(registry, mapper);
}
@@ -69,4 +71,14 @@ public class RegistryTranslator
return null;
}
public static <T extends IForgeRegistryEntry<T>> void register(IForgeRegistry<T> registry, String name, T entry)
{
register(registry, Main.MODID, name, entry);
}
public static <T extends IForgeRegistryEntry<T>> void register(IForgeRegistry<T> registry, String modid, String name, T entry)
{
registry.register(entry.setRegistryName(new ResourceLocation(modid, name)));
}
}