Solved Using a Custom Item in a Custom Recipe

Discussion in 'Plugin Help/Development/Requests' started by RBHB16, Oct 27, 2015.

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

    RBHB16

    So in this plugin I'm developing, I am making so to make a "blunt" (stick) you first need to make a unrolled blunt (coco beans). All these custom recipes work. The problem is in making the blunt, players can use regular coco beans + sugarcane (weed) and not the unrolled blunt display name version of coco beans to make a blunt. So in order to stop this, I attempted to use a listener on CraftItemEvent to stop the player from being able to craft with plain coco beans and sugar cane... The problem is that when I use the unrolled blunt version of coco beans, the crafting restrictor takes effect and doesn't let me craft it at all. I think I know what the problem may be, but I have looked everywhere and can't figure it out.

    If you look at these codes, I think the problem either relies in the if statement for the crafting restrictor, or the way I entered the recipe for the shapeless recipe for rolledblunt. I used unrolledblunt.getdata() because no other way would work.


    Help will be so greatly appreciated so please! I am completely stumped and been looking forever for a solution.

    Code for mainClass:
    Code:
    package com.redstery.prisondrugs;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.ShapelessRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.redstery.prisondrugs.events.player.CraftingBlunt;
    import com.redstery.prisondrugs.events.player.WeedEffect;
    import com.redstery.prisondrugs.events.player.CocaineEffect;
    
    public class MainClass extends JavaPlugin {
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            logger.info(pdfFile.getName() + "has been disabled V" + pdfFile.getVersion());
            getServer().clearRecipes();
        }
       
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
           
            ItemStack unrolledblunt = new ItemStack(Material.INK_SACK);
            unrolledblunt.setDurability((short) 3);
           
            ItemMeta meta = unrolledblunt.getItemMeta();
            meta.setDisplayName(ChatColor.BOLD + "" + ChatColor.BLUE + "Unrolled Blunt");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Use this to craft a rolled blunt!");
            meta.setLore(lore);
            unrolledblunt.setItemMeta(meta);
            ShapedRecipe unrolledrecipe = new ShapedRecipe(unrolledblunt);
            unrolledrecipe.shape("   ", "PPP", "   ");
            unrolledrecipe.setIngredient('P', Material.PAPER);
           
            ItemStack rolledblunt = new ItemStack(Material.STICK);
           
            ItemMeta meta2 = rolledblunt.getItemMeta();
            meta2.setDisplayName(ChatColor.BOLD + "" + ChatColor.GREEN + "Blunt");
            List<String> lore2 = new ArrayList<String>();
            lore2.add(ChatColor.GRAY + "Shift + Right Click to get baked");
            meta2.setLore(lore2);
            rolledblunt.setItemMeta(meta2);
           
            ShapelessRecipe rolledrecipe = new ShapelessRecipe(rolledblunt);
            rolledrecipe.addIngredient(Material.SUGAR_CANE);
            rolledrecipe.addIngredient(unrolledblunt.getData());
           
            getServer().addRecipe(unrolledrecipe);
            getServer().addRecipe(rolledrecipe);
           
           
            registerListensers();
           
            logger.info(pdfFile.getName() + "has been enabled V" + pdfFile.getVersion());
        }
        public void registerListensers() {
            getServer().getPluginManager().registerEvents(new CocaineEffect(this), this);
            getServer().getPluginManager().registerEvents(new WeedEffect(this), this);
            getServer().getPluginManager().registerEvents(new CraftingBlunt(this), this);
        }
    
    }
    
    Code where I want to restrict crafting of using the version opposite version (plain coco beans) of my renamed item
    Code:
    package com.redstery.prisondrugs.events.player;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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.ShapelessRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import com.redstery.prisondrugs.MainClass;
    
    public class CraftingBlunt implements Listener {
       
        public CraftingBlunt(MainClass mainClass) {
        }
    
        @EventHandler
        public void bRecipe(CraftItemEvent evt) {
            ItemStack unrolledblunt = new ItemStack(Material.INK_SACK);
            unrolledblunt.setDurability((short) 3);
           
            ItemMeta meta = unrolledblunt.getItemMeta();
            meta.setDisplayName(ChatColor.BOLD + "" + ChatColor.BLUE + "Unrolled Blunt");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Use this to craft a rolled blunt!");
            meta.setLore(lore);
            unrolledblunt.setItemMeta(meta);
            ShapedRecipe unrolledrecipe = new ShapedRecipe(unrolledblunt);
            unrolledrecipe.shape("   ", "PPP", "   ");
            unrolledrecipe.setIngredient('P', Material.PAPER);
           
            ItemStack rolledblunt = new ItemStack(Material.STICK);
           
            ItemMeta meta2 = rolledblunt.getItemMeta();
            meta2.setDisplayName(ChatColor.BOLD + "" + ChatColor.GREEN + "Blunt");
            List<String> lore2 = new ArrayList<String>();
            lore2.add(ChatColor.GRAY + "Shift + Right Click to get baked");
            meta2.setLore(lore2);
            rolledblunt.setItemMeta(meta2);
           
            ShapelessRecipe rolledrecipe = new ShapelessRecipe(rolledblunt);
            rolledrecipe.addIngredient(Material.SUGAR_CANE);
            rolledrecipe.addIngredient(unrolledblunt.getData());
           
            if(!evt.getRecipe().equals(lore)) {
                evt.setCancelled(true);
            }
        }
    
    }
    
     
    Last edited: Oct 28, 2015
  2. Offline

    RBHB16

    24 hour bump. Guys please help!
     
Thread Status:
Not open for further replies.

Share This Page