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

Solved Whats wrong with this?

Discussion in 'Development' started by artulloss, Mar 14, 2018.

  1. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    I can't figure out what's wrong with this

    public function onDamage(EntityDamageEvent $event){
    $player = $event->getPlayer();
    $entity = $event->getEntity();
    $world = $player->getLevel();
    $kblow = $config->get("kb-low");
    $kbhigh = $config->get("kb-high");
    $kbdefault = $config->get("kb-default");
    $config = $this->getConfig();
    if ($event instanceof EntityDamageByEntityEvent) {
    if($config->get("particles") === true) {
    $particle = new CriticalParticle($entity);
    $entity->getLevel()->addParticle($particle);
    }
    switch($world->getName()) {
    case $config->get($config->get("world-low")):
    $event->setKnockback($kblow);
    break;
    case $config->get($config->get("world-high")):
    $event->setKnockback($kbhigh);
    break;
    default:
    $event->setKnockback($kbdefault);
    break;
    }

    The log says
    [18:40:38] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\entity\EntityDamageByEntityEvent' to 'KBEdit v1': Call to undefined method pocketmine\event\entity\EntityDamageByEntityEvent::getPlayer() on KBEdit\Main
    [18:40:38] [Server thread/CRITICAL]: Error: "Call to undefined method pocketmine\event\entity\EntityDamageByEntityEvent::getPlayer()" (EXCEPTION) in "KnockBack-master/src/KBEdit/Main" at line 32
     
    Last edited: Mar 15, 2018
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Let me give you an example. Here's a class I've created.
    PHP:
    class A{
        public function 
    greet() : string{
            return 
    "Hello, Earthling!";
        }
    }
    Now, what do you expect will happen if I do this?
    PHP:
    $a = new A();
    echo 
    $a->getPlayer();
     
    artulloss and Daniel Pereira like this.
  3. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    I got it so it doesn't have errors anymore however the knock back that I'm trying to change doesn't change at all, can I send it later and can you tell me how to fix it?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Create a new thread for it, I'll try helping.
     
  5. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    $config is not defined
     
  6. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
  7. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    Yea that's the error I kept having once I fixed it up, now it doesn't work and doesn't have errors, I'll show it later idk whats wrong
     
  8. korado531m7

    korado531m7 Slime

    Messages:
    77
    GitHub:
    korado531m7
    Code:
    public function onDamage(EntityDamageEvent $event){
        $entity = $event->getEntity();
        if(!$entity instanceof Player) return true;
        $world = $entity->getLevel();
        $config = $this->getConfig();
        $kblow = $config->get("kb-low");
        $kbhigh = $config->get("kb-high");
        $kbdefault = $config->get("kb-default");
        if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
            if($config->get("particles") === true) {
                $pos = new Vector3($entity->x,$entity->y,$entity->z);
                $particle = new CriticalParticle($pos);
                $count = 20;
                for($i = 0;$i < $count; ++$i){
                    $entity->level->addParticle($particle);
                }
            }
            switch($world->getName()) {
                case $config->get($config->get("world-low")):
                    $event->setKnockback($kblow);
                    break;
                case $config->get($config->get("world-high")):
                    $event->setKnockback($kbhigh);
                    break;
                default:
                    $event->setKnockback($kbdefault);
                    break;
            }
        }
    }
    
    Did you add use 'EntityDamageByEntityEvent' ?
     
    artulloss likes this.
  9. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    Thanks, I'm horrible at writing code :( xD
     
    Last edited: Mar 16, 2018
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Everyone were. It takes time to learn a language. I still do not know some basics.
     
    artulloss likes this.
  11. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Mark this as solved and @dktapps move that to help
     
  12. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    It should work. Do you get any error when you hit someone?
     
  13. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    There are 0 error messages, nothing seems to happens at all though.
     
  14. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Please use php tag to read your code better. ;):p
     
    artulloss likes this.
  15. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    I was trying to, I looked up the HTML tag tag for adding code and that didn't work so I removed it
     
    Last edited: Mar 16, 2018
  16. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    please just do it for them rather then saying that, it's as easy as how you would tell them to add it, let me show you!!!
    PHP:
    <?php
    namespace KBEdit;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\level\particle\CriticalParticle;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{

    public 
    $config;

    public function 
    onEnable(){
    @
    mkdir($this->getDataFolder());
    if(!(
    file_exists($this->getDataFolder(). "/config.yml"))){
    $this->$config = new Config($this->getDataFolder()."/config.yml"Config::YAML, array(
    "world-low" => "PotPvP",
    "world-high" => "Diamond",
    "particles" => "true",
    "particle-count" => "20",
    "kb-high" => ".43",
    "kb-default" => ".4",
    "kb-low" => ".37"));
    $this->saveResource("/config.yml");
    }
    }
    public function 
    onDamage(EntityDamageEvent $event){
    $entity $event->getEntity();
    if(!
    $entity instanceof Player) return true;
    $world $entity->getLevel();
    $config $this->getConfig();
    $numpart $config->get("particle-count");
    $kblow $config->get("kb-low");
    $kbhigh $config->get("kb-high");
    $kbdefault $config->get("kb-default");
    if(
    $event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
    if(
    $config->get("particles") === true) {
    $pos = new Vector3($entity->x,$entity->y,$entity->z);
    $particle = new CriticalParticle($pos);
    $count = ($numpart);
    for(
    $i 0;$i $count; ++$i){
    $entity->level->addParticle($particle);
    }
    }
    switch(
    $world->getName()) {
    case 
    $config->get($config->get("world-low")):
    $event->setKnockback($kblow);
    break;
    case 
    $config->get($config->get("world-high")):
    $event->setKnockback($kbhigh);
    break;
    default:
    $event->setKnockback($kbdefault);
    break;
    }
    }
    }
    }
    ?>
     
    artulloss likes this.
  17. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Don’t u have to register Listener
     
    artulloss likes this.
  18. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    use pocketmine\event\entity\EntityDamageEvent;
    public function onDamage(EntityDamageEvent $event){
    if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {

    Change the first line to: use pocketmine\event\entity\EntityDamageByEntityEvent;
    And the second to: public function onDamage(EntityDamageByEntityEvent $event){
     
    artulloss likes this.
  19. artulloss

    artulloss Witch

    Messages:
    63
    GitHub:
    artulloss
    Solved this via paid commission rip :( xD
    Idk why I didn't get notified of the messages, closing this thanks for the help <3
     
  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.