Skip to content
This repository has been archived by the owner on Jan 1, 2021. It is now read-only.

Commit

Permalink
Added custom names, fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenophilicy committed Jul 1, 2020
1 parent 3ba4d6c commit 0c50256
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 75 deletions.
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)*
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 33 additions & 16 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 54 additions & 34 deletions src/Xenophilicy/FoodEffects/FoodEffects.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,66 @@

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;
}
foreach($effects as $effValues){
$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;
}
Expand All @@ -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;
Expand All @@ -89,30 +102,37 @@ 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));
}
}
}

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));
}
}
}
Expand Down

0 comments on commit 0c50256

Please sign in to comment.