doufeng5059 2015-12-11 18:30
浏览 100
已采纳

传递给:: __构造的参数1必须是

i am working on an addon, but i got a problem

[22:03:08] [CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerInteractEvent' to 'MTeamPvP v1.0.0 Beta': Argument 1 passed to MCrafters\TeamPvP\GameManager::__construct() must be an instance of MCrafters\TeamPvP\TeamPvP, none given, called in C:\Users\USER\Desktop\Taha\FlashCraft PE\lobby 1\plugins\DevTools\src\MCrafters\TeamPvP\TeamPvP.php on line 120 and defined on MCrafters\TeamPvP\TeamPvP
[22:03:08] [NOTICE]: InvalidArgumentException: "Argument 1 passed to MCrafters\TeamPvP\GameManager::__construct() must be an instance of MCrafters\TeamPvP\TeamPvP, none given, called in C:\Users\USER\Desktop\Taha\FlashCraft PE\lobby 1\plugins\DevTools\src\MCrafters\TeamPvP\TeamPvP.php on line 120 and defined" (E_RECOVERABLE_ERROR) in "/DevTools/src/MCrafters/TeamPvP/GameManager" at line 13

also sorry for the different error broadcasting

code : (please don't care about the other classes except GameManager and TeamPvP) TeamPvP.php:

<?php

namespace MCrafters\TeamPvP;

use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat as Color;
use pocketmine\utils\Config;
use pocketmine\event\Listener;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\math\Vector3;
use pocketmine\level\Position;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\block\Block;
use pocketmine\item\Item;
use pocketmine\block\WallSign;
use pocketmine\block\PostSign;
use pocketmine\scheduler\ServerScheduler;

class TeamPvP extends PluginBase implements Listener
{

// Teams
public $reds = [];
public $blues = [];
public $gameStarted = false;
public $yml;


public function onEnable()
{
    // Initializing config files
    $this->saveResource("config.yml");
    $yml = new Config($this->getDataFolder() . "config.yml", Config::YAML);
    $this->yml = $yml->getAll();

    $this->getLogger()->debug("Config files have been saved!");

    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Tasks\SignUpdaterTask($this), 15);
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    $this->getServer()->getLogger()->info(Color::BOLD . Color::GOLD . "M" . Color::AQUA . "TeamPvP " . Color::GREEN . "Enabled" . Color::RED . "!");
}

public function isFriend($p1, $p2)
{
    if ($this->getTeam($p1) === $this->getTeam($p2) && $this->getTeam($p1) !== false) {
        return true;
    } else {
        return false;
    }
}

// isFriend
public function getTeam($p)
{
    if (in_array($p, $this->reds)) {
        return "red";
    } elseif (in_array($p, $this->blues)) {
        return "blue";
    } else {
        return false;
    }
}

public function setTeam($p, $team)
{
    if (strtolower($team) === "red") {
        if (count($this->reds) < 5) {
            if ($this->getTeam($p) === "blue") {
                unset($this->blues[array_search($p, $this->blues)]);
            }
            array_push($this->reds, $p);
            $this->getServer()->getPlayer($p)->setNameTag("§c§l" . $p);
            $this->getServer()->getPlayer($p)->teleport(new Vector3($this->yml["waiting_x"], $this->yml["waiting_y"], $this->yml["waiting_z"]));
            return true;
        } elseif (count($this->blues) < 5) {
            $this->setTeam($p, "blue");
        } else {
            return false;
        }
    } elseif (strtolower($team) === "blue") {
        if (count($this->blues) < 5) {
            if ($this->getTeam($p) === "red") {
                unset($this->reds[array_search($p, $this->reds)]);
            }
            array_push($this->blues, $p);
            $this->getServer()->getPlayer($p)->setNameTag("§b§l" . $p);
            $this->getServer()->getPlayer($p)->teleport(new Vector3($this->yml["waiting_x"], $this->yml["waiting_y"], $this->yml["waiting_z"]));
            return true;
        } elseif (count($this->reds) < 5) {
            $this->setTeam($p, "red");
        } else {
            return false;
        }
    }
}

public function removeFromTeam($p, $team)
{
    if (strtolower($team) == "red") {
        unset($this->reds[array_search($p, $this->reds)]);
        return true;
    } elseif (strtolower($team) == "blue") {
        unset($this->blues[array_search($p, $this->blues)]);
        return true;
    }
}

public function onInteract(PlayerInteractEvent $event)
{
    $p = $event->getPlayer();
    $teams = array("red", "blue");
    if ($event->getBlock()->getX() === $this->yml["sign_join_x"] && $event->getBlock()->getY() === $this->yml["sign_join_y"] && $event->getBlock()->getZ() === $this->yml["sign_join_z"]) {
        if (count($this->blues) < 5 && count($this->reds) < 5) {
            $this->setTeam($p->getName(), $teams[array_rand($teams, 1)]);
            $s = new GameManager();
            $s->run();
        } else {
            $p->sendMessage($this->yml["teams_are_full_message"]);
        }
  }
}

public function onEntityDamage(EntityDamageEvent $event)
{
    if ($event instanceof EntityDamageByEntityEvent) {
        if ($event->getEntity() instanceof Player) {
            if ($this->isFriend($event->getDamager()->getName(), $event->getEntity()->getName()) && $this->gameStarted == true) {
                $event->setCancelled(true);
                $event->getDamager()->sendMessage(str_replace("{player}", $event->getPlayer()->getName(), $this->yml["hit_same_team_message"]));
            }

            if ($this->isFriend($event->getDamager()->getName(), $event->getEntity()->getName())) {
                $event->setCancelled(true);
                 }
            }
        }
    }


public function onDeath(PlayerDeathEvent $event)
{
    if ($this->getTeam($event->getEntity()->getName()) == "red" && $this->gameStarted == true) {
        $this->removeFromTeam($event->getEntity()->getName(), "red");
        $event->getEntity()->teleport($this->getServer()->getLevelByName($this->yml["spawn_level"])->getSafeSpawn());
    } elseif ($this->getTeam($event->getEntity()->getName()) == "blue" && $this->gameStarted == true) {
        $this->removeFromTeam($event->getEntity()->getName(), "blue");
        $event->getEntity()->teleport($this->getServer()->getLevelByName($this->yml["spawn_level"])->getSafeSpawn());
    }
    foreach ($this->blues as $b) {
        foreach ($this->reds as $r) {
            if (count($this->reds) == 0 && $this->gameStarted == true) {

                $this->getServer()->getPlayer($b)->getInventory()->clearAll();
                $this->removeFromTeam($b, "blue");
                $this->getServer()->getPlayer($b)->teleport($this->getServer()->getLevelByName($this->yml["spawn_level"])->getSafeSpawn());
                $this->getServer()->broadcastMessage("Blue Team won TeamPvP!");
            } elseif (count($this->blues) == 0 && $this->gameStarted == true) {
                $this->getServer()->getPlayer($r)->getInventory()->clearAll();
                $this->removeFromTeam($r, "red");
                $this->getServer()->getPlayer($r)->teleport($this->getServer()->getLevelByName($this->yml["spawn_level"])->getSafeSpawn());
            }
        }
    }
}
}//class

GameManager.php :

<?php
namespace MCrafters\TeamPvP;

use pocketmine\scheduler\ServerScheduler as Tasks;

class GameManager
{
public $reds;
public $blues;
public $gst;
public $gwt;

public function __construct(\MCrafters\TeamPvP\TeamPvP $plugin)
{
 parent::__construct($plugin);
 $this->plugin = $plugin;
}


public function run()
{
    $this->reds = $this->plugin->reds;
    $this->blues = $this->plugin->blues;

    if (count($this->reds) < 5 && count($this->blues) < 5) {
        $this->gst = Tasks::scheduleRepeatingTask(new Tasks\GameStartTask($this), 20)->getTaskId();
        Tasks::cancelTask($this->gwt);
    } else {
        $this->gwt = Tasks::scheduleRepeatingTask(new Tasks\GameWaitingTask($this), 15)->getTaskId();
    }
}
}

namespace correct, class and file name correct, and all other functions from other classes have nothing to do with this :) check the line where there is GameManager::run() in TeamPvP class.

i already know there is one about this, but i didn't understand it.

Thank You for your help.

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-12-11 18:47
    关注

    You have a type hint

    public function __construct(\MCrafters\TeamPvP\TeamPvP $plugin)
    {
     parent::__construct($plugin);
     $this->plugin = $plugin;
    }
    

    So when you go to instantiate MCrafters\TeamPvP\GameManager you have to pass it an instance of \MCrafters\TeamPvP\TeamPvP

    $team = new \MCrafters\TeamPvP\TeamPvP();
    $manager = new \MCrafters\TeamPvP\GameManager($team)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请专家看看这个问题出在哪里?
  • ¥15 tensorrt 推理 yolov8框出现错误
  • ¥15 用hc 05,做蓝牙控制小车 小车只能说初始遥控方向影响,后续不能更改
  • ¥15 51单片机怎么把每次输入键值保存到数组作为我的确认键的判断条件呢
  • ¥15 c语言字符串单词入门问题报错
  • ¥30 我现在是双非人工智能专业不知道要不要转到软件工程专业
  • ¥15 请问12脉波整流机组的外特性曲线具体是如何推导
  • ¥15 abaqus里的python算法
  • ¥100 如何在思科模拟器中进入CEF模式
  • ¥15 Simulink 仿真如何使用MATLAB function模块获取到仿真过程中某一仿真时刻输入到模块的函数的值?