ModderDojo Java Modding 8: A Simple Item Mod

2014-02-15_08.36.11

Note: my main sources of information for this post were the following, though I had to update them quite a bit:

You need at least three things to create a new Item:

  1. A new item class that extends the standard Item class
  2. A mod class where you add it to the game registry
  3. A language file where you specify its name.

modfiles

Later we will add a texture. Until then, its appearance will be a pink and black chequer pattern.

In the src/main/minecraft folder, create a new package. I called it modderdojo.firstitem.

In this package, create a new class. I called it MyFirstItem. Replace the automatically generated code with the code below:

package modderdojo.firstitem;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class MyFirstItem extends Item
{
   public MyFirstItem()
   {
      // This is the constructor for our new item. Set basic properties in it.
      setMaxStackSize(64);
      setCreativeTab(CreativeTabs.tabTransport);
      setUnlocalizedName("MichaelFirstItem");
   }
}

The code above does the following:

  • It defines a new Java class  for your new type of item: extends Item means that it is based on the standard Item type, and you will then modify it. Note that you could extend another kind of item, such as Diamond.
  • In its constructor (a special method with the same name as the class, which is called whenever you create an item, it sets basic properties: the max stack size, which tab it will be on, and an unlocalized name (a name that will be used internally in Minecraft).
  • Note: normally the unlocalised name does not have any spaces.

Now create a second class that initialises the mod that will include this item:

package modderdojo.firstitem;

import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "firstitem", name = "First Item Mod from ModderDojo by Michael", version = "1.0")
public class MyFirstMod
{
   @EventHandler
   public void preInit(FMLPreInitializationEvent event)
   {
      MyFirstItem first = new MyFirstItem();
      GameRegistry.registerItem(first, "MichaelFirstItem"); // This name doesn't have to be same as unlocalised name, but no harm in them being the same
   }
}

The code above creates a basic mod, like we did earlier. The main difference is in the preInit method, where we create an object of our MyFirstItem class. We call the object first. We use this object to add the item to the game registry, again giving it a name that will be used internally.

You can run your code now, and the mod should appear, displaying its unlocalised name and with the default texture. Next we will see how to change them.

ModderDojo Java Modding 7: Adding Smelting

This is even easier than a recipe. The code can be added to init or preInit. (Note: In Forge 1.7, they recommend using putting recipes in preInit rather than init.)

You specify an input, an output and experience. The input and output are both an ItemStack with either a single item or you can specify how many items, as in recipes. Experience is a number between 0 and 1. In Java, we use a float (short floating-point number) to represent most decimal numbers, so 0.1f means a float with value 0.1.

(Thanks to Luke for this code.)

smelt

Web Development – Making text bold and italics!

Hi all,

This weeks notes are all about making text bold and using italics. In the notes we learn about these new tags and add them to the Coderdojo Athenry information page we have been working on. As we go along we can  add what we have learned to this webpage! coderdojo_first_web_pageNext week we will learn more tags! Remember if you have any questions to just send me an email, my email address is in the notes! Here are this weeks notes! WebsiteDevelopment4

Python Beginners – Driving Game

trackCap

This week we looked at code for the bare bones of a race car game.
We started off by just moving a block around the screen and then rotating it. Then we figured out how to drive it around the screen.
Finally, we looked at how to give the impression of movement to our car sprite by scrolling the track around behind it.
For next week I want the Python group to look at Scratch Beginners week 2 and week 3 as we are going to attempt the same thing in Pygame.
You can find our code from this week here.

Week 3, 2014 Scratch Beginners- Scrolling Backgrounds Contd

Hi everyone,

We completed our Mario game this week. We coded Mario so that he always floated down on to the wall. We added a fraction of a second of a wait so that it appears that he floats as he comes down. This also allows time for you to navigate left or right as needed.

CDA-S3-Challenge09-Scrolling

We also introduced a more advanced concept, the Parallax effect, whereby objects further away appear to move slower than objects nearer. We coded mountains and a Sun to demonstrate this.

CDA-S3-Challenge-Scrolling

Next week, everyone is going to start on there own games, using the code and concepts we have learnt over the last couple of weeks, so bring your imagination with you.

You will a full version of the my notes in pdf form here.CDA-S3-Challenge09-Scrolling.pdf

See you all next week

Martha

Website Development Week 3

Hi All,

This week the notes are mostly a refresher because a lot of ground has been covered in the last two weeks and I want to give a chance to anybody who wants to start learning HTML a chance to catch up. The notes just recap what we have done so far and also explain why it’s not a good idea to use Microsoft Word to make our HTML files! The notes also show that there are some excellent websites on the internet for learning HTML. You can type the HTML on one side, and see what kind of page this code creates on the other. This is super for experimenting with the different HTML tags so give it a go! When I went around to the classes this week some people did mention to me that they were interested in learning HTML, remember if you ever have any questions feel free to email me or talk to me at the Coderdojo every Saturday!

Here are this weeks notes!

James

Python Beginners – Space Defender

                                       ImageToday in the Python group we looked at how a few simple changes can change the look and feel of a game. Our space Defender game uses code recycled from last weeks Bunnies And Badgers game combined with a few new sprites to make a completely new game. We also looked at how to use Geometry when moving sprites around the screen.

movex=math.cos(angle_z)*10

movey=math.sin(angle_z)*10

xpos+=movex

ypos-=movey

The above code calculates how many pixels the x and y coordinates of our sprite needs to be changed for it to move 10 pixels in the direction of angle_z. You can learn more about Sin and Cos here.

Week 7 – 2015 Scratch Beginners – Scrolling Backgrounds

Hi everyone,

Thank you all for coming Saturday on such a nice day and such a busy day of sport as well!
We started our Mario game, where we will use scrolling of objects in the background to achieve movement rather than making Mario move.
scrolling

It can be a difficult concept to grasp for the younger ones, but we will go through it again next week before we continue on with the game.

scrolling2

Rather than putting up the notes from this week, I will wait until we have the game finished before putting up the completed notes.
I have however, put the game (as far as we did today) on the Scratch Website http://www.scratch.mit.edu. Login with the User name coderdojodathenry and password xxxxxxx123 and you will be able to download the game if you weren’t here this week or have a look at the code to get your own game work working.

MarioGamehttps://wordpress.com/post/40558549/912

See you all next week, when we will make Shrink and add some more scrolling sprites!

Martha

Website Development 2 – Links, Paragraphs, Headings and Images!

Hi everybody!

This weeks notes are all about putting headings, paragraphs, links and images on our webpages! Have a look at the notes and play around with the new tags that you can learn from them. If you have any questions just send me an email! My email address is on the last slide of this weeks notes. I am going to be at Gairmscol Mhuire during Coderdojo on Saturdays so you can ask me questions there too!

Here is a link to this weeks notes!

Happy Coding,

James