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

I get an error with a plugin

Discussion in 'Facepalm' started by LoveYouAll, Jun 17, 2017.

  1. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    How to teleport to an other world? I used this code but it did not work:
    PHP:
        public function onInteract(PlayerInteractEvent $this$event){
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $item $player->getInventory()->getItemInHand();
            
    $itemid $item->getID();
            
    $block $event->getBlock();
            
    $level $this->owner->getServer()->getLevelByName("$lvname");


            if(
    $item->getName() == C::BOLD.C::GREEN."Teleporter"){
                
    $this->TeleportItems($player);
            }

            elseif (
    $item->getName() == C::BOLD C::GREEN "KitPvP"){
                
    $this->MainItems($player);
                
    $lvname KitPvP;
                
    $player->teleport($level->getSafeSpawn());
           }
    I get this error:
    Code:
    [11:38:01] [CRITICAL]: ParseError: "syntax error, unexpected '$this' (T_VARIABLE)" (EXCEPTION) in "/plugins/Core_v0.0.1.phar/src/Core/Main" at line 123
    Line 123:
    PHP:
            $level $this->owner->getServer()->getLevelByName("$lvname");
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    PHP:
    public function onInteract(PlayerInteractEvent $event){
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $item $player->getInventory()->getItemInHand();
            
    $itemid $item->getID();
            
    $block $event->getBlock();
            
    $level $this->owner->getServer()->getLevelByName("$lvname"); //what is $levname? Have you store any value?
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    PlayerInteractEvent $this, $event???
    Please learn a bit of PHP. It should be:
    PHP:
    public function onInteract(PlayerInteractEvent $event) { }
    $this is a reserved variable in PHP, and can not be re-assigned, because it refers to the current object.
     
  4. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
  5. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    Same result. And yes I have stored:
    PHP:
            elseif ($item->getName() == C::BOLD C::GREEN "KitPvP"){
                
    $this->MainItems($player);
                
    $lvname KitPvP;
                
    $player->teleport($level->getSafeSpawn());
           }
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Show your code now please.
     
  7. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    PHP:
    <?php

    namespace Core;

    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\level\particle\FlameParticle;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\math\Vector2;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\nbt\tag\IntTag;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\utils\TextFormat as C;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\player\PlayerDropItemEvent;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\entity\EntityTeleportEvent;

    class 
    Main extends PluginBase implements Listener {
        
        public 
    $prefix TextFormat::GRAY."[".TextFormat::AQUA."Lobby".TextFormat::GRAY."] ";

        public function 
    onEnable(){
            
    $this->getLogger()->info($this->prefix.C::GREEN."Enabled!");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->ZMusicBox $this->getServer()->getPluginManager()->getPlugin("ZMusicBox");
        }

        public function 
    MainItems(Player $player){
            
    $player->getInventory()->clearAll();
            
    $player->getInventory()->setItem(0Item::get(345)->setCustomName(C::BOLD.C::GREEN."Teleporter"));
            
    $player->getInventory()->setItem(2Item::get(339)->setCustomName(C::BOLD.C::GOLD."Info"));
            
    $player->getInventory()->setItem(6Item::get(280)->setCustomName("§l§eHide §aPlayers"));
            
    $player->getInventory()->setItem(8Item::get(360)->setCustomName(C::BOLD.C::GREEN."Next Song"));
            
    $player->removeAllEffects();
            
    $player->getPlayer()->setHealth(20);
                
    $player->getPlayer()->setFood(20);

        }
        
            public function 
    killm(PlayerDeathEvent $event)
            {
                
    $event->setDeathMessage("");
            }

            

            public function 
    TeleportItems(Player $player){          //Teleport
            
    $player->getInventory()->clearAll();
            
    $player->getInventory()->setItem(0Item::get(399)->setCustomName(C::BOLD.C::BLUE."SkyWars"));
            
    $player->getInventory()->setItem(8Item::get(355)->setCustomName(C::BOLD.C::RED."Back"));
            
    $player->getInventory()->setItem(4Item::get(378)->setCustomName(C::BOLD.C::RED."Plots"));
            
    $player->getInventory()->setItem(2Item::get(381)->setCustomName(C::BOLD.C::GREEN."KitPvP"));
            
    $player->removeAllEffects();
            
    $player->getPlayer()->setHealth(20);
            
    $player->getPlayer()->setFood(20);
        }


        public function 
    onJoin(PlayerJoinEvent $event){         //OnJoin
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $ds $this->getServer()->getDefaultLevel()->getSafeSpawn();
            
    $player->setGamemode(0);
            
    $player->teleport($ds);
            
    $event->setJoinMessage("");
            
    $this->MainItems($player);
            
    $player->setGamemode(0);
            if(
    $player->isOP()){
                
    $event->setJoinMessage(C::RED.$name.C::AQUA." joined the game");
            }
        }
        
        public function 
    onQuit(PlayerQuitEvent $event){
            
    $player $event->getPlayer();
            
    $event->setQuitMessage("");
        }

        public function 
    onDeath(PlayerDeathEvent $event){         //OnDeath
            
    $player $event->getPlayer();
            
    $this->MainItems($player);
            
    $player->setGamemode(0);
            }

        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
            
    $name $sender->getName();
            switch (
    $cmd->getName()){
                case 
    "info":
                if(!empty(
    $args[0])){
                    if(
    $args[0] == "ranks"){
                        
    $sender->sendMessage($this->prefix "§aYou can buy a rank in our server website");
                        return 
    true;
                    }
                    if(
    $args[0] == "server"){
                        
    $sender->sendMessage($this->prefix "§aWelcome to our server!  Our server has the following MiniGames: SkyWars, KitPvP, Survival, Plots and 1vs1!");
                        return 
    true;
                }else{
                    
    $sender->sendMessage($this->prefix"§a/info ranks|server");
                    return 
    true;
                }
            }
        }
    }   
        public function 
    onInteract(PlayerInteractEvent $event){
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $item $player->getInventory()->getItemInHand();
            
    $itemid $item->getID();
            
    $block $event->getBlock();
            
    $level $this->owner->getServer()->getLevelByName("$lvname");


            if(
    $item->getName() == C::BOLD.C::GREEN."Teleporter"){
                
    $this->TeleportItems($player);
            }

            elseif (
    $item->getName() == C::BOLD C::GOLD "Info"){
               
    $player->sendMessage($this->prefix "§aUsage: /info ranks|server");
           }

            elseif (
    $item->getName() == C::BOLD C::RED "Back"){
                
    $this->MainItems($player);
           }


            elseif (
    $item->getName() == C::BOLD C::GREEN "KitPvP"){
                
    $this->MainItems($player);
                
    $lvname KitPvP;
                
    $player->teleport($level->getSafeSpawn());
           }

            elseif (
    $item->getName() == C::BOLD.C::RED."Plots"){
           
    $this->MainItems($player);
                
    $lvname Plots;
                
    $player->teleport($level->getSafeSpawn());
           }
            
            elseif (
    $item->getName() == C::BOLD.C::BLUE."SkyWars"){
           
    $this->MainItems($player);
                
    $lvname SkyWars;
                
    $player->teleport($level->getSafeSpawn());
           }

            elseif (
    $item->getName() == "§l§eHide §aPlayers") {
                
    $player->getInventory()->remove(Item::get(280)->setCustomName("§l§eHide §aPlayers"));
                
    $player->getInventory()->setItem(6Item::get(369)->setCustomName("§l§eShow §aPlayers"));
                
    $player->sendMessage($this->prefix "§cDisabled Player Visibility!");
                
    $this->hideall[] = $player;
                foreach (
    $this->getServer()->getOnlinePlayers() as $p2) {
                    
    $player->hideplayer($p2);
                }
           }

            elseif (
    $item->getName() == "§l§eShow §aPlayers"){
                
    $player->getInventory()->remove(Item::get(369)->setCustomName("§l§eShow §aPlayers"));
                
    $player->getInventory()->setItem(6Item::get(280)->setCustomName("§l§eHide §aPlayers"));
                
    $player->sendMessage($this->prefix "§aEnabled Player Visibility!");
                unset(
    $this->hideall[array_search($player$this->hideall)]);
                foreach (
    $this->getServer()->getOnlinePlayers() as $p2) {
                    
    $player->showplayer($p2);
                }
           }

            elseif (
    $item->getName() == C::BOLD.C::GREEN."Next Song"){
                               
    $this->ZMusicBox->StartNewTask();

           }
        }
        
        public function 
    onItemHeld(PlayerItemHeldEvent $event){
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $item $player->getInventory()->getItemInHand()->getID();
            switch(
    $item){
                case 
    10:
                
    $player->getInventory()->setItemInHand(Item::get(Item::AIR00));
                
    $player->sendMessage($this->prefix.TextFormat::RED." You cannot use lava");
                return 
    true;
                case 
    11:
                
    $player->getInventory()->setItemInHand(Item::get(Item::AIR00));
                
    $player->sendMessage($this->prefix.TextFormat::RED." You cannot use lava");
                return 
    true;
            }
        }
    }
     
  8. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    Who moved it to Facepalm?
     
  9. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    $lvname is clearly undefined. What should it be?
     
  10. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    It should be the level name I want to tp.
     
  11. LoveYouAll

    LoveYouAll Shog Chips

    Messages:
    0
    My problem is not $lvname yet. My problem is $this in line 123:
    $level = $this->owner->getServer()->getLevelByName("$lvname");
     
  12. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    You defined $lvname below that line
    All code run line by line
     
  13. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    So that's why this was moved to facepalm. :facepalm::facepalm::facepalm:
    Can't help you much further if you don't at least know the basic syntax of PHP.
     
  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.