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

I have managed to implement your code after finally managing to sort out my error with exporting to .zip, though when the game launches, it will not allow me to proceed further and gives this error:

 

Caused by: java.lang.IllegalArgumentException: Duplicate registration orange

 

I have checked over my code and no errors or warnings are present that would affect my orange item. I am not sure what to do here.

(It is only registered in the ModItems, as it should be).

Link to comment
Share on other sites

It sounds like you accidentally have two items that are both named "orange". Ensure that you give items unique names in the string when you register them. That's one of the more annoying errors to track down if you don't know what's causing it, though.

Link to comment
Share on other sites

I have now easily fixed the duplication error present, I was just not looking.

 

I have been working for the past half hour to try and fix another error present, this time with the Creative Mode Tab.

 

I have changed some things around to get where I am currently. (ModFoods to ModDrinks*) and it cannot find the symbol ".get" at the end of the code.

*The custom class you recommended

pOutput.accept(ModDrinks.ORANGE_JUICE.get());

I think the point I am at currently is the closest I have to how it should be but because I am not as experienced with java I would not know. 

I have also removed ORANGE_JUICE and LEMON_JUICE from the ModFoods class, to avoid confliction.

I do hope all this can be fully resolved soon.

 

Link to comment
Share on other sites

I have made the changes to the creative mode tab and have managed to run the game successfully and to join my test world. Though, disappointingly, the Lemon Juice and Orange Juice items have not changed at all, their properties are exactly the same as how they were when I originally asked for help: (making the food eating sound and producing particles).

 

I have absolutely no idea what to do and I am wondering why it has not worked at all, would you know how to fix this?

Link to comment
Share on other sites

All I know is what I've told you; I can't help more without inspecting the entirety of your code, and I think you'd probably learn the most from working this problem out yourself at this point. If you're still confused, try finding some other tutorials for modding; if you don't understand what the code is doing, try looking for Java tutorials. I hope this helps.

  • Thanks 1
Link to comment
Share on other sites

Okay, I shall look into this problem more in depth and look at Kaupenjoe's Java tutorials on YouTube. Thank you so much for all of your support over these last two weeks, you have helped me so much and I think that now I have a better understanding of Java thanks to you. 😊

  • Like 1
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

    • This is now replaced in the latest version, under Advanced Settings. Not available under the registry anymore.
    • You have to set the java path in your start script
    • i tried that and i got rid of java to install the new one but it still says i have the old one and i cant get the new one because of the old one  
    • I created a boss for Minecraft and when is it called «An unexpected error occurred while trying to run this command» "net.minecraft.world.entity.ai.attributes.attribute instance.m_22100_ (double)" because the return value "net.minecraft.world.entity.monster.zombie.m_21051_(net.minecraft.world.entity.ai.attributes.attribute)" is null. I don't fully understand what the error is. But it seems to be related to attributes. Please help me figure it out   Here is the boss class itself: package org.mymod.afraid; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.ai.goal.*; import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal; import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal; import net.minecraft.world.entity.monster.Zombie; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.phys.Vec3; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.entity.projectile.SmallFireball; import org.jetbrains.annotations.NotNull; public class AfraidBoss extends Zombie { private static final int MAX_HEALTH = 1000; private static final double ATTACK_DAMAGE = 10.0D; private static final double FOLLOW_RANGE = 50.0D; private static final double ATTACK_KNOCKBACK = 1.0D; private static final double MOVEMENT_SPEED = 0.25D; private static final int TELEPORT_RADIUS = 20; private static final int FIREBALL_COOLDOWN = 100; // 5 seconds (20 ticks per second) private static final int FIREBALL_COUNT = 3; private int fireballCooldown = 0; private int fireDashCooldown = 0; public AfraidBoss(EntityType<? extends Zombie> type, Level level) { super(type, level); this.setHealth(MAX_HEALTH); this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.DIAMOND_SWORD)); this.setItemSlot(EquipmentSlot.OFFHAND, new ItemStack(Items.DIAMOND_SWORD)); } @Override protected void registerGoals() { this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.0, true)); this.goalSelector.addGoal(2, new MoveTowardsTargetGoal(this, 1.0, (float) TELEPORT_RADIUS)); this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F)); this.goalSelector.addGoal(4, new WaterAvoidingRandomStrollGoal(this, 1.0)); this.goalSelector.addGoal(5, new HurtByTargetGoal(this)); this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Player.class, true)); } @Override public void aiStep() { super.aiStep(); if (this.getTarget() instanceof Player) { Player player = (Player) this.getTarget(); double distanceToPlayer = this.distanceToSqr(player); // Fire Dash ability if (distanceToPlayer <= TELEPORT_RADIUS * TELEPORT_RADIUS && fireDashCooldown == 0) { this.fireDash(player); fireDashCooldown = 200; // Cooldown for fire dash (10 seconds) } // Fireball attack if (fireballCooldown == 0) { this.shootFireballs(player); fireballCooldown = FIREBALL_COOLDOWN; // Cooldown for fireball attack (5 seconds) } // Decrement cooldowns if (fireDashCooldown > 0) { fireDashCooldown--; } if (fireballCooldown > 0) { fireballCooldown--; } } } private void fireDash(Player player) { Vec3 direction = player.position().subtract(this.position()).normalize(); Vec3 newPos = this.position().add(direction.scale(10)); this.teleportTo(newPos.x, newPos.y, newPos.z); this.createFireTrail(newPos); player.hurt(DamageSource.mobAttack(this), 20.0F); // Damage the player } private void createFireTrail(Vec3 position) { for (int x = -2; x <= 2; x++) { for (int z = -2; z <= 2; z++) { BlockPos firePos = new BlockPos(position.x + x, position.y, position.z + z); this.level.setBlock(firePos, Blocks.FIRE.defaultBlockState(), 11); } } } private void shootFireballs(Player player) { Vec3 direction = player.position().subtract(this.position()).normalize(); for (int i = 0; i < FIREBALL_COUNT; i++) { SmallFireball fireball = new SmallFireball(this.level, this, direction.x, direction.y, direction.z); fireball.setPos(this.getX() + direction.x, this.getY() + direction.y, this.getZ() + direction.z); this.level.addFreshEntity(fireball); } } public static AttributeSupplier.Builder createAttributes() { return Zombie.createMobAttributes() .add(Attributes.MAX_HEALTH, MAX_HEALTH) .add(Attributes.ATTACK_DAMAGE, ATTACK_DAMAGE) .add(Attributes.FOLLOW_RANGE, FOLLOW_RANGE) .add(Attributes.ATTACK_KNOCKBACK, ATTACK_KNOCKBACK) .add(Attributes.MOVEMENT_SPEED, MOVEMENT_SPEED); } @Override public boolean hurt(@NotNull DamageSource source, float amount) { boolean flag = super.hurt(source, amount); if (flag && source.getEntity() instanceof Player) { Player player = (Player) source.getEntity(); if (this.random.nextInt(10) == 0) { this.teleportAndAttack(player); } } return flag; } private void teleportAndAttack(Player player) { Vec3 randomPos = player.position().add((this.random.nextDouble() - 0.5) * 4, 0, (this.random.nextDouble() - 0.5) * 4); if (this.randomTeleport(randomPos.x, randomPos.y, randomPos.z, true)) { player.hurt(DamageSource.mobAttack(this), 10.0F); // Damage the player } } }  
  • Topics

×
×
  • Create New...

Important Information

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