Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

 

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

 

  • 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?

  • 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

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

 

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

 

  • 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.
 */
  • 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 by gosvoh

  • 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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.