Home > Articles > Home & Office Computing > Entertainment/Gaming/Gadgets

This chapter is from the book

Crafting a Recipe

Now you will finally start modding the game. Recipes may not be that big of a change, but several combined can change the way the game is played, especially in combination with custom Items, which are explained in Hour 4, “Making Your First Item.”

Recipe code has to be added in the mod file. Listing 3.1 is the mod file from Hour 2 without the System.out.println lines.

LISTING 3.1 Clean Mod File

package com.wuppy.samsmod;

import net.minecraft.init.Blocks;
import net.minecraftforge.mods.fml.common.Mod;
import net.minecraftforge.mods.fml.common.Mod.EventHandler;
import net.minecraftforge.mods.fml.common.event.FMLInitializationEvent;

@Mod(modid = SamsMod.MODID, version = SamsMod.VERSION)
public class SamsMod
{
    public static final String MODID = "wuppy29_samsmod";
    public static final String VERSION = "1.0";

    @EventHandler
    public void init(FMLInitializationEvent event)
    {

    }
}

The code for recipes can look quite complicated to start with and is really hard to understand without starting off with a code example. In the code that follows, you can see an example of a recipe. When you add this code, make sure you import the new files.

GameRegistry.addRecipe(new ItemStack(Items.apple),
       "XXX",
       "XXX",
       "XXX",
       'X', Blocks.leaves
);

This recipe will look like Figure 3.1.

FIGURE 3.1

FIGURE 3.1 The Apple recipe in game.

Using GameRegistry

The first part of this line is GameRegistry. GameRegistry is a file you will frequently use when coding Minecraft Forge mods. This file contains the registry of every recipe, and also items and blocks.

Because there are so many things you can do with this file, it has many methods that all do different things. To create a basic shaped recipe, you need the addRecipe method. This method takes two parameters. The first parameter is an ItemStack. An ItemStack is another class you will use frequently. It’s used to handle items or blocks in stacks. This recipe shows the most basic type of ItemStack, but you can add two more numbers as parameters. What those do will be shown in a different recipe. When you just use an Item or Block as parameter, it will create an ItemStack with that object with a stack size of 1 and a metadata of 0. Metadata and how to code it are explained in Hour 5, but examples of metadata are dyes and wool blocks.

The first parameter is the Item or Block the recipe will return. In this case it will return a single apple.

The second parameter in the addRecipe mode is a weird one. If you take a look at the parameter in the GameRegistry class, it’s written as follows:

Object... params

The ... means that it’s a parameter that can take any amount of values you want. This parameter can be interpreted as some sort of list. Everything you put after the first parameter of the addRecipe method will be added in this list and used in the recipe.

The Shape of the Recipes

The way these Recipes are done in Minecraft is by typing the shape of the crafting table in the recipe using three strings. Each of the three strings contains three spaces to put an Item or a Block. Another example of the crafting table space:

"ABC",
"DEF",
"GHI",

Each letter represents one of the usable crafting areas in the crafting grid in the crafting table If you want one of the spots to be empty, you replace one of the lines with a space.

Up to this point in the code, it’s just letters. Somehow those letters have to be changed into Items and Blocks. That is done just below the crafting grid shape. After the last row that ends with a comma, the letter X is surrounded by apostrophes, which turns it into a character. After this character you need another comma, followed by the Block that you want it to represent, which are leaves in this example. The recipe created by the preceding code would require each area in the crafting grid to be filled with a leaf and it will then return an apple.

Alternative Shapes

Sometimes you don’t require a three-by-three space to create a crafting recipe. In some cases you just need a two-by-two area. This is done using the following code.

"AB",
"CD",

With this code, the two-by-two shape can be placed anywhere in the crafting table. Another example of a differently shaped recipe is bread, which would look something like this:

"AAA",

In here the letter is the same each time, because bread is crafted using three wheat, but you can use different letters.

If you have more than a single Item or Block that is used in the shape, you have to add a comma behind the last Item or Block, write another pair of apostrophes, and follow that up by the other Item or Block.

An example of using a smaller grid, empty spaces, and using multiple components is shown next:

GameRegistry.addRecipe(new ItemStack(Items.arrow),
       "YZ",
       "X ",
       'X', Items.flint, 'Y', Items.stick, 'Z', Blocks.leaves
);

This will allow you to craft an arrow in your player inventory using leaves. Figure 3.2 shows the crafting of the recipe.

FIGURE 3.2

FIGURE 3.2 A smaller recipe in the player’s inventory.

Another thing you may want to have in your recipes is that the returned ItemStack has a size of more than 1. Two other things that may be interesting for a recipe are metadata for the used and the resulting Item or Block. You can see all of these things in a single recipe, as follows:

GameRegistry.addRecipe(new ItemStack(Items.dye, 2, 1),
       "XY",
       'X', Items.redstone, 'Y', new ItemStack(Items.dye, 1, 1)
);

Each of the ItemStacks used in this recipe have three parameters now. The first is still an Item or Block. The second is the stack size. You can return a stack size of more than one, but you can’t use a higher stack size in a used item. The third parameter is the damage or metadata of the item. Metadata starts counting at 0 and goes up to 15. This recipe will combine one redstone and one red dye to make two red dyes. Red dye is the second metadata for dyes. Therefore the damage is 1.

This recipe may seem a little illogical, because this recipe will work only when the dye is on the right. For recipes like this, generally a Shapeless Recipe is used.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020