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

Solved Exec command by clicking an item

Discussion in 'Development' started by AlexSima, Jun 29, 2020.

  1. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Problem: When i start the server there's no error but when i click a book, its crashing the game & giving me internal server error

    Error: [Server thread/CRITICAL]: Error: "Call to undefined method Alexander\PurpleCraftPlugin\Main: OpenServerForm()" (EXCEPTION) in "plugins/PurpleCraftPlugin/src/Alexander/PurpleCraftPlugin/Main" at line 114
    Line 114 -> $this->openServerForm ($player);
    PHP:
         public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool{
                   switch(
    $cmd->getName()){
                        case 
    "info" :
                             if (
    $sender instanceof Player){
                                 
    $this->openServerForm ($sender);
                             }
                        break;
                   }

         }


         public function 
    openMyCustomToggleForm(Player $player){
             
    $api $this->getServer()->getPluginManager()->getPlugin"FormAPI" );
         
             
    $form $api->createSimpleForm(function (Player $playerint $data null){
                if (
    $data === null){
                return 
    true;
                }
             
             });

             
    $form->setTitle("Title here");
             
    $form->setLabel("Label here");
         
             
    $form->sendToPlayer($player);
         
             return 
    $form;
         
         

         }


         public function 
    onInteract (PlayerInteractEvent $event){
                
    $player $event->getPlayer();
                
    $item $event->getItem();
                if(
    $item->getId() == 340){
                  if(!isset(
    $this->interactDelay[$player->getName()])){
                  
    $this->interactDelay[$player->getName()] = time() + 1;
                  
    $this->openServerForm ($player);
                  } else{
                       if(
    time() >=$this->interactDelay[$player->getName()]){
                          unset (
    $this->interactDelay[$player->getName()]);
                       }
                }

         }


    }
    }
     
    Last edited: Jun 29, 2020
  2. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    ur issue is that that the function u made is named openMyCustomToggleForm but your using a method called openServerForm
     
  3. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    and ur using old methods of youtube the new way to create forms is by using
    Code:
     $form = new \jojoe77777\FormAPI\SimpleForm(function (Player $sender, $data){
                if ($data === null) {
                    return true;
                }
                switch($data) {
                    case 0:
                        $this->getServer()->loadLevel("nodebuff");
                        $sender->teleport($this->getServer()->getLevelByName("nodebuff")->getSpawnLocation());
                        $sender->sendMessage(TextFormat::RED.$this->prefix." You Have Joined The FFA nodebuff Arena!");
                        return true;
                        break;
                    case 1:
                        $this->getServer()->loadLevel("gapple");
                        $sender->teleport($this->getServer()->getLevelByName("gapple")->getSpawnLocation());
                        $sender->sendMessage(TextFormat::YELLOW.$this->prefix." You Have Joined The FFA Gapple Arena!");
                        return true;
                        break;
                }
                return true;
            });
            $form->setTitle("FFA Arenas");
            $form->setContent("Please Select A Free for all arena!");
            $form->addButton(TextFormat::LIGHT_PURPLE."No-Debuff \nPlayers In This Arena: {$this->playersinnodebuff}");
            $form->addButton(TextFormat::YELLOW."Gapple \nPlayers In This Arena: {$this->playersingapple}");
            $form->sendToPlayer($player);
        }
    this code allows for easier making of simple forms simple only allows buttons while modal allows for other types of buttons and custom allows for use of all the $form->attributes like dropdown menus and also switches and others.
     
  4. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Ok thanks but what should i delete? I only want a title and some text inside (Im trying to create a menu for /info) !!! ----> THIS IS SOLVED, I FOUND OUT BY MYSELF!

    Also, how i can make the item to be forced to stay in a slot? Even if players try to change the slot of an item, the item will go back where it was, not in another slot and how to enchant the item that execute the menu?
     
    Last edited: Jun 29, 2020
  5. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    just delete the buttons and cases and for the force slot i would use this taken from my practice core
    PHP:
      public function onDrop(PlayerDropItemEvent $e)
        {
            
    $player $e->getPlayer();
            
    $name $player->getName();
            
    $player->sendMessage($this->prefix "Dropping Items Is Not Allowed In This Server");
            if (
    $player->isOp()) {
                
    $e->setCancelled(false);
            } else {
                
    $e->setCancelled(true);
            }
        }
    ,but to use this make sure your plugin has a public $variable; before any function and then make it equal something when ur plugin is enabled. This Code Only allows Operators to drop items.
    public $prefix;
    public function onEnable()
    {
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    $this->getLogger()->info(TextFormat::GREEN . "Practice Core Is Now Online");
    $this->prefix = TextFormat::AQUA . "[" . TextFormat::RED . "Practice Core" . TextFormat::AQUA . "] ";
    }
     
    AlexSima likes this.
  6. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Yes, but actually im looking for something that wont let players change the slot of an item, like the compass of NaviCompass plugin
     
  7. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    use this i just wrote it for my server core and used a few things for NaviCompass It Uses Lores To Identify The Items U Dont Want Moved So Just Put The Lore on the item to something consistent i have also put in a way for u to check for a second lore at the same time.
    CODE:
    PHP:
        public function onInvMove (InventoryTransactionEvent $e)
        {
            
    $transaction $e->getTransaction();
            foreach (
    $transaction->getActions() as $action) {
                
    $item $action->getSourceItem();
                
    $source $transaction->getSource();
                if (
    $source instanceof Player && $this->isSelectorItem($item)) {
                    if (
    $action instanceof SlotChangeAction || $action instanceof DropItemAction) {
                        
    $e->setCancelled();
                    }
                }
            }
        }
        private function 
    isSelectorItem(Item $item): bool
        
    {
                if (
    $item->getLore() == TextFormat::AQUA."Click!" || $item->getLore() == TextFormat::RED."Click!"){
                    return 
    true;
            }
            return 
    false;
        }
     
    Last edited: Jul 1, 2020
    AlexSima likes this.
  8. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    I tried it but nothing is happening, like the code does nothing..
    I changed $transaction = $e->getTransaction(); to
    $transaction = $e->getTransactions(); and when i try to change the slot of item, internal server error
     
  9. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    huh let me try and recode it give me like 30 mins
     
    AlexSima likes this.
  10. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    found the issue Lore is returned as a string[] so just put the $item->getlore() == [(TheLoreYourItemHas)] make sure its wrapped in brackets like i have put in the example
     
    AlexSima likes this.
  11. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Sorry but i didnt quite understand..
     
  12. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    use this instead for the code:
    PHP:
       public function onInvMove (InventoryTransactionEvent $e)
        {
            
    $transaction $e->getTransaction();
            foreach (
    $transaction->getActions() as $action) {
                
    $item $action->getSourceItem();
                
    $source $transaction->getSource();
                if (
    $source instanceof Player && $this->isSelectorItem($item)) {
                    if (
    $action instanceof SlotChangeAction || $action instanceof DropItemAction) {
                        
    $e->setCancelled();
                    }
                }
            }
        }
        private function 
    isSelectorItem(Item $item): bool
        
    {
                if (
    $item->getLore() == [TextFormat::AQUA."Click!"] || $item->getLore() == [TextFormat::RED."Click!"]){
                    return 
    true;
            }
            return 
    false;
        }
     
    Last edited: Jul 1, 2020
  13. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    Ok thanks but where exactly should i put the id of the item?
     
  14. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    id? just give the item u want to not be moved a lore using $item->setlore([(LORE YOU WANT)]) EXAMPLE:
    PHP:
    $item->setLore([TextFormat::RED."Click!"]);
     
  15. AlexSima

    AlexSima Spider Jockey

    Messages:
    38
    GitHub:
    AlexSima003
    I think now i understand what you're trying to say but..

    So, $item was undefined and i did this:

    PHP:
    $player->getInventory()->setItem(2Item::get(34001)->setCustomName("§5Info")); //This is just the code to give item onJoin
         
    $item $player->getInventory()->setItem(2Item::get(34001)->setCustomName("§5Info")); //This is what i did
         
    $item->setLore[("§5§l• §fClick for informations §5§l•")]; 
         }
    But now there's another error:

    [11:55:20] [Server thread/CRITICAL]: ErrorException: "Trying to get property 'setLore' of non-object" (EXCEPTION) in "plugins/PurpleCraftPlugin/src/Alexander/PurpleCraftPlugin/Main" at line 72
     
    Last edited: Jul 1, 2020
  16. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    ITs because ur using ids of the items for the item the best way to get items is to do this code:
    Code:
    $item = Item::get(Item::ITEMNAME);
    $item->setlore([LORE]);
    $pinv = $player->getinventory();
    $pinv->setItem(0,$item);
     
    Last edited: Jul 1, 2020
    AlexSima likes this.
  17. JviguyGamesYT

    JviguyGamesYT Baby Zombie

    Messages:
    113
    GitHub:
    jviguy
    here u go
    PHP:
        public function onInvMove (InventoryTransactionEvent $e)
        {
            
    $transaction $e->getTransaction();
            foreach (
    $transaction->getActions() as $action) {
                
    $item $action->getSourceItem();
                
    $source $transaction->getSource();
                if (
    $source instanceof Player && $this->isSelectorItem($item)) {
                    if (
    $action instanceof SlotChangeAction || $action instanceof DropItemAction) {
                        
    $world $source->getLevel()->getName();
                        if (
    $world == 'spawn') {
                            
    $e->setCancelled(true);
                            
    $source->sendMessage($this->prefix "Moving Items Is Not Allowed In This Server");
                        }
                    }
                }
            }
        }
        private function 
    isSelectorItem(Item $item): bool
        
    {
                if (
    $item->getLore() == [TextFormat::AQUA."Click!"] || $item->getLore() == [TextFormat::RED."Click!"]) {
                    return 
    true;
            }
                return 
    false;
        }
     
    AlexSima 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.