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

Need help making plugins

Discussion in 'Development' started by WinterBuild7074, Apr 25, 2017.

  1. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I'm a beginner in PHP programming, I made 2 plugins I want to put on Poggit for GitHub later, and I want them to work perfectly. One plugin crashes my server (a bunch of plugins won't load anymore). The second one, money doesn't work on my plugin (Example: $money = $money - strlen($msg);).

    Can you help me fix these 2 plugins?

    Plugin 1 – TellMeID:
    Server isn't loading 90% of all plugins if my plugin is on my server:
    PHP:
    <?php

    namespace WinterBuild7;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase {
        
        public 
    $prefix "§7[§f§lTellMeID§r§7]§f ";
        
        public function 
    onEnable() {
            
    $this->getLogger()->info($prefix "by WinterBuild7 was loaded!");
        }
        
        public function 
    onCommand(CommandSender $senderCommand $command, array $args) {
            if(
    $command->getName() === "tellmeid" && $sender->hasPermission("tellmeid.info")) {
                
    $sender->sendMessage($prefix "TellMeID by WinterBuild7. Tells you the item in hand.");
            }
            if(
    $command->getName() === "hand" && $sender->hasPermission("tellmeid.use")) {
                
    $id $this->getInventory()->getItemInHand()->getID();
                
    $meta $this->getInventory()->getItemInHand()->getDamage();
                
    $sender->sendMessage($prefix "You are holding item ID: §7" $id ":" $meta);
            }
        }
    }
    -----------------------

    Plugin 2 – EconomyChat:
    I tried breaking wood, but it doesn't add money. Plus, it doesn't take away money too, when using the /pm command:
    PHP:
    <?php
    namespace WinterBuild\EconomyChat;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandMap;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\permission\ServerOperator;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerPreLoginEvent;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\event\block\BlockBreakEvent;

    class 
    Main extends PluginBase implements Listener {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->saveDefaultConfig();
        }

        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args) {
        
    $money 100;
        
    $plname "§7[§e§lPM§r§7]§f ";
        
    $errortag "§7[§c§lPM§r§7] ";
            if(
    $cmd->getName() === "pm" || $cmd->getName() === "privatemsg") {
                if(
    $args[0] === "about") {
                    
    $sender->sendMessage($plname "EconomyChat/PM by WinterBuild. Send private messages to players with in-game money.");
                }
                if(
    $args[0] === "send") {
                    if(isset(
    $args[1])) {
                        if(isset(
    $args[2])) {
                            if(
    $money >= strlen($args[2])) {
                                
    $msg $args[2];
                                
    $me $sender->getName();
                                
    $money $money strlen($msg);
                                
    $tousr $sender->getServer()->getPlayer($args[1]);
                                if(
    $tousr instanceof Player) {
                                    
    $usrtag "§7[PM: Â§f§l" $me "§r§7->§f§l" $tousr->getName() . "§r§7] Â§f";
                                    
    $sender->sendMessage($usrtag $msg);
                                    
    $tousr->sendMessage($usrtag $msg);
                                }
                                else {
                                    
    $sender->sendMessage($errortag "There's no player with that name or is offline!");
                                }
                            }
                            else {
                                
    $sender->sendMessage($errortag "You don't have enough money! Your money:" $money);
                            }
                        }
                        else {
                                
    $sender->sendMessage(§errortag "You cannot send an empty message!");
                        }
                    }
                    else {
                            
    $sender->sendMessage($errortag "How to use: /pm send <player> <private-message>");
                    }
                }
                if(
    $args[0] === "money") {
                    
    $sender->sendMessage($plname "You have $" $money ".");
                }
                if(!isset(
    $args[0]) || $args[0] === "help") {
                    
    $tutorial $this->getConfig()->get("HowToGetMoney");
                    
    $sender->sendMessage("§7---------- Â§e§lPM Commands Â§r§7----------");
                    
    $sender->sendMessage("§7§lEconomyChat PM§r§f - Send PrivateMessages (PMs) to other players with in-game money.");
                    
    $sender->sendMessage($tutorial);
                    
    $sender->sendMessage("§7/pm help" " Â§f- Show all EconomyChat/PM commands.");
                    
    $sender->sendMessage("§7/pm about" " Â§f- About EconomyChat/PM");
                    
    $sender->sendMessage("§7/pm send <player> <message>" " Â§f- Send a PrivateMessage (PM) to a player.");
                    
    $sender->sendMessage("§7/pm money" " Â§f- See how much money you have.");
                    
    $sender->sendMessage("§7-------------------------------------");
                }
            }
        }
        public function 
    onBreak(BlockBreakEvent $event) {
            
    $blockbreak $this->getConfig()->get("BlockBreak");
            
    $moneyget $this->getConfig()->get("BreakMoney");
            if(
    $event->getBlock()->getId() === $blockbreak) {
                
    $money $money $moneyget;
            }
        }
    }
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If you have multiple plugins using the same namespace, the plugins will interfere with each other when they are constructed.
    PHP:
    <?php

    namespace WinterBuild7;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase {
      
        public 
    $prefix "§7[§f§lTellMeID§r§7]§f ";
      
        public function 
    onEnable() {
            
    $this->getLogger()->info($prefix "by WinterBuild7 was loaded!");
        }
      
        public function 
    onCommand(CommandSender $senderCommand $command, array $args) {
            if(
    $command->getName() === "tellmeid" && $sender->hasPermission("tellmeid.info")) {
                
    $sender->sendMessage($prefix "TellMeID by WinterBuild7. Tells you the item in hand.");
            }
            if(
    $command->getName() === "hand" && $sender instanceof Player && $sender->hasPermission("tellmeid.use")) { //make sure the sender is a player
                
    $id $sender->getInventory()->getItemInHand()->getID(); //use $sender instead of main class
                
    $meta $sender->getInventory()->getItemInHand()->getDamage(); //use $sender instead of main class
                
    $sender->sendMessage($prefix "You are holding item ID: §7" $id ":" $meta);
            }
        }
    }
     
    Last edited: Apr 25, 2017
    corytortoise likes this.
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you are not saving the values of your money thus it will always stays the same
     
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I change and fix this?
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    when you are setting the amount of money for each player, set the data to a global array using the player names as the keys
     
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How? I never did that before. Code? I know how to use array and keys, but I don't know how to make it really.
     
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    something like this:
    PHP:
    $this->money[$playerName] = $money;
     
  8. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    But I still need a code that adds to player to the array.
     
  9. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    what do you mean?
     
  10. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Um, I don't unerstand. What will the whole code be? Do I need to construct?
     
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    you are trying to make an economy system correct? You need a way to track each player's money amount. You need to use an array.
     
  12. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Yes. But how can I add the player's name to the array, plus, how much money this player has? Also, do I need to add those?
     
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    use the code I gave you. the variable names should be self explanatory.
     
  14. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Where do I need to add that?
    Plus, do I still need to add any more code except that piece?
     
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    You need to learn more about how to program before asking these questions on this forum. We are here to help with PMMP API, not generic php.
     
  16. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    For the plugin no. 1, I did all that, but no changes. 90% of all plugins not loading.
     
  17. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You must be getting errors. $money is undefined in your BlockBreakEvent handler.
     
    jasonwynn10 likes this.
  18. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I gave up with my plugins, don't need help with that anymore. But I'm still wondering why my plugin no. 1 isn't working.
    Plus, if any more replies here, then admins, please move this to 'Development', I'll be more careful with forum categories.
     
  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.