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



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hello. I decided to try to make a mod, but for some reason I stumbled at the very beginning. How do I register a block for it to appear in the inventory? It seems that I'm trying to do it according to the manual, but only the procedure for registering new blocks is described there. So I made a mod class. I registered the registration, but when I install it into the game, I do not see my block in the intent. And I will also be grateful if you give a link to some kind of detailed manual. So that step by step it was described how what can be created. Thanks.   @Mod("examplemod") public class ExampleMod { // Directly reference a log4j logger. private static final Logger LOGGER = LogManager.getLogger(); private static final String MODID = "0.1"; private static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); public static final RegistryObject<Block> MyBlock = BLOCKS.register("mysuperblockreg", () -> new SuperBlock(BlockBehaviour.Properties.of(Material.WOOD).lightLevel((s) -> { return 1; }).sound(SoundType.WOOD))); public ExampleMod() { // Register the setup method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); // Register the enqueueIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC); // Register the processIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC); BLOCKS.register(FMLJavaModLoadingContext.get().getModEventBus()); // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); } private void setup(final FMLCommonSetupEvent event) { // some preinit code LOGGER.info("HELLO FROM PREINIT"); LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); } private void enqueueIMC(final InterModEnqueueEvent event) { // some example code to dispatch IMC to another mod InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world"; }); } private void processIMC(final InterModProcessEvent event) { // some example code to receive and process InterModComms from other mods LOGGER.info("Got IMC {}", event.getIMCStream().map(m -> m.messageSupplier().get()).collect(Collectors.toList())); } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(FMLServerStartingEvent event) { // do something when the server starts LOGGER.info("HELLO from server starting"); } // You can use EventBusSubscriber to automatically subscribe events on the // contained class (this is subscribing to the MOD // Event bus for receiving Registry Events) @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public static class RegistryEvents { @SubscribeEvent public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) { // register a new block here LOGGER.info("HELLO from Register Block"); } } }  
    • I'm probably really stupid but how do I fix this error. It happens whenever I try to startup Minecraft forge 1.8.9 recommended [19:13:10 ERROR]: Gave up trying to download https://maven.minecraftforge.net/jline/jline/2.13/jline-2.13.jar for job 'Version & Libraries' [19:13:10 ERROR]: Job 'Version & Libraries' finished with 1 failure(s)! (took 0:00:13.982)   How do I fix this?
    • Override attackEntityFrom and do checks there, for example DamageSource#isProjectile. Or check how Enderman does it.
    • Hey, I just started modding, and especially texturing, so I'm still new to all of this. I thought a fun thing to do would be to turn the world to glass (turn blocks such as grass and stone and ores etc..) to have the grass texture, so the world would be glass. Seemed kinda fun. So I just copy-pasted the glass texture into my pack, renamed it to stone, and ran into a problem. It is rendering the glass texture, but is not transparent. Its just grey on the inside, with the glass textures on the side. I am completely lost as of what to do. Does anyone have a solution ? thank you
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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