Jump to content

How do I make a drink item?


Recommended Posts

I have been working on my mod for some time now and I am wanting to add a drink item, I am new to modding and am currently self teaching myself java as I follow tutorials. I am hoping that the code would not need a massive reworking. 

public static final FoodProperties LEMON_JUICE = new FoodProperties.Builder().nutrition(3)
            .saturationMod(0.25f).effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500),0.01f)
            .effect(() -> new MobEffectInstance(MobEffects.CONFUSION,200),1).build();

In game the lemon juice acts like a food, making eating audio and creating particles. For the audio I would like to use the honey drinking sound. 

How would I get this to work as I am wanting it to? 

Link to comment
Share on other sites

It looks like you'll need to implement a custom class for the item; that's how the sounds are handled. Look at the class HoneyBottleItem to see how it's done. Alternatively, you could just use the HoneyBottleItem class for your item, if you're trying to make a bottled drink of some kind. That might be simpler. Reply to this post if you're having trouble, and I'll see if I can help.

You'll use the HoneyBottleItem class (or your custom class) when you're registering the item, not when you're creating the properties. Thought I should probably make that clear.

Link to comment
Share on other sites

For the HoneyBottleItem class, would I just copy/paste my Lemon Juice item into the class or would I have to make other changes? Also, for what you first said, I have copied the "getEatingSound" and the "getDrinkingSound" from the class into my ModFoods class (I could link the whole class if that would help you understand more), though nothing seems to have been done. I also do not see a way to prevent particles from appearing when the item is drank. (Could you also post some examples if possible, that would be a great help to see where I went wrong).

 

I am at a point where I am quite clueless as the tutorials on YouTube do not cover the area I am needing to go into. I do hope that this is not too much of a bother for you. 

Link to comment
Share on other sites

Apologies for taking so long to respond, I forgot to check the forums for a while. You can probably just use the HoneyBottleItem class when registering your item, if you don't need custom behavior.

Here's an example of what you might have to do:

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register(
  			"lemon_juice",
			() -> new Item(
            	new HoneyBottleItem.Properties().stacksTo(1).food(
                	(new FoodProperties.Builder())
                    .nutrition(4)
                    .saturationMod(0.1F)
                    .effect(() -> new MobEffectInstance(MobEffects.GLOWING, 100, 0), 0.8F)
                    .build()
             	)
         	)
         );

This is a random food item I grabbed from the mod I'm working on, I just renamed it to lemon juice so you could see what it would look like. 

Don't worry about being clueless, YouTube will only take you so far. I'd recommend looking at published mods on GitHub to see how to do things, it makes life easier. Just remember to give attribution if you use any of their code, and check their licenses. 

 

If you do need to register your item, you can just make a class that inherits from HoneyBottleItem, and then modify what you need in there.

 

  • Thanks 1
Link to comment
Share on other sites

Thank you so, so much for your help! I have implemented the code from the example you gave me and it has been excepted, the only downside currently being that the game crashes on launch, saying: 

  • Caused by: java.lang.IllegalStateException: Cannot register new entries to DeferredRegister after RegisterEvent has been fired.  (Three times)

and 

  • Caused by: java.lang.ExceptionInInitializerError (Two times)

With further scanning I have found this error:

  • 2024-05-05T18:41:24.287+0100 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]

I am not exactly sure what to do with these errors but I am sure I can resolve them. Once again, thank you for your help! It is much appreciated. 😁

Link to comment
Share on other sites

It sounds like you're probably registering the item in the wrong place, try looking at this tutorial for how to register items: 

Forge Modding Tutorial - Minecraft 1.20: Custom Items & Creative Mode Tab | #2

This (free) tutorial series is excellent, by the way, and I'd highly recommend watching through some or all of the videos. There may also be an error in the code I showed above since I was in a hurry, but it should be enough for the general idea. I can't be more specific since I don't know exactly what you plan to do.

Link to comment
Share on other sites

I have been following these exact tutorials for quite a while, I must agree that they are amazing and easy to follow. I have registered the item in the ModFoods class, I tried to do it in ModItems (Where all the items should be registered) but got errors, I think I may need to revert this and figure it out from there. Once again, thank you for your help! 👍

Just looking back, I have noticed in your code you added ITEMS.register, which I am guessing means that they are being registered in ModFoods, I shall go through the process of trial and error to figure this out.

Link to comment
Share on other sites

I have done this now but have got the error: 

 'food(net.minecraft.world.food.FoodProperties)' in 'net.minecraft.world.item.Item.Properties' cannot be applied to                '(net.minecraftforge.registries.RegistryObject<net.minecraft.world.item.Item>)'

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register(
            "lemon_juice",
            () -> new Item(
                    new HoneyBottleItem.Properties().stacksTo(1).food(
                            (new FoodProperties.Builder())
                                    .nutrition(3)
                                    .saturationMod(0.25F)
                                    .effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.01f )
                                    .build()
                    )
            ));

The code above is from the ModFoods class, the one below from the ModItems class.

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice",
            () -> new Item(new Item.Properties().food(ModFoods.LEMON_JUICE)));

 

I shall keep going between them to try and figure out the cause. I am sorry if this is too much for you to help with, though I thank you greatly for your patience and all the effort you have put in to help me.

Link to comment
Share on other sites

Apologies for the late reply.

You'll need to register the item in ModItems; if you're following those tutorials, that's the only place you should ever register items. Otherwise, the mod will fail to register them properly and you'll get all sorts of interesting errors. Looking back at the code snipped I posted, I think that actually has some errors. I'm adding a lemon juice bottle to my mod just to ensure that it works correctly, and I will reply when I have solved the problems.

Link to comment
Share on other sites

Corrected item registration code: (for the ModItems class)

public static final RegistryObject<Item> LEMON_JUICE_BOTTLE = ITEMS.register("lemon_juice_bottle",
			() -> new HoneyBottleItem(new Item.Properties().stacksTo(1)
					.food((new FoodProperties.Builder()).nutrition(3).saturationMod(0.25F)
							.effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.5f).build())));
Link to comment
Share on other sites

I have corrected the code as you have written it, though there have been errors present in the ModItems class when I have registered the item. (These have been present before but I forgot to mention them 🤦‍♂️). 

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice",
            () -> new Item(new Item.Properties().food(ModItems.LEMON_JUICE)));

The first error occurs with the ModItems.LEMON_JUICE, the message is as follows:

'food(net.minecraft.world.food.FoodProperties)' in 'net.minecraft.world.item.Item.Properties' cannot be applied to '(net.minecraftforge.registries.RegistryObject<net.minecraft.world.item.Item>)'

 

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice",
            () -> new Item(new Item.Properties().food(FoodProperties.LEMON_JUICE)));

The second error occurs with the FoodProperties.LEMON_JUICE, saying:

Cannot resolve symbol 'LEMON_JUICE'

This should be all that is left to do to finally fix all these errors that has been going on for two weeks now surprisingly. 

Link to comment
Share on other sites

It looks like you're trying to pass the lemon juice item itself to the .food method in your first code snippet; that won't work. The parameter to the .food method needs to be a FoodProperties object, as the error says.

The second way you're doing it there is much closer to how it should be. The problem there is that you're trying to access the LEMON_JUICE field of the vanilla FoodProperties class. This won't work because the vanilla food properties class does not have LEMON_JUICE information in it. You'll need to make your own ModFoods class, looking something like the vanilla Foods class. Here's an example:

public class ModFoods {

	public static final FoodProperties LEMON_JUICE = (new FoodProperties.Builder()).nutrition(3).saturationMod(0.25F)
			.effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.5f).build();
}

Then, your completed item registration would be:

public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice",
            () -> new Item(new Item.Properties().food(ModFoods.LEMON_JUICE)));

Notice that I changed the FoodProperties to ModFoods, since that's the class where you'd be storing the food data.

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

    • Hi, has anyone dealt with gradlew :runClient crashing on start before? I've currently made a forge 1.18.2 mod with the latest mdk and I use IntelliJ IDEA 2024.1 as my IDE, and now whenever I try to run the `:runClient` command with gradle, it works at first, but then the process stops reporting this issue: `Caused by: java.lang.reflect.InvocationTargetException` and `Caused by: java.lang.NoSuchMethodError: 'int net.minecraft.util.Mth.m_14045_(int, int, int)'`. I've pasted the full runClient gradle log in this message. I investigated this issue further on the forge forums to find that not much people had encountered it, and those who did encounter it somehow fixed it with fixes that does not work for me like deleting cache and let gradle redownload the cache or anything and that this issue is caused by a "Corrupted Cache", or whatever the heck that meant. I tried cloning my entire repo (https://github.com/Type-32/PreciseManufacturing) to another directory to "start fresh" but the same issue persists. I created a new project with a clean forge mod 1.18.2 template but the issue persists. I tried all the fixes I can find but none of them worked. even `.\gradlew --refresh-dependencies` didn't work. I am getting desparate for any help now ~~this is so freaking frustrating~~ This is my build.gradle file plugins { id 'eclipse' id 'idea' id 'net.minecraftforge.gradle' version '[6.0.16,6.2)' id 'org.parchmentmc.librarian.forgegradle' version '1.+' } group = mod_group_id version = mod_version base { archivesName = mod_id } java { toolchain.languageVersion = JavaLanguageVersion.of(17) } minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { // applies to all the run configs below configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id property 'mixin.env.remapRefMap', 'true' property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg" } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { // example of overriding the workingDirectory set in configureEach above workingDirectory project.file('run-data') // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources. args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } jarJar.enable() reobf { jarJar { } } tasks.jarJar.finalizedBy('reobfJarJar') // Include resources generated by data generators. sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { maven { name = 'tterrag maven' url = 'https://maven.tterrag.com/' } mavenLocal() } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation fg.deobf("com.simibubi.create:create-${create_minecraft_version}:${create_version}:slim") { transitive = false } implementation fg.deobf("com.jozufozu.flywheel:flywheel-forge-${flywheel_minecraft_version}:${flywheel_version}") implementation fg.deobf("com.tterrag.registrate:Registrate:${registrate_version}") // [MC<minecraft_version>,MC<next_minecraft_version>) jarJar(group: 'com.tterrag.registrate', name: 'Registrate', version: "[MC1.18.2,MC1.19)") // Example mod dependency with JEI - using fg.deobf() ensures the dependency is remapped to your development mappings // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-common-api:${jei_version}") // compileOnly fg.deobf("mezz.jei:jei-${mc_version}-forge-api:${jei_version}") // runtimeOnly fg.deobf("mezz.jei:jei-${mc_version}-forge:${jei_version}") // Example mod dependency using a mod jar from ./libs with a flat dir repository // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar // The group id is ignored when searching -- in this case, it is "blank" // implementation fg.deobf("blank:coolmod-${mc_version}:${coolmod_version}") } tasks.named('processResources', ProcessResources).configure { var replaceProperties = [ minecraft_version: minecraft_version, minecraft_version_range: minecraft_version_range, forge_version: forge_version, forge_version_range: forge_version_range, loader_version_range: loader_version_range, mod_id: mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version, mod_authors: mod_authors, mod_description: mod_description, ] inputs.properties replaceProperties filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { expand replaceProperties + [project: project] }} // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ "Specification-Title": mod_id, "Specification-Vendor": mod_authors, "Specification-Version": "1", // We are version 1 of ourselves "Implementation-Title": project.name, "Implementation-Version": project.jar.archiveVersion, "Implementation-Vendor": mod_authors, "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ") ]) } // This is the preferred method to reobfuscate your jar file finalizedBy 'reobfJar' } tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation }  
    • pastebin.com and paste.ee couldn't handle the debug and crash log sizes, apologies I have to send it on mega, and I hope it's not a problem Basically, Minecraft turns on normally but when I try to create a world, it goes to 100%, joining world, saving world and crashes   debug log https://mega.nz/file/kP1nGDKZ decryption key: C_VSH-IO6Kpi9IdqUs2Z0KDu0Fpujmen_I3rI1yUyVw crash log https://mega.nz/file/RXVEhRpZ 0r05xiqoGmL8rQEXjYaf8Q8BO-XbJGzpeBDek3aqb0w
  • Topics

×
×
  • Create New...

Important Information

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