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

Detecting Entity around an entity?

Comments in 'Plugin Development' started by JonsMC, Aug 27, 2016.

  1. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    EntityMoveEvent is deprecated. This means that EntityMoveEvent will not be called at all.
    EntityMoveEvent was originally just an event that fires an event for all entities except entities that are stationary. It's pretty useless.
    Just use a Task and iterate through all entities in all levels. That's the same as EntityMoveEvent, except stationary entities are detected as well. Using an efficient sort algorithm, this won't bother you at all.
    For each level, prevent self-comparison and duplicated comparison using for($i = 0; $i < $max; $i++){ for($j = $i + 1; $j < $max; $j++){ compare($i, $j); } } on all entities.
  2. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    How can a loop check entity? :/
  3. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Iterate over $level->getEntities()...

    PHP:
    $entities $level->getEntities();
    for(
    $i 0$i count($entities); $i++){
      for(
    $j $i 1$j count($entities); $j++){
        
    $entity1 $entities[$i];
        
    $entity2 $entities[$j];
        if(
    $entity1->distanceSquared($entity2) <= 25){ // use distanceSquared for better performance
          // ...
        
    }
      }
    }
    Nawaf_Craft1b likes this.
  4. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Trying to call it but failed :(, nothing happend
    PHP:
    public function createStack(Level $level) {
            
    $entities $level->getEntities();
           
            for(
    $i 0$i count($entities); $i++) {
              for(
    $j $i+1$j count($entities); $j++) {
                 
    $e1 $entities[$i];
                 
    $e2 $entities[$j];
                 
                if(!(
    $e1 instanceof Player) && !($e2 instanceof Player)) {
               
                 if(
    $e1->distanceSquared($e2) <= 5) {
                   
                    if(
    $this->isStacked($e1) && !($this->isStacked($e2))) {
                      
    $e2->kill();
                     
                      
    $this->addToStack($e1);
                      } elseif(!(
    $this->isStacked($e1)) && !($this->isStacked($e2))) {
                        
    $this->stacked[$e1->getId()] = 1;
                       
                        
    $e2->kill();
                       
                       
                       }
                       
                      }
                     
                    }
                   }
                  }
                 }
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Vector3::distanceSquared() returns distance ** 2, isn't that obvious?
    PHP:
    if($e1->distanceSquared($e2) <= 25) {
    Also, it's good to learn some basic debugging techniques, for example
    PHP:
    echo "Stacking...\n";
    echo 
    "Measuring distance between {$e1->getName()} and {$e2->getName()}";
    # ...
    There is so many problems you can solve yourself by debugging. Use functions like var_dump, printf, print_r, echo and Exception::getTraceAsString().
    MyNameIsTriXz likes this.
  6. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Should i put that in a task? And what task+When run? Im not really good at task :p
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Your own class extending PluginTask but don't think you can use Async worker for this because you do things on main thread. Got to warn you, expect lag.

    Here is simple example of tasks
    PHP:
    namespace example;

    class 
    ExampleTask extends PluginTask {
      
       public function 
    __construct(Plugin $plugin) { // Idk why I put this here :D
           
    parent::__construct($plugin);
       }

        public function 
    onRun($currentTick) {
            
    # Code to run repeatedly
        
    }
    }
    This is the way how you can schedule it
    PHP:
    // ... Context of PluginBase
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new ExampleTask($this), 20); // Interval in ticks. (second / 20)
    Last edited: Sep 3, 2016
  8. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Ooo, i didn't know task could handle those thing :p
  9. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    $e1 returned null not an Entity object, so i cannot use distanceSquared :(

Share This Page

Advertisement