PR codestyle

This commit is contained in:
Auxilor 2022-10-03 13:27:46 +01:00
parent 8728f080fb
commit 0081efce98
2 changed files with 41 additions and 34 deletions

View File

@ -2,7 +2,9 @@ package com.willfp.ecoenchants.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.drops.DropQueue
import com.willfp.eco.core.items.builder.EnchantedBookBuilder
import com.willfp.eco.util.NumberUtils
import com.willfp.ecoenchants.display.getFormattedName
import com.willfp.ecoenchants.enchants.EcoEnchants
import com.willfp.ecoenchants.enchants.wrap
@ -12,22 +14,33 @@ 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 {
val playerName = args.getOrNull(0)
if (playerName == null) {
sender.sendMessage(plugin.langYml.getMessage("requires-player"))
return
}
val player = Bukkit.getPlayer(playerName)
if (player == null) {
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
val filterName = args.getOrNull(1)
val filter = if (filterName != null) {
EnchantmentTypes.getByID(filterName) ?: EnchantmentRarities.getByID(filterName)
} else null
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"))
@ -42,43 +55,37 @@ class CommandGiveRandomBook(plugin: EcoPlugin) : PluginCommand(
else -> true
} && it.maxLevel >= minLevel
}
.randomOrNull()?: run {
sender.sendMessage(plugin.langYml.getMessage("no-enchantments-found"))
return
}
.randomOrNull() ?: run {
sender.sendMessage(plugin.langYml.getMessage("no-enchantments-found"))
return
}
val level = (minLevel..maxLevel.coerceAtMost(enchantment.maxLevel)).random()
val level = NumberUtils.randInt(minLevel, maxLevel.coerceAtMost(enchantment.maxLevel))
player.inventory.addItem(EnchantedBookBuilder()
val item = EnchantedBookBuilder()
.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)))
.build()
DropQueue(player)
.addItem(item)
.forceTelekinesis()
.push()
sender.sendMessage(
plugin.langYml.getMessage("gave-random-book")
.replace("%player%", player.name)
.replace("%enchantment%", enchantment.wrap().getFormattedName(level))
)
}
override fun tabComplete(sender: CommandSender, args: List<String>): List<String> {
// OfTeN wrote this - it's cursed, and I am *not* going to try refactor this.
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() }
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,7 +14,7 @@ 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!"
gave-random-book: "&fGave &a%player%&f a random book (&r%enchantment%&f)!"
no-enchantments-found: "&cNo enchantments found!"
invalid-levels: "&cMinimum level can't be higher than maximum level!"