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

Event Not Triggerening

Discussion in 'Development' started by Junkdude, May 15, 2017.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    On my interact event, the potion line isnt where, where i get haste if the name that name, no errors or anything

    PHP:
    <?php
    namespace CustomPotions;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    CustomPotions\EventHandler;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\sound;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\nbt\NBT;
    use 
    pocketmine\nbt\tag\ByteTag;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\nbt\tag\DoubleTag;
    use 
    pocketmine\nbt\tag\FloatTag;
    use 
    pocketmine\nbt\tag\IntTag;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\nbt\tag\ListTag;
    use 
    pocketmine\nbt\tag\ShortTag;
    use 
    pocketmine\nbt\tag\StringTag;
    use 
    pocketmine\nbt\tag\IntArrayTag;
    class 
    Main extends PluginBase implements Listener {
      public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getServer()->getPluginManager()->registerEvents(new EventHandler($this), $this);
        }

      public function 
    onDisable() {
      }

      
    //I REALISE THESE ARE FROM ESSENTIALSPE!

      
    public function validateName(string $string$allowColorCodes false): bool{
            if(
    trim($string) === ""){
                return 
    false;
            }
            
    $format = [];
            if(
    $allowColorCodes){
                
    $format[] = "/(\&|\ยง)[0-9a-fk-or]/";
            }
            
    $format[] = "/[a-zA-Z0-9_]/"// Due to color codes can be allowed, then check for them first, so after, make a normal lookup
            
    $s preg_replace($format""$string);
            if(
    strlen($s) !== 0){
                return 
    false;
            }
            return 
    true;
        }

        public function 
    givePotion($player$potion) {
            switch(
    $potion) {
                case 
    "haste1":
                
    $potion Item::get(37312341);
                
    $potion->setDamage(500);
                
    $tag = new CompoundTag('', []);
                
    $tag->haste1 = new IntTag('haste1'1);      
                
    $tag->display = new CompoundTag("display", [
                
    "Name" => new StringTag("Name"TF::YELLOW."Potion Of Haste I\n".TF::WHITE."1 Minute"."\n".TF::AQUA."To mine a little faster...")
                ]);
                
    $inventory $player->getInventory();
                
    $potion->setCompoundTag($tag);
                
    $inventory->addItem($potion);
                return;
            }
        }

        public function 
    onInteract(PlayerInteractEvent $event) {
            
    $item $event->getItem();
            
    $held $event->getPlayer()->getInventory()->getItemInHand();
            
    $player $event->getPlayer();
            
    $block $event->getBlock();
            
    $slot $player->getInventory()->getHeldItemSlot();
            
    $potion Item::get(37312341);
            if(
    $potion->getName()  ===     TF::YELLOW."Potion Of Haste I\n".TF::WHITE."1 Minute"."\n".TF::AQUA."To mine a little faster...") {
                
    $haste Effect::getEffect(3)->setVisible(false)->setAmplifier(0);
                
    $haste->setDuration(1020);
                
    $player->addEffect($haste);
            }
            
    $player->sendMessage("asah dood");

            }

      public function 
    getPlayer($player){
            if(!
    $this->validateName($playerfalse)){
                return 
    false;
            }
            
    $player strtolower($player);
            
    $found false;
            foreach(
    $this->getServer()->getOnlinePlayers() as $p){
                if(
    strtolower(TF::clean($p->getDisplayName(), true)) === $player || strtolower($p->getName()) === $player){
                    
    $found $p;
                    break;
                }
            }
            
    // If cannot get the exact player name/nick, try with portions of it
            
    if(!$found){
                
    $found = ($f $this->getServer()->getPlayer($player)) === null false $f// PocketMine function to get from portions of name
            
    }
            
    /*
             * Copy from PocketMine's function (use above xD) but modified to work with Nicknames :P
             *
             * ALL THE RIGHTS FROM THE FOLLOWING CODE BELONGS TO POCKETMINE-MP
             */
            
    if(!$found){
                
    $delta = \PHP_INT_MAX;
                foreach(
    $this->getServer()->getOnlinePlayers() as $p){
                    
    // Clean the Display Name due to colored nicks :S
                    
    if(\stripos(($n TF::clean($p->getDisplayName(), true)), $player) === 0){
                        
    $curDelta = \strlen($n) - \strlen($player);
                        if(
    $curDelta $delta){
                            
    $found $p;
                            
    $delta $curDelta;
                        }
                        if(
    $curDelta === 0){
                            break;
                        }
                    }
                }
            }
            return 
    $found;
        }

        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            if (
    $sender instanceof Player) {
                
    $player $sender->getPlayer()->getName();
                if (empty(
    $args[0]) or empty($args[1]) or empty($args[2])) {
                        
    $sender->sendMessage("Please read the ReadME!");
                        return 
    true;
                }

                if(isset(
    $args[2]) && !($player $this->getPlayer($args[2]))){
                
    $sender->sendMessage(TF::RED "Player Not Online!");
                return 
    false;
                }

                if (
    $command->getName('cp')) {
                    if(
    $args[0] == "give") {
                        if(
    $args[1] == "haste1" or $args[1] == "drunk1") {//THIS IS JUST TO CHECK THE ARGS[1] AND MAKE SURE NOTHINGS WRONG  
                            
    if(isset($args[2]) && !($player $this->getPlayer($args[2]))){
                                
    $sender->sendMessage(TF::RED "Player Not Online!");
                                return 
    false;
                            }

                        }
                        switch(
    $args[1]) {
                            case 
    "haste1":
                                
    $player $this->getServer()->getPlayer($args[2]);
                                
    $this->givePotion($player"haste1");
                                return;
                        }
                    }
                }
            }
        }

      public function 
    translateColors($symbol$message) {
        
    $message str_replace($symbol."0"TF::BLACK$message);
        
    $message str_replace($symbol."1"TF::DARK_BLUE$message);
        
    $message str_replace($symbol."2"TF::DARK_GREEN$message);
        
    $message str_replace($symbol."3"TF::DARK_AQUA$message);
        
    $message str_replace($symbol."4"TF::DARK_RED$message);
        
    $message str_replace($symbol."5"TF::DARK_PURPLE$message);
        
    $message str_replace($symbol."6"TF::GOLD$message);
        
    $message str_replace($symbol."7"TF::GRAY$message);
        
    $message str_replace($symbol."8"TF::DARK_GRAY$message);
        
    $message str_replace($symbol."9"TF::BLUE$message);
        
    $message str_replace($symbol."a"TF::GREEN$message);
        
    $message str_replace($symbol."b"TF::AQUA$message);
        
    $message str_replace($symbol."c"TF::RED$message);
        
    $message str_replace($symbol."d"TF::LIGHT_PURPLE$message);
        
    $message str_replace($symbol."e"TF::YELLOW$message);
        
    $message str_replace($symbol."f"TF::WHITE$message);
        
    $message str_replace($symbol."k"TF::OBFUSCATED$message);
        
    $message str_replace($symbol."l"TF::BOLD$message);
        
    $message str_replace($symbol."m"TF::STRIKETHROUGH$message);
        
    $message str_replace($symbol."n"TF::UNDERLINE$message);
        
    $message str_replace($symbol."o"TF::ITALIC$message);
        
    $message str_replace($symbol."r"TF::RESET$message);
        return 
    $message;
      }


       
    }  
     
    Last edited: May 15, 2017
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can you rephrase that?
     
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    My bad, the part where it checks the potions name isnt working. no errors or anything
     
  4. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Have you added a default case to check if the switch statement is being called? Because if I understand correctly, you're saying it isn't called? Correct?
     
    corytortoise likes this.
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    its not the switch arguement its the on interact, the line where it checks the name isnt workng yet the name is right
     
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Maybe because the item you are getting the name of is an item you created without any extra data, therefore it will never have the specified custom name.
     
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    How do i fix this
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Did anyone notice that the amplifier is set to 0? the effect wont be recognised anyway unless the amplifier is 1 or more
     
  9. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    fixed, i just need help with the error at hand
     
  10. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Use an instance of an item that has the custom name. How you get it is up to you.
     
  11. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    SOFe, corytortoise and jasonwynn10 like this.
  12. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Already banned :p
     
  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.