tutorial/Create Items

How to create own items

This tutorial will only work for version 2.0. The next version is not uploaded, but the way that items are created (mainly the export) will become a lot easier and the options, that the developers have will get greater.

Set up the Project

If you want to create your own item you have to set up your Project first.

Open: File -> New -> Java Project

You can name the project to whatever you want and then click Next.

Now you have to go to the tab Libraries. Click on Add External JARs.

Select the craftbukkit.jar

And then use Add External JARs to add the ItemEffects.jar

Now you can click the Finish button.

Create Project

Creation of the item

Now that the Project is set up you can start making your own Item.

First you have to create your own class. You can name the package whatever you want. The name of the classfile will be the name of the item that will be shown ingame.

Then your class has to extend EffectItem and implements the needed methods. Them Your class should look like this:

package EffectItem;

import org.bukkit.entity.Player;

import me.leo.itemeffects.item.EffectItem;

public class MyItem extends EffectItem{

	@Override
	public void giveItem(Player p) {
		// TODO Auto-generated method stub
		
	}

}

Now you have to set the code for giveItem(Player p). I would recommend to create a getItem() and isItem() method:

package EffectItem;

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

import me.leo.itemeffects.item.EffectItem;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class MyItem extends EffectItem{

	@Override
	public void giveItem(Player p) {
		p.getInventory().addItem(getItem());
	}
	
	private ItemStack getItem() {
		ItemStack i = new ItemStack(Material.DIAMOND_SWORD);
		ItemMeta m = i.getItemMeta();
		List<String> lore = new ArrayList<>();
		
		m.setDisplayName(ChatColor.LIGHT_PURPLE + "Godlike Sword");
		lore.add(ChatColor.DARK_PURPLE + "Looks Godlike!");
		
		m.setLore(lore);
		i.setItemMeta(m);
		return i;
	}
	
	private boolean isItem(ItemStack i) {
		ItemStack i1 = getItem();
		return (i1.getType() == i.getType()
				&& i1.hasItemMeta() == i.hasItemMeta() && i1.getItemMeta()
				.getDisplayName().equals(i.getItemMeta().getDisplayName()));
	}

}

Now you could use your Item, but it won't do anything. Now we can add some effects to it. In this case i will make it so the enemys explode:

package EffectItem;

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

import me.leo.itemeffects.item.EffectItem;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class MyItem extends EffectItem {

	@Override
	public void giveItem(Player p) {
		p.getInventory().addItem(getItem());
	}
	
	@EventHandler
	public void atack(EntityDamageByEntityEvent e) {
		if(e.getDamager() instanceof Player) {
			System.out.println("Test");
			Player p = (Player) e.getDamager();
			if(isItem(p.getItemInHand())) {
				e.setDamage(0);
				e.getEntity().getWorld().createExplosion(e.getEntity().getLocation(), 1);
			}
		}
	}

	private ItemStack getItem() {
		ItemStack i = new ItemStack(Material.DIAMOND_SWORD);
		ItemMeta m = i.getItemMeta();
		List<String> lore = new ArrayList<>();

		m.setDisplayName(ChatColor.LIGHT_PURPLE + "Godlike Sword");
		lore.add(ChatColor.DARK_PURPLE + "Looks Godlike!");

		m.setLore(lore);
		i.setItemMeta(m);
		return i;
	}

	private boolean isItem(ItemStack i) {
		ItemStack i1 = getItem();
		return (i1.getType() == i.getType()
				&& i1.hasItemMeta() == i.hasItemMeta() && i1.getItemMeta()
				.getDisplayName().equals(i.getItemMeta().getDisplayName()));
	}

}

Here are some methods you can use in your itemclass that will make coding easier:

  • getPlugin()
    • this will return the Item EffectsPlugin
  • newMetadata(Object value)
    • Its usefull when you use Entity.setMetadata() because you can just do Entity.setMetadata("KEY", newMetadata("test")) without creating your own Metadata class.

      That is everything you need to create your item, but there are two more methods that you can override.
  • public void update()
    • This method will be called every 0.5 Seconds, you can use it for cooldown or something else
  • public void init()
    • This method is called when your item is loaded. You can print stuff to the console or do some other stuff.

Export your item

To export your item go open a navigator window. You can find it at: Window -> Show View -> Navigator

There you have to open the folder of your project, go to 'bin' and copy the *.class file of your item.

You can paste the file where ever you want and change the name from *.class to *.item

To install your item go to the server folder and put the file to 'plugins/Item Effects/items' and reload.

Now you item should be displayed when you use '/ie items', if not please check the console for errors.


Comments

Posts Quoted:
Reply
Clear All Quotes