diff --git a/README.md b/README.md index 1037d9d..80690a0 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,12 @@ With this plugin, you are able to assign different effects to items so that when ## Item Formatting ```yaml -"Item-ID:Item-Damage": - - [Effect-ID,Effect-Amplifier,Effect-Duration] - - [Effect-ID,Effect-Amplifier,Effect-Duration] - - [Effect-ID,Effect-Amplifier,Effect-Duration] +"Item-ID:Item-Damage: + "Name": "Custom-Name" + "Effects": + - [Effect-ID,Effect-Amplifier,Effect-Duration] + - [Effect-ID,Effect-Amplifier,Effect-Duration] + - [Effect-ID,Effect-Amplifier,Effect-Duration] ``` ## Item Listing Example @@ -28,32 +30,30 @@ Desired configuration: Configuration in config.yml: ```yaml Consumables: - "364": - - [15,1,10] - - [5,2,30] - "260": - - [1,3,15] - - [16,1,0] + "364:0": + "Name": "§aAwesome §6Steak" + "Effects": + - [15,1,10] + - [5,2,30] + "260:0": + "Effects": + - [1,3,15] + - [16,1,0] Non-Consumables: - "6:3": - - [2,1,60] - - [12,1,30] - - [1,3,5] - "40": - - [2,1,5] + "6:3": + "Effects": + - [2,1,60] + - [12,1,30] + - [1,3,5] + "40:0": + "Name": "§bLuck §cMushroom" + "Effects": + - [2,1,5] ``` *** -## FoodEffects Details -* **API:** 3.0.0+ -* **Version:** 1.1.0 -* **Basic Description:** Enables customizable effects for interacting with and consuming items -* *Easy to edit config.yml file* -* *Simple code for editing and debugging* -*** - ## Dependencies **DevTools → https://github.com/pmmp/PocketMine-DevTools** *(If you're running the plugin from source)* diff --git a/plugin.yml b/plugin.yml index f63fbf7..3ce69a7 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: FoodEffects author: Xenophilicy -version: 1.1.0 +version: 1.2.0 main: Xenophilicy\FoodEffects\FoodEffects api: 3.0.0 Description: Enables customizable effects for interacting with and consuming items diff --git a/resources/config.yml b/resources/config.yml index 007bf61..8cb5174 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -11,37 +11,54 @@ # $$ | / \__$$ | # $$ | $$ $$/ # $$/ $$$$$$/ -VERSION: "1.1.0" # Internal use only +VERSION: "1.2.0" # Internal use only + +# Choose if a player can consume food items even if their hunger bar is full +# Accepted values are true or false +Require-Hunger: true + +# Choose whether or not a player's hunger should increase/decrease when consuming food items +# Accepted values are true or false +Affects-Hunger: true # This is the syntax for adding effects to items # You can use any number of effects # If you want an infinite duration for an effect, set it to 0 # Maximum amplifier for effects is 255 # See the included examples and format below for more clarification +# You can also add a custom name to check for or leave it out to just use the ID and damage # Add items using this format: -# "Item-ID:Item-Damage": -# - [Effect-ID,Effect-Amplifier,Effect-Duration] -# - [Effect-ID,Effect-Amplifier,Effect-Duration] -# - [Effect-ID,Effect-Amplifier,Effect-Duration] +# "Item-ID:Item-Damage: +# "Name": "Custom-Name" +# "Effects": +# - [Effect-ID,Effect-Amplifier,Effect-Duration] +# - [Effect-ID,Effect-Amplifier,Effect-Duration] +# - [Effect-ID,Effect-Amplifier,Effect-Duration] # These effects will be added when a player CONSUMES an item Consumables: - "364": # When steak(ID = 364) is consumed: - - [15,1,10] # Give blindness(ID = 15) level 1 for 10 seconds - - [5,2,30] # Give strength(ID = 5) level 2 for 30 seconds - "260": # When an apple(ID = 260) is consumed: - - [1,3,15] # Give speed(ID = 1) level 3 for 15 seconds - - [16,1,0] # Give night vision(ID = 16) level 1 infinitely(0) + "364:0": # When steak(ID = 364) is consumed: + "Name": "§aAwesome §6Steak" # Check for this custom name + "Effects": + - [15,1,10] # Give blindness(ID = 15) level 1 for 10 seconds + - [5,2,30] # Give strength(ID = 5) level 2 for 30 seconds + "260:0": # When an apple(ID = 260) is consumed: + "Effects": + - [1,3,15] # Give speed(ID = 1) level 3 for 15 seconds + - [16,1,0] # Give night vision(ID = 16) level 1 infinitely(0) # These effects will be added when a player INTERACTS with the item Non-Consumables: "6:3": # When interacting with a jungle sapling(ID = 6:3): - - [2,1,60] # Give slowness(ID = 2) level 1 for 60 seconds - - [12,1,30] # Give fire resistance(ID = 12) level 1 for 30 seconds - - [1,3,5] # Give speed(ID = 1) level 3 for 5 seconds - "40": # When interacting with a red mushroom(ID = 40): - - [2,1,5] # Give slowness(ID = 2) level 1 for 5 seconds + "Effects": + - [2,1,60] # Give slowness(ID = 2) level 1 for 60 seconds + - [12,1,30] # Give fire resistance(ID = 12) level 1 for 30 seconds + - [1,3,5] # Give speed(ID = 1) level 3 for 5 seconds + "40:0": # When interacting with a red mushroom(ID = 40): + "Name": "§bLuck §cMushroom" # Check for this custom name + "Effects": + - [2,1,5] # Give slowness(ID = 2) level 1 for 5 seconds # Here's a list of all available effects with their IDs # ABSORPTION = 22 diff --git a/src/Xenophilicy/FoodEffects/FoodEffects.php b/src/Xenophilicy/FoodEffects/FoodEffects.php index 3a43ec9..a30c1c2 100644 --- a/src/Xenophilicy/FoodEffects/FoodEffects.php +++ b/src/Xenophilicy/FoodEffects/FoodEffects.php @@ -17,39 +17,52 @@ use pocketmine\plugin\PluginBase; use pocketmine\event\Listener; -use pocketmine\utils\Config; use pocketmine\event\player\{PlayerItemConsumeEvent,PlayerInteractEvent}; use pocketmine\entity\{Effect,EffectInstance}; class FoodEffects extends PluginBase implements Listener { - private $config; - private $consumables; - private $nonConsumables; + private const CONFIG_VERSION = "1.2.0"; + + private static $settings; + private static $consumables; + private static $nonConsumables; public function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); - $this->saveResource("config.yml"); - $this->config = new Config($this->getDataFolder()."config.yml", Config::YAML); - $version = $this->config->get("VERSION"); - if($version != "1.1.0"){ + $this->saveDefaultConfig(); + self::$settings = $this->getConfig()->getAll(); + $configVersion = self::$settings["VERSION"]; + if(version_compare(self::CONFIG_VERSION, $configVersion, "gt")){ $this->getLogger()->critical("You have updated FoodEffects but have an outdated config! Please delete your old config to continue using FoodEffects!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } $oldFile = $this->getDataFolder()."items.json"; if(file_exists($oldFile)){ - $this->getLogger()->critical("You have updated FoodEffects but are still using an outdated file. Please tranfer your entries from 'items.json' to the new config file using the provided format to continue using FoodEffects!"); + $this->getLogger()->critical("You have updated FoodEffects but are still using an outdated file. Please transfer your entries from 'items.json' to the new config file using the provided format to continue using FoodEffects!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } - $this->consumables = $this->config->get("Consumables"); - $this->nonConsumables = $this->config->get("Non-Consumables"); - $itemGroups = [$this->consumables,$this->nonConsumables]; + self::$consumables = $this->getConfig()->get("Consumables"); + self::$nonConsumables = $this->getConfig()->get("Non-Consumables"); + $itemGroups = [self::$consumables, self::$nonConsumables]; foreach($itemGroups as $itemGroup){ - foreach($itemGroup as $key => $effects){ - if(!is_numeric($key) && strpos($key, ":") > 1){ - $this->getLogger()->warning("Invalid item ID found! Invalid item ID: ".$key." not supported, disabling plugin!"); + foreach($itemGroup as $item => $values){ + $effects = $values["Effects"]; + $name = isset($values["Name"]) ? $values["Name"] : false; + if($name && !is_string($name)){ + $this->getLogger()->warning("Invalid item name found! Item names must be strings, disabling plugin!"); + $this->getServer()->getPluginManager()->disablePlugin($this); + return; + } + if(strpos($item, ":") === false || count($itemVal = explode(":", $item)) !== 2){ + $this->getLogger()->warning("Invalid item ID found! Items must have an ID and a damage value, disabling plugin!"); + $this->getServer()->getPluginManager()->disablePlugin($this); + return; + } + if(!is_numeric($itemVal[0]) || !is_numeric($itemVal[1])){ + $this->getLogger()->warning("Invalid item ID found! Invalid item ID: ".$item." not supported, disabling plugin!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } @@ -57,13 +70,13 @@ public function onEnable(){ $subscripts = [0,1,2]; foreach($subscripts as $subscript){ if(!isset($effValues[$subscript])){ - $this->getLogger()->warning("Invalid effect string found, you must have 3 values for each effect. Effect for item ID: ".$key." is invalid, disabling plugin!"); + $this->getLogger()->warning("Invalid effect string found, you must have 3 values for each effect. Effect for item ID: ".$item." is invalid, disabling plugin!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } } if(!is_numeric($effValues[0])){ - $this->getLogger()->warning("Invalid effect string found, all effect related arguments must be numerical! Invalid argument: ".$value[0]." not supported, disabling plugin!"); + $this->getLogger()->warning("Invalid effect string found, all effect related arguments must be numerical! Invalid argument: ".$effValues[0]." not supported, disabling plugin!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; } @@ -74,7 +87,7 @@ public function onEnable(){ $this->getServer()->getPluginManager()->disablePlugin($this); return; } - } else{ + }else{ $this->getLogger()->warning("Invalid effect ID found, please see the ID list in the config.yml! Effect: ".$effValues[0]." not found, disabling plugin!"); $this->getServer()->getPluginManager()->disablePlugin($this); return; @@ -89,15 +102,19 @@ public function onItemConsume(PlayerItemConsumeEvent $event){ $player = $event->getPlayer(); $item = $event->getItem(); $idFull = $item->getId().":".$item->getDamage(); - $id = $item->getId(); - if($player->getFood() < 20){ - foreach($this->consumables as $key => $effects){ - if($idFull == (string)$key || $id == (string)$key){ - foreach($effects as $effValues){ - $effectInstance = new EffectInstance(Effect::getEffect($effValues[0])); - $player->addEffect($effectInstance->setAmplifier($effValues[1])->setDuration($effValues[2]*20)); - } - } + var_dump(round($player->getFood()) < 20); + if(!(round($player->getFood()) < 20) && self::$settings["Require-Hunger"]) return; + foreach(self::$consumables as $key => $values){ + $effects = $values["Effects"]; + $name = isset($values["Name"]) ? $values["Name"] : false; + if($name && $item->getCustomName() !== $name) continue; + if($idFull !== (string)$key) continue; + if(!self::$settings["Affects-Hunger"]) $event->setCancelled(); + var_dump($effects); + foreach($effects as $effValues){ + $effectInstance = new EffectInstance(Effect::getEffect($effValues[0])); + $duration = $effValues[2] > 0 ? $effValues[2]*20 : 2147483647; + $player->addEffect($effectInstance->setAmplifier($effValues[1])->setDuration($duration)); } } } @@ -105,14 +122,17 @@ public function onItemConsume(PlayerItemConsumeEvent $event){ public function onItemInteract(PlayerInteractEvent $event){ $player = $event->getPlayer(); $item = $event->getItem(); + $item->pop(); $idFull = $item->getId().":".$item->getDamage(); - $id = $item->getId(); - foreach($this->nonConsumables as $key => $effects){ - if($idFull == (string)$key || $id == (string)$key){ - foreach($effects as $effValues){ - $effectInstance = new EffectInstance(Effect::getEffect($effValues[0])); - $player->addEffect($effectInstance->setAmplifier($effValues[1])->setDuration($effValues[2]*20)); - } + foreach(self::$nonConsumables as $key => $values){ + $effects = $values["Effects"]; + $name = isset($values["Name"]) ? $values["Name"] : false; + if($name && $item->getCustomName() !== $name) continue; + if($idFull !== (string)$key) continue; + foreach($effects as $effValues){ + $effectInstance = new EffectInstance(Effect::getEffect($effValues[0])); + $duration = $effValues[2] > 0 ? $effValues[2]*20 : 2147483647; + $player->addEffect($effectInstance->setAmplifier($effValues[1])->setDuration($duration)); } } }