diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEcoEnchants.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEcoEnchants.kt index 15635fc0..2ae4d365 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEcoEnchants.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandEcoEnchants.kt @@ -14,5 +14,6 @@ class CommandEcoEnchants(plugin: EcoPlugin) : PluginCommand(plugin, "ecoenchants init { addSubcommand(CommandReload(plugin)) .addSubcommand(CommandToggleDescriptions(plugin)) + .addSubcommand(CommandGiveRandomBook(plugin)) } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt new file mode 100644 index 00000000..63616e3e --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt @@ -0,0 +1,84 @@ +package com.willfp.ecoenchants.commands + +import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.command.impl.PluginCommand +import com.willfp.eco.core.items.builder.EnchantedBookBuilder +import com.willfp.ecoenchants.display.getFormattedName +import com.willfp.ecoenchants.enchants.EcoEnchants +import com.willfp.ecoenchants.enchants.wrap +import com.willfp.ecoenchants.rarity.EnchantmentRarities +import com.willfp.ecoenchants.rarity.EnchantmentRarity +import com.willfp.ecoenchants.type.EnchantmentType +import com.willfp.ecoenchants.type.EnchantmentTypes +import org.bukkit.Bukkit +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + + +class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand( + plugin, "giverandombook", "ecoenchants.command.giverandombook", false +) { + override fun onExecute(sender: CommandSender, args: List) { + val player = (args.firstOrNull()?: run { sender + .sendMessage(plugin.langYml.getMessage("requires-player")); return }).asPlayer()?: run { + sender.sendMessage(plugin.langYml.getMessage("invalid-player")) + return + } + + val filter = args.getOrNull(1)?.asRarity()?: args.getOrNull(1)?.asType() + val minLevel = args.getOrNull(2)?.toIntOrNull()?: 1 + val maxLevel = args.getOrNull(3)?.toIntOrNull()?: Int.MAX_VALUE + + if (minLevel > maxLevel) { + sender.sendMessage(plugin.langYml.getMessage("invalid-levels")) + return + } + + val enchantment = EcoEnchants.values() + .filter { + when (filter) { + is EnchantmentRarity -> it.enchantmentRarity == filter + is EnchantmentType -> it.type == filter + else -> true + } && it.maxLevel >= minLevel + } + .randomOrNull()?: run { + sender.sendMessage(plugin.langYml.getMessage("no-enchantments-found")) + return + } + + val level = (minLevel..maxLevel).random() + + player.inventory.addItem(EnchantedBookBuilder() + .addStoredEnchantment(enchantment, level.coerceAtMost(enchantment.maxLevel)) + .build()).values.forEach { + player.world.dropItem(player.location, it) + } + sender.sendMessage(plugin.langYml.getMessage("random-book-given") + .replace("%playername%", player.name) + .replace("%enchantment%", enchantment.wrap() + .getFormattedName(level.coerceAtMost(enchantment.maxLevel)))) + } + + override fun tabComplete(sender: CommandSender, args: List): List { + return when (args.size) { + 1 -> Bukkit.getOnlinePlayers().map { it.name } + 2 -> (EnchantmentRarities.values().map { it.id } + EnchantmentTypes.values().map { it.id }) + 3 -> (1..10).map { it.toString() } + 4 -> ((args[3].toIntOrNull()?: 1)..(args[3].toIntOrNull()?: 1)+10).map { it.toString() } + else -> emptyList() + } + } + + private fun String.asPlayer(): Player? { + return Bukkit.getPlayer(this) + } + + private fun String.asRarity(): EnchantmentRarity? { + return EnchantmentRarities.getByID(this.lowercase()) + } + + private fun String.asType(): EnchantmentType? { + return EnchantmentTypes.getByID(this.lowercase()) + } +} diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index fc40c5a3..c449e13c 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -14,6 +14,9 @@ messages: descriptions-disabled: "&cEnchantment descriptions are disabled on this server." not-found: "&cCannot find an enchantment matching name: &f%name%." missing-enchant: "&cYou must specify an enchantment!" + random-book-given: "&fYou have been given &a%playername%&f a &r%enchantment%&f random book!" + no-enchantments-found: "&cNo enchantments found!" + invalid-levels: "&cMinimum level can't be higher than maximum level!" all: "All" no-conflicts: "No Conflicts" \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 22306566..71049711 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -45,6 +45,7 @@ permissions: ecoenchants.command.reload: true ecoenchants.command.ecoenchants: true ecoenchants.command.toggledescriptions: true + ecoenchants.command.giverandombook: true ecoenchants.command.enchantinfo: true ecoenchants.anvil.*: description: All anvil perks @@ -58,6 +59,9 @@ permissions: ecoenchants.command.reload: description: Allows reloading the config default: op + ecoenchants.command.giverandombook: + description: Allows giving random books + default: op ecoenchants.command.ecoenchants: description: Allows the use of /ecoenchants. default: true