-
-
Save Commoble/a60d03e52fb4a38c42ac4fd2a7153b4c to your computer and use it in GitHub Desktop.
Using addListener to subscribe event listeners in Forge for Minecraft 1.15
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
@Mod(YourMod.MODID) | |
public class YourMod | |
{ | |
public static final String MODID = "yourmod"; | |
// @Mod tells forge to instantiate this class once for you, this constructor will run before any registry events fire | |
public YourMod() | |
{ | |
IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); | |
IEventBus forgeBus = MinecraftForge.EVENT_BUS; | |
// registry events and most init events are on the MOD bus | |
// registry events and other events with generic parameters require addGenericListener | |
modBus.addGenericListener(Block.class, this::onRegisterBlocks); | |
modBus.addListener(this::onCommonSetup); | |
// most during-the-game events are on the FORGE bus | |
forgeBus.addListener(this::onPlayerInteractWithEntity) | |
// client-only events can be safely subscribed to by using DistExector to | |
// invoke a static method in another class and have that method subscribe to events | |
// ("Improved Proxying") | |
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> ClientEvents.subscribeClientEvents(modBus, forgeBus)); | |
// do anything else that should run before registry in this constructor | |
} | |
public void onRegisterBlocks(RegistryEvent.Register<Block> event) | |
{ | |
// register blocks | |
} | |
public void onCommonSetup(FMLCommonSetupEvent event) | |
{ | |
// do misc. things after registry concludes | |
} | |
public void onPlayerInteractWithEntity(PlayerInteractEvent.EntityInteract event) | |
{ | |
// example of an event that runs on the FORGE bus and not the MOD bus | |
} | |
} | |
// this should be in an entirely separate file | |
// any event listeners that should only be called on the client should be subscribed in here | |
// it's safe to refer to Minecraft and other client-only classes here thanks to DistExecutor | |
public class ClientEvents | |
{ | |
public static void subscribeClientEvents(IEventBus modBus, IEventBus forgeBus) | |
{ | |
// you can also subscribe static methods to the event bus | |
modBus.addListener(ClientEvents::onClientSetup); | |
} | |
// this event is for registering miscellaneous client things (fires *after* FMLCommonSetupEvent, and only on the client) | |
public static void onClientSetup(FMLClientSetupEvent event) | |
{ | |
// example: use ClientRegistry to register an Entity renderer or TileEntity renderer | |
ClientRegistry.bindTileEntityRenderer(YourTileEntityTypes.YOUR_TE_TYPE, YourTileEntityRenderer::new); // as of forge build 1.15.1-30.0.17 | |
// example: register an IBlockColor or IItemColor | |
Minecraft.getInstance().getItemColors().register(new YourItemColor(), YourItems.YOUR_ITEM); | |
// in 1.15+, this is also where RenderTypes are assigned to blocks (solid, cutout, translucent) | |
RenderTypeLookup.setRenderLayer(YourBlocks.YOUR_BLOCK, RenderType.func_228645_f_()); // declare your block translucent | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment