From 4e193ca36be8d8aea580e1dbb7ef14e4ddbdc734 Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Mon, 26 Sep 2022 23:26:16 +0300 Subject: [PATCH 1/5] Added /ecoenchants giverandombook command --- .../commands/CommandEcoEnchants.kt | 1 + .../commands/CommandGiveRandomBook.kt | 84 +++++++++++++++++++ .../core-plugin/src/main/resources/lang.yml | 3 + .../core-plugin/src/main/resources/plugin.yml | 4 + 4 files changed, 92 insertions(+) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt 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 From b2ad46cb477ed1e91aa8b21ba94be6eac2577d9b Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Tue, 27 Sep 2022 03:37:31 +0300 Subject: [PATCH 2/5] Little fix --- .../willfp/ecoenchants/commands/CommandGiveRandomBook.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 63616e3e..3bdd9e5e 100644 --- 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 @@ -47,17 +47,17 @@ class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand( return } - val level = (minLevel..maxLevel).random() + val level = (minLevel..maxLevel.coerceAtMost(enchantment.maxLevel)).random() player.inventory.addItem(EnchantedBookBuilder() - .addStoredEnchantment(enchantment, level.coerceAtMost(enchantment.maxLevel)) + .addStoredEnchantment(enchantment, level) .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)))) + .getFormattedName(level))) } override fun tabComplete(sender: CommandSender, args: List): List { From 131eb611953124c443dea3b7151ac6361558fa0b Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Mon, 26 Sep 2022 23:26:16 +0300 Subject: [PATCH 3/5] Added /ecoenchants giverandombook command --- .../commands/CommandEcoEnchants.kt | 1 + .../commands/CommandGiveRandomBook.kt | 84 +++++++++++++++++++ .../core-plugin/src/main/resources/lang.yml | 3 + .../core-plugin/src/main/resources/plugin.yml | 4 + 4 files changed, 92 insertions(+) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/commands/CommandGiveRandomBook.kt 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 From acfff341de805dce6e040b59e023c2d19af9606d Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Tue, 27 Sep 2022 03:37:31 +0300 Subject: [PATCH 4/5] Little fix --- .../willfp/ecoenchants/commands/CommandGiveRandomBook.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 63616e3e..3bdd9e5e 100644 --- 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 @@ -47,17 +47,17 @@ class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand( return } - val level = (minLevel..maxLevel).random() + val level = (minLevel..maxLevel.coerceAtMost(enchantment.maxLevel)).random() player.inventory.addItem(EnchantedBookBuilder() - .addStoredEnchantment(enchantment, level.coerceAtMost(enchantment.maxLevel)) + .addStoredEnchantment(enchantment, level) .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)))) + .getFormattedName(level))) } override fun tabComplete(sender: CommandSender, args: List): List { From 962bdf4c0820fc948f4ef9d86f681e05fa6bfcef Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Sat, 1 Oct 2022 13:07:34 +0300 Subject: [PATCH 5/5] Fixed snipe.yml --- eco-core/core-plugin/src/main/resources/enchants/snipe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eco-core/core-plugin/src/main/resources/enchants/snipe.yml b/eco-core/core-plugin/src/main/resources/enchants/snipe.yml index dd9cf743..affe0145 100644 --- a/eco-core/core-plugin/src/main/resources/enchants/snipe.yml +++ b/eco-core/core-plugin/src/main/resources/enchants/snipe.yml @@ -19,7 +19,7 @@ enchantable: true effects: - id: damage_multiplier args: - multiplier: "1 + (%distance% / (11 - ceil(%level / 2))) * 0.01 * %level%" + multiplier: "1 + (%distance% / (11 - ceil(%level% / 2))) * 0.01 * %level%" triggers: - bow_attack