Jump to content

[1.12.2] Individual Instances of Items


SaemonMoki

Recommended Posts

Hey everyone!

 

I've come across an interesting use case of the custom items I am creating for my mod. The item is simple, and has no real complicated code. All I need, is each instance of the item to have a special array of different values sent for it to store given to it by what creates it.

 

Below are code snippets showing the default implementation. The names have been generalized for easy of readability.

 

ObjectHolder:

Spoiler

@ObjectHolder(Reference.MODID)
public class ModItems {
    public static final Item ENHANCED_CUSTOM_ITEM = new EnhancedCustomItem("enhancedItem", "enhanced_item");
}

 

The item class:

Spoiler

public class EnhancedItem extends Item {

    private String[] arrayOfDifferences;

    public EnhancedItem(String unlocalizedName, String registryName) {
        setUnlocalizedName(Reference.MODID + "." + unlocalizedName);
        setRegistryName(registryName);
        setCreativeTab(CreativeTabs.FOOD);
        setMaxStackSize(1);
    }
    
    public void setDifference(String[] arrayOfDifferences){
        this.arrayOfDifferences = arrayOfDifferences;
    }

}

 

Registry:

Spoiler

@EventBusSubscriber
public class RegistryHandler {

    @SubscribeEvent
    public static void registerItems(Register<Item> event) {
        final Item[] items = {ModItems.ENHANCED_CUSTOM_ITEM};

        event.getRegistry().registerAll(items);
    }

 

Renderer:

Spoiler

@EventBusSubscriber(Side.CLIENT)
public class ModelRegistryHandler {

    @SubscribeEvent
    public static void registerModels(ModelRegistryEvent event) {
        registerModel(ModItems.ENHANCED_CUSTOM_ITEM);
    }

    private static void registerModel(Item item) {
        ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));
    }

 

Entity that 'spawns'/drops the item

Spoiler

    public EnhancedEntity(World worldIn) {
        super(worldIn);
        this.setSize(0.4F, 0.7F);
    }

    public void onLivingUpdate()
    {
        super.onLivingUpdate();
        if (spawnReady) {
            this.dropItem(MobItems.ENHANCED_CUSTOM_ITEM), 1);
            this.spawnReady= false;
        }

    }

 

Now obviously the main issues, is that the item instantiated in MobItems, is the one and only instance, so obviously whenever I try to call the "setDifferences" method, it overrides what is in use. 

I can very easily circumvent this, by creating a new instance of the object(using the same registry names) when the entity calls "dropItem", passing it through the new item that I have called setDifferences.

 

This works. However the result is obviously an object that is a purpleblack box because the renderer doesn't have THAT specific object registered.

 

So my question is how can I possible have the spawning of new items done in a fashion were I can treat them all as their own individual objects and set their own variables?

 

If anyone has some ideas or possibly even separate implementations, I'm all ears. :)

 

 

Link to comment
Share on other sites

1 minute ago, SaemonMoki said:

If anyone has some ideas or possibly even separate implementations, I'm all ears. :)

Use an ItemStack capability, there will only ever be one Item instance of all items, however there can/will be multiple ItemStack instances.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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



×
×
  • Create New...

Important Information

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