[Tutoial] How to create Custom Enchantments Semi-Noob-Friendly!

Discussion in 'Resources' started by Monkey_Swag, Feb 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Monkey_Swag

    Introduction
    Finally guys, a tutorial for noobies on how to create custom enchantments! Or at least a little advanced noobs! I would like to share this concept that I found very easy to understand. I want to mention that the item will not have the "shiny" enchanted effect on it, however if you want it, you can click here for CaptainBern 's tutorial, which adds the "shiny" effect to the item! If you follow my tutorial, then add his to your plugin, you will get the item with its nice shiny effects!

    How this works / basic procedure
    What we'll be doing is creating a new ItemStack called "sword" and setting a lore for it, on this tutorial, we'll make a new enchantment, "Poison" (how original right?) and when a player gets hit with the sword, they will get a poison effect for 5 seconds! For the sake of the tutorial, I am going to be teaching you how to do it by right-clicking a sign. We will be calling a PlayerInteractEvent, and a EntityDamageByEntityEvent. For the PlayerInteractEvent, we will be checking if the item in hand is a sword, and if the text on the sign has what we want it to have (will make more sense later). If the sign has what we need, we will set the lore of the sword to the name of our custom enchantment. For the EntityDamageByEntityEvent, we will be checking if the damager has a sword with the lore of our enchantment name, and if it does have it, we will give the damaged player a poison effect for 5 seconds.

    Let's Start!
    Alright, so first of all, you'll want to extend JavaPlugin and implement Listener, then register your events on your onEnable().
    Code:java
    1. package me.YOUR_NAME.CustomEnchants;
    2.  
    3. public class Main extends JavaPlugin implements Listener{
    4. public void onEnable() {
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. }
    7. }

    This may sound stupid, but I have seen so many people forget to register their events and have no clue what happened.

    Now we're going to want to create our first event!
    Code:java
    1. @EventHandler
    2. public void onRightClick(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && p.getItemInHand().getType() == Material.DIAMOND_SWORD || p.getItemInHand().getType() == Material.STONE_SWORD || p.getItemInHand().getType() == Material.GOLD_SWORD || p.getItemInHand().getType() == Material.WOOD_SWORD || p.getItemInHand().getType() == Material.IRON_SWORD){
    6. if(e.getClickedBlock().getState() instanceof Sign) {
    7. Sign s = (Sign) e.getClickedBlock().getState();
    8. if(s.getLine(0).equalsIgnoreCase("[Enchant]"))
    9. if(s.getLine(1).equalsIgnoreCase("Poison"))
    10. if(s.getLine(2).equalsIgnoreCase("I")) {
    11. //we will get to adding the lore right here ;)))
    12. }
    13. {

    we are checking if a player right-clicks the sign with either a stone, gold, wood, iron, or diamond sword, and if the sign they clicked has our requested fields (FIRST LINE: [Enchant], SECOND LINE: Poison, THIRD LINE: I) and if it does, it will give the sword the lore we need.

    BUT MONKEY, YOU PUT IF THE LINE #0 HAS [Enchant] TO DO THE MAGIC, THERE IS NO LINE #0 ON SIGNS DOMMY!
    Although you should know this, I will explain it anways. When you tell a computer and a human to count to five, the human will normally say 1,2,3,4,5. However, a computer would say 0,1,2,3,4,5. Therefore, you need to know that the first line on the sign is line #0, second line is line #1, etc etc until you get to line 3.

    BUT WHAT ABOUT LINE #3, YOU DIDN'T TELL IT TO SAY ANYTHING!?!??!?!?!
    If you do not specify a field for a certain line, the code will ignore that line, and if the other lines have the text we requested it to, it'll run successfully.

    Now let's move on to adding the lore to our sword!
    Code:java
    1. ItemStack sword = p.getItemInHand(); //getting the item in the player's hand, and calling it sword.
    2. ItemMeta swordMeta = sword.getItemMeta(); //getting the item's meta so we can edit the lore, and (not doing it on this tutorial) but you can also edit the Diplay-Name.
    3. List<String> swordLore = new ArrayList<String>(); //Adding an arraylist of strings to our sword's lore.
    4. swordLore.add(ChatColor.GRAY + "Poison I"); //adding the lore in [U]CHATCOLORGRAY[/U] so that it looks like an enchantment!
    5. swordMeta.setLore(swordLore); //setting the sword's meta to our specified lore
    6. sword.setItemMeta(swordMeta); //setting the sword's new, edited meta


    We also want to send them a message saying that they enchanted their item successfully, so we're going to send the player a message saying it worked!
    Code:java
    1. p.sendMessage(ChatColor.GREEN + "Successfully enchanted your sword!");


    Now let's move on to the EntityDamageByEntityEvent!
    So let's create our Event, and add a cast to the damager and the damaged player
    Code:java
    1. @EventHandler
    2. public void onDamageEvent(EntityDamageByEntityEvent e) {
    3. if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) //We are seeing if the damaged Entity is a Player, and we're also trying to see if the damager Entity is a player {
    4. Player damager = (Player) e.getDamager(); //now we cast the damager
    5. Player damaged = (Player) e.getEntity(); //now we cast the damaged player
    6.  
    7. }
    8.  


    Now we have to check if the item in their hand is a sword with the lore of "Poison I" on it.
    Code:java
    1.  
    2. if(damager.getItemInHand().getItemMeta().getLore().contains(gray + "Poison I")) //we check if the item in the player's hand has the lore of "Poison I" on it.{
    3. //now we add the potion effect
    4. damaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100 // this part is in ticks, (20 ticks is 1 second, so we set it to 100 ticks for 5 seconds.
    5. , 1 //this is just Poison I, Poison II, etc.));
    6.  
    7. }
    8.  

    We do not need to have an "else" statement if the sword does not have a lore of "Posion I", it won't do anything if it doesn't!

    YOU ARE DONE!
    Here is a screenshot of this in action!
    http://imgur.com/QUIk3eI

    If you have any errors or questions feel free to ask! Thank you for reading through my first tutorial! If you want me to add something or if there's a better way to do anything, let me know, I am not the best developer ever ;p Again, thanks a lot, and please, if you think this is bad, do not flamebait, just say that this is not the best tutorial, and if you think it should be taken down, say so, but again, in a polite manner, thanks!
     
    GregMC likes this.
  2. Offline

    calebbfmv

    I would STRONGLY recommend not checking the color of the contains,
    Code:
    if(player.getItemInHandStuff().getLore().contains(gray + 'Bilbo Baggins");
    
    Simply because this could break EVERY thing if you decide to change your colors.
    So simply do
    Code:
     ChatColor.stripColor("Bilbo Baggins");
    This makes life much easier in the event of a change.

    Another thing, I would recommend having a
    Code:
    for(String s  : itemMeta.getLore()){
    Then doing checks in there, just in case you wanted to have multiple Enchants.
    I recently just wrote something like this, but very nice!
     
    Monkey_Swag likes this.
Thread Status:
Not open for further replies.

Share This Page