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

Major error that I can't understand

Discussion in 'Plugin Help' started by LikeZCodes, Feb 23, 2018.

Thread Status:
Not open for further replies.
  1. LikeZCodes

    LikeZCodes Spider

    Messages:
    6
    So I understand some pieces of PHP and stuff but I can;t seem to fix this problem. I really need help with this! Please tell me what it means THEN fix it; that is if you have a solution.


    The code itself

    Code:
    <?php
    /**
     * Created by PhpStorm.
     * User: Matt
     * Date: 2/10/2017
     * Time: 7:43 PM
     */
    
    namespace sys\arenapvp\kit;
    
    
    use pocketmine\entity\Effect;
    use pocketmine\item\enchantment\Enchantment;
    use pocketmine\item\Item;
    use pocketmine\item\ItemFactory;
    use pocketmine\utils\Config;
    use pocketmine\utils\TextFormat;
    use sys\arenapvp\arena\ArenaManager;
    use sys\arenapvp\ArenaPvP;
    use sys\arenapvp\utils\GoldenHead;
    
    class KitManager {
    
        /** @var Config */
        private $config;
    
        /** @var Kit[] */
        private $kits = [];
    
        /** @var ArenaPvP */
        private $plugin;
    
        public function __construct(ArenaPvP $plugin) {
            $this->plugin = $plugin;
            $plugin->saveResource("kits.json", false);
            $this->config = new Config($plugin->getDataFolder() . "kits.json", Config::JSON);
            ItemFactory::registerItem(new GoldenHead(), true);
            $this->loadKits();
        }
    
        public function loadKits() {
            foreach ($this->getConfig()->getAll() as $kit => $data) {
                $armor = $this->parseArmor($data);
                $items = $this->parseItems($data);
                $icon = $data["icon"];
                if (is_array($icon)) {
                    $iconItem = Item::get($icon[0], $icon[1]);
                } else {
                    $iconItem = Item::get($icon);
                }
                $iconItem->setCustomName(TextFormat::GREEN . $kit);
    
                $kitClass = new Kit($kit, $iconItem, $armor, $items, $data["mapType"] ?? ArenaManager::TERRAIN, $data["shouldRegen"] ?? true, $data["allowsBuilding"] ?? false);
                if (isset($data["effects"]) and count($data["effects"]) > 0) {
                    foreach ($data["effects"] as $effect) {
                        if ($effect[2] == "infinite") {
                            $effect[2] = INT32_MAX;
                        }
                        $kitClass->addEffect(Effect::getEffect($effect[0])->setAmplifier($effect[1])->setDuration($effect[2]));
                    }
                }
                $this->addKit($kitClass);
            }
            $this->getPlugin()->getLogger()->info(TextFormat::GREEN . "The kits have been loaded! Number of kits: " . count($this->getKits()));
        }
    
        public function addKit(Kit $kit) {
            $this->kits[$kit->getName()] = $kit;
        }
    
        /**
         * @param string $name
         * @return null|Kit
         */
        public function getKitByName(string $name): ?Kit {
            foreach ($this->getKits() as $kit) {
                if ($kit->isKit($name)) {
                    return $kit;
                }
            }
            return null;
        }
    
        /**
         * @param array $data
         * @return Item[]
         */
        public function parseItems(array $data): array {
            $parsedItems = [];
            foreach ($data["items"] as $item) {
                $parsedItem = Item::get($item["id"], $item["meta"]);
                if (isset($item["enchantments"])) {
                    foreach ($item["enchantments"] as $enchantment) {
                        $enchantmentObject = Enchantment::getEnchantment($enchantment[0]);
                        if ($enchantmentObject instanceof Enchantment) {
                            $parsedItem->addEnchantment($enchantmentObject->setLevel($enchantment[1]));
                        }
                    }
                }
                if (isset($item["customName"])) {
                    $parsedItem->setCustomName($item["customName"]);
                }
                if (($item["count"] > $parsedItem->getMaxStackSize()) or ($item["id"] == Item::AIR and $item["count"] > 1) or ($item["id"] == Item::SPLASH_POTION and $item["count"] > 1)) {
                    for ($i = 1; $i <= $item["count"]; $i++) {
                        $parsedItems[] = $parsedItem;
                    }
                } else {
                    $parsedItem->setCount($item["count"]);
                    $parsedItems[] = $parsedItem;
                }
            }
            return $parsedItems;
        }
    
        /**
         * @param array $data
         * @return Item[]
         */
        public function parseArmor(array $data): array {
            $parsedArmor = [];
            $armor[] = $data["helmet"] ?? [0];
            $armor[] = $data["chestplate"] ?? [0];
            $armor[] = $data["leggings"] ?? [0];
            $armor[] = $data["boots"] ?? [0];
            foreach ($armor as $item) {
                $parsedItem = Item::get($item[0]);
                if (isset($item[1])) {
                    foreach ($item[1] as $enchantment) {
                        $enchantmentObject = Enchantment::getEnchantment($enchantment[0]);
                        if ($enchantmentObject instanceof Enchantment) {
                            $parsedItem->addEnchantment($enchantmentObject->setLevel($enchantment[1]));
                        }
                    }
                }
                $parsedArmor[] = $parsedItem;
            }
            return $parsedArmor;
        }
    
        /**
         * @return Config
         */
        public function getConfig(): Config {
            return $this->config;
        }
    
        /**
         * @return Kit[]
         */
        public function getKits(): array {
            return $this->kits;
        }
    
        /**
         * @return ArenaPvP
         */
        public function getPlugin(): ArenaPvP {
            return $this->plugin;
        }
    
        /**
         * @return Item[]
         */
        public function getAllKitItems(): array {
            $items = [];
            foreach ($this->getKits() as $kit) {
                $items[] = $kit->getIcon();
            }
            return $items;
        }
    
    }
    
    The Error

    Code:
    [04:00:46] [Server thread/CRITICAL]: Error: "Call to undefined method pocketmine\item\enchantment\ProtectionEnchantment::setLevel()" (EXCEPTION) in "plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager" at line 131
    [04:00:46] [Server thread/DEBUG]: #0 plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager(43): sys\arenapvp\kit\KitManager->parseArmor(array Array())
    [04:00:46] [Server thread/DEBUG]: #1 plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager(38): sys\arenapvp\kit\KitManager->loadKits()
    [04:00:46] [Server thread/DEBUG]: #2 plugins/ArenaPvP-master/src/sys/arenapvp/ArenaPvP(86): sys\arenapvp\kit\KitManager->construct(sys\arenapvp\ArenaPvP object)
    [04:00:46] [Server thread/DEBUG]: #3 plugins/ArenaPvP-master/src/sys/arenapvp/ArenaPvP(55): sys\arenapvp\ArenaPvP->loadKitManager()
    [04:00:46] [Server thread/DEBUG]: #4 src/pocketmine/plugin/PluginBase(90): sys\arenapvp\ArenaPvP->onEnable()
    [04:00:46] [Server thread/DEBUG]: #5 plugins/PocketMine-DevTools.phar/src/FolderPluginLoader/FolderPluginLoader(127): pocketmine\plugin\PluginBase->setEnabled(boolean 1)
    [04:00:46] [Server thread/DEBUG]: #6 src/pocketmine/plugin/PluginManager(572): FolderPluginLoader\FolderPluginLoader->enablePlugin(sys\arenapvp\ArenaPvP object)
    [04:00:46] [Server thread/DEBUG]: #7 src/pocketmine/Server(1962): pocketmine\plugin\PluginManager->enablePlugin(sys\arenapvp\ArenaPvP object)
    [04:00:46] [Server thread/DEBUG]: #8 src/pocketmine/Server(1948): pocketmine\Server->enablePlugin(sys\arenapvp\ArenaPvP object)
    [04:00:46] [Server thread/DEBUG]: #9 src/pocketmine/Server(1742): pocketmine\Server->enablePlugins(integer 1)
    [04:00:46] [Server thread/DEBUG]: #10 src/pocketmine/PocketMine(385): pocketmine\Server->construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string C:\Users\Aaman\Desktop\new, string C:\Users\Aaman\Desktop\new\plugins\)[04:00:46] [Server thread/INFO]: Disabling ArenaPvP v1.2.0
     
  2. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    try this
    PHP:
    $parsedItem->addEnchantment($enchantmentObject)->setLevel($enchantment[1]);
     
  3. LikeZCodes

    LikeZCodes Spider

    Messages:
    6
    It comes up with this error.

    Code:
    [14:56:02] [Server thread/INFO]: [ArenaPvP] There are no arenas to load!
    [14:56:02] [Server thread/CRITICAL]: TypeError: "Argument 1 passed to pocketmine\item\Item::addEnchantment() must be an instance of pocketmine\item\enchantment\EnchantmentInstance, instance of pocketmine\item\enchantment\ProtectionEnchantment given, called in /root/zehox/plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager.php on line 132" (EXCEPTION) in "src/pocketmine/item/Item" at line 363
    [14:56:02] [Server thread/DEBUG]: #0 plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager(132): pocketmine\item\Item->addEnchantment(pocketmine\item\enchantment\ProtectionEnchantment object)
    [14:56:02] [Server thread/DEBUG]: #1 plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager(44): sys\arenapvp\kit\KitManager->parseArmor(array Array())
    [14:56:02] [Server thread/DEBUG]: #2 plugins/ArenaPvP-master/src/sys/arenapvp/kit/KitManager(39): sys\arenapvp\kit\KitManager->loadKits()
    [14:56:02] [Server thread/DEBUG]: #3 plugins/ArenaPvP-master/src/sys/arenapvp/ArenaPvP(86): sys\arenapvp\kit\KitManager->__construct(sys\arenapvp\ArenaPvP object)
    [14:56:02] [Server thread/DEBUG]: #4 plugins/ArenaPvP-master/src/sys/arenapvp/ArenaPvP(55): sys\arenapvp\ArenaPvP->loadKitManager()
    [14:56:02] [Server thread/DEBUG]: #5 src/pocketmine/plugin/PluginBase(90): sys\arenapvp\ArenaPvP->onEnable()
    [14:56:02] [Server thread/DEBUG]: #6 plugins/PocketMine-DevTools.phar/src/FolderPluginLoader/FolderPluginLoader(127): pocketmine\plugin\PluginBase->setEnabled(boolean 1)
    [14:56:02] [Server thread/DEBUG]: #7 src/pocketmine/plugin/PluginManager(572): FolderPluginLoader\FolderPluginLoader->enablePlugin(sys\arenapvp\ArenaPvP object)
    [14:56:02] [Server thread/DEBUG]: #8 src/pocketmine/Server(1962): pocketmine\plugin\PluginManager->enablePlugin(sys\arenapvp\ArenaPvP object)
    [14:56:02] [Server thread/DEBUG]: #9 src/pocketmine/Server(1948): pocketmine\Server->enablePlugin(sys\arenapvp\ArenaPvP object)
    [14:56:02] [Server thread/DEBUG]: #10 src/pocketmine/Server(1742): pocketmine\Server->enablePlugins(integer 1)
    [14:56:02] [Server thread/DEBUG]: #11 src/pocketmine/PocketMine(385): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string /root/zehox/, string /root/zehox/plugins/)
    
     
  4. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    PHP:
    $parsedItem->addEnchantment(new EnchantmentInstance($enchantmentObject$enchantment[1]));
     
    kenygamer likes this.
  5. LikeZCodes

    LikeZCodes Spider

    Messages:
    6
    Thanks for the help! But now I get this.

     
Thread Status:
Not open for further replies.
  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.