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

something wrong with that code?

Discussion in 'Help' started by SaintsDEV, Jun 8, 2020.

  1. SaintsDEV

    SaintsDEV Silverfish

    Messages:
    16
    GitHub:
    yukishiro
    $this->getScheduler()->scheduleRepeatingTask(new Task([$this, "updateKey"]), 20*30);
     
  2. RicardoMilos384

    RicardoMilos384 Slime

    Messages:
    82
    GitHub:
    ricardomilos384
    PHP:
    $this->getScheduler()->scheduleRepeatingTask(new Task($this"updateKey"), 20 30);
     
  3. SaintsDEV

    SaintsDEV Silverfish

    Messages:
    16
    GitHub:
    yukishiro
    Error: "Cannot instantiate abstract class pocketmine\scheduler\Task" (EXCEPTION) in "plugins/Key_v1/src/key/Main" at line 24

    is giving this error.
     
  4. RicardoMilos384

    RicardoMilos384 Slime

    Messages:
    82
    GitHub:
    ricardomilos384
    send full code
     
  5. SaintsDEV

    SaintsDEV Silverfish

    Messages:
    16
    GitHub:
    yukishiro
    <?php

    namespace key;

    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    use pocketmine\utils\Config;
    use pocketmine\scheduler\Task;
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\command\ConsoleCommandSender;

    class Main extends PluginBase{

    const PREFIX = "§9[§6#§9]";
    const COUNT_KEY = 8;

    public function onEnable(){
    @mkdir($this->getDataFolder());
    $this->time = new Config($this->getDataFolder()."time.yml", Config::YAML);
    $this->data = new \SQLite3($this->getDataFolder()."keys.db");

    $this->pureperms = $this->getServer()->getPluginManager()->getPlugin("PurePerms");
    $this->getScheduler()->scheduleRepeatingTask(new Task([$this, "updateKey"]), 20*30);

    $this->data->query("CREATE TABLE if not exists keys(
    key TEXT PRIMARY KEY NOT NULL,
    type TEXT NOT NULL,
    active INT NOT NULL
    );");
    }

    public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args) : bool{
    switch($cmd->getName()){
    case "gerarkey":
    if(!isset($args[0])){
    $sender->sendMessage(self::pREFIX." §7Use §a/gerarkey §8[§b<mensal> ou <eterno>§8]");
    }else{
    if(strtolower($args[0])=="mensal"){
    $key = $this->gerarkey(self::COUNT_KEY);
    $sender->sendMessage(self::pREFIX." §7Key gerada com sucesso: §8[ §b {$key} §8]");
    $this->addKey($key, "mensal");
    }else if(strtolower($args[0])=="eterno"){
    $key = $this->gerarkey(self::COUNT_KEY);
    $sender->sendMessage(self::pREFIX." §7Key gerada com sucesso: §8[§b {$key} §8]");
    $this->addKey($key, "eterno");
    }else{
    $sender->sendMessage(self::pREFIX."§7Use §a/gerarkey §8[§b<mensal> ou <eterno>§8]");
    }
    }
    break;
    case "removerkey":
    if(!isset($args[0])){
    $sender->sendMessage(self::pREFIX." §7Use §a/remkey <key> §7!");
    }else{
    if($this->existsKey($args[0])){
    $this->remKey($args[0]);
    $sender->sendMessage(self::pREFIX." §7Key removida com sucesso!");
    }else{
    $sender->sendMessage(self::pREFIX." §7Não existe nenhuma key com este nome!");
    }
    }
    break;
    case "ativar":
    if(!isset($args[0])){
    $sender->sendMessage(self::pREFIX." §7Use §8[ §b/ativarkey <key> §8]");
    }else{
    if($this->existsKey($args[0])){
    if($this->isActive($args[0])){
    $sender->sendMessage(self::pREFIX." §7Essa key ja foi ativada!");
    }else{
    $sender->sendMessage(self::pREFIX." §aAguarde...");
    $this->activeKey($sender, $args[0]);
    }
    }else{
    $sender->sendMessage(self::pREFIX." §7Não existe nenhuma key com este nome!");
    }
    }
    break;
    }
    return true;
    }

    public function existsKey(string $key){
    $res = $this->data->query("SELECT * FROM keys WHERE key = '$key'");
    $data = $res->fetchArray(SQLITE3_ASSOC);
    if(isset($data["key"]) && $data["key"] == $key) return true; else return false;
    }

    public function gerarkey(int $count){
    $gr = "";
    $pl = "abcdefghijklmnopqrstuvwxyz0123456789";
    $pla = str_split($pl);
    for($i = 0; $i < $count; $i++){
    $ra = array_rand($pla);
    $gr .= "".$pla[$ra];
    }
    return $gr;
    }

    public function addKey(string $key, string $type){
    if(!$this->existsKey($key)){
    $this->data->query("INSERT INTO keys(key, type, active) VALUES ('$key', '$type', 0)");
    }
    }

    public function remKey(string $key){
    if($this->existsKey($key)){
    $this->data->query("DELETE * FROM keys WHERE key = '$key'");
    }
    }

    public function isActive(string $key){
    if($this->existsKey($key)){
    $res = $this->data->query("SELECT * FROM keys WHERE key = '$key'");
    $data = $res->fetchArray(SQLITE3_ASSOC);
    if($data["active"]==0) return false; else return true;
    }else{
    return false;
    }
    }

    public function activeKey(Player $p, string $key){
    if($this->existsKey($key)&&!$this->isActive($key)){
    if($this->pureperms->getUserDataMgr()->getNode($p, "group")=="Vip"){
    $p->sendMessage(self::pREFIX." §7Você ja tem §6§lVIP§r §7no servidor!");
    return;
    }
    $name = strtolower($p->getName());
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup {$name} Vip");
    $this->data->query("UPDATE keys SET active = 1 WHERE key = '$key'");
    $res = $this->data->query("SELECT * FROM keys WHERE key = '$key'");
    $data = $res->fetchArray(SQLITE3_ASSOC);
    $p->sendMessage(self::pREFIX." §aVocê ativou seu vip {$data["type"]} com sucesso!");
    if($data["type"]=="mensal"){
    date_default_timezone_set("America/Sao_Paulo");
    $time = (time() + 2592000);
    $this->time->set($name, "{$time}");
    $this->time->save();
    $p->sendMessage(self::pREFIX." §7O seu §bVIP§7 vai acabar em: §f".date("d/m/Y", $time)."§7!");
    }
    foreach($this->getServer()->getOnlinePlayers() as $pl){
    $pl->addTitle("§6{$p->getName()} §7tornou-se §b§lVIP§r");
    }
    }
    }

    public function updateKey(){
    foreach($this->getServer()->getOnlinePlayers() as $p){
    $name = strtolower($p->getName());
    if($this->time->exists($name)){
    date_default_timezone_set("America/Sao_Paulo");
    $time = $this->time->get($name);
    if(time()>=$time){
    $p->sendMessage(self::pREFIX." §7Seu §6§lVIP §r§7mensal acabou :(");
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup {$name} Guest");
    $this->time->remove($name);
    $this->time->save();
    }
    }
    }
    }
    }
     
  6. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    You have to make a class that extends Task, not use Task directly.
     
    Last edited: Jun 8, 2020
  7. SaintsDEV

    SaintsDEV Silverfish

    Messages:
    16
    GitHub:
    yukishiro
    ok I will do it if it goes right back here if it goes wrong too hehe.
     
  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.