Jump to content

[1.15] Custom countable stat


gosvoh

Recommended Posts

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.
 */

 

Link to comment
Share on other sites

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);
}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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;
    }

 

Link to comment
Share on other sites

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.
 */
Link to comment
Share on other sites

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 by gosvoh
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.