Solved Check if custom recipe is being made not working.

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

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

    kasszz

    Dear bukketiers,

    I'm trying to make a mod with a custom recipe, but when I check if I'm making it, it doesn't show up.

    This is my code:
    Code:
        ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
     
    public void OnEnable(){
    getServer().addRecipe(LightningWand);
    }
        @EventHandler(ignoreCancelled = true)
        public void onPrepareItemCraft (PrepareItemCraftEvent event){
            if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
    The logger gives me the Something Something, but not the Does it work? HELLPPPPP

    Thanks :)
     
  2. Offline

    Burnett1

    First of all the event will not be used because it is in your onEnable. Have it below that not in it. Is your plugin even working? Test it to make sure its active and your EVENTS ARE REGISTERED.
     
  3. Offline

    kasszz

    But it does work because I do get the getLogger().info("Something Something");
    Code:
    if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand)

    that is the line that isn't "working", it doesn't give an error
    but I want it to work when the LightningWand is created.
    But when I create the shape and get the stick the serverlog doesn't say "Does it work ?".
    and that is my problem :(
     
  4. Offline

    Burnett1

    What is the log message?
     
  5. Offline

    Aqua

    He means register the Event:

    Code:
        ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
     
    public void OnEnable(){
    Bukkit.getPluginManager().registerEvents(this, this);
    getServer().addRecipe(LightningWand);
    }
        @EventHandler(ignoreCancelled = true)
        public void onPrepareItemCraft (PrepareItemCraftEvent event){
            if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
     
  6. Offline

    kasszz

    what do you mean ?
     
  7. Offline

    Burnett1

    You said it said "something something" but what did it actually say.
     
  8. Offline

    kasszz

    ooohh sorry, it's just a getLogger().info(""); with some random text in it. :p just to check if it is getting to that line :)
     
  9. Offline

    Burnett1

    Mind if you show the whole class without cutting anything out?
     
  10. Offline

    Thej0y

    Look at a post i made yesterday. the last post by Digi might help you a LOT
    http://forums.bukkit.org/threads/list-custom-recipe-one-code-for-all-perm.144262/...
    it did for me!:)
    You should also make a class for your listener and register it..:
    Code:
    --registering the event--
    public final MyCraftListener mcl = new MyCraftListener();
     
    -- the public class-
    public class MyCraftListener implements Listener{
    //event to listen....
    Those are only examples :p
     
  11. Offline

    kasszz

    This is all my coding:
    Code:
    package me.kasszz.magicmadness;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MagicMadness extends JavaPlugin implements Listener {
       
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            onAddRecipe();
        }
       
        public void onDisable(){
            getServer().clearRecipes();
        }
       
        ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
        @EventHandler(ignoreCancelled = true)
        public void onPrepareItemCraft (PrepareItemCraftEvent event){
            if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR)||event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(player.getInventory().getHelmet().getType().equals(Material.IRON_HELMET)){
                    if(player.getItemInHand().getType().equals(Material.STICK)){
                        player.sendMessage("Vuurbal VUURBAL");
                        /*ItemStack LightningWand = new ItemStack(Material.STICK, 1);
                        ItemMeta LightningWandMeta = LightningWand.getItemMeta();
                        LightningWandMeta.setDisplayName("Lightning Wand");
                        LightningWand.setItemMeta(LightningWandMeta);
                        PlayerInventory playerInventory = player.getInventory();
                        playerInventory.addItem(LightningWand);*/
                    }
                }
            }
        }
       
        public void onAddRecipe(){
            getServer().addRecipe(LightningWand);
        }
    }
     
  12. Offline

    Burnett1

    Trying moving the Lightning wand bit inside the onEnable and the addRecipe
     
  13. Offline

    kasszz

    then the LightningWand inside of the IF wont work anymore :(
     
  14. Offline

    Burnett1

    I said move this

    Code:
    ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
    
    and this

    Code:
    getServer().addRecipe(LightningWand);
    Inside this

    Code:
     public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            onAddRecipe();
        }
    and add this @Override to to onEnable and onDisable.
     
  15. Offline

    ZeusAllMighty11

    I would compare the results instead of the recipes
     
  16. Offline

    kasszz

    Now I have this:

    Code:
    package me.kasszz.magicmadness;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MagicMadness extends JavaPlugin implements Listener {
       
        @Override
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
            getServer().addRecipe(LightningWand);
            onAddRecipe();
        }
       
        @Override
        public void onDisable(){
            getServer().clearRecipes();
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onPrepareItemCraft (PrepareItemCraftEvent event){
            if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR)||event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(player.getInventory().getHelmet().getType().equals(Material.IRON_HELMET)){
                    if(player.getItemInHand().getType().equals(Material.STICK)){
                        player.sendMessage("Vuurbal VUURBAL");
                        /*ItemStack LightningWand = new ItemStack(Material.STICK, 1);
                        ItemMeta LightningWandMeta = LightningWand.getItemMeta();
                        LightningWandMeta.setDisplayName("Lightning Wand");
                        LightningWand.setItemMeta(LightningWandMeta);
                        PlayerInventory playerInventory = player.getInventory();
                        playerInventory.addItem(LightningWand);*/
                    }
                }
            }
        }
       
        public void onAddRecipe(){
        }
    }
    but I get red lines onder LightningWand in this line:
    if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){

    @TheGreenGamerHD

    How ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  17. Offline

    ZeusAllMighty11

    .getResult() returns an itemstack, compare with that
     
  18. Offline

    kasszz

    can you set it in an example please :X and I read that that wasn't clean coding or something :S
    ?
    when I try it, The method getResult() is undefined for the type PrepareItemCraftEvent
     
  19. Offline

    Burnett1

    Do this outside above onEnable

    Code:
    ShapedRecipe LightningWand;
    Then do

    Code:
    LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
     
  20. Offline

    kasszz

    Still the same :( no errors but it isn't going past the IF

    This isn't working aswell :(

    Code:
        @EventHandler(ignoreCancelled = true)
        public void onCraftItem (CraftItemEvent event){
            if(event.getResult().equals(LightningWand)){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  21. Offline

    Burnett1

    Lets see the whole new code?
     
  22. Offline

    kasszz

    oke :)

    Code:
    Code:
    package me.kasszz.magicmadness;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MagicMadness extends JavaPlugin implements Listener {
     
        ShapedRecipe LightningWand;
        @Override
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1)).shape(" W ", " W ", " W ").setIngredient('W', Material.WOOD_DOOR);
            getServer().addRecipe(LightningWand);
            onAddRecipe();
        }
     
        @Override
        public void onDisable(){
            getServer().clearRecipes();
        }
     
        @EventHandler(ignoreCancelled = true)
        public void onCraftItem (CraftItemEvent event){
            if(event.getResult().equals(LightningWand)){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
     
        @EventHandler(ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR)||event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(player.getInventory().getHelmet().getType().equals(Material.IRON_HELMET)){
                    if(player.getItemInHand().getType().equals(Material.STICK)){
                        player.sendMessage("Vuurbal VUURBAL");
                        /*ItemStack LightningWand = new ItemStack(Material.STICK, 1);
                        ItemMeta LightningWandMeta = LightningWand.getItemMeta();
                        LightningWandMeta.setDisplayName("Lightning Wand");
                        LightningWand.setItemMeta(LightningWandMeta);
                        PlayerInventory playerInventory = player.getInventory();
                        playerInventory.addItem(LightningWand);*/
                    }
                }
            }
        }
     
        public void onAddRecipe(){
        }
    }
    tried it with this code aswell:

    Code:
        @EventHandler(ignoreCancelled = true)
        public void onPrepareItemCraft (PrepareItemCraftEvent event){
            if(event.getRecipe().equals(LightningWand) || event.getRecipe() == LightningWand){
                getLogger().info("Does it work ?");
            }
            getLogger().info("Something Something");
        }
     
  23. Offline

    kasszz

    can you give me an example on how to use it because it looks pretty overwelming :X (I never used 2 classes). Because it looks like you are only printing if it's working or something. (Sorry if I'm being a noob :X )
     
  24. kasszz
    Yes, I'm printing the boolean value in the example, it's basic Java to convert that to a condition.
    For your code just do:
    Code:
    if(RecipeUtil.areEqual(event.getRecipe(), LightningWand)) {
        // ...
    }
     
  25. Offline

    kasszz

    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page