summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/command/CommandsPrivilegesLocal.kt
blob: e3aba22b2addeb4d5b631376a40940c4e5448223 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package io.dico.parcels2.command

import io.dico.dicore.command.Validate
import io.dico.dicore.command.annotation.Cmd
import io.dico.dicore.command.annotation.Desc
import io.dico.parcels2.*
import io.dico.parcels2.PrivilegeChangeResult.*
import io.dico.parcels2.util.ext.PERM_ADMIN_MANAGE
import io.dico.parcels2.util.ext.hasPermAdminManage
import org.bukkit.entity.Player

class CommandsPrivilegesLocal(plugin: ParcelsPlugin) : AbstractParcelCommands(plugin) {

    private fun ParcelScope.checkPrivilege(sender: Player, key: PrivilegeKey) {
        val senderPrivilege = parcel.getEffectivePrivilege(sender, PERM_ADMIN_MANAGE)
        val targetPrivilege = parcel.getStoredPrivilege(key)
        Validate.isTrue(senderPrivilege > targetPrivilege, "You may not change the privilege of ${key.notNullName}")
    }

    private fun ParcelScope.checkOwned(sender: Player) {
        Validate.isTrue(parcel.owner != null || sender.hasPermAdminManage, "This parcel is unowned")
    }

    @Cmd("entrust")
    @Desc(
        "Allows a player to manage this parcel",
        shortVersion = "allows a player to manage this parcel"
    )
    @RequireParcelPrivilege(Privilege.OWNER)
    suspend fun ParcelScope.cmdEntrust(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        return when (parcel.allowManage(key)) {
            FAIL_OWNER -> err("The target already owns the parcel")
            FAIL -> err("${player.name} is already allowed to manage this parcel")
            SUCCESS -> "${player.name} is now allowed to manage this parcel"
        }
    }

    @Cmd("distrust")
    @Desc(
        "Disallows a player to manage this parcel,",
        "they will still be able to build",
        shortVersion = "disallows a player to manage this parcel"
    )
    @RequireParcelPrivilege(Privilege.OWNER)
    suspend fun ParcelScope.cmdDistrust(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        return when (parcel.disallowManage(key)) {
            FAIL_OWNER -> err("The target owns the parcel and can't be distrusted")
            FAIL -> err("${player.name} is not currently allowed to manage this parcel")
            SUCCESS -> "${player.name} is not allowed to manage this parcel anymore"
        }
    }

    @Cmd("allow", aliases = ["add", "permit"])
    @Desc(
        "Allows a player to build on this parcel",
        shortVersion = "allows a player to build on this parcel"
    )
    @RequireParcelPrivilege(Privilege.CAN_MANAGE)
    suspend fun ParcelScope.cmdAllow(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        checkPrivilege(sender, key)

        return when (parcel.allowBuild(key)) {
            FAIL_OWNER -> err("The target already owns the parcel")
            FAIL -> err("${player.name} is already allowed to build on this parcel")
            SUCCESS -> "${player.name} is now allowed to build on this parcel"
        }
    }

    @Cmd("disallow", aliases = ["remove", "forbid"])
    @Desc(
        "Disallows a player to build on this parcel,",
        "they won't be allowed to anymore",
        shortVersion = "disallows a player to build on this parcel"
    )
    @RequireParcelPrivilege(Privilege.CAN_MANAGE)
    suspend fun ParcelScope.cmdDisallow(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        checkPrivilege(sender, key)

        return when (parcel.disallowBuild(key)) {
            FAIL_OWNER -> err("The target owns the parcel")
            FAIL -> err("${player.name} is not currently allowed to build on this parcel")
            SUCCESS -> "${player.name} is not allowed to build on this parcel anymore"
        }
    }

    @Cmd("ban", aliases = ["deny"])
    @Desc(
        "Bans a player from this parcel,",
        "making them unable to enter",
        shortVersion = "bans a player from this parcel"
    )
    @RequireParcelPrivilege(Privilege.CAN_MANAGE)
    suspend fun ParcelScope.cmdBan(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        checkPrivilege(sender, key)

        return when (parcel.disallowEnter(key)) {
            FAIL_OWNER -> err("The target owns the parcel")
            FAIL -> err("${player.name} is already banned from this parcel")
            SUCCESS -> "${player.name} is now banned from this parcel"
        }
    }

    @Cmd("unban", aliases = ["undeny"])
    @Desc(
        "Unbans a player from this parcel,",
        "they will be able to enter it again",
        shortVersion = "unbans a player from this parcel"
    )
    @RequireParcelPrivilege(Privilege.CAN_MANAGE)
    suspend fun ParcelScope.cmdUnban(sender: Player, player: PlayerProfile): Any? {
        checkConnected("have privileges changed")
        checkOwned(sender)

        val key = toPrivilegeKey(player)
        checkPrivilege(sender, key)

        return when (parcel.allowEnter(key)) {
            FAIL_OWNER -> err("The target owns the parcel")
            FAIL -> err("${player.name} is not currently banned from this parcel")
            SUCCESS -> "${player.name} is not banned from this parcel anymore"
        }
    }

}