Can someone please help me out. I need help with a level up system like when you get 5 kills you level up to level 2 and to level up to level 3 you need 10 kills.
This is the plugin development section, if you want someone to code a plugin for you, please use plugin requests
public function PlayerDeath(PlayerDeathEvent $event){ $cause = $event->getEntity(); $name = $cause->getName(); $this->death[$name] = $this->death[$name]+1; //Add Death if($cause instanceof EntityDamageByEntityEvent){ if($killers instanceof Player){ $killers = $cause->getDamager(); $this->kill[$killers->getName] = $this->kill[$killers->getName]+1; //Add Kill } }
Use [PHP ] Code [/PHP ] And you doest not must remove post if is anything bad you can use edit button and
PHP: public function PlayerDeath(PlayerDeathEvent $event){ $cause = $event->getEntity(); $name = $cause->getName(); $this->death[$name] = $this->death[$name]+1; //Add Death if($cause instanceof EntityDamageByEntityEvent){ if($killers instanceof Player){ $killers = $cause->getDamager(); $this->kill[$killers->getName] = $this->kill[$killers->getName]+1; //Add Kill $this->config->save(); } }
It looks like $this->death and $this->kills is an array but you have $this->config->save()??? Do you want to save kills and death to a config?
Oops PHP: public function PlayerDeath(PlayerDeathEvent $event){ $cause = $event->getEntity(); $name = $cause->getName(); $this->death[$name] = $this->death[$name]+1; //Add Death $this->deaths->save(); } if($cause instanceof EntityDamageByEntityEvent){ if($killers instanceof Player){ $killers = $cause->getDamager(); $this->kill[$killers->getName] = $this->kill[$killers->getName]+1; //Add Kill $this->kills->save(); } }
You have quite a few issues here. I'm going to dissect your code and point out the issues so you can learn what is wrong and how to prevent it in the future. Let's start with this: PHP: $cause = $event->getEntity();//Later in your code... if($cause instanceof EntityDamageByEntityEvent){ This causes issues because you got the player that died, and here you are checking if that Player is an EntityDamageByEntityEvent. Obviously, this doesn't work because the Player is not an Event. You need to get the last damage given to the player before they died. You can do that with this: PHP: $cause = $deathevent->getPlayer()->getLastDamageCause(); if($cause instanceof EntityDamageByEntityEvent) { $killer = $cause->getDamager(); } You also used this: PHP: if($killers instanceof Player){ $killers = $cause->getDamager(); The issue here is that you are checking if $killers is an instance of Player, but you don't define what $killers is before you do this check. Before using a variable, ALWAYS define it in some way. One thing you did was use +1 to add one to the current value of the kills or deaths for a player. While this is not invalid and won't likely cause errors, you can use $value++ and it will automatically add one to that value. This is debatably simpler. Unless you are storing in a file, $this->kills->save() is just wrong. You can't "save" an array. It saves automatically, and stays until reset or overwritten. I assume you want to save the kills and deaths of each player in a file, right? In that case, check out this post by SOFe that explains managing data. If you are still confused, or you just don't want to bother with this, I would actually enjoy writing a small plugin like this, if needed.