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

Disable commands at spawn area

Comments in 'Plugin Development' started by iJoshuaHD, Apr 1, 2014.

  1. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Im not a good plugin maker because of lack php skills regarding pocketmine, can i ask for help where i can disable certain command at spawn area? thank you.

    here is the code so far basing at wiess peaceful spawn.
    PHP:
                case "command":
                    
    /////disable spawn command////
                    
    if (!isset($issuer->iusername)) return false;
                    
    $c $this->api->entity->get($issuer->iusername);
                    if (!isset(
    $c)) return false;
                    
    $t = new Vector3($c->x$c->y$c->z);
                    
    $s = new Vector3($this->server->spawn->x$this->server->spawn->y$this->server->spawn->z);
                    
    $distance $t->distance($s);
                    if (
    $distance <= $this->server->api->getProperty("spawn-protection")) {
                    
                    return (
    "You cant use this command at spawn.");
                    }
                    else
                    {
    bla blah blahhh blahhahah
                     
    }
                     break;
                    
    ///////////////////////////////
    thanks :)
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    API 12:
    PHP:
    public function init(){
        
    ServerAPI::request()->addHandler("console.command", array($this"onCmd"));
    }
    public function 
    onCmd($c$a$p){
        if(!(
    $p instanceof Player))return;
        if(
    $this->isAtSpawn($p->entity))return false;
    }
    public function 
    isAtSpawn(Position $pos){
        
    // your definition of spawn: $pos->x, $pos->y, $pos->z, $pos->level->getName() ...
    }
    New API:
    PHP:
    <?php

    namespace PEMapModder\NoCmdAtSpawn;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\level\Position as Pos;

    class 
    MainPlugin extends PluginBase implements Listener{
        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
    /**
         * @param PlayerCommandPreproccessEvent $event
         * @priority NORMAL
         * @ignoreCancelled true
        */
        
    public function onCommand($event){
            if(
    $this->isAtSpawn($event->getPlayer()->entity))
                
    $event->setCancelled(true);
        }
        public function 
    isAtSpawn(Pos $pos){
            
    // your method ($pos->x, $pos->y, $pos->z, $pos->level->getName())
        
    }
    }
    Last edited: Apr 1, 2014
    iJoshuaHD likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Please anyone who knows the API help me check the above. I am not very sure about the event thing. (I am terrible at RegExp)
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    can u pls explain this a little further? thanks :)
    Code:
    public function isAtSpawn(Position $pos){
        // your definition of spawn: $pos->x, $pos->y, $pos->z, $pos->level->getName() ...
    }
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Any functions as the definition of "at spawn area":
    e.g.
    PHP:
    public function isAtSpawn(pocketmine\level\Position $pos){
        return 
    $pos->level->getSafeSpawn()->distance($pos)<=10;
    }
    iJoshuaHD likes this.
  6. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    ill try the codes now. thank u :)
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Oops forgot to close block comment :oops:
  8. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    what you mean? i get errors with it....

    Error: array (
    'type' => 'E_RECOVERABLE_ERROR',
    'message' => 'Argument 1 passed to pluginPVP::isAtSpawn() must be an instance of Position, null given, called in ..,blahblahblah...
  9. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    i made it work now but how to fix this:

    A E_WARNING error happened: "strtolower() expects parameter 1 to be string, array given ....
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    idk think of your own solution.
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    I made the code work :O thanks :D
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Too small of a plugin to be released
    iJoshuaHD likes this.
  13. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    pemapmodder already placed the code. use your guts how will u put it in ur plugin xD
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    sorry, im not ur slave or nothing but here is the code by pemapmodder

    PEMapModder likes this.
  15. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    Here is it:
    PHP:
    <?php

    namespace PEMapModder\NoCmdAtSpawn;

    use 
    pocketmine\level\Position;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;

    class 
    MainPlugin extends PluginBase implements Listener{

        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
    /**
         * @param PlayerCommandPreproccessEvent $event
         * @priority NORMAL
         * @ignoreCancelled true
         */
        
    public function onPlayerPreCommand(PlayerCommandPreprocessEvent $event){
            if(
    $this->isAtSpawn($event->getPlayer()->entity))
                
    $event->setCancelled(true);
        }
        public function 
    isAtSpawn(Position $pos)
        {
            
    $spawn $this->getServer()->getDefaultLevel()->getSpawn();
            
    $radius $this->getServer()->getSpawnRadius();

            if(
    $spawn-><= $pos->&& $pos->x  <= $spawn->$radius)
            {
                if(
    $spawn-><= $pos->&& $pos-><= $spawn->$radius)
                {
                    if(
    $spawn-><= $pos->&& $pos-><= $spawn->$radius)
                    {
                        if(
    $spawn->level === $pos->level)
                            return 
    true;
                    }
                }
            }
            return 
    false;
        }
    }
    iJoshuaHD and PEMapModder like this.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Thanks!

    I like to use the text logical operator ("and" and "or") though. Sounds more formal :D
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why not
    PHP:
    return $this->getServer()->getDefaultLevel()->getSpawn()->distance($pos) <= $this->getServer()->getSpawnRadius();
    iJoshuaHD likes this.
  18. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    Yes, that should work
  19. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    tried this:
    and this:
    doesnt work.
  20. Topic
    Offline

    Topic New Member Plugin Developer

    Joined:
    Aug 29, 2013
    Posts:
    47
    Plugins:
    3
    Minecraft User:
    topic
    Try this function:
    PHP:
    public function checkInside($x$z){

            
    $radius 10//Count blocks from spawn

            
    $spawn $this->getServer()->getDefaultLevel()->getSpawn();
            
    $t = new Vector2($x$z);
            
    $s = new Vector2($spawn->x$spawn->z);
            if(
    $t->distance($s) =< $radius){
                return 
    true;
            }else{
                return 
    false;
            }
        }
    And in command:
    PHP:
    case "command":
                  if(
    $this->checkInside($sender->getX(), $sender->getZ())){
                            
    $sender->sendMessage("You cant use this command at spawn.");
                  }
    break;
    iJoshuaHD likes this.

Share This Page

Advertisement