Minecraft Blogs / Tutorial

Creating Bukkit plugins part 4 - Custom Item Meta + Custom Crafting Recipe

  • 8,810 views, 1 today
  • 2
  • 2
  • 3
gabe4356's Avatar gabe4356
Level 52 : Grandmaster Blob
205
In my last three tutorials I ate cheese and blew up showed you how to create your plugin, add commands, events, and more. Now I will be teaching you how to add your own Item Meta and Crafting recipe, now some of you might not know what Item Meta is. A simple way of explaining that, is just to say, that custom Item Meta is basicly making a new item, using the texture of an already made item, but without a lot of code.

So, Now we want to add the custom item meta, so paste this in your onCommand Method:
Code - Custom Item Meta (explained below)
    ItemStack item = new ItemStack(Material.DIAMOND_AXE);
    ItemMeta im = item.getItemMeta();
    im.setDisplayName (ChatColor.RED + "" + ChatColor.BOLD + "Thors Hammer");
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.DARK_AQUA + "Hammer of the Gods! Click to use.");
    im.setLore(lore);
    item.setItemMeta(im);
the first line says that we are creating a new Item stack using a Diamond Axe
Second line Sets the name we will use in the code for our Item Meta, and then gets it. you can change "im" to anything, but you just have to change all of the 'im''s
the third line sets the display name, in this case, it is Bold and red, you can change it to whatever.
the third line adds the lore
The fourth line sets what the lore will be
and the fifth line basicly confirms that there is lore (sort of)
and now, the last line creates the item meta

Now, we can add a crafting recipe to craft the custom item. Add this right after what we just added:
Code - Crafting
                ShapedRecipe disk = new ShapedRecipe(item);
                disk.shape(new String[]{"***","&*&","*$*"}).setIngredient('$', Material.STICK).setIngredient('*', Material.DIAMOND).setIngredient('&', Material.EMERALD_BLOCK);
                Bukkit.getServer().addRecipe(disk);
                
            }
the ShapredRecipe creates the recipe as a disk (like all crafting table recipes)
then the next one actually creates the recipe and the needed materials for the recipe. The first line, which in this case is *** is the top row, &*& is the middle, and *$* is the bottom.
Then we set the ingrediant, and what symbol we will use for the ingredient in the crafting recipe. so in this case, $ is a Stick, so all '$' in the crafting recipes are sticks. and same with the rest, just different materials.
Finaly, the last line, actually adds the recipe in to the game.

The final code should look something like this:
Code - Final
package com.gabe.tutorial;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

@SuppressWarnings("deprecation")
public class MainTutorial extends JavaPlugin implements Listener {
    
    
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
            player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
            player.damage(5);
            player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
        }
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("Hello")){
        player.sendMessage(ChatColor.AQUA + "Hello Cheese! I mean...World!");     
        
        }
        
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
                player.sendMessage(ChatColor.GOLD+ "=================================");
                player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
                player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
        }
        
        if (cmd.getName().equalsIgnoreCase("Alist")) {
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "      Cheese Commands            ");
            player.sendMessage(ChatColor.GOLD+ "=================================");
            player.sendMessage(ChatColor.AQUA+ "Cheese : Special surprise" + ChatColor.BOLD + " :)");
            player.sendMessage(ChatColor.AQUA+ "Bob : Lists these commands");
            
            } else if (foo()) {
            player.sendMessage(ChatColor.DARK_RED + "Unknown command, type /Alist");
            }
            
        
    
        return true;
    }
    
    private boolean foo() {
// TODO Auto-generated method stub
return false;
    }
    
    public void onDisable() {
        //On Disable method stub
    }

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);
       //on Enable Method stub
    
    ItemStack item = new ItemStack(Material.DIAMOND_AXE);
    ItemMeta im = item.getItemMeta();
    im.setDisplayName (ChatColor.RED + "" + ChatColor.BOLD + "Thors Hammer");
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.DARK_AQUA + "Hammer of the Gods! Click to use.");
    im.setLore(lore);
    item.setItemMeta(im);
    ShapedRecipe disk = new ShapedRecipe(item);
    disk.shape(new String[]{"***","&*&","*$*"}).setIngredient('$', Material.STICK).setIngredient('*', Material.DIAMOND).setIngredient('&', Material.EMERALD_BLOCK);
    Bukkit.getServer().addRecipe(disk);
    
}
        {
            
            for (Player player: Bukkit.getServer().getOnlinePlayers()) {
                if (player.hasPermission("hello.world")) {
                    player.sendMessage(ChatColor.DARK_AQUA + "You can say hello! Just type /hello");
                }
            }

}
    }


I hope you enjoyed, and I hope this helped!

Part 5: (COMMING SOON)
Tags

Create an account or sign in to comment.

1
10/29/2015 5:07 am
Level 39 : Artisan Explorer
avrona
avrona's Avatar
Could you also create some blogs for vanillla servers please?
1
10/30/2015 7:02 pm
Level 52 : Grandmaster Blob
gabe4356
gabe4356's Avatar
You mean for creating vanilla servers?
1
10/31/2015 5:12 am
Level 39 : Artisan Explorer
avrona
avrona's Avatar
Well not create them but do stuff with command blocks on them.
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome