Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.14.2] How to add custom food items


kwpugh
 Share

Recommended Posts

Hi,

 

Looking for some guidance .

 

I had ported some of my mod from 1.12.2 -> 1.13.2 which worked, but the food items do not work moving from 1.13.2 -> 1.14.2.

 

Here is what I had in 1.13.2 in my main class: 

 

    @SubscribeEvent
        public static void registerItems(final RegistryEvent.Register<Item> event)
        {
            event.getRegistry().registerAll
            {
                ItemList.gobber2_goo = new ItemFood(8, 1, false, new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_goo")),
                ItemList.gobber2_gooey_bread = new ItemFood(9, 1, false, new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_gooey_bread"))
            );
            
            logger.info("Items registered.");
        }

 

Since I am quite new to programming, if the above was the incorrect way to do it, feedback is welcome.

 

I saw that ItemFood changed to Food, but am unclear how to properly form the registration.

 

Here is the ItemList class:

 

public class ItemList
{
    public static Item gobber2_goo;
    public static Item gobber2_gooey_bread;
}

 

Any guidance would be appreciated.

 

Regards

 

Link to comment
Share on other sites

In Eclipse, I get an error "ItemFood cannot be resolved to a type", which makes sense that the class got renamed.

 

I then change the ItemFood reference to Food as seen below:

ItemList.gobber2_goo = new Food(8, 1, false, new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_goo")),

ItemList.gobber2_gooey_bread = new Food(9, 1, false, new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_gooey_bread")),

 

Eclipse gives the error "Food cannot be resolved to a type".  I take the suggested fix to "import 'Food' (net.minecraft.item)".  Eclipse then gives the error "The constructor Food(int, int, boolean, Item.Properties) is undefined".

 

I'm stuck there.

 

 

Link to comment
Share on other sites

ItemFood is gone now, whether or not something is a food is defined by a field in the item's properties (this is nice because something can now easily be a food and also something else, because it doesn't have to be an ItemFood AND an ItemArmour, for example). This is the same object you give it when you set the registry name and group on, so you can just call all the necessary methods on that object.

 

If you have a look at how a food item (bread, for example) is created in the vanilla Items class, a (currently unnamed) method is called on the properties given to the item. This method takes a Food object, and you can check the Foods class to see how you make the Food objects. The methods it uses for this are also still unnamed, but you can probably work out what each one does by looking at what food each Food object corresponds to.

 

If you're new to programming aren't certain about how to check these things, I'm not sure about the exact process in Eclipse but if you right-click on the name of a class somewhere in your code there should be the option to view the source somewhere, this is a good way of learning how things work by checking how vanilla does things. You can also just browse through the vanilla code in your project libraries, which in Eclipse are (I think?) down the side underneath all your classes you've written and stuff.

Link to comment
Share on other sites

ah yes, I have been reading through the reference libraries to try to figure out how things work.

 

So, would it be formed something like this:

ItemList.gobber2_goo = new Food(new Item.Properties().group(gobber2), "gobber2_foo").value(4).saturation(0.6F).build()

 

But it seems the Item.Properties works with that.   Is that what the Food.Builder is for?

Link to comment
Share on other sites

gobber2_goo still needs to be an Item, which Food is not (it doesn't extend Item like ItemFood did).

 

Take your original code using ItemFood and change it to Item. This will give you a generic non-food item, like you've probably created elsewhere. You can now call another method on the Item.Properties object you are giving it to give the Item.Properties food properties.

 

If you look into the Items class, you'll see where all the vanilla items are created. This is where you can find the method you need, because vanilla foods use it (this will also give you some usage examples). The method is in Item.Properties (my mappings are a bit old so it's func_221540_a for me, it might have a name for you updated my mappings, it's called food now). This method takes a single argument, which would be the Food you just constructed.

 

It'd end up looking like this:

Food gooFood = (new Food.Builder()).value(4).saturation(0.6F).build();

ItemList.gobber2_goo = new Item(new Item.Properties().group(gobber2).setRegistryName(location("gobber2_goo").food(gooFood));

 

Edited by fweo
  • Thanks 1
Link to comment
Share on other sites

I think I understand, but let me run through it.

 

I declare a method to setup the food item in my FoodList.java:

public static Food gooFood = (new Food.Builder()).hunger(4).saturation(0.6F).build();

 

Then I setup an item in my ItemList.java:

public static Item gobber2_goo;

 

Lastly, I setup a line to register the item in my main class, Gobber2.java and pass it the food properties:

ItemList.gobber2_goo = new Item(new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_goo")).food(FoodList.gooFood)

 

At which point, I get an error saying "The method food(Food) is undefined for the type Item"

 

In referencing the Minecraft Item.class, I reviewed this section, which appears to list the available Item.Properties:

public Item(Item.Properties properties) {

      this.addPropertyOverride(new ResourceLocation("lefthanded"), LEFTHANDED_GETTER);

      this.addPropertyOverride(new ResourceLocation("cooldown"), COOLDOWN_GETTER);

      this.addPropertyOverride(new ResourceLocation("custom_model_data"), MODELDATA_GETTER);

      this.group = properties.group;

      this.rarity = properties.rarity;

      this.containerItem = properties.containerItem;

      this.maxDamage = properties.maxDamage;

      this.maxStackSize = properties.maxStackSize;

      this.food = properties.food;

      if (this.maxDamage > 0) {

         this.addPropertyOverride(new ResourceLocation("damaged"), DAMAGED_GETTER);

         this.addPropertyOverride(new ResourceLocation("damage"), DAMAGE_GETTER);

      }

      this.canRepair = properties.canRepair;

      this.toolClasses.putAll(properties.toolClasses);

      Object tmp = properties.teisr == null ? null : net.minecraftforge.fml.DistExecutor.callWhenOn(Dist.CLIENT, properties.teisr);

      this.teisr = tmp == null ? null : () -> (net.minecraft.client.renderer.tileentity.ItemStackTileEntityRenderer) tmp;

 

If I am reading correctly, .food is a valid property for type Item.   So, I must have something else goofed up.

 

The last one mentioned in the main class, Gobber2.java looks like this:

 

@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)

public static class RegistryEvents

{

@SubscribeEvent

public static void registerItems(final RegistryEvent.Register<Item> event)

{

event.getRegistry().registerAll

(

ItemList.gobber2_goo = new Item(new Item.Properties().group(gobber2)).setRegistryName(location("gobber2_goo")).food(FoodList.gooFood),

);

logger.info("Items registered.");

}

 

 

Edited by kwpugh
Link to comment
Share on other sites

[SOLVED]

Turns out the order of the properties matter.   I moved the .food property to the front and it works.

 

ItemList.gobber2_goo = new Item(new Item.Properties().food(FoodList.gooFood).group(gobber2)).setRegistryName(location("gobber2_goo")),

 

Thank you for all the guidance.

 

 

Link to comment
Share on other sites

Last question related to food (hopefully).

 

I have a food that is a stew.   How best to give the bowl back after eating?

 

In my 1.12.2 mod, I had a class for each stew that extended ItemFood and gave the player a bowl in the onFoodEaten method.

 

Is there a better way with 1.14.2?

 

 

Link to comment
Share on other sites

It's not that the order of the properties that matters. Let's look at this line for a second:

 new Item(newItem.Properties().group(gobber2)).setRegistryName(location("gobber2_goo")).food(FoodList.gooFood)

 

If I space it out a bit so we can read it a little more easily (you can keep spacing as it was in the actual code):

new Item(
    // start constructing item properties
  new Item.Properties().group(gobber2)
    // finish constructing item properties
).setRegistryName(location("gobber2_goo")).food(FoodList.gooFood)

that middle line is creating the Properties object. You were calling the food method on the Item object. You're right that food is a thing in Item, but it's just a variable not a method. As it happens, all the Properties::food method does is tell the Properties object to put your Food into that field, as you can see in the Item constructor you posted.

 

Comparing to your working one:

new Item(
    // start constructing item properties
  new Item.Properties().food(FoodList.gooFood).group(gobber2)
    // finish constructing item properties
).setRegistryName(location("gobber2_goo"))

Now you're (correctly) calling the food method on the Item.Properties object. The only actual difference from your working one is the placement of a single bracket, which is determining what object you're calling the methods on. Changing the order works fine, you can apply properties to the Item.Properties object in whatever order you like (each method returns the Item.Properties object allowing you to "chain" the method calls like you have here). For example, this would also work:

new Item(
    // start constructing item properties
  new Item.Properties().group(gobber2).food(FoodList.gooFood)
    // finish constructing item properties
).setRegistryName(location("gobber2_goo"))

If you had more changes to properties, like rarity or max stack size, you could put them in there too in any order. Hopefully understanding this will help clear up future problems.

 

Also, if you click the button that looks like < > at the top of your editor when you're posting, you can post code in the style that I'm using here, which will make your post look a lot cleaner.

Edited by fweo
  • Like 1
Link to comment
Share on other sites

There's a class called SoupItem that does exactly what you want, simply changing Item to SoupItem will give you a bowl back once you've finished eating it. If you want something other than a vanilla bowl you can just create a modified version of that class to give you whatever you want.

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

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.