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

Config/Integer issue

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

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    So, I fixed my code, but no matter what I try to set it, it only goes up 30. No error or anything, just goes up by 30 :(

    PHP:
    <?php

    namespace MCMMOPE\Skills;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as c;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Cancellable;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    MCMMOPE\Loader;
    use 
    pocketmine\entity\Effect;

    class 
    Mining extends PluginBase implements Listener{
        public function 
    dataPath()
        {
          return 
    $this->getDataFolder();
        }
      public 
    $mining;

        public function 
    __construct(Loader $plugin){
              
    $this->loader $plugin;
        }

        public function 
    getPlugin() {
            return 
    $this->plugin;
        }



        public function 
    onBreak(BlockBreakEvent $event){
          
    $player $event->getPlayer();
          
    $name $player->getName();
          
    $this->miningexp = new Config($this->loader->getDataFolder()."Mining/"."miningexp.yml"Config::YAML,array());
          
    $this->mininglevel = new Config($this->loader->getDataFolder()."Mining/"."mininglevel.yml"Config::YAML,array()); 
          
    $miningcooldown $this->loader->getDataFolder()."Mining/"strtolower($player->getName());
             if(
    $event->getBlock()->GetId() == or 89 or 48 or 87 or 24){
              if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 30);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

             if(
    $event->getBlock()->GetId() == 174 or 159){
              if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 50);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

             if(
    $event->getBlock()->GetId() == 16 or 179 or 153 ){
              if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 100);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

             if(
    $event->getBlock()->GetId() == 49){
              if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 150);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

             if(
    $event->getBlock()->GetId() == 15){
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 250);
             
    $this->miningexp->save();
             return 
    true;
           }

             if(
    $event->getBlock()->GetId() == 14){
              if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 1);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

           if(
    $event->getBlock()->GetId() == 21){
            if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 400);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

           if(
    $event->getBlock()->GetId() == 56){
            if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 750);
             
    $this->miningexp->save();
             return 
    true;
           }
         }

           if(
    $event->getBlock()->GetId() == 129){
            if(!
    $event->isCancelled()) {
             
    $this->miningexp->set($name,$this->miningexp->get($name) + 1000);
             
    $this->miningexp->save();
             return 
    true;
           }
         }
           if(
    $this->miningexp->get($name) == 15000){
             
    $level $this->mininglevel->get($name);
             
    $this->mininglevel->set($name,$this->mininglevel->get($name) + 1);
             
    $this->mininglevel->save();
             
    $player->sendMessage(c::GREEN "Your Mining Skill Is Now Level" $level);
             return 
    true;
           }

           if(
    $this->miningexp->get($name) == 30000){
             
    $level $this->mininglevel->get($name);
             
    $this->mininglevel->set($name,$this->mininglevel->get($name) + 1);
             
    $this->mininglevel->save();
             
    $player->sendMessage(c::GREEN "Your Mining Skill Is Now Level" $level);
             return 
    true;
           }

           if(
    $this->miningexp->get($name) == 45000){
             
    $level $this->mininglevel->get($name);
             
    $this->mininglevel->set($name,$this->mininglevel->get($name) + 1);
             
    $this->mininglevel->save();
             
    $player->sendMessage(c::GREEN "Your Mining Skill Is Now Level" $level);
             return 
    true;
           }

           if(
    $this->miningexp->get($name) == 60000){
             
    $level $this->mininglevel->get($name);
             
    $this->mininglevel->set($name,$this->mininglevel->get($name) + 1);
             
    $this->mininglevel->save();
             
    $player->sendMessage(c::GREEN "Your Mining Skill Is Now Level" $level);
             return 
    true;
           }
               if(
    $event->getBlock()->GetId() == or 89 or 48 or 87 or 24 or 174 or 159 or 16 or 179 or 153  or 49 or 15 or 14 or 21 or 56 or 129){
                if(
    is_file($miningcooldown)) {
               
    $data yaml_parse_file($miningcooldown);
               
    $lastTime $data["last-execute-miningskill"];
               } else {
               
    $lastTime 0;
               }
               if(
    time() - $lastTime 5) {
               return 
    true;
               }else if(
    time() - $lastTime 5) {
               
    $player->SendMessage("Skill Activated");
               
    $player->addEffect(Effect::getEffect(3)->setDuration(100)->setAmplifier(10));
               
    $this->miningexp->set($name,$this->miningexp->get($name) + 50);
               
    $this->miningexp->save();
               }
               
    $data["last-execute-miningskill"] = time();
               
    yaml_emit_file($miningcooldown$data);
               return 
    true;
             }
           }
         }
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    a switch would probably be better in this case
    And also the redundant if(!$event->isCancelled()) { should be replaced by just one big one around all the code on the event. (And you don't really need that check anyway)
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Learn PHP.
    It's tiring to explain this every time, but well...

    PHP is not English. You should look at what `or` means before using it.
    `$a or $b` resolves to true if $a is true or $b is true. Not mentioning the problem in operator precedence here (`getId() == 1` is resolved before the `or` operator), assuming all your integers are resolved into one value, please look at what `==` means.
    `$a == $b` resolves to true if `$a` and `$b` represent the same value. Your `$a` here is the block ID, and what is your `$b`? Fine, let's forget what `or` actually means. You expect the `==` to work like "if block ID is in one of the integers behind...". in one of. This is not what the == operator means.
    Now looking at the `or` operator again, the result always resolves into a boolean. And even if you add parentheses around `1 or 89 or 48 or 87 or 24`, you will just end up comparing an integer to a boolean, which is also wrong.

    Correct code:
    PHP:
    $id $event->getBlock()->getId();
    if(
    $id == or $id == 89 or $id == 48 or $id == 87 or $id ==27) {...}
    Or if you don't want to write `$id ==` so many times, or if you have a variable number of integers in an array to compare, you can use in_array:
    PHP:
    if(in_array($id, [189488727])) {...}
    Hint: one more closely related problem: `1 <= $x <= 3` also doesn't do what you expect it to do! You will be comparing `1 <= $x` (a boolean) against the integer 3!
     
  4. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    They should use Switch-Case ^^
     
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    There's no point in making a switch statement if you want the outcome to be the same for every value that returns true...
     
    Jack Noordhuis and corytortoise like this.
  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.