Jump to content

[1.15.2] Fix order in world when re-sorting item registries


xanderindalzone

Recommended Posts

1 hour ago, diesieben07 said:

Old tutorial, but with some changes to the method names it still applies:

 

thx for the guide...

 

33 minutes ago, poopoodice said:

probably fill() (not so sure)

and thx for the update...

its working

 

For anyone interesed on the end result, here it is:

 

1.I made a class to store the static comparator with a static list with all the items in the order I want

Quote

public class SortedItemList 
{
    
    private static List<Item> sortedItemList = Arrays.asList(
            InitItems.PISTOL_CAL_45_PROTOTYPE_BASIC.get(),
            InitItems.BARREL_CAL_45_LIGHT.get(), 
            InitItems.BARREL_CAL_45_MEDIUM.get(), 
            InitItems.BARREL_CAL_45_HEAVY.get(),
            InitItems.MAG_CAL_45_SMALL.get(),
            InitItems.MAG_CAL_45_BIG.get(),
            InitItems.MAG_CAL_45_DRUM.get(),
            InitItems.MAG_CAL_45_BOX.get(),
            InitItems.GUN_BODY_LIGHT.get(),
            InitItems.GUN_BODY_MEDIUM.get(),
            InitItems.GUN_BODY_HEAVY.get(),
            InitItems.PIPE_CAL_45.get(),
            InitItems.GUN_GRIP.get(),
            InitItems.GUN_BUTT.get());
    
    public static Comparator<ItemStack> tabSorter = Ordering.explicit(sortedItemList).onResultOf(ItemStack::getItem);
}

 

 

2. I overriden the fill() method in the CustomTab class that I wanted to sort, sorting the item list after calling super:

Quote

public class GunPartsGroup extends ItemGroup
{

    public GunPartsGroup(String label) 
    {
        super(label);
    }

    @Override
    public ItemStack createIcon() 
    {
        return new ItemStack(InitItems.BARREL_CAL_45_LIGHT.get());
    }
    
    @Override
    public void fill(NonNullList<ItemStack> items) {
        super.fill(items);
        items.sort(SortedItemList.tabSorter);
    }
    
    
}

 

Link to comment
Share on other sites

19 minutes ago, diesieben07 said:

Do not call RegistryObject#get in a static initializer, it defeats the purpose and can cause initialization-order problems. Store the RegistryObject itself instead.

Can I return the list from a static method instead, like this? this static variable is in the Mod's class

Quote

3a028db0777a4277d6ab55f177869b8d.png

 

Quote

75598a07f23ccbd5e38e3028f07e601b.png

 

if its not posible, how do I deal with this error changing the List<Item> to List<RegistryObject>? like you said here

22 minutes ago, diesieben07 said:

Store the RegistryObject itself instead.

Quote

a913bf851bceb50e9f929027363a1a8a.png

 

Link to comment
Share on other sites

6 hours ago, diesieben07 said:

The best way I can think of to do this is to just not do this in a static initializer, but in e.g. FMLCommonSetupEvent.

is this ok now?

I'm using the first List I had, List<Item>, because you said the issue was in the "static" initializing thing.

 

I'm creating a new SortedItemList object in CommonSetup so I can get the predifined List for the Comparator, then I use the Mod Class instance field to access that mod object's Comparator field.

 

its still working as expected ingame at least 😅

Quote

public class SortedItemList 
{
    
    public List<Item> sortedList = Arrays.asList(
                //Guns
                InitItems.PISTOL_COLT_1911.get(),
                
                //Bullets
                InitItems.BULLET_CAL_45.get(),
                
                //GunParts
                InitItems.PISTOL_CAL_45_PROTOTYPE_BASIC.get(),
                InitItems.BARREL_CAL_45_LIGHT.get(), 
                InitItems.BARREL_CAL_45_MEDIUM.get(), 
                InitItems.BARREL_CAL_45_HEAVY.get(),
                InitItems.MAG_CAL_45_SMALL.get(),
                InitItems.MAG_CAL_45_BIG.get(),
                InitItems.MAG_CAL_45_DRUM.get(),
                InitItems.MAG_CAL_45_BOX.get(),
                InitItems.GUN_BODY_LIGHT.get(),
                InitItems.GUN_BODY_MEDIUM.get(),
                InitItems.GUN_BODY_HEAVY.get(),
                InitItems.PIPE_CAL_45.get(),
                InitItems.GUN_GRIP.get(),
                InitItems.GUN_BUTT.get(),
                
                //Blocks
                InitItems.ARMORED_GLASS_ITEM_BLOCK.get()
                );
    ...............................................

Quote

/*===================*/
/*CREATIVE TAB SORTER*/
/*===================*/
public Comparator<ItemStack> tabSorter;

 

private void setup(final FMLCommonSetupEvent event)
    {
        PacketHandler.registerPackets();
        InitCapabilities.registerCapabilities();
        
        tabSorter = Ordering.explicit(new SortedItemList().sortedList).onResultOf(ItemStack::getItem);
    }

 

 

Overriden method in the ItemGroup class:

Quote

    @Override
    public void fill(NonNullList<ItemStack> items) {
        super.fill(items);
        items.sort(CustomGunsMod.instance.tabSorter);
    }

 

Link to comment
Share on other sites

The fuck is wrong with:

    public void fill(NonNullList<ItemStack> items) {
        items.add(
                InitItems.PISTOL_COLT_1911.get(),
                InitItems.BULLET_CAL_45.get(),
                InitItems.PISTOL_CAL_45_PROTOTYPE_BASIC.get(),
                InitItems.BARREL_CAL_45_LIGHT.get(), 
                InitItems.BARREL_CAL_45_MEDIUM.get(), 
                InitItems.BARREL_CAL_45_HEAVY.get(),
                InitItems.MAG_CAL_45_SMALL.get(),
                InitItems.MAG_CAL_45_BIG.get(),
                InitItems.MAG_CAL_45_DRUM.get(),
                InitItems.MAG_CAL_45_BOX.get(),
                InitItems.GUN_BODY_LIGHT.get(),
                InitItems.GUN_BODY_MEDIUM.get(),
                InitItems.GUN_BODY_HEAVY.get(),
                InitItems.PIPE_CAL_45.get(),
                InitItems.GUN_GRIP.get(),
                InitItems.GUN_BUTT.get(),
                InitItems.ARMORED_GLASS_ITEM_BLOCK.get()
        ); 
    }

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

40 minutes ago, diesieben07 said:

The issue is the static initializer, because you cannot guarantee when exactly it runs.

are you saying this because "Array.asList();" may run before the items are initialized?

 

47 minutes ago, xanderindalzone said:

public List<Item> sortedList = Arrays.asList(

 

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • The game crashed whilst exception ticking world Error: java.lang.NullPointerException: Cannot invoke "net.minecraft.resources.ResourceLocation.equals(Object)" because "this.lootTableId" is null Error given is above. I was already playing for around 15 minutes and wasn't doing anything specific or even breaking anything when the crashed happened. This is update 1.19.2 forge: 43.2.23 Mod list: ESSENTIAL Mod (by SparkUniverse_) Traveler's Titles (Forge) (by YUNGNICKYOUNG) Resourceful Config (by ThatGravyBoat) Dynamic Lights (by atomicstrykergrumpy) TenzinLib (by CommodoreThrawn) Nature's Compass (by Chaosyr) Library Ferret - Forge (by jtl_elisa) Cold Sweat (by Mikul) Simple Voice Chat (by henkelmax) Waystones (by BlayTheNinth) Carry On (by Tschipp) [Let's Do] Meadow (by satisfy) Creeper Overhaul (by joosh_7889) AutoRegLib (by Vazkii) Moonlight Lib (by MehVahdJukaar) AppleSkin (by squeek502) Xaero's World Map (by xaero96) Rotten Creatures (by fusionstudiomc) YUNG's API (Forge) (by YUNGNICKYOUNG) Village Artifacts (by Lothrazar) Right Click, Get Crops (by TeamCoFH) Supplementaries (by MehVahdJukaar) Automatic Tool Swap (by MelanX) Better Third Person (by Socolio) Supplementaries Squared (by plantspookable) Traveler's Backpack (by Tiviacz1337) Caelus API (Forge/NeoForge) (by TheIllusiveC4) Creatures and Beasts (by joosh_7889) Architectury API (Fabric/Forge/NeoForge) (by shedaniel) Quark Oddities (by Vazkii) Origins (Forge) (by EdwinMindcraft) Villager Names (by Serilum) GeckoLib (by Gecko) Realistic Bees (by Serilum) Illuminations Forge 🔥 (by dimadencep) Serene Seasons (by TheAdubbz) Critters and Companions (by joosh_7889) [Let's Do] Bakery (by satisfy) Falling Leaves (Forge) (by Cheaterpaul) Jade 🔍 (by Snownee) Collective (by Serilum) TerraBlender (Forge) (by TheAdubbz) [Let's Do] API (by Cristelknight) Towns and Towers (by Biban_Auriu) More Villagers (by SameDifferent) Biomes O' Plenty (by Forstride) Goblin Traders (by MrCrayfish) Corpse (by henkelmax) Tree Harvester (by Serilum) Balm (Forge Edition) (by BlayTheNinth) Mouse Tweaks (by YaLTeR) Sound Physics Remastered (by henkelmax) Xaero's Minimap (by xaero96) Just Enough Items (JEI) (by mezz) Terralith (by Starmute) Quark (by Vazkii) [Let's Do] Vinery (by satisfy) [Let's Do] Candlelight (by satisfy) Repurposed Structures (Neoforge/Forge) (by telepathicgrunt) Dusty Decorations (by flint_mischiff) Immersive Armors [Fabric/Forge] (by Conczin) Serene Seasons Fix (by Or_OS) Shutup Experimental Settings! (by Corgi_Taco) Skin Layers 3D (Fabric/Forge) (by tr7zw)
    • Make sure Java is running via Nvidia GPU https://windowsreport.com/minecraft-not-using-gpu/  
    • Also make a test with other Custom Launchers like AT Launcher, MultiMC or Technic Launcher
    • Embeddium and valkyrienskies are not working together - remove one of these mods With removing Embeddium, also remove Oculus, TexTrue's Embeddium Options and Embeddium Extras Or use Rubidium instead
  • Topics

×
×
  • Create New...

Important Information

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