CustomRecipe and CraftItem

Discussion in 'Plugin Development' started by Sicka_gp, Apr 30, 2013.

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

    Sicka_gp

    Hi, How to add permissions to create a custom item?
    Here I tried it but it does not work, does anyone know where is the problem?

    Code:
    package cz.Sicka_gp.experiences;
     
    import java.util.ArrayList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
     
    public class ExperiencesCraftListener implements Listener{
        ExperiencesMain plugin;
        ShapedRecipe newitemrecipe;
       
        public ExperiencesCraftListener(ExperiencesMain instance) {
            plugin = instance;
        }
     
     
        public void addRecipe(){
            ItemStack newitem = new ItemStack(Material.DIAMOND_PICKAXE, 1);
            ItemMeta im = newitem.getItemMeta();
            im.setDisplayName(ChatColor.DARK_RED + "Super Krumpac");
            ArrayList<String> arraylist = new ArrayList<String>();
            arraylist.add("Experiences");
            im.setLore(arraylist);
            newitem.setItemMeta(im);
            Enchantment efficiency = Enchantment.DIG_SPEED;
            Enchantment unbreaking = Enchantment.DURABILITY;
            //Enchantment fortune = Enchantment.LOOT_BONUS_BLOCKS;
            newitem.addEnchantment(efficiency, 5);
            newitem.addEnchantment(unbreaking, 3);
           
           
            ShapedRecipe newitemrecipe = new ShapedRecipe(newitem);
            newitemrecipe.shape(" a ", " a ", " a ");
            newitemrecipe.setIngredient("a".charAt(0), Material.DIAMOND);
            this.plugin.getServer().addRecipe(newitemrecipe);
            }
       
        @EventHandler
        public void onPlayerCraft(CraftItemEvent e){
            Player p = (Player) e.getWhoClicked();
            if(e.getRecipe() == newitemrecipe){
                String cnf = this.plugin.Player.getString(p.getName() + ".Schopnosti");
                if(cnf.contains("Hornik")){
                    p.sendMessage(ChatColor.DARK_GREEN + "Uspesne jsi vykraftil super krumpac :)");
                }else{
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.DARK_RED + "Nemas potrebne zkusenosti na crafteni super krumpace!");
                }
            }
           
        }
     
    }
     
  2. Offline

    iliasdewachter

    If (player.hasPermission("your.permission"))
    ?

    Note: you'll have to define your permission in your plugin.yml file
     
  3. Offline

    Sicka_gp

    I want it by experience..
    For example:
    If the player will have 10 experience points you earn the right to create item
     
  4. Offline

    iliasdewachter

    If (player.getExperience()>9)

    ?
     
  5. To check for recipe requirements I suggest you use PrepareItemCraftEvent and getInventory().setResult(null) to prevent crafting, it's alot cleaner.

    The == operator compares instances of objects (not values, equals() does that) and when the event is given that recipe it gives you is a diferent instance, therefore it will never be true on your == condition.

    But equals() is not properly implemented for recipe objects therefore it's really tricky to compare them, you should use this: http://forums.bukkit.org/threads/util-class-for-comparing-recipes-properly.144319/

    Also I don't see you calling addRecipe() method.
     
  6. Offline

    Sicka_gp

    Thank you very much Digi :)
    It works :)

    I still have one question .. How can I determine that item was created?
    I would like to send a message to players that create item..

    Code:
    package cz.Sicka_gp.experiences;
     
    import java.util.ArrayList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
     
    public class ExperiencesCraftListener implements Listener{
        ExperiencesMain plugin;
        ShapedRecipe newitemrecipe;
       
        public ExperiencesCraftListener(ExperiencesMain instance) {
            plugin = instance;
        }
     
     
        public void addRecipe(){
            ItemStack newitem = new ItemStack(Material.DIAMOND_PICKAXE, 1);
            ItemMeta im = newitem.getItemMeta();
            im.setDisplayName(ChatColor.DARK_RED + "Super Krumpac");
            ArrayList<String> arraylist = new ArrayList<String>();
            arraylist.add("Experiences");
            im.setLore(arraylist);
            newitem.setItemMeta(im);
            Enchantment efficiency = Enchantment.DIG_SPEED;
            Enchantment unbreaking = Enchantment.DURABILITY;
            //Enchantment fortune = Enchantment.LOOT_BONUS_BLOCKS;
            newitem.addEnchantment(efficiency, 5);
            newitem.addEnchantment(unbreaking, 3);
           
           
            ShapedRecipe newitemrecipe = new ShapedRecipe(newitem);
            newitemrecipe.shape(" a ", " a ", " a ");
            newitemrecipe.setIngredient("a".charAt(0), Material.DIAMOND);
            this.plugin.getServer().addRecipe(newitemrecipe);
            }
       
        @EventHandler
        public void onPlayerCraft(PrepareItemCraftEvent e){
            Player p = (Player) e.getView().getPlayer();
            if(e.getRecipe().equals(newitemrecipe));{
                String cnf = this.plugin.Player.getString(p.getName() + ".Schopnosti");
                if(cnf.contains("Hornik")){
                    p.sendMessage(ChatColor.DARK_GREEN + "Uspesne jsi vykraftil super krumpac :)");
                }else{
                    e.getInventory().setResult(null);
                    p.sendMessage(ChatColor.DARK_RED + "Nemas potrebne zkusenosti na crafteni super krumpace!");
                }
            }
           
        }
     
    }
     
  7. Sicka_gp
    You missed some critical parts:
    And CraftItemEvent is fine for detecting when item is crafted, the PrepareItemCraftEvent is to detect recipes when ingredients are placed in the crafting grid.
     
Thread Status:
Not open for further replies.

Share This Page