Helping restricting players from crafting items!

Discussion in 'Plugin Development' started by RBHB16, Jan 24, 2016.

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

    RBHB16

    Hey guys, so my problem here is that I am trying to make a drug plugin that has many shaped recipes to make drugs. So in this case, 3 pieces of paper across crafting table = unrolled blunt(cocoa bean). Then an unrolled blunt(cocoa bean) + sugar cane = rolled blunt. Problem is, players can just use a regular cocoa bean, instead of an unrolled blunt with the item meta and craft a rolled blunt. So I tried using a listener, but all it does is prevent me from crafting anything but the rolled blunt. Help would be greatly appreciated! BTW I have a lot of classes with different methods such as recipes and etc, so if you need to see those as well let me know.

    Crafting Event Class Listener:
    Code:
    package com.redstery11.prisondrugs.events.player;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.inventory.CraftingInventory;
    import org.bukkit.inventory.ItemStack;
    
    import com.redstery11.prisondrugs.DrugStorage;
    import com.redstery11.prisondrugs.MainClass;
    import com.redstery11.prisondrugs.getrecipes.RecipeGetter;
    
    
    public class Crafting implements Listener {
        public DrugStorage drugstorage = new DrugStorage();
        public RecipeGetter getrecipe = new RecipeGetter();
      
      
        public Crafting(MainClass mainClass) {
    
        }
    
    
        @EventHandler
        public void CraftRestrict(CraftItemEvent e) {
          
            if(e.getInventory() instanceof CraftingInventory){
                CraftingInventory inv = (CraftingInventory) e.getInventory();
                if(inv.getSize() !=4 && e.getRecipe().equals(getrecipe.getRolled())){
                    ItemStack unrolled = inv.getMatrix()[4];
                    ItemStack bean = inv.getMatrix()[5];
                    if(unrolled.hasItemMeta() && bean.hasItemMeta()){
                        return;
                                }
                    }
                    e.setCancelled(true);
                    e.getWhoClicked().sendMessage(ChatColor.RED + "YOU NEED A UNROLLED BLUNT TO CRAFT THIS!");
                  
                  
                  
                }
        }
    }
              
      
    
    
     
  2. Offline

    Zombie_Striker

    You do not own redstery11.com. Please choose a domain you own.
    What is this even supposed to do? You can remove this whole constructor and use the default constructor if you're not going to set anything.

    1. What you're put down checks if both items have itemmeta. The thing is minecraft will automatically create it if getItemMeta is ever called for those items. Most likely this is not what you meant.
    2. You are not even logging if this is the case. You may want to debug to see if this ever is the case.
     
  3. Offline

    mythbusterma

    @RBHB16

    So you can either add in the CraftItemEvent listener to check for the item meta when crafting and cancel if not, or you can mess with NMS to change the recipes.

    There's a tutorial about this somewhere in the resources section.
     
  4. Offline

    RBHB16

    RBHB16 is my alt. Redstery11 is my main account lol. I am not sure what you mean by minecraft will automatically create it if getItemMeta is ever called. Thank you for the response though, and I am not sure what is causing the problem though. Something in my code causes every crafting attempt to be blocked.

    That's kind of what I have, put even though I specifically told it to check for the item metas, it still blocks every crafting attempt. So if I put oak logs into a crating table to get planks, the message comes up saying "YOU NEED A UNROLLED BLUNT TO CRAFT THIS"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  5. Offline

    Chloe-chan

    Either ways, you do not own redstery11.com.
     
  6. Offline

    JoaoBM

    @RBHB16 You should you me.yourname.pluginname
     
  7. Offline

    RBHB16

    Ok, I was never taught that, so I'll change it. But do you see a problem with the code that is causing this? @Chloe-chan @mythbusterma @Zombie_Striker @JoaoBM ? According to Zombie_Striker, my code only has it to check for an item meta. Is it possible if I did a check to see if the items were in the crafting table were coco beans and sugarcane, then have the code with a listener to restrict it?
     
    Last edited: Jan 25, 2016
  8. Offline

    JoaoBM

    @RBHB16 I dont know which event would be better in this case but there are CraftItemEvent and PrepareItemCraftEvent.
     
  9. Offline

    RBHB16

    Correct, I've been playing with both, but the thing is with PrepareItemCraftEvent, I can't send messages to players saying they need a specific item to craft this etc.. Also, I tried using getRecipe() from prepareItemcraftevent, but the problem is I can't cancel the event with PrepareItemCraftEvent
     
  10. Offline

    JoaoBM

    @RBHB16 I had this problem too when cancelling the event. I have a class now so if when I'm back the thread is still unsolved I'll try to search for a way to do this. :)
     
  11. Offline

    RBHB16

    Ok, thanks I appreciate it!
     
  12. Offline

    Zombie_Striker

    If ANY plugin calls [your Itemstack].getItemMeta(), an item meta will automatically be created. Although it will not contain any different information, hasItemMeta will return true. What I'm suggesting is to at least log if this is ever the case, to see if either itemstack has itemmeta.
     
  13. Offline

    JoaoBM

    @RBHB16 Ok, so I think I found a way to do this:

    "You need to check in PrepareItemCraftEvent. Check each slot for the itemstack (make sure it's not null), and check if it has item meta, if so if it's X item meta (yours), then do stuff. (set result)"

    Didn't test it and see if it works.
     
  14. Offline

    RBHB16

    Ahh, Ok I understand now. Thank you very much I'll try that
    I will test it soon, thank you!

    Edit: This is what I got so far, I am struggling to find a way to disable the player from crafting it but I can't figure out how. I left the part at the else if statement uncompleted because I had no idea what to write.
    Code:
    package me.redstery11.prisondrugs.events.player;
    
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.CraftingInventory;
    import org.bukkit.inventory.ItemStack;
    
    import me.redstery11.prisondrugs.getrecipes.RecipeGetter;
    
    import me.redstery11.prisondrugs.DrugStorage;
    import me.redstery11.prisondrugs.MainClass;
    
    
    public class Crafting implements Listener {
        public DrugStorage drugstorage = new DrugStorage();
        public RecipeGetter getrecipe = new RecipeGetter();
      
      
        public Crafting(MainClass mainClass) {
    
        }
    
    
      
      
        @EventHandler
        public void UnrolledCraftCheck(PrepareItemCraftEvent evt){
            if (evt.getInventory() instanceof CraftingInventory){
            CraftingInventory inv = (CraftingInventory) evt.getInventory();
            ItemStack unrolled = inv.getMatrix()[5];
            ItemStack cane = inv.getMatrix()[4];
            if(unrolled.hasItemMeta() && cane.equals(Material.SUGAR_CANE)){
                inv.setResult(drugstorage.getDrug("blunt"));
              
            }
            else if(unrolled()){
                if(cane.equals(Material.SUGAR_CANE)){
                    inv.setResult(drugstorage.getDrug("air"));
                }
            }
            }
        }
    }
          
          
      
        
     
    Last edited: Jan 25, 2016
  15. Offline

    PresentingTulip

    redstery, from convicted is that you? ComputerGuy365/zzwerling here :D
     
    RBHB16 likes this.
  16. Offline

    RBHB16

    what's up man! help me out here :)

    Edit: Maybe its my drugstorage class that isn't working properly? That has all the item meta information and switch case. Tell me if you guys see anything wrong here: @JoaoBM
    @Chloe-chan @Zombie_Striker @mythbusterma
    Code:
    package me.redstery11.prisondrugs;
    
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.FurnaceRecipe;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.ShapedRecipe;
    
    
    public class DrugStorage {
      
    
    
     
    
        //create your drug itemstacks and lore lists here
        private ItemStack unrolledblunt = new ItemStack(Material.INK_SACK, 1, (short)3);
    
        private ItemMeta unrolledbluntmeta = unrolledblunt.getItemMeta();
     
      //------------------------------------------------------------------------------------
     
        private ItemStack rolledblunt = new ItemStack(Material.STICK);
     
        private ItemMeta bluntmeta = rolledblunt.getItemMeta();
     
      //------------------------------------------------------------------------------------
     
        private ItemStack emptypipe = new ItemStack(Material.BLAZE_ROD);
     
        private ItemMeta emptypipemeta = emptypipe.getItemMeta();
     
      //------------------------------------------------------------------------------------
     
        private ItemStack pipe = new ItemStack(Material.BLAZE_ROD);
     
        private ItemMeta pipemeta = pipe.getItemMeta();
     
      //------------------------------------------------------------------------------------
     
        private ItemStack coke = new ItemStack(Material.SUGAR);
     
        private ItemMeta cokemeta = coke.getItemMeta();
    
      //------------------------------------------------------------------------------------
      
        //This will update all these itemstacks with the correct lore
        public void updateDruglores() {
        
            unrolledbluntmeta.setDisplayName(ChatColor.DARK_BLUE + "Unrolled Blunt");
        
            unrolledbluntmeta.setLore(Arrays.asList(ChatColor.GOLD + "Use this to craft a blunt!"));
         
            unrolledblunt.setItemMeta(unrolledbluntmeta);
         
          //------------------------------------------------------------------------------------
         
            bluntmeta.setDisplayName(ChatColor.DARK_GREEN + "Blunt");
         
            bluntmeta.setLore(Arrays.asList(ChatColor.GOLD + "Shift + Right Click to get baked!", ChatColor.GRAY + "Unstack blunts before consuming!"));
         
            rolledblunt.setItemMeta(bluntmeta);
         
          //------------------------------------------------------------------------------------
         
            emptypipemeta.setDisplayName(ChatColor.YELLOW + "Pipe");
         
            emptypipemeta.setLore(Arrays.asList(ChatColor.GOLD + "Use this to craft a functioning pipe!"));
         
            emptypipe.setItemMeta(emptypipemeta);
         
          //------------------------------------------------------------------------------------
         
            pipemeta.setDisplayName(ChatColor.DARK_BLUE + "Pipe");
         
            pipemeta.setLore(Arrays.asList(ChatColor.GOLD + "Shift + Right click to hit it!", ChatColor.GRAY + "Unstack pipes before consuming!"));
         
            pipe.setItemMeta(pipemeta);
         
          //------------------------------------------------------------------------------------
         
            cokemeta.setDisplayName(ChatColor.BLUE + "Pure Cocaine");
         
            cokemeta.setLore(Arrays.asList(ChatColor.GOLD + "Purest of all cocaine"));
         
            coke.setItemMeta(cokemeta);
        
            //More drugs go here for instance
        }
     
    
        //CRAFTING
        //--------------------------------------------------------------------------------------
     
    
        //registers the crafting recipes
        public void registerRecipes() {
            //Unrolled blunt----------------------------------------------------------------------------------
            ShapedRecipe unrolledrecipe = new ShapedRecipe(getDrug("unrolledblunt"));
            unrolledrecipe.shape("   ", "PPP", "   ");
            unrolledrecipe.setIngredient('P', Material.PAPER);
            Bukkit.getServer().addRecipe(unrolledrecipe);
            //Rolled Blunt----------------------------------------------------------------------------------------------
            ShapedRecipe rolledrecipe = new ShapedRecipe(getDrug("blunt"));
            rolledrecipe.shape("   ", " SU", "   ");
            rolledrecipe.setIngredient('S', Material.SUGAR_CANE);
            rolledrecipe.setIngredient('U', getDrug("unrolledblunt").getData());
            Bukkit.getServer().addRecipe(rolledrecipe);
            //Coke Pipe-------------------------------------------------------------------------------------------------------
            ShapedRecipe cokepiperecipe = new ShapedRecipe(getDrug("emptypipe"));
            cokepiperecipe.shape("   ", "GGG", "   ");
            cokepiperecipe.setIngredient('G', Material.GLASS);
            Bukkit.getServer().addRecipe(cokepiperecipe);
            //Coke In Pipe-----------------------------------------------------------------------------------------------
            ShapedRecipe cokeinpipe = new ShapedRecipe(getDrug("pipe"));
            cokeinpipe.shape("   ", " RC", "   ");
            cokeinpipe.setIngredient('R', Material.BLAZE_ROD);
            cokeinpipe.setIngredient('C', getDrug("coke").getData());
            Bukkit.getServer().addRecipe(cokeinpipe);
            //Sugar cane to Coke-----------------------------------------------------------------------------
            FurnaceRecipe cokerecipe = new FurnaceRecipe(getDrug("coke"), Material.SUGAR_CANE);
            Bukkit.getServer().addRecipe(cokerecipe);
        
     
        }
    
        public ItemStack getDrug(String drugname) {
        
            //Initialize the drug lore
        
            switch(drugname) {
                case "unrolledblunt":
                    return this.unrolledblunt;
                 
                case "blunt":
                    return this.rolledblunt;
                 
                case "emptypipe":
                    return this.emptypipe;
                 
                case "pipe":
                    return this.pipe;
                 
                case "coke":
                    return this.coke;
             
                case "air":
                    return null;
                //More drugs here again
                default:
                    return null;
            }
        
        }
    
        }
    
    
    
     
    Last edited: Jan 26, 2016
  17. Offline

    Zombie_Striker

    @RBHB16
    Have you tried debugging? Are you sure all your code works as you expected?

    EDIT:
    This is testing if an itemstack is equal to a material.
     
  18. Offline

    RBHB16

    Code:
    package me.redstery11.prisondrugs.events.player;
    
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.CraftingInventory;
    import org.bukkit.inventory.ItemStack;
    import me.redstery11.prisondrugs.getrecipes.RecipeGetter;
    
    import me.redstery11.prisondrugs.DrugStorage;
    import me.redstery11.prisondrugs.MainClass;
    
    
    public class Crafting implements Listener {
        public DrugStorage drugstorage = new DrugStorage();
        public RecipeGetter getrecipe = new RecipeGetter();
    
    
    
        public Crafting(MainClass mainClass) {
    
        }
    
    
        @EventHandler
        public void UnrolledCraftCheck(PrepareItemCraftEvent evt){
            if (evt.getInventory() instanceof CraftingInventory){
            CraftingInventory inv = (CraftingInventory) evt.getInventory();
            ItemStack unrolled = inv.getMatrix()[5];
            ItemStack cane = inv.getMatrix()[4];
            if(unrolled.hasItemMeta() && cane.getType() == Material.SUGAR_CANE){
                inv.setResult(drugstorage.getDrug("blunt"));
      
            }
            else if(!unrolled.hasItemMeta()){
                if(cane.equals(Material.SUGAR_CANE)){
                    inv.setResult(drugstorage.getDrug("air"));
                }
            }
            }
        }
    }
    It somewhat worked. Now this is where it gets confusing. So, using a unrolled blunt(cocoa bean) + a sugar cane, it gives me just a plain stick. This stick should say its a blunt and have a lore with it. Its just a plain stick. Then I tested to see if it was denying the use of regular cocoa beans and I put plain cocoa beans with sugar cane and it gave me the blunt with a lore??? I'm so confused I have no idea.

    I will set up remote debugging right now

    Edit: Well, I looked through the variables of the debug, and it says that the unrolled itemstack is Air. This seems like the problem, but I'm not sure if I am right. It is also not returning a meta, says meta null
     
    Last edited: Jan 26, 2016
  19. Offline

    Zombie_Striker

    @RBHB16
    I think one problem is that you are not calling the events that add the displayname/lore to the items in DrugStorage. Try referencing DrugStorage.updateDruglores(); and registerRecipes() in that order.

    One thing I notice is you still have not fixed the Itemstack equal Material problem from the previous post.
     
  20. Offline

    RBHB16

    What do you mean referencing them in that order? And about the itemstack check. What I did was changed it to getType because it has a material modifier. During the debug process though the listener would pick it up as sugar cane though, so I guess it works.

    Edit: Well, you are right. I get an error in console when there is just plain sugar cane so it is causing a problem. I'm not sure what I should do about it then

    Edit: I think what you're saying about referencing them in the main class? This is my main class:
    Code:
    package me.redstery11.prisondrugs;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.redstery11.prisondrugs.events.player.CocaineEffect;
    import me.redstery11.prisondrugs.events.player.Crafting;
    import me.redstery11.prisondrugs.events.player.WeedEffect;
    
    public class Main extends JavaPlugin {
        DrugStorage drugstorage = new DrugStorage();
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
           
            drugstorage.registerRecipes();
    
            logger.info(pdfFile.getName() + "has been disabled V" + pdfFile.getVersion());
            getServer().clearRecipes();
        }
       
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
           
            drugstorage.updateDruglores();
            drugstorage.registerRecipes();
            registerListeners();
    
           
            logger.info(pdfFile.getName() + "has been enabled V" + pdfFile.getVersion());
        }
        public void registerListeners() {
            getServer().getPluginManager().registerEvents(new CocaineEffect(this), this);
            getServer().getPluginManager().registerEvents(new WeedEffect(this), this);
            getServer().getPluginManager().registerEvents(new Crafting(this), this);
        }   
    }
    
    
    
    
               
    
           
    
    
    
    
     
    Last edited: Jan 26, 2016
  21. Offline

    Zombie_Striker

    @RBHB16
    Yes, that is what I meant.

    Do you know if this is triggered? If you print out .getResults's lore after you set the result, what does it print out?
     
  22. Offline

    RBHB16

    I went ahead and tried it, and it doesn't run. When debugging, everytime I click resume on the line right before it, it gives me the error. So the setresult line isn't working. I think it is something wrong with the switch case I have in drugstorage or something.. No idea. I got this error
    Error Code (open)

    Code:
    [08:46:34 ERROR]: Could not pass event PrepareItemCraftEvent to PrisonDrugs v3.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPreCraftEvent(CraftEventFactory.java:727) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.CraftingManager.craft(CraftingManager.java:298) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.ContainerWorkbench.a(ContainerWorkbench.java:56) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.InventoryCrafting.splitStack(InventoryCrafting.java:111) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.Slot.a(Slot.java:72) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.Container.clickItem(Container.java:297) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1569) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
        at me.redstery11.prisondrugs.events.player.Crafting.UnrolledCraftCheck(Crafting.java:32) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_65]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 21 more


    This is my drugstorage class:
    DrugStorage Class (open)

    Code:
    package me.redstery11.prisondrugs;
    
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.FurnaceRecipe;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.ShapedRecipe;
    
    
    public class DrugStorage {
    
    
    
    
    
        private ItemStack unrolledblunt = new ItemStack(Material.INK_SACK, 1, (short)3);
    
        private ItemMeta unrolledbluntmeta = unrolledblunt.getItemMeta();
    
      //------------------------------------------------------------------------------------
    
        private ItemStack rolledblunt = new ItemStack(Material.STICK);
    
        private ItemMeta bluntmeta = rolledblunt.getItemMeta();
    
      //------------------------------------------------------------------------------------
    
        private ItemStack emptypipe = new ItemStack(Material.BLAZE_ROD);
    
        private ItemMeta emptypipemeta = emptypipe.getItemMeta();
    
      //------------------------------------------------------------------------------------
    
        private ItemStack pipe = new ItemStack(Material.BLAZE_ROD);
    
        private ItemMeta pipemeta = pipe.getItemMeta();
    
      //------------------------------------------------------------------------------------
    
        private ItemStack coke = new ItemStack(Material.SUGAR);
    
        private ItemMeta cokemeta = coke.getItemMeta();
    
      //------------------------------------------------------------------------------------
    
        //This will update all these itemstacks with the correct lore
        public void updateDruglores() {
     
            unrolledbluntmeta.setDisplayName(ChatColor.DARK_BLUE + "Unrolled Blunt");
     
            unrolledbluntmeta.setLore(Arrays.asList(ChatColor.GOLD + "Use this to craft a blunt!"));
      
            unrolledblunt.setItemMeta(unrolledbluntmeta);
      
          //------------------------------------------------------------------------------------
      
            bluntmeta.setDisplayName(ChatColor.DARK_GREEN + "Blunt");
      
            bluntmeta.setLore(Arrays.asList(ChatColor.GOLD + "Shift + Right Click to get baked!", ChatColor.GRAY + "Unstack blunts before consuming!"));
      
            rolledblunt.setItemMeta(bluntmeta);
      
          //------------------------------------------------------------------------------------
      
            emptypipemeta.setDisplayName(ChatColor.YELLOW + "Pipe");
      
            emptypipemeta.setLore(Arrays.asList(ChatColor.GOLD + "Use this to craft a functioning pipe!"));
      
            emptypipe.setItemMeta(emptypipemeta);
      
          //------------------------------------------------------------------------------------
      
            pipemeta.setDisplayName(ChatColor.DARK_BLUE + "Pipe");
      
            pipemeta.setLore(Arrays.asList(ChatColor.GOLD + "Shift + Right click to hit it!", ChatColor.GRAY + "Unstack pipes before consuming!"));
      
            pipe.setItemMeta(pipemeta);
      
          //------------------------------------------------------------------------------------
      
            cokemeta.setDisplayName(ChatColor.BLUE + "Pure Cocaine");
      
            cokemeta.setLore(Arrays.asList(ChatColor.GOLD + "Purest of all cocaine"));
      
            coke.setItemMeta(cokemeta);
     
    
        }
    
    
        //CRAFTING
        //--------------------------------------------------------------------------------------
    
    
        //registers the crafting recipes
        public void registerRecipes() {
            //Unrolled blunt----------------------------------------------------------------------------------
            ShapedRecipe unrolledrecipe = new ShapedRecipe(getDrug("unrolledblunt"));
            unrolledrecipe.shape("   ", "PPP", "   ");
            unrolledrecipe.setIngredient('P', Material.PAPER);
            Bukkit.getServer().addRecipe(unrolledrecipe);
            //Rolled Blunt----------------------------------------------------------------------------------------------
            ShapedRecipe rolledrecipe = new ShapedRecipe(getDrug("blunt"));
            rolledrecipe.shape("   ", " SU", "   ");
            rolledrecipe.setIngredient('S', Material.SUGAR_CANE);
            rolledrecipe.setIngredient('U', getDrug("unrolledblunt").getData());
            //Bukkit.getServer().addRecipe(rolledrecipe);
            //Coke Pipe-------------------------------------------------------------------------------------------------------
            ShapedRecipe cokepiperecipe = new ShapedRecipe(getDrug("emptypipe"));
            cokepiperecipe.shape("   ", "GGG", "   ");
            cokepiperecipe.setIngredient('G', Material.GLASS);
            Bukkit.getServer().addRecipe(cokepiperecipe);
            //Coke In Pipe-----------------------------------------------------------------------------------------------
            ShapedRecipe cokeinpipe = new ShapedRecipe(getDrug("pipe"));
            cokeinpipe.shape("   ", " RC", "   ");
            cokeinpipe.setIngredient('R', Material.BLAZE_ROD);
            cokeinpipe.setIngredient('C', getDrug("coke").getData());
            Bukkit.getServer().addRecipe(cokeinpipe);
            //Sugar cane to Coke-----------------------------------------------------------------------------
            FurnaceRecipe cokerecipe = new FurnaceRecipe(getDrug("coke"), Material.SUGAR_CANE);
            Bukkit.getServer().addRecipe(cokerecipe);
            //Unrolled Blunt to Rolled---------------------------------------------------------------------------------
    
        }
    
    
        public ItemStack getDrug(String drugname) {
     
            //Initialize the drug lore
     
            switch(drugname) {
                case "unrolledblunt":
                    return this.unrolledblunt;
              
                case "blunt":
                    return this.rolledblunt;
              
                case "emptypipe":
                    return this.emptypipe;
              
                case "pipe":
                    return this.pipe;
              
                case "coke":
                    return this.coke;
          
                case "air":
                    return null;
                //More drugs here again
                default:
                    return null;
            }
     
        }
    
        }
    
    
    

    Edit: Maybe because the ItemStacks/Metas are private?

    Edit Again: So, the event.setresult works, but it gives me a plain stick. It turns out to be that that plain stick also functions for a different event that I have(which is consuming it to get effects). The problem is, when I use the plain cocoa beans and the sugar cane it gives me the stick with the item meta and that stick isn't function able at all. It should be vise versa
     
    Last edited: Jan 27, 2016
  23. Offline

    Zombie_Striker

    @RBHB16
    Try the following steps if you think the problem is with the result:
    1. Test the lore of the itemstack if you use .getDrug(). If it contains the lore, it is a problem with the way MC stores recipes. if it does not contain the lore
    2. Test the lore for blunt/pipe directly by testing both the "emptypipemeta" and "emptypip"'s lores. If they contain the lore, then it is a problem with the switch cases. if they do not contain the lore
    3. Test to see what happens why you try to apply/change the lore.
     
  24. Offline

    RBHB16

    Well, what I think the problem is that adding a custom recipe to the server and setting a result don't mix. I know that there is a way to set a lore to an item that has just been crafted but I forget the event name... maybe you could remind me of it? The reason why I think they don't mix is because, when I use the getDrug call, it gives me a stick with no lore. When I use the regular cocoa bean and gives me the blunt outcome is because the ShapedRecipe can't accept custom item metas? It seems like it takes the itemstack where it has a lore or not.

    Edit: Also, is it possible because when adding an ingredient, it asks for a material, but the unrolled blunt is an item stack? In this case, the unrolled blunt just becomes a cocoa bean? Because I do drugstorage.getDrug("unrolledblunt").getData(), so wouldn't that just take the item it is but not the lores and stuff? Not sure though

    Edit Again: I played around with it a little more and changed the method a little. This somewhat works as now it accepts the unrolled blunt + sugar cane to make a blunt, but it still accepts a regular cocoa bean. :/
    Code:
      
    package me.redstery11.prisondrugs.events.player;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.CraftingInventory;
    import org.bukkit.inventory.ItemStack;
    import me.redstery11.prisondrugs.getrecipes.RecipeGetter;
    
    import me.redstery11.prisondrugs.DrugStorage;
    import me.redstery11.prisondrugs.Main;
    
    
    public class Crafting implements Listener {
        public DrugStorage drugstorage = new DrugStorage();
        public RecipeGetter getrecipe = new RecipeGetter();
        public Material Cane = Material.SUGAR_CANE;
        public String UnRolled = ChatColor.DARK_BLUE + "Unrolled Blunt";
        public String Blunt = ChatColor.DARK_GREEN + "Blunt";
      
      
      
        public Crafting(Main mainClass) {
          
        }
      
        @EventHandler
        public void craftRestrictor(PrepareItemCraftEvent event){
            CraftingInventory ci = event.getInventory();
            if (event.getRecipe().getResult().hasItemMeta() && Blunt.equals(event.getRecipe().getResult().getItemMeta())){
                boolean found1 = false;
                for (ItemStack item: event.getInventory().getMatrix()) {
                    if (item != null && item.hasItemMeta()){
                        if (UnRolled.equals(item.hasItemMeta())){
                            found1 = true;
                      
                    }
                    if(!found1){
                        ci.setResult(null);
                    }
                }
            }
            }
        }
    }
      
    
        
    Edit once again: Well, I just noticed the for loop won't work because not every item has an item meta in the crafting inventory (sugar cane doesn't). So I'm stuck again.
     
    Last edited: Jan 27, 2016
Thread Status:
Not open for further replies.

Share This Page