Posted August 9, 20205 yr Hello. How I can add and register custom countable stat without using net.minecraft.util.registry.Registry class? I know about this solution, but in Registry class we have this comment: /* * Attention Modders: This SHOULD NOT be used, you should use ForgeRegistries instead. As it has a cleaner modder facing API. * We will be wrapping all of these in our API as necessary for syncing and management. */
August 9, 20205 yr Author 45 minutes ago, diesieben07 said: The StatType registry has been "taken over" by Forge (as you can see by StatType extending ForgeRegistryEntry). This means you can register them the same way you would blocks, items or any other forge registry entry. Yes, I saw that, but when I'm trying to register new stat, it throws the IllegalStateException. public static final StatType<ResourceLocation> stat = Stats.CUSTOM; public static final ResourceLocation STAT_RL = new ResourceLocation(ModUncrafting.MODID, "stat_name"); @SubscribeEvent public static void registerCustomStat(RegistryEvent.Register<StatType<?>> event) { stat.setRegistryName(STAT_RL); event.getRegistry().register(stat); }
August 9, 20205 yr Author 1 minute ago, diesieben07 said: You need to make your own stat instance... Just like you cannot register Blocks.STONE as your own block you cannot register Stats.CUSTOM. Oh, man... It's basics of the OOP... Anyway, the constructor of StatType accepts only Registry instances. Can I avoid it?
August 9, 20205 yr Author public static final StatType<ResourceLocation> stat = new StatType<>(Registry.CUSTOM_STAT); public static final ResourceLocation STAT_RL = new ResourceLocation(ModUncrafting.MODID, "stat_name"); @SubscribeEvent public static void registerCustomStat(RegistryEvent.Register<StatType<?>> event) { stat.setRegistryName(STAT_RL); event.getRegistry().register(stat); } @SubscribeEvent public static void onMyEvent(MyEvent event) { if (event.player instanceof ServerPlayerEntity) { event.player.addStat(STAT_RL, 1); LOGGER.warn("Added +1 to stat"); } } And this code throws NullPointerException on addStat method
August 9, 20205 yr Author Yeah, I guess I have to. Even if I created a my own StatType class, I can't create Stat class, it throws null pointer in constructor. public class MyStatType extends StatType<ResourceLocation> { public MyStatType(ResourceLocation name) { super(Registry.CUSTOM_STAT); this.setRegistryName(name); } } public class MyStat extends Stat<ResourceLocation> { public MyStat(StatType<ResourceLocation> typeIn, ResourceLocation valueIn) { super(typeIn, valueIn, IStatFormatter.DEFAULT); //NullPointerException at Stat.locationToKey(Stat.java:28) } } public static final ResourceLocation STAT_RL = new ResourceLocation(MODID, "stat_name"); public static final UncraftedItemsStatType STAT_TYPE = new UncraftedItemsStatType(STAT_RL); public static final UncraftedItemsStat STAT = new UncraftedItemsStat(STAT_TYPE, STAT_RL);
August 9, 20205 yr Author It works just fine, but it uses the Registry class method, which is undesirable to use. public static final ResourceLocation MY_STAT = registerCustomStat("my_stat"); private static ResourceLocation registerCustomStat(String name) { ResourceLocation resourcelocation = new ResourceLocation(ModUncrafting.MODID, name); Registry.register(Registry.CUSTOM_STAT, resourcelocation, resourcelocation); Stats.CUSTOM.get(resourcelocation, IStatFormatter.DEFAULT); return resourcelocation; }
August 9, 20205 yr Author 2 minutes ago, diesieben07 said: Why? I don't know actually, but on top of the Registry class you can found this comment: /* * Attention Modders: This SHOULD NOT be used, you should use ForgeRegistries instead. As it has a cleaner modder facing API. * We will be wrapping all of these in our API as necessary for syncing and management. */
August 9, 20205 yr Author Oh, ok. And the last question, how I can add new stat via new DeferredRegister system? As far as I understand, every custom stat is referenced via Stats.CUSTOM, even player.addStat() do the same. public static final DeferredRegister<StatType<?>> register = new DeferredRegister<>(ForgeRegistries.STAT_TYPES, ModUncrafting.MODID); public static final RegistryObject<StatType<?>> type = register.register("my_stat", () -> new StatType<>(Registry.CUSTOM_STAT)); Edited August 9, 20205 yr by gosvoh
August 9, 20205 yr Author Actually, I just want to add a stat, but ResourceLocation is not extending from ForgeRegistryEntry, and I don't know how to make it
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.