Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] compile error #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/kotlin/me/elsiff/morefish/command/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ abstract class ConfigurationValueAccessor {

private inline fun <reified T> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,9 +13,9 @@ class EnchantmentMapLoader : CustomLoader<Map<Enchantment, Int>> {
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, Int>(enchantment, level)
Pair(enchantment, level)
}.toMap()
} else {
emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,7 +51,7 @@ class FishConditionSetLoader : CustomLoader<Set<FishCondition>> {
BiomeCondition(args.map { Biome.valueOf(it.toUpperCase()) })
"enchantment" ->
EnchantmentCondition(
Enchantment.getByKey(NamespacedKey.minecraft(args[0])),
NamespacedKeyUtils.enchantment(args[0]),
args[1].toInt()
)
"level" ->
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/me/elsiff/morefish/dao/yaml/YamlRecordDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -65,13 +65,13 @@ class YamlRecordDao(

override fun all(): List<Record> {
val records = mutableListOf<Record>()
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -74,7 +74,7 @@ class FishingCompetitionTimerBarHandler(
override fun run() {
remainingSeconds--
timerBar!!.run {
title = timerBarTitle(remainingSeconds)
setTitle(timerBarTitle(remainingSeconds))
progress = remainingSeconds.toDouble() / duration
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/me/elsiff/morefish/hooker/VaultHooker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ 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 {
val itemStack = fish.type.icon.clone()
if (!fish.type.hasNotFishItemFormat) {
val replacement = getFormatReplacementMap(fish, catcher)
itemStack.edit<ItemMeta> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/me/elsiff/morefish/shop/FishShopGui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FishShopGui(

init {
val bottomBarIcon = ItemStack(Material.LIGHT_BLUE_STAINED_GLASS_PANE)
bottomBarIcon.edit<ItemMeta> { displayName = " " }
bottomBarIcon.edit<ItemMeta> { setDisplayName(" ") }
for (slot in bottomBarSlots) {
inventory.setItem(slot, bottomBarIcon)
}
Expand Down Expand Up @@ -94,16 +94,16 @@ class FishShopGui(

private fun allFishItemStacks(): List<ItemStack> {
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<ItemMeta> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/elsiff/morefish/util/InventoryUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object InventoryUtils {
fun emptyStack(): ItemStack = ItemStack(Material.AIR)

fun deliverTo(inventory: Inventory, delivery: ItemStack, acceptableSlots: List<Int> = 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
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/me/elsiff/morefish/util/NamespacedKeyUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -27,4 +28,11 @@ object NamespacedKeyUtils {
return PotionEffectType.getByName(namespacedKey.key)
?: throw IllegalStateException("There's no potion effect type whose id is '$namespacedKey'")
}
}

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'")
}
}