1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

[Solved] Weird CraftItemEvent bug

Discussion in 'Development' started by xBeastMode, Mar 8, 2017.

  1. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    I've got some kind of bug with CraftItemEvent. When I craft an item once the code works, when I do it more than once the code does not run anymore. Even if I die or rejoin the server it still does not allow me to craft an item twice. There are no errors on the console. I know applyEnchantments() could not be the cause, this method only add enchantments to the output item.

    Here's the code:
    PHP:
            /**
             * @priority MONITOR
             *
             * @param CraftItemEvent $event
             */
            
    public function onCraftEvent(CraftItemEvent $event){
                    
    $player $event->getPlayer();

                    
    $item $event->getRecipe()->getResult();
                    
    $this->plugin->applyEnchantments($item);
                    
    $player->getInventory()->addItem($item);

                    foreach(
    $event->getInput() as $key => $input){
                        
    $player->getInventory()->remove($input);
                    }

                    
    $player->sendMessage(str_replace("&""\xc2\xa7"$this->plugin->getConfig()->get('mensaje')));
                    
    $event->setCancelled();
            }
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    @priority MONITOR
    If the priority is on MONITOR, I don't think setCancelled() would work effectively (citation needed). You aren't supposed to modify anything from the event if you declare priority as MONITOR. You can only and only listen to the event and get your needs from the event.

    EventPriority.php
    PHP:
        /**
         * Event call is of very low importance and should be ran first, to allow
         * other plugins to further customise the outcome
         */
        
    const LOWEST 5;
        
    /**
         * Event call is of low importance
         */
        
    const LOW 4;
        
    /**
         * Event call is neither important or unimportant, and may be ran normally
         */
        
    const NORMAL 3;
        
    /**
         * Event call is of high importance
         */
        
    const HIGH 2;
        
    /**
         * Event call is critical and must have the final say in what happens
         * to the event
         */
        
    const HIGHEST 1;
        
    /**
         * Event is listened to purely for monitoring the outcome of an event.
         *
         * No modifications to the event should be made under this priority
         */
        
    const MONITOR 0;
     
  3. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Ohh :eek: didn't knew it, I always thought MONITOR was the last one to be called. I'm stupid. I will try this out later. :D
     
    jasonwynn10 and Muqsit like this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You're not alone. Guess who else has made the mistake but isn't aware of the mistake?
    YES, THIS GUY => :shoghi:
    Click on that emoji for magic.
     
    Jack Noordhuis and HimbeersaftLP like this.
  5. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    This guy, LMAO! It's still not working, I will try to figure it out on my own.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I don't even think this is a helpful statement to say but I'll say it anyway..
    Try cancelling the event before doing anything else.
     
  7. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    I found the bug. I just had to clone the recipe result cause PHP can't make two of the exactly same objects.

    PHP:
            /**
             * @param CraftItemEvent $event
             */
            
    public function onCraftEvent(CraftItemEvent $event){
                    
    $player $event->getPlayer();

                    
    $item = clone $event->getRecipe()->getResult();
                    
    $this->plugin->applyEnchantments($item);
                    
    $player->getInventory()->addItem($item);

                    foreach(
    $event->getInput() as $key => $input){
                        
    $player->getInventory()->removeItem($input);
                    }

                    
    $player->sendMessage(str_replace("&""\xc2\xa7"$this->plugin->getConfig()->get('mensaje')));
                    
    $event->setCancelled();
            }
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.