Jump to content

Recommended Posts

Posted

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

 

 

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

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

    • Please read the FAQ and post logs as described there.   Also, do not just add a post onto someone else's thread with your issue, create a new one please.
    • I am creating a server with mods but when i try tostart it it say in the logs:   [29Jan2025 20:36:50.715] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/fmlcore/1.20.1-47.3.27/fmlcore-1.20.1-47.3.27.jar is missing mods.toml file 159[29Jan2025 20:36:50.717] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/javafmllanguage/1.20.1-47.3.27/javafmllanguage-1.20.1-47.3.27.jar 160[29Jan2025 20:36:50.717] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/javafmllanguage/1.20.1-47.3.27/javafmllanguage-1.20.1-47.3.27.jar is missing mods.toml file 161[29Jan2025 20:36:50.718] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/lowcodelanguage/1.20.1-47.3.27/lowcodelanguage-1.20.1-47.3.27.jar 162[29Jan2025 20:36:50.718] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/lowcodelanguage/1.20.1-47.3.27/lowcodelanguage-1.20.1-47.3.27.jar is missing mods.toml file 163[29Jan2025 20:36:50.719] [main/DEBUG] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Considering mod file candidate /server/libraries/net/minecraftforge/mclanguage/1.20.1-47.3.27/mclanguage-1.20.1-47.3.27.jar 164[29Jan2025 20:36:50.719] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file /server/libraries/net/minecraftforge/mclanguage/1.20.1-47.3.27/mclanguage-1.20.1-47.3.27.jar is missing mods.toml file
    • How do you configure the entity reach of a custom weapon? Asking for 1.21 Minecraft parchment
    • This topic is over a year old. If you are having an issue, please read the FAQ for the proper way to post logs, and create your own thread.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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