diff --git a/README.md b/README.md index 9d9e678e..27d08fac 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ ca.bypass.level: Allow player to bypass every level limit (no custom limit) # Command permissions ca.command.reload: Allow administrator to reload the plugin's configs -ca.command.diagnostic: Allow adminastator to diagnistic some simple problem with the plugin +ca.command.diagnostic: Get debug information about the plugin and server +ca.command.enchantment: Allows to set enchantment to holden item ca.config.edit: Allow administrator to edit the plugin's config in game # ----------------------------------------------------------------------------- diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index a5cac722..906e023f 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -57,6 +57,8 @@ open class CustomAnvil : JavaPlugin() { // Permission string required to edit the plugin's config const val editConfigPermission = "ca.config.edit" + // Permission string required to edit the plugin's config + const val giveEnchantmentPermission = "ca.command.enchantment" // Command Name to reload the config const val commandReloadName = "anvilconfigreload" diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt index 4c71c68b..60dcdbee 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CASubCommand.kt @@ -4,6 +4,7 @@ import org.bukkit.ChatColor import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender +import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry abstract class CASubCommand: CommandExecutor { @@ -43,4 +44,14 @@ abstract class CASubCommand: CommandExecutor { return "no description" } + protected fun allEnchantmentsByName(): Collection { + val names = mutableSetOf() + for (enchantment in CAEnchantmentRegistry.getInstance().values()) { + names.add(enchantment.name) + names.add(enchantment.key.toString()) + } + + return names + } + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt index 9287a7c7..9b9402c9 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/CustomAnvilCommand.kt @@ -7,7 +7,6 @@ import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter import xyz.alexcrea.cuanvil.util.MetricsUtil -import java.util.ArrayList class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { @@ -18,13 +17,14 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { private val editConfigCommand = EditConfigExecutor() private val helpCommand = HelpExecutor() - private val commands = ImmutableMap.of( + private val commands: ImmutableMap = ImmutableMap.of( "gui", editConfigCommand, "config", editConfigCommand, "reload", ReloadExecutor(), "diagnostic", DiagnosticExecutor(), "debug", DebugToggleExecutor(), "help", helpCommand, + "enchant", EnchantExecutor(), ) init { @@ -95,9 +95,11 @@ class CustomAnvilCommand(plugin: CustomAnvil) : CommandExecutor, TabCompleter { } //assumed all provided tab completed string are lowercase + val prefix = args[args.size - 1] return result.stream() - .filter { it.startsWith(args[args.size - 1]) } + .filter { it.startsWith(prefix) } .sorted() .toList() } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt index b11bfb4a..c6bc191b 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EditConfigExecutor.kt @@ -7,7 +7,6 @@ import org.bukkit.entity.HumanEntity import xyz.alexcrea.cuanvil.api.EnchantmentApi import xyz.alexcrea.cuanvil.dependency.util.PlatformUtil import xyz.alexcrea.cuanvil.enchant.CAEnchantment -import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry import xyz.alexcrea.cuanvil.gui.config.MainConfigGui import xyz.alexcrea.cuanvil.gui.config.global.EnchantConfigGui import xyz.alexcrea.cuanvil.gui.config.global.ItemConfigGui @@ -103,16 +102,6 @@ class EditConfigExecutor : CASubCommand() { // Tab completer // --------------- - private fun allEnchantmentsByName(): Collection { - val names = mutableSetOf() - for (enchantment in CAEnchantmentRegistry.getInstance().values()) { - names.add(enchantment.name) - names.add(enchantment.key.toString()) - } - - return names - } - override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { list.addAll( when (args.size) { @@ -127,7 +116,6 @@ class EditConfigExecutor : CASubCommand() { else -> listOf() } ) - } } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/EnchantExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EnchantExecutor.kt new file mode 100644 index 00000000..dd2957fa --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/EnchantExecutor.kt @@ -0,0 +1,113 @@ +package xyz.alexcrea.cuanvil.command + +import io.delilaheve.CustomAnvil +import io.delilaheve.util.ConfigOptions +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.entity.HumanEntity +import xyz.alexcrea.cuanvil.api.EnchantmentApi +import xyz.alexcrea.cuanvil.util.MaterialUtil.isAir + +class EnchantExecutor : CASubCommand() { + + + override fun allowed(sender: CommandSender): Boolean { + return sender.hasPermission(CustomAnvil.giveEnchantmentPermission) + } + + override fun description(): String { + return "Allows to set enchantment to holden item" + } + + override fun executeCommand( + sender: CommandSender, + cmd: Command, + cmdstr: String, + args: Array + ): Boolean { + if (sender !is HumanEntity) return true + + if (!allowed(sender)) { + sender.sendMessage("No permission to execute this command") + return true + } + + when (args.size) { + 0 -> { + sender.sendMessage("Missing enchantment parameter") + return true + } + + 1 -> { + sender.sendMessage("Missing level parameter") + return true + } + } + + val enchants = EnchantmentApi.getListByName(args[0].lowercase()) + if (enchants.isEmpty()) { + sender.sendMessage("Enchantment not found: ${args[0]}") + return true + } + + val enchant = enchants.iterator().next() + + val level = args[1].toIntOrNull()?.coerceIn(0, ConfigOptions.ENCHANT_LIMIT) + if (level == null) { + sender.sendMessage("Invalid number: ${args[1]}") + return true + } + + val inHand = sender.inventory.itemInMainHand + if (inHand.isAir) { + sender.sendMessage("Cannot enchant this item") + return true + } + + if (level == 0) { + enchant.removeFrom(inHand) + sender.sendMessage("${enchant.prettyName} removed") + } else { + enchant.addEnchantmentUnsafe(inHand, level) + sender.sendMessage("${enchant.prettyName} set to level $level") + } + + return true + } + + override fun tabCompleter(sender: CommandSender, args: Array, list: MutableList) { + if (sender !is HumanEntity) return + + list.addAll( + when (args.size) { + 1 -> allEnchantmentsByName() + 2 -> findEnchantmentLevels(sender, args[0]) + + else -> listOf() + } + ) + } + + private fun findEnchantmentLevels( + sender: HumanEntity, + name: String + ): Collection { + val enchants = EnchantmentApi.getListByName(name.lowercase()) + if (enchants.isEmpty()) return listOf() + + val enchant = enchants.iterator().next() + val limit = ConfigOptions.enchantLimit(enchant) + val result = mutableListOf() + + for (i in 1 until limit + 1) { + result.add(i.toString()) + } + + val inHand = sender.inventory.itemInMainHand + if (enchant.isEnchantmentPresent(inHand)) + result.add("0") + + return result + } + +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt index ef23e7d6..cb0d24a9 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/command/ReloadExecutor.kt @@ -3,7 +3,6 @@ package xyz.alexcrea.cuanvil.command import io.delilaheve.CustomAnvil import org.bukkit.Bukkit import org.bukkit.command.Command -import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import xyz.alexcrea.cuanvil.api.event.CAConfigReadyEvent import xyz.alexcrea.cuanvil.config.ConfigHolder diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index dab9997b..d436b10a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -43,6 +43,9 @@ permissions: ca.command.diagnostic: default: op description: Get debug information about the plugin and server + ca.command.enchantment: + default: op + description: Allows to set enchantment to holden item ca.config.edit: default: op description: Allow administrator to edit the plugin's config in game