From a5bab212977980901b30915101d76962a46b2081 Mon Sep 17 00:00:00 2001 From: Xeno Date: Sat, 21 Mar 2020 10:52:31 -0500 Subject: [PATCH] Update v1.2.0 Added custom enchantment levels --- README.md | 2 +- plugin.yml | 2 +- resources/config.yml | 39 +++++++++++++-- src/Xenophilicy/MaxEnchants/MaxEnchants.php | 54 ++++++++++++++++----- 4 files changed, 80 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a9f7103..3cd3829 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ MaxEnchants is a simple plugin to allow servers to take full advantage of the en ## MaxEnchants Details * **API:** 3.0.0+ -* **Version:** 1.1.0 +* **Version:** 1.2.0 * **Basic Description:** Enchant items up to level 32k! * *Simple code for editing and debugging* *** diff --git a/plugin.yml b/plugin.yml index ab65782..bc79b2d 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: MaxEnchants author: Xenophilicy -version: 1.1.0 +version: 1.2.0 main: Xenophilicy\MaxEnchants\MaxEnchants api: 3.0.0 description: Enable enchants up to level 32k! diff --git a/resources/config.yml b/resources/config.yml index 24194ba..3143f86 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -11,18 +11,28 @@ # $$ | / \__$$ | # $$ | $$ $$/ # $$/ $$$$$$/ -VERSION: "1.1.0" # Internal use only +VERSION: "1.2.0" # Internal use only # This is the max allowed enchanting level with the command in-game # Minecraft: Bedrock Edition has a maximum of 32767 enchantment levels +# This will be used as a hard limit for any enchantment not listed in the custom levels Max-Level: 5000 +# Here you can list enchantments that should have specific max levels +# Set to false if you want all enchantments to use the same max level +# Format: enchantmentID:maxLevel +# A list of enchantment IDs is supplied at the bottom of this file +Custom-Max-Levels: + 4: 3500 # Projectile Protection (4) has a max level of 3500 + 15: 7000 # Efficiency (15) has a max level of 7000 + 22: 1 # Infinity (22) has a max level of 1 + # Here you can specify enchantment broadcast message settings Broadcast: # Choose who should receive the broadcast messages # Options are sender, target, console - # Add any number of recipients to this key + # Add any combination of recipients to this key # Leave blank if you want no broadcasts SendTo: - "sender" @@ -42,7 +52,7 @@ Command: Name: "/enchant" # This is the command's description shown in the command window - Description: "Enchant items up to level 5000" + Description: "Adds enchantments on items" Permission: @@ -54,4 +64,25 @@ Command: # This is an option to override an existing command with the one from MaxEnchants # Keeping this true makes sure that there are not two types of the same command - Override: true \ No newline at end of file + Override: true + +# Implemented enchantment IDs +# Protection - 0 +# Fire Protection - 1 +# Feather Falling - 2 +# Blast Protection - 3 +# Projectile Protection - 4 +# Thorns - 5 +# Respiration - 6 +# Sharpness - 9 +# Knockback - 12 +# Fire Aspect - 13 +# Efficiency - 15 +# Silk Touch - 16 +# Unbreaking - 17 +# Power - 19 +# Punch - 20 +# Flame - 21 +# Infinity - 22 +# Mending - 26 +# Vanishing - 28 \ No newline at end of file diff --git a/src/Xenophilicy/MaxEnchants/MaxEnchants.php b/src/Xenophilicy/MaxEnchants/MaxEnchants.php index 69a51ba..bad7f36 100644 --- a/src/Xenophilicy/MaxEnchants/MaxEnchants.php +++ b/src/Xenophilicy/MaxEnchants/MaxEnchants.php @@ -32,12 +32,12 @@ public function onEnable(){ $this->config->getAll(); $version = $this->config->get("VERSION"); $this->pluginVersion = $this->getDescription()->getVersion(); - if($version < "1.1.0"){ + if($version < "1.2.0"){ $this->getLogger()->warning("You have updated MaxEnchants to v".$this->pluginVersion." but have a config from v$version! Please delete your old config for new features to be enabled and to prevent unwanted errors! Plugin will remain disabled..."); $pluginManager->disablePlugin($this); } $include = $this->config->getNested("Broadcast.SendTo"); - if($include !== "" && $include !== null){ + if($include !== "" && !is_null($include)){ foreach($include as $inclusion){ if(strtolower($inclusion) == "console" || strtolower($inclusion) == "sender" || strtolower($inclusion) == "target"){ continue; @@ -48,9 +48,27 @@ public function onEnable(){ } } } + $this->buildVanillaEnchantArray(); + $maxLevels = $this->config->get("Custom-Max-Levels"); + if($maxLevels !== false){ + if(is_null($maxLevels) || $maxLevels == "" || !is_array($maxLevels)){ + $this->getLogger()->critical("Invalid custom max levels array found, disabling plugin..."); + $pluginManager->disablePlugin($this); + return; + } else{ + foreach($maxLevels as $id => $level){ + if(!is_int($id) || !is_int($level) || !$this->isValidEnchant($id)){ + $this->getLogger()->warning("Invalid max level found at $id, it will not be included!"); + } else{ + $enchantment = Enchantment::getEnchantment($id); + $this->customMaxLevels[$enchantment->getName()] = $level; + } + } + } + } $this->maxLevel = $this->config->get("Max-Level") >= 32767 ? 32767:$this->config->get("Max-Level"); $this->cmdName = str_replace("/","",$this->config->getNested("Command.Name")); - if($this->cmdName == null || $this->cmdName == ""){ + if(is_null($this->cmdName) || $this->cmdName == ""){ $this->getLogger()->critical("Invalid enchant command string found, disabling plugin..."); $pluginManager->disablePlugin($this); return; @@ -72,7 +90,15 @@ public function onEnable(){ } $this->getServer()->getCommandMap()->register("MaxEnchants", $cmd, $this->cmdName); } - $this->buildVanillaEnchantArray(); + } + + private function isValidEnchant($id) : bool{ + foreach(array_values($this->vanillaEnchants) as $ench){ + if($ench[1] === $id){ + return true; + } + } + return false; } private function buildVanillaEnchantArray() : void{ @@ -86,7 +112,7 @@ private function buildVanillaEnchantArray() : void{ } $enchantment = Enchantment::getEnchantment($id); if($enchantment instanceof Enchantment){ - $this->vanillaEnchants[$enchantment->getName()] = ucwords(strtolower(str_replace("_", " ", $name))); + $this->vanillaEnchants[$enchantment->getName()] = [ucwords(strtolower(str_replace("_", " ", $name))), $lastId]; } } return; @@ -118,12 +144,18 @@ private function enchantItem(CommandSender $sender, array $args) : void{ if(!is_numeric($args[2])){ $sender->sendMessage(TF::RED."Enchantment level must be numeric"); return; - } elseif(($desiredLevel = (int) $args[2]) > $this->maxLevel){ - $sender->sendMessage(TF::RED."Enchantment level must be less than ".TF::YELLOW.$this->maxLevel); - return; - } elseif($desiredLevel < 1){ + } elseif(($desiredLevel = (int) $args[2]) < 1){ $sender->sendMessage(TF::RED."Enchantment level must be greater than ".TF::YELLOW."1"); return; + } + if(in_array($enchantment->getName(), array_keys($this->customMaxLevels))){ + $max = $this->customMaxLevels[$enchantment->getName()]; + } else{ + $max = $this->maxLevel; + } + if($desiredLevel > $max){ + $sender->sendMessage(TF::RED."Enchantment level can be at most ".TF::YELLOW.$max); + return; } else{ $level = $args[2]; } @@ -132,7 +164,7 @@ private function enchantItem(CommandSender $sender, array $args) : void{ } $item->addEnchantment(new EnchantmentInstance($enchantment, $level)); $player->getInventory()->setItemInHand($item); - $enchantmentName = $this->vanillaEnchants[$enchantment->getName()] ?? $enchantment->getName(); + $enchantmentName = $this->vanillaEnchants[$enchantment->getName()][0] ?? $enchantment->getName(); $this->broadcast($enchantmentName, $level, $player, $sender); return; } @@ -149,7 +181,7 @@ private function broadcast(string $name, string $level, Player $target, $sender) $msgString = str_replace("{sender}", "CONSOLE", $msgString); } $msgString = str_replace("&", "ยง", $msgString); - if($include !== "" && $include !== null && $include !== []){ + if($include !== "" && !is_null($include) && $include !== []){ $include = strtolower(implode(",", $include)); if(strpos($include, "console") !== false){ $this->getLogger()->info(TF::clean($msgString));