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

Config generation

Comments in 'Plugin Development' started by Dinokiller, Jul 13, 2014.

  1. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Hello, I've run into a small problem while rewriting PVPWorld for 1.4. My config file is generated but is blank. There or no errors on console and the code works fine. Even when saved the file stays blank.
    PHP:
    <?php

    namespace pvpworld;

    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase implements Listener {
        private 
    $config;

        public function 
    onEnable() {     
            
    $this->config $this->getConfig();
            if(!
    $this->config->exists("message")) {
                
    $this->config->set("message""You're not allowed to hurt players here.");
                
    $this->config->save();
            }     
            if(!
    $this->config->exists("pvp-worlds")) {
                
    $this->config->set("pvp-worlds", []);
                
    $this->config->save();
            } 
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getServer()->setConfigBool("pvp"true);
        }

        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            if(
    strtolower($command->getName()) == "pvpworld") {
                if(
    $sender instanceof Player) {
                    
    $world $sender->getLevel()->getName();
                }else{
                    
    $world $this->getServer()->getDefaultLevel()->getName();
                }
                if(isset(
    $args[1])) {
                    if(
    $this->getServer()->isLevelLoaded($args[1])) {
                        
    $world $args[1];
                    }else{
                        
    $sender->sendMessage("That world doesn't exist.\n");
                        return 
    true;
                    }
                }
                
    $mode in_array($world$this->config->get("pvp-worlds"));
                if(isset(
    $args[0])) {
                    
    $mode $args[0] == "on";
                }
                if(
    $mode) {
                    
    $sender->sendMessage("PVP is now enabled in \"$world\"");
                    if((
    $key array_search($world$this->config->get("pvp-worlds"))) !== false) {
                        
    $arr $this->config->get("pvp-worlds");
                        unset(
    $arr[$key]);
                        
    $this->config->set("pvp-worlds"$arr);
                    }
                }else{
                    
    $sender->sendMessage("PVP is now disabled in \"$world\"");
                    
    $arr $this->config->get("pvp-worlds");
                    
    array_push($arr$world);
                    
    $this->config->set("pvp-worlds"$arr);
                }
                
    $this->config->save();
            }
            return 
    true;
        }
     
        public function 
    onEntityDamageByEntity(EntityDamageByEntityEvent $event){
            
    $victim $event->getEntity();
            
    $attacker $event->getDamager();
            if(
    $victim instanceof Player && $attacker instanceof Player){
                if(
    in_array($attacker->getLevel->getName(), $this->config->get("pvp-worlds")) && !$attacker->hasPermission("pvpworld.bypass")) {
                    
    $attacker->sendMessage($this->config->get("message"));
                    
    $event->setCancelled();
                }
            }
        }
     
    }
  2. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    how about you generate a new config and not use the already madden one
    Last edited: Jul 13, 2014
  3. Renzo
    Offline

    Renzo Active Member

    Joined:
    Mar 12, 2014
    Posts:
    96
    Minecraft User:
    Renzo_4
    Dont you need another 'use' for the config?
  4. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    what ?
  5. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Why?
    swagboy47 likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. The code that uses the Config class is at PluginBase.php, not here. However, my suggestion is to instantiate a new config file rather than $this->getConfig() if you don't already have a config file present.
    Dinokiller likes this.
  7. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Thanks guys.

Share This Page

Advertisement