From 0081efce981df1110be8bbed4c34284c4565b4bd Mon Sep 17 00:00:00 2001 From: Auxilor Date: Mon, 3 Oct 2022 13:27:46 +0100 Subject: [PATCH] PR codestyle --- .../commands/CommandGiveRandomBook.kt | 73 ++++++++++--------- .../core-plugin/src/main/resources/lang.yml | 2 +- 2 files changed, 41 insertions(+), 34 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 3bdd9e5e..d2edcc21 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 @@ -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) { - 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): List { + // 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()) - } } diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index c449e13c..220cc432 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -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!"