What is wrong with my code? CraftItemEvent Listener

Discussion in 'Plugin Development' started by RBHB16, Oct 30, 2015.

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

    RBHB16

    Hey guys I'm really confused right now. I had just solved this problem I had and the next day I open it up and test it to find out it doesn't work. Maybe one of you guys could help me but this makes no sense at all. So what I'm trying to do is restrict players from crafting a custom made recipe without the custom made items I made. So for example, and unrolled blunt = coco beans but with a lore and stuff. So I want them to not be able to use just regular coco beans to craft it. Please tell me what I'm doing wrong... It was working yesterday!

    CraftingBlunt Class:
    Code:
    package com.redstery.prisondrugs.events.player;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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.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 beans = new ItemStack(Material.INK_SACK);
            beans.setDurability((short) 3);
           
            Player p = (Player) evt.getWhoClicked();
            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");
            meta2.setLore(Arrays.asList(ChatColor.GRAY + "Shift + Right Click to get baked", (ChatColor.GOLD + "Unstack blunts before consuming!")));
            rolledblunt.setItemMeta(meta2);       
           
            ShapelessRecipe rolledrecipe = new ShapelessRecipe(rolledblunt);
            rolledrecipe.addIngredient(Material.SUGAR_CANE);
            rolledrecipe.addIngredient(unrolledblunt.getData());
    
            if(evt.getInventory().contains(unrolledblunt)){
                return;
            }
           
            if(evt.getRecipe().equals(rolledblunt)){
                if(evt.getInventory().contains(beans)){
                    p.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "You need to use a unrolled blunt to craft this!");
                    evt.setCancelled(true);
                }
            }
    
    }
    }
       
    
    MainClass:
    Code:
    package com.redstery.prisondrugs;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    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");
            meta2.setLore(Arrays.asList(ChatColor.GRAY + "Shift + Right Click to get baked", (ChatColor.GOLD + "Unstack blunts before consuming!")));
            rolledblunt.setItemMeta(meta2);
           
           
            ShapelessRecipe rolledrecipe = new ShapelessRecipe(rolledblunt);
            rolledrecipe.addIngredient(Material.SUGAR_CANE);
            rolledrecipe.addIngredient(unrolledblunt.getData());
           
            /*
            ItemStack emptypipe = new ItemStack(Material.BLAZE_ROD);
            ItemMeta meta3 = emptypipe.getItemMeta();
            meta3.setDisplayName(ChatColor.BOLD + "" + ChatColor.YELLOW + "Empty Pipe");
            List<String> lore3 = new ArrayList<String>();
            lore3.add(ChatColor.GRAY + "Use this to craft a working pipe!");
            meta3.setLore(lore3);
            emptypipe.setItemMeta(meta3);
           
           
            ShapedRecipe cokepiperecipe = new ShapedRecipe(emptypipe);
            cokepiperecipe.shape("   ", "GGG", "   ");
            cokepiperecipe.setIngredient('G', Material.GLASS);*/
            /*
            ItemStack fullpipe = new ItemStack(Material.BLAZE_ROD);
            ItemMeta meta4 = fullpipe.getItemMeta();
            meta4.setDisplayName(ChatColor.BOLD + "" + ChatColor.RED + "Pipe");
            meta4.setLore(Arrays.asList(ChatColor.GRAY + "Use this to smoke cocaine!", (ChatColor.GOLD + "Unstack pipes before consuming!")));
            fullpipe.setItemMeta(meta4);
    
            ItemStack coke = new ItemStack(Material.SUGAR);
            ItemMeta meta5 = coke.getItemMeta();
            meta5.setDisplayName(ChatColor.BOLD + "" + ChatColor.DARK_BLUE + "Pure Cocaine");
            meta5.setLore(Arrays.asList(ChatColor.GRAY + "Purest of all cocaine", ChatColor.GOLD + "Combine this and a pipe to make a working pipe!"));
            coke.setItemMeta(meta5);
    
            ShapelessRecipe cokeinpipe = new ShapelessRecipe(fullpipe);
            cokeinpipe.addIngredient(Material.BLAZE_ROD);
            cokeinpipe.addIngredient(coke.getData());
           
            FurnaceRecipe cokerecipe = new FurnaceRecipe(coke, Material.SUGAR_CANE);*/
           
           
           
           
           
            getServer().addRecipe(unrolledrecipe);
            getServer().addRecipe(rolledrecipe);
            /*getServer().addRecipe(cokerecipe);
            getServer().addRecipe(cokeinpipe);*/
           
            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);
        }
    
    }
    
     
  2. Offline

    SuperSniper

    @RBHB16 You aren't registering your listeners in your onEnable(), you made the method, you are just never calling it.

    Also, in your CraftingBlunt class, there's a grammatical error, you don't have to change it, but I would, just to make it correct.

    Code:
    p.sendMessage(ChatColor.BOLD+""+ ChatColor.RED+"You need to use a unrolled blunt to craft this!");
    
    change to

    Code:
    p.sendMessage(ChatColor.BOLD+""+ ChatColor.RED+"You need to use an unrolled blunt to craft this!");
    
    Always use "an" instead of "a" whenever the next word starts with a vowel (A,E,I,O,U):)
     
    RBHB16 likes this.
  3. Offline

    RBHB16

    Wow I didn't even notice I thought I remembered myself registering my listeners. Thanks english isn't my main language :p

    But the CraftItemEvent still has an odd error and it won't restrict the player from using coco beans :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 30, 2015
  4. Offline

    Scimiguy

    Does it?
    Weird..

    All I know is, I have no idea what is broken without that error
     
Thread Status:
Not open for further replies.

Share This Page