Last active
May 29, 2019 09:48
-
-
Save Cadiboo/b825d62fb46538e7b7b262c5612d62aa to your computer and use it in GitHub Desktop.
[1.12.2] A simple EventSubscriber to correctly instantiate and register your Blocks, TileEntities, Items and Entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.cadiboo.examplemod; | |
import com.google.common.base.Preconditions; | |
import net.minecraft.block.Block; | |
import net.minecraft.block.material.Material; | |
import net.minecraft.crash.CrashReport; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemBlock; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraft.util.ReportedException; | |
import net.minecraft.util.ResourceLocation; | |
import net.minecraftforge.event.RegistryEvent; | |
import net.minecraftforge.fml.common.Mod; | |
import net.minecraftforge.event.RegistryEvent; | |
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | |
import net.minecraftforge.fml.common.registry.EntityEntry; | |
import net.minecraftforge.fml.common.registry.EntityEntryBuilder; | |
import net.minecraftforge.fml.common.registry.GameRegistry; | |
import net.minecraftforge.registries.IForgeRegistry; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import javax.annotation.Nonnull; | |
/** | |
* Subscribe to events that should be handled on both PHYSICAL sides in this class | |
* | |
* @author Cadiboo | |
*/ | |
@Mod.EventBusSubscriber(modid = ModReference.MOD_ID) | |
public final class EventSubscriber { | |
private static final Logger LOGGER = LogManager.getLogger(ModReference.MOD_ID + " Event Subscriber"); | |
private static int entityId = 0; | |
/** | |
* This method will be called by Forge when it is time for the mod to register its blocks. | |
* This method will always be called before the item registry method. | |
*/ | |
@SubscribeEvent | |
public static void onRegisterBlocksEvent(@Nonnull final RegistryEvent.Register<Block> event) { | |
event.getRegistry().registerAll( | |
setup(new Block(Material.GROUND), "example_block") | |
); | |
LOGGER.debug("Registered blocks"); | |
// registerTileEntity(TileEntityExampleTileEntity.class, "example_tile_entity"); | |
LOGGER.debug("Registered tile entities"); | |
} | |
/** | |
* Helper method to setup and register a TileEntity with the specified name. | |
*/ | |
private static void registerTileEntity(@Nonnull final Class<? extends TileEntity> clazz, @Nonnull final String name) { | |
GameRegistry.registerTileEntity(clazz, new ResourceLocation(ModReference.MOD_ID, name)); | |
} | |
/** | |
* This method will be called by Forge when it is time for the mod to register its items. | |
* This method will always be called after the block registry method. | |
*/ | |
@SubscribeEvent | |
public static void onRegisterItemsEvent(@Nonnull final RegistryEvent.Register<Item> event) { | |
final IForgeRegistry<Item> registry = event.getRegistry(); | |
registry.registerAll( | |
setup(new Item(), "example_item") | |
); | |
ForgeRegistries.BLOCKS.getValues().stream() | |
.filter(block -> block.getRegistryName().getNamespace().equals(ModReference.MOD_ID)) | |
.filter(EventSubscriber::hasItemBlock) | |
.forEach(block -> { | |
registry.register(setup(new ItemBlock(block), block.getRegistryName())); | |
}); | |
LOGGER.debug("Registered items"); | |
} | |
/** | |
* Helper method to determine if a block has an ItemBlock | |
*/ | |
private static boolean hasItemBlock(@Nonnull final Block block) { | |
return true; //change this to return false if the block doesn’t have an ItemBlock | |
} | |
/** | |
* This method will be called by Forge when it is time for the mod to register its entity entries. | |
*/ | |
@SubscribeEvent | |
public static void onRegisterEntitiesEvent(@Nonnull final RegistryEvent.Register<EntityEntry> event) { | |
final ResourceLocation exampleEntity1RegistryName = new ResourceLocation(ModReference.MOD_ID, "example_entity_1"); | |
final ResourceLocation exampleEntity2RegistryName = new ResourceLocation(ModReference.MOD_ID, "example_entity_2"); | |
// Comment out the code from here all the way down to "LOGGER.debug(" if you don't have entities | |
event.getRegistry().registerAll( | |
EntityEntryBuilder.create() | |
.entity(EntityExampleEntity1.class) | |
.id(exampleEntity1RegistryName, entityId++) | |
.name(exampleEntity1RegistryName.getPath()) | |
.tracker(REPLACE_maxUpdateDistanceInBlocks, REPLACE_timeBetweenUpdatePackets, REPLACE_shouldSendVelocityUpdates) | |
.egg(REPLACE_primaryColor, REPLACE_secondaryColor) | |
.build(), | |
EntityEntryBuilder.create() | |
.entity(EntityExampleEntity2.class) | |
.id(exampleEntity2RegistryName, entityId++) | |
.tracker(REPLACE_maxUpdateDistanceInBlocks, REPLACE_timeBetweenUpdatePackets, REPLACE_shouldSendVelocityUpdates) | |
.egg(REPLACE_primaryColor, REPLACE_secondaryColor) | |
.build() | |
); | |
LOGGER.debug("Registered entities"); | |
} | |
/** | |
* Helper method to correctly setup a registry entry. | |
* Calls setRegistryName with the appropriate parameters | |
* If applicable also calls setTranslationKey with the appropriate parameters | |
* @return Returns the entry passed in | |
*/ | |
@Nonnull | |
public static <T extends IForgeRegistryEntry> T setup(@Nonnull final T entry, @Nonnull final String name) { | |
return setup(entry, new ResourceLocation(ModReference.MOD_ID, name)); | |
} | |
/** | |
* Helper method to correctly setup a registry entry. | |
* Calls setRegistryName with the appropriate parameters | |
* If applicable also calls setTranslationKey with the appropriate parameters | |
* @return Returns the entry passed in | |
*/ | |
@Nonnull | |
public static <T extends IForgeRegistryEntry> T setup(@Nonnull final T entry, @Nonnull final ResourceLocation registryName) { | |
Preconditions.checkNotNull(entry, "entry to setup must not be null!"); | |
Preconditions.checkNotNull(registryName, "registry name to assign must not be null!"); | |
entry.setRegistryName(registryName); | |
if (entry instanceof Block) { | |
((Block) entry).setTranslationKey(registryName.getNamespace() + "." + registryName.getPath()); | |
} | |
if (entry instanceof Item) { | |
((Item) entry).setTranslationKey(registryName.getNamespace() + "." + registryName.getPath()); | |
} | |
return entry; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment