/
ModEventSubscriber.java
85 lines (78 loc) · 3.51 KB
/
ModEventSubscriber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package io.github.cadiboo.examplemod;
import io.github.cadiboo.examplemod.config.ConfigHelper;
import io.github.cadiboo.examplemod.config.ConfigHolder;
import io.github.cadiboo.examplemod.init.ModBlocks;
import io.github.cadiboo.examplemod.init.ModItemGroups;
import io.github.cadiboo.examplemod.item.ModdedSpawnEggItem;
import net.minecraft.entity.EntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.registries.IForgeRegistry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Subscribe to events from the MOD EventBus that should be handled on both PHYSICAL sides in this class
*
* @author Cadiboo
*/
@EventBusSubscriber(modid = ExampleMod.MODID, bus = EventBusSubscriber.Bus.MOD)
public final class ModEventSubscriber {
private static final Logger LOGGER = LogManager.getLogger(ExampleMod.MODID + " Mod Event Subscriber");
/**
* 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 onRegisterItems(final RegistryEvent.Register<Item> event) {
final IForgeRegistry<Item> registry = event.getRegistry();
// Automatically register BlockItems for all our Blocks
ModBlocks.BLOCKS.getEntries().stream()
.map(RegistryObject::get)
// You can do extra filtering here if you don't want some blocks to have an BlockItem automatically registered for them
// .filter(block -> needsItemBlock(block))
// Register the BlockItem for the block
.forEach(block -> {
// Make the properties, and make it so that the item will be on our ItemGroup (CreativeTab)
final Item.Properties properties = new Item.Properties().group(ModItemGroups.MOD_ITEM_GROUP);
// Create the new BlockItem with the block and it's properties
final BlockItem blockItem = new BlockItem(block, properties);
// Set the new BlockItem's registry name to the block's registry name
blockItem.setRegistryName(block.getRegistryName());
// Register the BlockItem
registry.register(blockItem);
});
LOGGER.debug("Registered BlockItems");
}
/**
* This method will be called by Forge when a config changes.
*/
@SubscribeEvent
public static void onModConfigEvent(final ModConfig.ModConfigEvent event) {
final ModConfig config = event.getConfig();
// Rebake the configs when they change
if (config.getSpec() == ConfigHolder.CLIENT_SPEC) {
ConfigHelper.bakeClient(config);
LOGGER.debug("Baked client config");
} else if (config.getSpec() == ConfigHolder.SERVER_SPEC) {
ConfigHelper.bakeServer(config);
LOGGER.debug("Baked server config");
}
}
/**
* Exists to work around a limitation with Spawn Eggs:
* Spawn Eggs require an EntityType, but EntityTypes are created AFTER Items.
* Therefore it is "impossible" for modded spawn eggs to exist.
* To get around this we have our own custom SpawnEggItem, but it needs
* some extra setup after Item and EntityType registration completes.
*/
@SubscribeEvent(priority = EventPriority.LOWEST)
public static void onPostRegisterEntities(final RegistryEvent.Register<EntityType<?>> event) {
ModdedSpawnEggItem.initUnaddedEggs();
}
}