summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/command/AbstractParcelCommands.kt
blob: 6d62729a9810ee8e33cb11bd9e405387eb22ca17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package io.dico.parcels2.command

import io.dico.dicore.command.*
import io.dico.parcels2.ParcelWorld
import io.dico.parcels2.ParcelsPlugin
import io.dico.parcels2.PlayerProfile
import io.dico.parcels2.blockvisitor.Worker
import io.dico.parcels2.util.ext.hasPermAdminManage
import io.dico.parcels2.util.ext.parcelLimit
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import java.lang.reflect.Method

abstract class AbstractParcelCommands(val plugin: ParcelsPlugin) : ICommandReceiver.Factory {
    override fun getPlugin(): Plugin = plugin

    override fun getReceiver(context: ExecutionContext, target: Method, cmdName: String): ICommandReceiver {
        return getParcelCommandReceiver(plugin.parcelProvider, context, target, cmdName)
    }

    protected fun error(message: String): Nothing {
        throw CommandException(message)
    }

    protected fun checkConnected(action: String) {
        if (!plugin.storage.isConnected) error("Parcels cannot $action right now because of a database error")
    }

    protected suspend fun checkParcelLimit(player: Player, world: ParcelWorld) {
        if (player.hasPermAdminManage) return
        val numOwnedParcels = plugin.storage.getOwnedParcels(PlayerProfile(player)).await()
            .filter { it.worldId.equals(world.id) }.size

        val limit = player.parcelLimit
        if (numOwnedParcels >= limit) {
            error("You have enough plots for now")
        }
    }

    protected fun areYouSureMessage(context: ExecutionContext) = "Are you sure? You cannot undo this action!\n" +
        "Run \"/${context.route.joinToString(" ")} -sure\" if you want to go through with this."

    protected fun ParcelScope.clearWithProgressUpdates(context: ExecutionContext, action: String) {
        Validate.isTrue(!parcel.hasBlockVisitors, "A process is already running in this parcel")
        world.blockManager.clearParcel(parcel.id)
    }

    protected fun Worker.reportProgressUpdates(context: ExecutionContext, action: String) {
        onProgressUpdate(1000, 1000) { progress, elapsedTime ->
            val alt = context.getFormat(EMessageType.NUMBER)
            val main = context.getFormat(EMessageType.INFORMATIVE)
            context.sendMessage(
                EMessageType.INFORMATIVE, false, "$action progress: $alt%.02f$main%%, $alt%.2f${main}s elapsed"
                    .format(progress * 100, elapsedTime / 1000.0)
            )
        }
    }

    protected fun err(message: String): Nothing = throw CommandException(message)

    override fun getCoroutineContext() = plugin.coroutineContext


}