Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Update v1.2.0
Browse files Browse the repository at this point in the history
Added custom enchantment levels
  • Loading branch information
Xenophilicy committed Mar 21, 2020
1 parent eec13e1 commit a5bab21
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
***
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: 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!
Expand Down
39 changes: 35 additions & 4 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:

Expand All @@ -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
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
54 changes: 43 additions & 11 deletions src/Xenophilicy/MaxEnchants/MaxEnchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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{
Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}
Expand All @@ -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));
Expand Down

0 comments on commit a5bab21

Please sign in to comment.