|
| 1 | +/* |
| 2 | + * Copyright (C) 2011-2022 lishid. All rights reserved. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, version 3. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.lishid.openinv.util; |
| 18 | + |
| 19 | +import org.bukkit.permissions.Permissible; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +/** |
| 23 | + * An enum containing all permissions directly checked by OpenInv. |
| 24 | + * |
| 25 | + * <p>Note that this is not an exhaustive list! This does not contain |
| 26 | + * all permissions managed by Bukkit, largely parent nodes.</p> |
| 27 | + */ |
| 28 | +public enum Permissions { |
| 29 | + |
| 30 | + /// Permission to open one's own inventory. |
| 31 | + INVENTORY_OPEN_SELF("inventory.open.self"), |
| 32 | + /// Permission to open someone else's inventory. |
| 33 | + INVENTORY_OPEN_OTHER("inventory.open.other"), |
| 34 | + /** |
| 35 | + * Permission to edit one's own inventory. |
| 36 | + * |
| 37 | + * <p>Note that this does not guarantee that the user has access to open the inventory! |
| 38 | + * Be sure to check {@link #INVENTORY_OPEN_SELF} first.</p> |
| 39 | + */ |
| 40 | + INVENTORY_EDIT_SELF("inventory.edit.self"), |
| 41 | + /** |
| 42 | + * Permission to edit someone else's inventory. |
| 43 | + * |
| 44 | + * <p>Note that this does not guarantee that the user has access to open the inventory! |
| 45 | + * Be sure to check {@link #INVENTORY_OPEN_OTHER} first.</p> |
| 46 | + */ |
| 47 | + INVENTORY_EDIT_OTHER("inventory.edit.other"), |
| 48 | + /// Permission to insert any item into the head slot. |
| 49 | + INVENTORY_SLOT_HEAD_ANY("inventory.slot.head.any"), |
| 50 | + /// Permission to insert any item into the chest slot. |
| 51 | + INVENTORY_SLOT_CHEST_ANY("inventory.slot.chest.any"), |
| 52 | + /// Permission to insert any item into the legs slot. |
| 53 | + INVENTORY_SLOT_LEGS_ANY("inventory.slot.legs.any"), |
| 54 | + /// Permission to insert any item into the feet slot. |
| 55 | + INVENTORY_SLOT_FEET_ANY("inventory.slot.feet.any"), |
| 56 | + /// Permission to drop items as the player via the drop slot. |
| 57 | + INVENTORY_SLOT_DROP("inventory.slot.drop"), |
| 58 | + |
| 59 | + /// Permission to open one's own ender chest. |
| 60 | + ENDERCHEST_OPEN_SELF("enderchest.open.self"), |
| 61 | + /// Permission to open someone else's ender chest. |
| 62 | + ENDERCHEST_OPEN_OTHER("enderchest.open.other"), |
| 63 | + /** |
| 64 | + * Permission to edit one's own ender chest. |
| 65 | + * |
| 66 | + * <p>Note that this does not guarantee that the user has access to open the inventory! |
| 67 | + * Be sure to check {@link #ENDERCHEST_OPEN_SELF} first.</p> |
| 68 | + */ |
| 69 | + ENDERCHEST_EDIT_SELF("enderchest.edit.self"), |
| 70 | + /** |
| 71 | + * Permission to edit someone else's ender chest. |
| 72 | + * |
| 73 | + * <p>Note that this does not guarantee that the user has access to open the inventory! |
| 74 | + * Be sure to check {@link #ENDERCHEST_OPEN_OTHER} first.</p> |
| 75 | + */ |
| 76 | + ENDERCHEST_EDIT_OTHER("enderchest.edit.other"), |
| 77 | + |
| 78 | + /// Permission to clear one's own inventory. |
| 79 | + CLEAR_SELF("clear.self"), |
| 80 | + /// Permission to clear someone else's inventory. |
| 81 | + CLEAR_OTHER("clear.other"), |
| 82 | + |
| 83 | + /// Permission to view inventories and ender chests from other worlds. |
| 84 | + ACCESS_CROSSWORLD("access.crossworld"), |
| 85 | + /// Permission to access offline players' inventories and ender chests. |
| 86 | + ACCESS_OFFLINE("access.offline"), |
| 87 | + /// Permission to access online players' inventories and ender chests. |
| 88 | + ACCESS_ONLINE("access.online"), |
| 89 | + /// Permission granting the ability to edit players with the same access level. |
| 90 | + ACCESS_EQUAL_EDIT("access.equal.edit"), |
| 91 | + /// Permission to view, but not edit, players with the same access level. |
| 92 | + ACCESS_EQUAL_VIEW("access.equal.view"), |
| 93 | + /// Permission to deny access to players with the same access level. |
| 94 | + ACCESS_EQUAL_DENY("access.equal.deny"), |
| 95 | + |
| 96 | + /// Permission to perform inventory interaction while in spectator mode. |
| 97 | + SPECTATE_CLICK("spectate.click"), |
| 98 | + |
| 99 | + /// Permission to use the AnyContainer feature. |
| 100 | + CONTAINER_ANY("container.any"), |
| 101 | + /// Permission to use the AnyContainer feature but not the command to toggle it. |
| 102 | + CONTAINER_ANY_USE("container.any.use"), |
| 103 | + /// Permission to use the SilentContainer feature. |
| 104 | + CONTAINER_SILENT("container.silent"), |
| 105 | + /// Permission to use the SilentContainer feature but not the command to toggle it. |
| 106 | + CONTAINER_SILENT_USE("container.silent.use"), |
| 107 | + /// Permission to search inventories and ender chests. |
| 108 | + SEARCH_INVENTORY("search.inventory"), |
| 109 | + /// Permission to search containers. |
| 110 | + SEARCH_CONTAINER("search.container"); |
| 111 | + |
| 112 | + private final String permission; |
| 113 | + |
| 114 | + Permissions(String permission) { |
| 115 | + this.permission = "openinv." + permission; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Check if a {@link Permissible} has this permission. |
| 120 | + * |
| 121 | + * @param permissible the Permissible |
| 122 | + * @return true if the permission is granted |
| 123 | + */ |
| 124 | + public boolean hasPermission(@NotNull Permissible permissible) { |
| 125 | + return permissible.hasPermission(permission); |
| 126 | + } |
| 127 | + |
| 128 | + public boolean hasPermission(@NotNull Permissible permissible, @NotNull Permissions parent) { |
| 129 | + if (permissible.hasPermission(permission)) return true; |
| 130 | + if (permissible.isPermissionSet(permission) && !permissible.hasPermission(permission)) return false; |
| 131 | + return permissible.hasPermission(parent.permission); |
| 132 | + } |
| 133 | +} |
0 commit comments