Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MagicTorch ToyBox

Comments in 'Plugin Development' started by KingDeadKnight, Dec 30, 2015.

  1. KingDeadKnight
    Offline

    KingDeadKnight New Member

    Joined:
    Oct 18, 2015
    Posts:
    33
    Minecraft User:
    KingDeadKnight
    I use ToyBox on my server for the magic torch that it follow us if we touch a torch with a torch in our hand, but when we take another item in our hand the magic torch disappear. What can I do to resolve that in the source code?

    PHP:
    <?php
    namespace aliuly\toybox;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Server;
    use 
    pocketmine\network\protocol\UpdateBlockPacket;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    aliuly\toybox\common\mc;
    class 
    TorchMgr implements Listener {
        public 
    $owner;
        protected 
    $item;
        protected 
    $block;
        private function 
    spawn(Position $p,$id,$meta) {
            
    $bl $p->getLevel()->getBlock($p);
            if (
    version_compare($this->owner->getServer()->getApiVersion(),"1.12.0") >= 0) {
                
    $p->getLevel()->sendBlocks($p->getLevel()->getChunkPlayers($p->getX()>>4,$p->getZ()>>4),
                                [
    Block::get($id,$meta,$p)],
                                
    UpdateBlockPacket::FLAG_ALL_PRIORITY);
            } else {
                
    $pk = new UpdateBlockPacket();
                
    $pk->$p->x;
                
    $pk->$p->y;
                
    $pk->$p->z;
                
    $pk->block $id;
                
    $pk->meta $meta;
                
    Server::broadcastPacket($p->getLevel()->getUsingChunk($p->>> 4,
                                                                                        
    $p->>> 4), $pk);
            }
            return [
    $bl->getId(),$bl->getDamage()];
        }
        private function 
    spawnTorch(Player $pl) {
            
    $state $this->owner->getState("Torch",$pl,null);
            if(
    $state){
                list(
    $pos,$id,$meta) = $state;
                
    $this->spawn($pos,$id,$meta); // Restore block
            
    }
            
    $pos = new Position(floor($pl->getX()),floor($pl->getY()+1),
                                      
    floor($pl->getZ()),$pl->getLevel());
            list(
    $id,$meta) = $this->spawn($pos,$this->block,0);
            
    $this->owner->setState("Torch",$pl,[$pos,$id,$meta]);
        }
        private function 
    deSpawnTorch(Player $pl) {
            
    $state $this->owner->getState("Torch",$pl,null);
            if(
    $state){
                list(
    $pos,$id,$meta) = $state;
                
    $this->spawn($pos,$id,$meta); // Restore block
            
    }
            
    $this->owner->unsetState("Torch",$pl);
        }
        public function 
    __construct($plugin,$cfg) {
            
    $this->owner $plugin;
            
    $this->owner->getServer()->getPluginManager()->registerEvents($this$this->owner);
            
    $this->item $this->owner->getItem($cfg["item"],Item::TORCH,"torch-item")->getId();
            
    $this->block $this->owner->getItem($cfg["block"],Item::TORCH,"torch-block")->getId();
        }
        public function 
    onItemHeld(PlayerItemHeldEvent $e) {
            
    //echo __METHOD__.",".__LINE__."\n";//##DEBUG
            
    $pl $e->getPlayer();
            if(!
    $this->owner->getState("Torch",$pl,null)) return;
            if (
    $e->getItem()->getId() != $this->item) {
                
    $this->deSpawnTorch($pl);
            }
        }
        
    /**
         * @priority HIGHEST
         */
        
    public function onMove(PlayerMoveEvent $e) {
            if (
    $e->isCancelled()) return;
            
    $pl $e->getPlayer();
            if(!
    $this->owner->getState("Torch",$pl,null)) return;
            
    $this->spawnTorch($pl);
        }
        public function 
    onInteract(PlayerInteractEvent $e) {
            
    // Activate torch
            
    $pl $e->getPlayer();
            if (!
    $pl->hasPermission("toybox.torch")) return;
            
    $bl $e->getBlock();
            if (
    $bl->isSolid()) return;
            
    $hand $pl->getInventory()->getItemInHand();
            if (
    $hand->getID() != $this->item) return;
            
    $state $this->owner->getState("Torch",$pl,null);
            if (
    $state) {
                
    $this->deSpawnTorch($pl);
                
    $pl->sendMessage(mc::_("Torch de-activated"));
            } else {
                
    $this->spawnTorch($pl);
                
    $pl->sendMessage(mc::_("Torch activated"));
            }
        }
        public function 
    onJoin(PlayerJoinEvent $e) {
            
    $pl $e->getPlayer();
            foreach(
    $this->owner->getServer()->getOnlinePlayers() as $online){
                if(!
    $this->owner->getState("Torch",$online,null)) continue;
                
    $this->spawnTorch($online);
            }
        }
    }
  2. iDirtPlayzMC
    Offline

    iDirtPlayzMC Active Member

    Joined:
    Apr 10, 2015
    Posts:
    264
    Minecraft User:
    iZenith_xPr
    :l what are you trying to do ? take a code out of that? its impossible lol, use aliuly\toybox\common\mc; that folder is not there, no point :l
  3. KingDeadKnight
    Offline

    KingDeadKnight New Member

    Joined:
    Oct 18, 2015
    Posts:
    33
    Minecraft User:
    KingDeadKnight
    ToyBox.phar works, so I'll decompile it after someone helps me
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    What are you trying to do? Prevent the "torch" from disappearing even if you are not holding a torch?
  5. KingDeadKnight
    Offline

    KingDeadKnight New Member

    Joined:
    Oct 18, 2015
    Posts:
    33
    Minecraft User:
    KingDeadKnight
    The MagicTorch, after activation, mustn't disappear if we have, for example, a pickaxe in our hand.

    Sorry for bad English.
  6. Yosshi999
    Offline

    Yosshi999 New Member Plugin Developer

    Joined:
    Mar 13, 2014
    Posts:
    29
    Plugins:
    1
    Minecraft User:
    Yosshi999
    delete "$this->deSpawnTorch($pl);" in function onItemHeld().
  7. KingDeadKnight
    Offline

    KingDeadKnight New Member

    Joined:
    Oct 18, 2015
    Posts:
    33
    Minecraft User:
    KingDeadKnight
    Thanks, it works

Share This Page

Advertisement