Skip to content

Commit 8f86dc8

Browse files
committed
Add debug profiler information to registry entries.
1 parent af61317 commit 8f86dc8

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

Common/src/main/java/net/darkhax/bookshelf/api/registry/ClassRegistryEntries.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77

88
public class ClassRegistryEntries<V> extends RegistryEntries<Tuple<Class, V>> {
99

10-
public ClassRegistryEntries(String ownerId) {
10+
public ClassRegistryEntries(Supplier<String> idProvider, String type) {
1111

12-
super(ownerId);
13-
}
14-
15-
public ClassRegistryEntries(Supplier<String> idProvider) {
16-
17-
super(idProvider);
12+
super(idProvider, type);
1813
}
1914

2015
public void add(Class clazz, Supplier<V> instance, String id) {

Common/src/main/java/net/darkhax/bookshelf/api/registry/RegistryDataProvider.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import net.darkhax.bookshelf.api.commands.ICommandBuilder;
88
import net.darkhax.bookshelf.mixin.item.AccessorItem;
99
import net.minecraft.commands.synchronization.ArgumentSerializer;
10+
import net.minecraft.commands.synchronization.ArgumentTypes;
11+
import net.minecraft.core.Registry;
1012
import net.minecraft.core.particles.ParticleType;
1113
import net.minecraft.sounds.SoundEvent;
1214
import net.minecraft.stats.StatType;
@@ -21,6 +23,7 @@
2123
import net.minecraft.world.item.alchemy.Potion;
2224
import net.minecraft.world.item.crafting.RecipeSerializer;
2325
import net.minecraft.world.item.enchantment.Enchantment;
26+
import net.minecraft.world.item.enchantment.EnchantmentHelper;
2427
import net.minecraft.world.level.ItemLike;
2528
import net.minecraft.world.level.block.Block;
2629
import net.minecraft.world.level.block.entity.BlockEntityType;
@@ -32,25 +35,25 @@ public class RegistryDataProvider {
3235

3336
private final String ownerId;
3437

35-
public final IOwnedRegistryEntries<Block> blocks = new RegistryEntries<>(this::getOwner);
36-
public final IOwnedRegistryEntries<Fluid> fluids = new RegistryEntries<>(this::getOwner);
37-
public final IOwnedRegistryEntries<Item> items = new RegistryEntries<>(this::getOwner);
38-
public final IOwnedRegistryEntries<MobEffect> mobEffects = new RegistryEntries<>(this::getOwner);
39-
public final IOwnedRegistryEntries<SoundEvent> sounds = new RegistryEntries<>(this::getOwner);
40-
public final IOwnedRegistryEntries<Potion> potions = new RegistryEntries<>(this::getOwner);
41-
public final IOwnedRegistryEntries<Enchantment> enchantments = new RegistryEntries<>(this::getOwner);
42-
public final IOwnedRegistryEntries<EntityType<?>> entities = new RegistryEntries<>(this::getOwner);
43-
public final IOwnedRegistryEntries<BlockEntityType<?>> blockEntities = new RegistryEntries<>(this::getOwner);
44-
public final IOwnedRegistryEntries<ParticleType<?>> particleTypes = new RegistryEntries<>(this::getOwner);
45-
public final IOwnedRegistryEntries<MenuType<?>> menus = new RegistryEntries<>(this::getOwner);
46-
public final IOwnedRegistryEntries<RecipeSerializer<?>> recipeSerializers = new RegistryEntries<>(this::getOwner);
47-
public final IOwnedRegistryEntries<Motive> paintings = new RegistryEntries<>(this::getOwner);
48-
public final IOwnedRegistryEntries<Attribute> attributes = new RegistryEntries<>(this::getOwner);
49-
public final IOwnedRegistryEntries<StatType<?>> stats = new RegistryEntries<>(this::getOwner);
50-
public final IOwnedRegistryEntries<VillagerProfession> villagerProfessions = new RegistryEntries<>(this::getOwner);
51-
52-
public final ClassRegistryEntries<ArgumentSerializer> commandArguments = new ClassRegistryEntries<>(this::getOwner);
53-
public final IOwnedRegistryEntries<ICommandBuilder> commands = new RegistryEntries<>(this::getOwner);
38+
public final IOwnedRegistryEntries<Block> blocks = new RegistryEntries<>(this::getOwner, Registry.BLOCK_REGISTRY);
39+
public final IOwnedRegistryEntries<Fluid> fluids = new RegistryEntries<>(this::getOwner, Registry.FLUID_REGISTRY);
40+
public final IOwnedRegistryEntries<Item> items = new RegistryEntries<>(this::getOwner, Registry.ITEM_REGISTRY);
41+
public final IOwnedRegistryEntries<MobEffect> mobEffects = new RegistryEntries<>(this::getOwner, Registry.MOB_EFFECT_REGISTRY);
42+
public final IOwnedRegistryEntries<SoundEvent> sounds = new RegistryEntries<>(this::getOwner, Registry.SOUND_EVENT_REGISTRY);
43+
public final IOwnedRegistryEntries<Potion> potions = new RegistryEntries<>(this::getOwner, Registry.POTION_REGISTRY);
44+
public final IOwnedRegistryEntries<Enchantment> enchantments = new RegistryEntries<>(this::getOwner, Registry.ENCHANTMENT_REGISTRY);
45+
public final IOwnedRegistryEntries<EntityType<?>> entities = new RegistryEntries<>(this::getOwner, Registry.ENTITY_TYPE_REGISTRY);
46+
public final IOwnedRegistryEntries<BlockEntityType<?>> blockEntities = new RegistryEntries<>(this::getOwner, Registry.BLOCK_ENTITY_TYPE_REGISTRY);
47+
public final IOwnedRegistryEntries<ParticleType<?>> particleTypes = new RegistryEntries<>(this::getOwner, Registry.PARTICLE_TYPE_REGISTRY);
48+
public final IOwnedRegistryEntries<MenuType<?>> menus = new RegistryEntries<>(this::getOwner, Registry.MENU_REGISTRY);
49+
public final IOwnedRegistryEntries<RecipeSerializer<?>> recipeSerializers = new RegistryEntries<>(this::getOwner, Registry.PARTICLE_TYPE_REGISTRY);
50+
public final IOwnedRegistryEntries<Motive> paintings = new RegistryEntries<>(this::getOwner, Registry.MOTIVE_REGISTRY);
51+
public final IOwnedRegistryEntries<Attribute> attributes = new RegistryEntries<>(this::getOwner, Registry.ATTRIBUTE_REGISTRY);
52+
public final IOwnedRegistryEntries<StatType<?>> stats = new RegistryEntries<>(this::getOwner, Registry.STAT_TYPE_REGISTRY);
53+
public final IOwnedRegistryEntries<VillagerProfession> villagerProfessions = new RegistryEntries<>(this::getOwner, Registry.VILLAGER_PROFESSION_REGISTRY);
54+
55+
public final ClassRegistryEntries<ArgumentSerializer> commandArguments = new ClassRegistryEntries<>(this::getOwner, "Command Argument");
56+
public final IOwnedRegistryEntries<ICommandBuilder> commands = new RegistryEntries<>(this::getOwner, "Command");
5457
public final VillagerTradeEntries trades = new VillagerTradeEntries();
5558

5659
public RegistryDataProvider(String ownerId) {

Common/src/main/java/net/darkhax/bookshelf/api/registry/RegistryEntries.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package net.darkhax.bookshelf.api.registry;
22

3+
import com.mojang.logging.LogUtils;
34
import net.darkhax.bookshelf.api.function.CachedSupplier;
5+
import net.darkhax.bookshelf.api.util.MathsHelper;
6+
import net.minecraft.core.Registry;
7+
import net.minecraft.resources.ResourceKey;
48
import net.minecraft.resources.ResourceLocation;
9+
import org.slf4j.Logger;
510

611
import java.util.Collections;
712
import java.util.Iterator;
@@ -14,6 +19,8 @@
1419

1520
public class RegistryEntries<V> implements IOwnedRegistryEntries<V> {
1621

22+
private final Logger logger = LogUtils.getLogger();
23+
private final String name;
1724
private final CachedSupplier<String> ownerId;
1825
private final Map<ResourceLocation, IRegistryObject<? extends V>> rawValues = new LinkedHashMap<>();
1926
private final Map<ResourceLocation, V> entries = new LinkedHashMap<>();
@@ -22,14 +29,15 @@ public class RegistryEntries<V> implements IOwnedRegistryEntries<V> {
2229

2330
private boolean built = false;
2431

25-
public RegistryEntries(String ownerId) {
32+
public RegistryEntries(Supplier<String> idProvider, ResourceKey<?> registryKey) {
2633

27-
this(() -> ownerId);
34+
this(idProvider, registryKey.registry() + " (" + registryKey.location() + ")") ;
2835
}
2936

30-
public RegistryEntries(Supplier<String> idProvider) {
37+
public RegistryEntries(Supplier<String> idProvider, String name) {
3138

3239
this.ownerId = CachedSupplier.cache(idProvider);
40+
this.name = name;
3341
}
3442

3543
@Override
@@ -81,20 +89,34 @@ public void addRegistryListener(BiConsumer<ResourceLocation, V> listener) {
8189
@Override
8290
public void build(BiConsumer<ResourceLocation, V> registerFunc) {
8391

92+
if (this.built) {
93+
94+
this.logger.debug("Rebuilding entries for {}. Previous entries {}", this.ownerId.get(), this.entries.size());
95+
}
96+
8497
this.built = true;
8598
this.entries.clear();
86-
this.rawValues.forEach((k, v) -> {
8799

88-
final V value = v.get();
100+
if (!this.rawValues.isEmpty()) {
101+
102+
final long startTime = System.nanoTime();
89103

90-
if (value != null) {
104+
this.rawValues.forEach((k, v) -> {
91105

92-
this.entries.put(k, value);
93-
}
106+
final V value = v.get();
94107

95-
registerFunc.accept(k, value);
96-
this.registryListeners.forEach(listener -> listener.accept(k, value));
97-
});
108+
if (value != null) {
109+
110+
this.entries.put(k, value);
111+
}
112+
113+
registerFunc.accept(k, value);
114+
this.registryListeners.forEach(listener -> listener.accept(k, value));
115+
});
116+
117+
final long endTime = System.nanoTime();
118+
this.logger.debug("({}) Built {} {} entries. Took {}.", this.ownerId.get(), this.entries.size(), this.name, MathsHelper.profileNanoTime(startTime, endTime));
119+
}
98120
}
99121

100122
@Override

0 commit comments

Comments
 (0)