diff --git a/src/main/kotlin/me/elsiff/morefish/command/MainCommand.kt b/src/main/kotlin/me/elsiff/morefish/command/MainCommand.kt index 2fafd1e7..7090ff52 100644 --- a/src/main/kotlin/me/elsiff/morefish/command/MainCommand.kt +++ b/src/main/kotlin/me/elsiff/morefish/command/MainCommand.kt @@ -148,7 +148,7 @@ class MainCommand( return } - val target = sender.server.getPlayerExact(args[0]) ?: null + val target = sender.server.getPlayerExact(args[0]) if (target == null) { val msg = Lang.format("player-not-found").replace("%s" to args[0]).output() sender.sendMessage(msg) diff --git a/src/main/kotlin/me/elsiff/morefish/configuration/ConfigurationValueAccessor.kt b/src/main/kotlin/me/elsiff/morefish/configuration/ConfigurationValueAccessor.kt index 3541558c..3c20ce78 100644 --- a/src/main/kotlin/me/elsiff/morefish/configuration/ConfigurationValueAccessor.kt +++ b/src/main/kotlin/me/elsiff/morefish/configuration/ConfigurationValueAccessor.kt @@ -63,13 +63,14 @@ abstract class ConfigurationValueAccessor { private inline fun findValue( path: String, - getter: (String) -> T, + getter: (String) -> T?, typeChecker: (String) -> Boolean, default: T? ): T { return if (currentSection.contains(path)) { require(typeChecker(path)) { "Value of '$path' in configuration is not a ${T::class.simpleName}" } - getter(path) + // typeChecker also checks nullability + getter(path)!! } else { require(default != null) { "Path '$path' must exist or have a default value" } default diff --git a/src/main/kotlin/me/elsiff/morefish/configuration/loader/EnchantmentMapLoader.kt b/src/main/kotlin/me/elsiff/morefish/configuration/loader/EnchantmentMapLoader.kt index 9bd413f0..5907fb68 100644 --- a/src/main/kotlin/me/elsiff/morefish/configuration/loader/EnchantmentMapLoader.kt +++ b/src/main/kotlin/me/elsiff/morefish/configuration/loader/EnchantmentMapLoader.kt @@ -1,6 +1,7 @@ package me.elsiff.morefish.configuration.loader import me.elsiff.morefish.configuration.ConfigurationValueAccessor +import me.elsiff.morefish.util.NamespacedKeyUtils import org.bukkit.NamespacedKey import org.bukkit.enchantments.Enchantment @@ -12,9 +13,9 @@ class EnchantmentMapLoader : CustomLoader> { return if (section.contains(path)) { section.strings(path).map { val tokens = it.split(DELIMITER) - val enchantment = Enchantment.getByKey(NamespacedKey.minecraft(tokens[0])) + val enchantment = NamespacedKeyUtils.enchantment(tokens[0]) val level = tokens[1].toInt() - Pair(enchantment, level) + Pair(enchantment, level) }.toMap() } else { emptyMap() diff --git a/src/main/kotlin/me/elsiff/morefish/configuration/loader/FishConditionSetLoader.kt b/src/main/kotlin/me/elsiff/morefish/configuration/loader/FishConditionSetLoader.kt index 5a06073b..362b82a2 100644 --- a/src/main/kotlin/me/elsiff/morefish/configuration/loader/FishConditionSetLoader.kt +++ b/src/main/kotlin/me/elsiff/morefish/configuration/loader/FishConditionSetLoader.kt @@ -10,6 +10,7 @@ import org.apache.commons.lang.math.DoubleRange import org.bukkit.NamespacedKey import org.bukkit.block.Biome import org.bukkit.enchantments.Enchantment +import java.lang.IllegalArgumentException /** * Created by elsiff on 2019-01-09. @@ -50,7 +51,7 @@ class FishConditionSetLoader : CustomLoader> { BiomeCondition(args.map { Biome.valueOf(it.toUpperCase()) }) "enchantment" -> EnchantmentCondition( - Enchantment.getByKey(NamespacedKey.minecraft(args[0])), + NamespacedKeyUtils.enchantment(args[0]), args[1].toInt() ) "level" -> diff --git a/src/main/kotlin/me/elsiff/morefish/dao/yaml/YamlRecordDao.kt b/src/main/kotlin/me/elsiff/morefish/dao/yaml/YamlRecordDao.kt index c3be1b7c..eb195cec 100644 --- a/src/main/kotlin/me/elsiff/morefish/dao/yaml/YamlRecordDao.kt +++ b/src/main/kotlin/me/elsiff/morefish/dao/yaml/YamlRecordDao.kt @@ -40,9 +40,9 @@ class YamlRecordDao( override fun update(record: Record) { val id = record.fisher.uniqueId.toString() - require(yaml.contains(id)) { "Record must exist in the ranking" } - - setRecord(yaml.getConfigurationSection(id), record) + require(yaml.isConfigurationSection(id)) { "Record must exist in the ranking" } + // isConfigurationSection checks nullability. + setRecord(yaml.getConfigurationSection(id)!!, record) yaml.save(file) } @@ -65,13 +65,13 @@ class YamlRecordDao( override fun all(): List { val records = mutableListOf() - for (section in yaml.getKeys(false).map(yaml::getConfigurationSection)) { - val id = UUID.fromString(section.name) + yaml.getKeys(false).filter(yaml::isConfigurationSection).map(yaml::getConfigurationSection).forEach { section -> + val id = UUID.fromString(section!!.name) val player = plugin.server.getOfflinePlayer(id) val fishTypeName = section.getString("fish-type") val fishType = fishTypeTable.types.find { it.name == fishTypeName } - ?: throw IllegalStateException("Fish type doesn't exist for '$fishTypeName'") + ?: throw IllegalStateException("Fish type doesn't exist for '$fishTypeName'") val fishLength = section.getDouble("fish-length") val fish = Fish(fishType, fishLength) diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/catchhandler/AbstractBroadcaster.kt b/src/main/kotlin/me/elsiff/morefish/fishing/catchhandler/AbstractBroadcaster.kt index 8d42d8d8..50a8a7a9 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/catchhandler/AbstractBroadcaster.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/catchhandler/AbstractBroadcaster.kt @@ -23,7 +23,7 @@ abstract class AbstractBroadcaster : CatchHandler { val receivers = fish.type.catchAnnouncement.receiversOf(catcher).toMutableList() if (Config.standard.boolean("messages.only-announce-fishing-rod")) { - receivers.removeIf { it.inventory.itemInMainHand?.type != Material.FISHING_ROD } + receivers.removeIf { it.inventory.itemInMainHand.type != Material.FISHING_ROD } } val msg = catchMessageFormat.replace( diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionHost.kt b/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionHost.kt index 5b5c1071..ee7c7370 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionHost.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionHost.kt @@ -125,7 +125,7 @@ class FishingCompetitionHost( return mapOf( "%ordinal%" to NumberUtils.ordinalOf(number), "%number%" to number.toString(), - "%player%" to record.fisher.name, + "%player%" to (record.fisher.name ?: "null"), "%length%" to record.fish.length.toString(), "%fish%" to record.fish.type.name ) diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionTimerBarHandler.kt b/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionTimerBarHandler.kt index f7f3e0ef..0cb92f7f 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionTimerBarHandler.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/competition/FishingCompetitionTimerBarHandler.kt @@ -49,12 +49,12 @@ class FishingCompetitionTimerBarHandler( barUpdatingTask = null timerBar!!.apply { - title = timerBarTitle(0) + setTitle(timerBarTitle(0)) progress = 0.0 removeAll() } - HandlerList.unregisterAll(barDisplayer) + barDisplayer?.let { HandlerList.unregisterAll(it) } barDisplayer = null plugin.server.removeBossBar(timerBarKey) @@ -74,7 +74,7 @@ class FishingCompetitionTimerBarHandler( override fun run() { remainingSeconds-- timerBar!!.run { - title = timerBarTitle(remainingSeconds) + setTitle(timerBarTitle(remainingSeconds)) progress = remainingSeconds.toDouble() / duration } } diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/competition/Prize.kt b/src/main/kotlin/me/elsiff/morefish/fishing/competition/Prize.kt index fb79cc29..794eb7dd 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/competition/Prize.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/competition/Prize.kt @@ -17,8 +17,10 @@ class Prize( } val server = plugin.server - for (command in commands) { - server.dispatchCommand(server.consoleSender, command.replace("@p", player.name)) + player.name?.let { + for (command in commands) { + server.dispatchCommand(server.consoleSender, command.replace("@p", it)) + } } } } \ No newline at end of file diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/condition/EnchantmentCondition.kt b/src/main/kotlin/me/elsiff/morefish/fishing/condition/EnchantmentCondition.kt index 8815d0a7..7aca5b24 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/condition/EnchantmentCondition.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/condition/EnchantmentCondition.kt @@ -14,7 +14,7 @@ class EnchantmentCondition( fisher: Player, competition: FishingCompetition ): Boolean { - val fishingRod = fisher.inventory.itemInMainHand ?: return false + val fishingRod = fisher.inventory.itemInMainHand return fishingRod.containsEnchantment(enchantment) && fishingRod.getEnchantmentLevel(enchantment) >= minLevel } } \ No newline at end of file diff --git a/src/main/kotlin/me/elsiff/morefish/fishing/condition/PotionEffectCondition.kt b/src/main/kotlin/me/elsiff/morefish/fishing/condition/PotionEffectCondition.kt index ff58e28f..6648e275 100644 --- a/src/main/kotlin/me/elsiff/morefish/fishing/condition/PotionEffectCondition.kt +++ b/src/main/kotlin/me/elsiff/morefish/fishing/condition/PotionEffectCondition.kt @@ -10,6 +10,6 @@ class PotionEffectCondition( private val minAmplifier: Int ) : FishCondition { override fun check(caught: Item, fisher: Player, competition: FishingCompetition): Boolean { - return fisher.hasPotionEffect(effectType) && fisher.getPotionEffect(effectType).amplifier >= minAmplifier + return fisher.hasPotionEffect(effectType) && fisher.getPotionEffect(effectType)!!.amplifier >= minAmplifier } } \ No newline at end of file diff --git a/src/main/kotlin/me/elsiff/morefish/hooker/VaultHooker.kt b/src/main/kotlin/me/elsiff/morefish/hooker/VaultHooker.kt index 4e1ea7ff..96276e05 100644 --- a/src/main/kotlin/me/elsiff/morefish/hooker/VaultHooker.kt +++ b/src/main/kotlin/me/elsiff/morefish/hooker/VaultHooker.kt @@ -14,7 +14,7 @@ class VaultHooker : PluginHooker { override fun hook(plugin: MoreFish) { PluginHooker.checkEnabled(this, plugin.server.pluginManager) - val registration = plugin.server.servicesManager.getRegistration(Economy::class.java) ?: null + val registration = plugin.server.servicesManager.getRegistration(Economy::class.java) if (registration != null) { economy = registration.provider } diff --git a/src/main/kotlin/me/elsiff/morefish/item/FishItemStackConverter.kt b/src/main/kotlin/me/elsiff/morefish/item/FishItemStackConverter.kt index 3e33e4c5..a7a2fb0a 100644 --- a/src/main/kotlin/me/elsiff/morefish/item/FishItemStackConverter.kt +++ b/src/main/kotlin/me/elsiff/morefish/item/FishItemStackConverter.kt @@ -31,11 +31,13 @@ class FishItemStackConverter( } fun isFish(itemStack: ItemStack): Boolean { - return fishReader.canRead(itemStack.itemMeta) + val itemMeta = itemStack.itemMeta ?: return false + return fishReader.canRead(itemMeta) } fun fish(itemStack: ItemStack): Fish { - return fishReader.read(itemStack.itemMeta) + require(isFish(itemStack)) { "The itemStack is not fish" } + return fishReader.read(itemStack.itemMeta!!) } fun createItemStack(fish: Fish, catcher: Player): ItemStack { @@ -43,7 +45,7 @@ class FishItemStackConverter( if (!fish.type.hasNotFishItemFormat) { val replacement = getFormatReplacementMap(fish, catcher) itemStack.edit { - displayName = formatConfig.format("display-name").replace(replacement).output(catcher) + setDisplayName(formatConfig.format("display-name").replace(replacement).output(catcher)) lore = formatConfig.formats("lore").replace(replacement).output(catcher) fishWriter.write(this, fish) } diff --git a/src/main/kotlin/me/elsiff/morefish/item/FishItemTagReader.kt b/src/main/kotlin/me/elsiff/morefish/item/FishItemTagReader.kt index 9e02c6d4..dd3a3e84 100644 --- a/src/main/kotlin/me/elsiff/morefish/item/FishItemTagReader.kt +++ b/src/main/kotlin/me/elsiff/morefish/item/FishItemTagReader.kt @@ -28,7 +28,7 @@ class FishItemTagReader( val typeName = tags.getCustomTag(fishTypeKey, ItemTagType.STRING) val type = fishTypeTable.types.find { it.name == typeName } ?: throw IllegalStateException("Fish type doesn't exist") - val length = tags.getCustomTag(fishLengthKey, ItemTagType.DOUBLE) + val length = tags.getCustomTag(fishLengthKey, ItemTagType.DOUBLE) ?: 0.0 Fish(type, length) } } diff --git a/src/main/kotlin/me/elsiff/morefish/shop/FishShopGui.kt b/src/main/kotlin/me/elsiff/morefish/shop/FishShopGui.kt index 87fe79ea..4e8822ec 100644 --- a/src/main/kotlin/me/elsiff/morefish/shop/FishShopGui.kt +++ b/src/main/kotlin/me/elsiff/morefish/shop/FishShopGui.kt @@ -40,7 +40,7 @@ class FishShopGui( init { val bottomBarIcon = ItemStack(Material.LIGHT_BLUE_STAINED_GLASS_PANE) - bottomBarIcon.edit { displayName = " " } + bottomBarIcon.edit { setDisplayName(" ") } for (slot in bottomBarSlots) { inventory.setItem(slot, bottomBarIcon) } @@ -94,16 +94,16 @@ class FishShopGui( private fun allFishItemStacks(): List { return fishSlots - .mapNotNull { slot -> inventory.getItem(slot) ?: null } + .mapNotNull { slot -> inventory.getItem(slot) } .filter { itemStack -> converter.isFish(itemStack) } } private fun updatePriceIcon(price: Double = totalPrice) { val emeraldIcon = ItemStack(Material.EMERALD) emeraldIcon.edit { - displayName = Lang.format("shop-emerald-icon-name") + setDisplayName(Lang.format("shop-emerald-icon-name") .replace("%price%" to price.toString()) - .output() + .output()) } inventory.setItem(priceIconSlot, emeraldIcon) } diff --git a/src/main/kotlin/me/elsiff/morefish/shop/FishShopSignListener.kt b/src/main/kotlin/me/elsiff/morefish/shop/FishShopSignListener.kt index 4ac01bc4..d1c299ec 100644 --- a/src/main/kotlin/me/elsiff/morefish/shop/FishShopSignListener.kt +++ b/src/main/kotlin/me/elsiff/morefish/shop/FishShopSignListener.kt @@ -35,10 +35,10 @@ class FishShopSignListener( @EventHandler fun onPlayerInteract(event: PlayerInteractEvent) { - if (event.action == Action.RIGHT_CLICK_BLOCK && - event.clickedBlock.state is Sign - ) { - val sign = event.clickedBlock.state as Sign + if (event.action != Action.RIGHT_CLICK_BLOCK) return + val clickedBlock = event.clickedBlock ?: return + if (clickedBlock.state is Sign) { + val sign = clickedBlock.state as Sign if (sign.lines[0] == shopSignTitle) { if (Config.standard.boolean("fish-shop.enable")) { fishShop.openGuiTo(event.player) diff --git a/src/main/kotlin/me/elsiff/morefish/util/InventoryUtils.kt b/src/main/kotlin/me/elsiff/morefish/util/InventoryUtils.kt index d5e9191f..95de1785 100644 --- a/src/main/kotlin/me/elsiff/morefish/util/InventoryUtils.kt +++ b/src/main/kotlin/me/elsiff/morefish/util/InventoryUtils.kt @@ -12,7 +12,7 @@ object InventoryUtils { fun emptyStack(): ItemStack = ItemStack(Material.AIR) fun deliverTo(inventory: Inventory, delivery: ItemStack, acceptableSlots: List = inventory.slots()) { - val contents = acceptableSlots.mapNotNull { slot -> inventory.getItem(slot) ?: null } + val contents = acceptableSlots.mapNotNull { slot -> inventory.getItem(slot) } for (invItem in contents.filter { it.isSimilar(delivery) }) { val givingAmount = min(delivery.amount, invItem.maxStackSize - invItem.amount) invItem.amount = invItem.amount + givingAmount diff --git a/src/main/kotlin/me/elsiff/morefish/util/NamespacedKeyUtils.kt b/src/main/kotlin/me/elsiff/morefish/util/NamespacedKeyUtils.kt index 8ce12a0a..b056a8ca 100644 --- a/src/main/kotlin/me/elsiff/morefish/util/NamespacedKeyUtils.kt +++ b/src/main/kotlin/me/elsiff/morefish/util/NamespacedKeyUtils.kt @@ -2,6 +2,7 @@ package me.elsiff.morefish.util import org.bukkit.Material import org.bukkit.NamespacedKey +import org.bukkit.enchantments.Enchantment import org.bukkit.potion.PotionEffectType /** @@ -27,4 +28,11 @@ object NamespacedKeyUtils { return PotionEffectType.getByName(namespacedKey.key) ?: throw IllegalStateException("There's no potion effect type whose id is '$namespacedKey'") } -} \ No newline at end of file + + fun enchantment(id: String): Enchantment = enchantment(fromMinecraft(id)) + + fun enchantment(namespacedKey: NamespacedKey): Enchantment { + return Enchantment.getByKey(namespacedKey) + ?: throw IllegalStateException("There's no enchantment whose id is '$namespacedKey'") + } +}