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

Kills and items. Name

Comments in 'Plugin Development' started by KHAV, Aug 23, 2016.

  1. KHAV
    Offline

    KHAV New Member

    Joined:
    Aug 7, 2016
    Posts:
    35
    Minecraft User:
    KHAV
    Hello

    How i can give player item name

    Same i want give iron sword to player but i want to make the name : Hi

    And i want how you when kill 5 give you thing :)
  2. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    PHP:
    $player->addItem();
    //$player is an instance of Player
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $item Item::get(Item::IRON_SWORD01);
    $item->setCustomName("Name");
    $player->addItem($item);
    While $player is your player. About the 5 kills:
    Just save them in an array or so, +1 whenever the player kills someone, and if it is 5, give the item to the player.
  4. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    PHP:
    <?php

    namespace GiveItemOnDeath;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\Player;

    private 
    $kills;

             public function 
    onDeath(PlayerDeathEvent $ev) {
                
    $cus $ev->getEntity()->getLastDamageCause();
            if(
    $cus instancof EntityDamageByEntityEvent) {
                     
    $killer $ev->getDamager();
                     
    $victim $ev->getEntity();
               if(
    $killer instanceof Player) {
                    
    $this->addKill($killer);
                    
    $this->rmKill($victim);
                    
    $killer->sendPopup("+1 kill");
                    
    $victim->sendMessage("Killed By "$killer);
                    
    $victim->sendMessage("kills reset to 0");
                 }
         }
    }

           public function 
    addKill(Player $killer) {
                 if(!isset(
    $this->kills[$killer->getName()])) {
                         
    $this->kills[$killer->getName()] = 0;
                }
                
                
    $this->kills[$killer->getName()]++;
               if(
    $this->kills[$killer->getName()] == 5) {
                      
    //input @CraftYourBukkit code here
                     
    }
            }
              
             public function 
    rmKill(Player $player) {
                     if(isset(
    $this->kills[$player->getName()])) {
                           unset(
    $this->kills[$player->getName()]);
         }
      }
    This might help you learn more and complete your plugin :)
  5. KHAV
    Offline

    KHAV New Member

    Joined:
    Aug 7, 2016
    Posts:
    35
    Minecraft User:
    KHAV
    Maaaan i really thank you and craftyourbukkit
  6. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    If you solved your problem, please mark this post as Solved
  7. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    I still hope that you have the very basic understanding that it is both syntactically and reasonably wrong to declare class properties and class methods without a proper class declaration.
    CraftYourBukkit likes this.
  8. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Oops forgot it :p
  9. Yeelze
    Offline

    Yeelze New Member

    Joined:
    Apr 2, 2016
    Posts:
    31
    Minecraft User:
    Yeelze
    I don't think that is gonna work (I made on phone without an IDE and so fast)
    PHP:
    config.yml
    ---
    default: 
    0
    ...

    Base.php (Main)
    public function 
    init() {
    if(!
    is_dir($this->getDataFolder()) {
    @
    mkdir($this->getDataFolder());
    }
    $this->saveDefaultConfig();
    }

    public function 
    onEnable() {
    $this->init();
    }

    public function 
    isRegistered(Player $player) {
    return 
    file_exists($this->getDataFolder().strtolower($player->getName()).".yml");
    }

    public function 
    register(Player $player) {
    $config = new Config($this->getDataFolder().strtolower($player->getName()).".yml"Config::YAML, ["currentKills" => 0]);
    $config->save;
    }

    public function 
    onLogin(PlayerLoginEvent $event) {
    $player $event->getPlayer();
    $config = new Config($this->getDataFolder().strtolower($player->getName()).".yml"Config::YAML);
    if(
    $this->isRegistered($player)) {
    $config->set("currentKills""0");
    $config->save();
    }
    else {
    $this->register($player);
    }

    public function 
    onDeath(PlayerDeathEvent $event) {
    $dead $event->getPlayer();
    $deadConfig = new Config($this->getDataFolder().strtolower($dead->getName()).".yml"Config::YAML);
    $deadConfig->set("currentKills""0");
    $deadConfig->save();
    if(
    $dead instanceof Player) {
    $cause $dead->getLastDamageCause();
    if(
    $cause instanceof EntityDamageByEntityEvent && ($killer $cause->getDamager()) instanceof Player) {
    $killerConfig = new Config($this->getDataFolder().strtolower($killer->getName()).".yml"Config::YAML);
    $currentKills $config->get("currentKills");
    if(
    $currentKills == "5") {
    $killerConfig->set("currentKills""0");
    $sword Item::get(Item::DIAMOND_SWORD));
    $sword->setCustomName("§dGift");
    $killer->addItem($sword);
    }
    else {
    $killerConfig->set("currentKills"$currentKills 1);
    }
    $killerConfig->save();
    }
    }
  10. ImagicalGamer
    Offline

    ImagicalGamer Active Member

    Joined:
    Feb 25, 2016
    Posts:
    141
    Minecraft User:
    ImagicalGamer
    The line where you put "if($this->isRegistered($player))" needs to be
    PHP:
    if(!$this->isRegistered($player){
    //code here
    }
    Your checking if the players file exists, you want to set the kills to 0 if the file doesn't exist :p
  11. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Maybe he isn't good at coding yet :/, try to use simple things so he can understand
  12. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    private $kill = [];
  13. Yeelze
    Offline

    Yeelze New Member

    Joined:
    Apr 2, 2016
    Posts:
    31
    Minecraft User:
    Yeelze
    That should work (not tested).
    PHP:
    <?php

    namespace Manager;

    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\player\PlayerLoginEvent;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;

    class 
    Test extends PluginBase implements Listener {

        
    /**
         * Initialize the plugin
         */
        
    public function initialize() {
            if(!
    is_dir($this->getDataFolder())) {
                @
    mkdir($this->getDataFolder());
            }
            
    $this->saveDefaultConfig();
        }

        
    /**
         * Enable the plugin
         */
        
    public function onEnable() {
            
    $this->initialize();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        
    /**
         * @param Player $player
         * @return mixed
         */
        
    public function isRegistered(Player $player) {
            return 
    file_exists($this->getDataFolder().strtolower($player->getName()).".yml");
        }

        
    /**
         * @param Player $player
         * @return Config
         */
        
    public function register(Player $player) {
            return new 
    Config($this->getDataFolder().strtolower($player->getName()).".yml"Config::YAML, ["currentKills" => 0]);
        }

        
    /**
         * @param Player $player
         * @return Config
         */
        
    public function getPlayerConfig(Player $player) {
            if(
    $this->isRegistered($player)) {
                return new 
    Config($this->getDataFolder().strtolower($player->getName()).".yml"Config::YAML);
            }
            else {
                
    $this->register($player);
            }
        }

        
    /**
         * @param Player $player
         */
        
    public function resetPlayerKills(Player $player) {
            
    $config $this->getPlayerConfig($player);
            
    $config->set("currentKills""0");
            
    $config->save();
        }

        
    /**
         * @param Player $player
         * @param string $item
         * @param string $name
         */
        
    public function giveCustomItem(Player $player$item$name) {
            
    $item Item::get($item);
            
    $item->setCustomName($name);
            
    $player->getInventory()->addItem($item);
        }

        
    /**
         * @param Player $player
         * @param int $amount
         */
        
    public function addPlayerKills(Player $player$amount) {
            
    $config $this->getPlayerConfig($player);
            
    $config->set("currentKills"$config->get("currentKills") + $amount);
        }

        
    /**
         * @param PlayerLoginEvent $event
         */
        
    public function onLogin(PlayerLoginEvent $event) {
            
    $this->resetPlayerKills($event->getPlayer());
        }

        
    /**
         * @param PlayerDeathEvent $event
         */
        
    public function onDeath(PlayerDeathEvent $event) {
            
    $dead $event->getPlayer();
            
    $this->resetPlayerKills($dead);
            if (
    $dead instanceof Player) {
                
    $cause $dead->getLastDamageCause();
                if (
    $cause instanceof EntityDamageByEntityEvent && ($killer $cause->getDamager()) instanceof Player) {
                    
    /** @var Player $killer */
                    
    $config $this->getPlayerConfig($killer);
                    if(
    $config->get("currentKills") == "5") {
                        
    $this->resetPlayerKills($killer);
                        
    $this->giveCustomItem($killer"276""§bGift");
                    }
                    else {
                        
    $this->addPlayerKills($killer1);
                    }
                    
    $config->save();
                }
            }
        }

    }
    Primus likes this.
  14. Yeelze
    Offline

    Yeelze New Member

    Joined:
    Apr 2, 2016
    Posts:
    31
    Minecraft User:
    Yeelze
    No wtf

Share This Page

Advertisement