Multiple Files In the SRC

Discussion in 'Development' started by Junkdude, Nov 13, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So basically, I have 3 .php files, Main, Commands, And Interact. The plugin.yml isnt wrong, as I've looked it over a couple of times. Here is what the files code is
    PHP:
    <?php

    namespace CustomEnchantments;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\level\sound\ExpPickupSound;

    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\level\sound\GhastSound;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\sound;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\level\particle\FlameParticle;
    use 
    pocketmine\level\sound\ExplodeSound;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;

    class 
    Main extends PluginBase implements Listener{

      public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getServer()->getLogger()->notice(c::AQUA "CustomEnchantments Have Been Enabled!");
      }
        public function 
    onDamage(EntityDamageEvent $e) {
            if (
    $e instanceof EntityDamageByEntityEvent) {
            
    $damager $e->getDamager();
            
    $p $e->getEntity();
            
    $thing $damager->getInventory()->getItemInHand();
            if (
    $thing->getId() === 276 and $thing->getCustomName() === "Snakes Blade") {
               switch (
    mt_rand(1,5)) {
                    case 
    1:
                        return;
                    case 
    2:
                        return;
                    case 
    3:
                        return;
                    case 
    4:
                        return;
                    case 
    5:
                        
    $p->addEffect(Effect::getEffect(19)->setDuration(20)->setAmplifier(1));
            }
            }
            if (
    $thing->getId() === 276 and $thing->getCustomName() === "Za Roc") {
                
    $x $p->getX();
                
    $y $p->getY();
                
    $z $p->getZ();
                
    $level $damager->getLevel();
                
    $damager $e->getDamager();
                  
    $p $e->getEntity();
                switch (
    mt_rand(1,3)) {
                    case 
    1:
                    
    $level->addParticle(new FlameParticle(new Vector3($x$y 1$z)));
                    
    $level->addSound(new ExplodeSound(new Vector3($x$y 1$z)));
                    
    $damager->sendMessage(c::RED "Explosive Hit!");
                    
    $p->setHealth(15);
                    case 
    2:
                    return;
                    case 
    3:
                        
    $p->addEffect(Effect::getEffect(20)->setDuration(13 20)->setAmplifier(1));
                        
    $level->addSound(new GhastSound(new Vector3($x$y 1$z)));
                        
    $damager->sendMessage(c::RED "You have unleashed the forces of evil upon your enemy");
                    }
                }
            }
        }

    }
    PHP:
    <?php

    namespace CustomEnchantments;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\Item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;


    class 
    Commands {

      public function 
    onCommand(CommandSender $sender,Command $cmd,$label,array $args){
          if(
    $cmd->getName() == "buy_zaroc"){//COMMAND #1
            
    $player $this->getServer()->getPlayer($sender->getName());
            
    $item1 Item::get(Item::DIAMOND_SWORD01);
            
    $itemname1 $item1->setCustomName("Za Roc");
            
    $money $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->myMoney($sender);
            if(
    $money 2500)
            {
                
    $sender->sendMessage(c::RED ."Not Enough Money!");
                return 
    false;
            }else{
                
    $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->reduceMoney($sender->getName(), 1500);
                
    $sender->getInventory()->addItem($item1);
                
    $sender->sendMessage(c::GREEN "Za Roc Has Been Bought!");
                
    $item1->setCustomName($itemname1);
                return 
    true;
            }
        }
    }
    }
    PHP:
    <?php
    namespace CustomEnchantments;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\level\sound\ExpPickupSound;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\level\sound\GhastSound;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\sound;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Server;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\level\particle\FlameParticle;
    use 
    pocketmine\level\sound\ExplodeSound;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;

    class 
    Interact {

        public function 
    onInteract(PlayerInteractEvent $event){
            
    $name $event->getPlayer()->getName();
            
    $x $name->getX();
            
    $y $name->getY();
            
    $z $name->getZ();
            
    $thing $name->getInventory()->getItemInHand();
            if (
    $thing->getId() === 276 and $thing->getCustomName() === "XP Book"){
                
    $player->addXpLevel(25);
                
    $level->addSound(new ExpPickupSound(new Vector3($x$y 1$z)));
            }
        }
    }
    If anyone of you could help me that would be awesome! Thank you!
     
    Google likes this.
  2. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    And what is the problem exactly?
     
    Google and SOFe like this.
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You have to register the events of your interact class ( and construct it ) and make your command class extend CommandExecutor, then register the command in your main file on Enable.
     
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Could you give an example, I'm quite new
     
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Okay so the command class would be class Commands extends CommandExecutor{ instead of just class Commands { correct?
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    That's right, add this code in your main class to register the command onEnable:
    PHP:
    $this->getCommand("buy_zaroc")->setExecutor(new Commands($this));
     
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So what exactly do I do about the interact class? Again thank you for your help
     
  8. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    I'm sorry to say I'm not sure if/how it's possible to add more listeners, but you can move that part onInteract in the Main class.
     
  9. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Yeah you lost me...
     
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Ohhh, move all the code to the Main class, I got you.
     
  11. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    New error
    Fatal error: Class CustomEnchantments\Commands cannot extend from interface pocketmine\command\CommandExecutor in C:\Users\Conner\Desktop\GensisyTestServer\plugins\CustomEnchantments\src\CustomEnchantments\Commands.php on line 0
     
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    you can registerevent(new EventListener($this,(and stuff)),$this)
    the first arg is a listener to do that just make a class and extend eventlistener and the stuff is the things the listener needs commonly we pass the main class and the last this is for who registered that thing
    I am sorry to say that this code needs some fixing
    1 i am unsure of what methods you are trying to use to register the command
    guess: the most easy way is to do that is put the command in plugin.yml and add a function called onCommand under main
    2 the interact did is not registered nor extended Listener
    fix: you should just merge it into main
     
    Kenn Fatt likes this.
  13. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I know I just wanted everything neat, but if its that big of a deal, I will just merge it.
     
  14. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    you don't have to
    but you will need to extend listener AND register it on main
     
  15. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Which one do i extend the listener on, the interact?
     
  16. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    Yes, make sure to use the use statement
     
    Matthew likes this.
  17. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Sorry use?
     
  18. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,143
    GitHub:
    Thunder33345
    use pocketmine/event/listener
     
  19. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Code:
    PHP:
    <?php
    namespace CustomEnchantments;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\{EntityArmorChangeEventEntityDamageByEntityEventEntityDamageEvent};
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\level\sound\ExpPickupSound;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\level\sound\GhastSound;
    use 
    pocketmine\Player;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\sound;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Server;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\level\particle\FlameParticle;
    use 
    pocketmine\level\sound\ExplodeSound;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;
    use 
    CustomEnchantments\Main;

    class 
    Interact extends Listener {

        public function 
    onInteract(PlayerInteractEvent $event){
            
    $name $event->getPlayer()->getName();
            
    $x $name->getX();
            
    $y $name->getY();
            
    $z $name->getZ();
            
    $thing $name->getInventory()->getItemInHand();
            if (
    $thing->getId() === 276 and $thing->getCustomName() === "XP Book"){
                
    $name->addXpLevel(25);
                
    $level->addSound(new ExpPickupSound(new Vector3($x$y 1$z)));
            }
        }
    }
    No error, doesnt do anything
     
    Google likes this.
  20. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    PHP:
    <?php

    namespace CustomEnchantments;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\Item\Item;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\CommandExecutor;


    class 
    Commands implements CommandExecutor {

      public function 
    onCommand(CommandSender $sender,Command $cmd,$label,array $args){
          if(
    $cmd->getName() == "buy_zaroc"){//COMMAND #1
            
    $item1 Item::get(Item::DIAMOND_SWORD01);
            
    $itemname1 $item1->setCustomName("Za Roc");
            
    $money $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->myMoney($sender);
        if(
    $money 2500)
        {
          
    $sender->sendMessage(c::RED ."Not Enough Money!");
          return 
    false;
        }else{
          
    $this->getServer()->getPluginManager()->getPlugin("EconomyAPI")->reduceMoney($sender->getName(), 1500);
          
    $sender->getInventory()->addItem($item1);
          
    $sender->sendMessage(c::GREEN "Za Roc Has Been Bought!");
            
    $item1->setCustomName($itemname1);
            return 
    true;
        }
      }
    }
    }
    Error
    Error: "Call to undefined method CustomEnchantments\Commands::getServer()" (EXCEPTION) in "/CustomEnchantments/src/CustomEnchantments/Commands" at line 24
     

Share This Page

  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.