Added /ecoenchants giverandombook <player> <type/rarity> <minLevel> <maxLevel> command

This commit is contained in:
_OfTeN_ 2022-09-26 23:26:16 +03:00
parent 1aa9118928
commit 4e193ca36b
4 changed files with 92 additions and 0 deletions

View File

@ -14,5 +14,6 @@ class CommandEcoEnchants(plugin: EcoPlugin) : PluginCommand(plugin, "ecoenchants
init {
addSubcommand(CommandReload(plugin))
.addSubcommand(CommandToggleDescriptions(plugin))
.addSubcommand(CommandGiveRandomBook(plugin))
}
}

View File

@ -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<String>) {
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<String>): List<String> {
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())
}
}

View File

@ -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"

View File

@ -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