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

Can someone please check my coding

Comments in 'Plugin Development' started by Deathcrow, Sep 17, 2015.

Thread Status:
Not open for further replies.
  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    At least he is trying to learn... Much better than those yelling and punching their PC screens.
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    *facepalm* There is no getItem() function for CommandSender... Oh and you never defined $event. Use $sender->getInventory()->getItemInHand() instead.
    Legoboy0215 likes this.
  3. BalAnce
    Offline

    BalAnce Active Member

    Joined:
    Mar 10, 2015
    Posts:
    125
    Minecraft User:
    BalAnce
    PHP:
    <?php
    namespace simpleenchant;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\Player;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\item\Item;
    class 
    main extends PluginBase {
        public function 
    onEnable(){
            
    $this->getLogger()->info("Test");
        }
        public function 
    onCommand(CommandSender $sender,Command $command$label,array $args){
            switch (
    strtolower($command->getName())){
                case 
    "enchantitem":
            if (
    $sender instanceof Player){
                
    $item = new Item($sender->getInventory()->getItemInHand()->getId(),0,1);

                if (isset(
    $args[0])){
                
    $sender->getInventory()->remove($item);
                
    $enchantment Enchantment::getEnchantment($args[0]);
                
    $enchantment->setLevel(10000);
                
    $item->addEnchantment($enchantment);
                
    $sender->getInventory()->addItem($item);
                
    $sender->sendMessage(TextFormat::AQUA."Enchanted Item!");
                }else {
                    
    $sender->sendMessage(TextFormat::RED."USAGE: /enchantitem [enchantmentid]");
                }
            }
            break;
            }
        }
     
    }
    this is what i used in my plugin and it works maybe you learn something? hopefully :) Im sure there was a better way to do it as if you enchant the same item with multiple enchants it will duplicate the item with the different enchantment but ye it works at least. haha ;)
    MCPEPIG and Legoboy0215 like this.
  4. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    One of the skills one should learn when learning any programming language is how to debug. Which is not complicated, but it is a skill related to problem solving. Unfortunately, schools usually fail at teaching good problem solving skills.

    In this particular case, you wrote code and then when it didn't work you only posted your code and only part of the error message.

    If you really want to debug code there are a number of tools and tricks. At the end of the day it is about using your brain and keeping your eyes open.

    In your example, if you were to read carefully, you would have noticed immediately that the error included "$id not defined". And it would even show the line number. So you could have fixed that bug right away without having to post your code and get rude comments.

    Another useful thing to do when debugging is to sprinkle code with "echo" and "print_r" statements. (Some people prefer var_dump instead). I tend to do this quite a lot. The nice thing about this, is that you don't really need fancy IDEs or special tools. And these type of things work no matter what programming language you use. Anyway, I myself like to use the line:

    PHP:
    echo __METHOD__.",".__LINE__."\n";//##DEBUG
    That way I can easily/quickly find out all the places where I added debugging code (search for ##DEBUG). This also lets me trace the flow of the code. That way you can spot problems by examining the console as you are testing.

    So yes, learning programming it not just learning the syntax and the API. You really need to know how to fix the breakage that you make.
    Skullex, PEMapModder, Primus and 2 others like this.
  5. Deathcrow
    Offline

    Deathcrow Active Member

    Joined:
    Aug 20, 2015
    Posts:
    61
    Minecraft User:
    Deathcrow
    I really appreciate this :)
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Debugging is not that hard. People tend to think so though.
  7. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Depends what is being debugged. A 20 line simple welcome-to-server-message plugin is a lot easier to debug than a Survival Games plugin with 500 lines of code.
    Deathcrow and CrazedMiner like this.
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    :D :D :D
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    $event? You havent defined it :p use
    PHP:
    $item $sender->getInventory()->getItemInHand();
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Actually i learned this from NetherChests, thanks ;)
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    OMG. You're making me crazy :mad: i just pointed out the most problem i saw (self->readTitle()) ! If you cant take the painful truth then get yourself a nanny.
  12. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    That's just plain mean. Even I'm not that mean.
    Well unfortunately @Primus is going to be a member on this forum for a very long time. :/
  13. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    It's your problem, and stop flaming. Still remember the time when you didn't know anything? Did anyone tell you that your coding style sucks? If I recall, it's no. So I think you should take the painful truth and get yourself a nanny. Even I'm not that mean.
  14. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Of course, since you got threatened by @Intyre :p
    @Primus, OMG stop being so upset! :)
  15. Deathcrow
    Offline

    Deathcrow Active Member

    Joined:
    Aug 20, 2015
    Posts:
    61
    Minecraft User:
    Deathcrow
    Whats another way to get info from config file the method im usig crashes the server

    PHP:
     $conItem $sender->getConfig()->get("itemid");
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    $sender? Sender represents a player, not plugin instance. Use
    PHP:
    $conItem $this->getConfig()->get("itemid");
    Reading the docs might help.
  17. Deathcrow
    Offline

    Deathcrow Active Member

    Joined:
    Aug 20, 2015
    Posts:
    61
    Minecraft User:
    Deathcrow
    Omg im so slow of course :D:p
    Legoboy0215 likes this.
  18. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    :eek: *Jumps into lava*
    Legoboy0215 likes this.
  19. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    *And becomes a cooked porkchop*
  20. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    You forgot bacon too .-.
    jojoe77777 likes this.
Thread Status:
Not open for further replies.

Share This Page

Advertisement