-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
PermissionAction.java
185 lines (165 loc) · 6.5 KB
/
PermissionAction.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package net.citizensnpcs.trait.shop;
import java.util.List;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.InputMenus;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.Menu;
import net.citizensnpcs.api.gui.MenuContext;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.Util;
import net.milkbowl.vault.permission.Permission;
public class PermissionAction extends NPCShopAction {
@Persist
public List<String> permissions = Lists.newArrayList();
public PermissionAction() {
}
public PermissionAction(List<String> permissions) {
this.permissions = permissions;
}
@Override
public String describe() {
String description = permissions.size() + " permissions";
for (int i = 0; i < permissions.size(); i++) {
description += "\n" + permissions.get(i);
if (i == 3) {
description += "...";
break;
}
}
return description;
}
@Override
public int getMaxRepeats(Entity entity) {
return -1;
}
@Override
public Transaction grant(Entity entity, int repeats) {
if (!(entity instanceof Player))
return Transaction.fail();
Player player = (Player) entity;
Permission perm = Bukkit.getServicesManager().getRegistration(Permission.class).getProvider();
return Transaction.create(() -> {
return true;
}, () -> {
for (String permission : permissions) {
perm.playerAdd(player, Placeholders.replace(permission, player));
}
}, () -> {
for (String permission : permissions) {
perm.playerRemove(player, Placeholders.replace(permission, player));
}
});
}
@Override
public Transaction take(Entity entity, int repeats) {
if (!(entity instanceof Player))
return Transaction.fail();
Player player = (Player) entity;
Permission perm = Bukkit.getServicesManager().getRegistration(Permission.class).getProvider();
return Transaction.create(() -> {
for (String permission : permissions) {
if (!perm.playerHas(player, Placeholders.replace(permission, player))) {
return false;
}
}
return true;
}, () -> {
for (String permission : permissions) {
perm.playerRemove(player, Placeholders.replace(permission, player));
}
}, () -> {
for (String permission : permissions) {
perm.playerAdd(player, Placeholders.replace(permission, player));
}
});
}
@Menu(title = "Permissions editor", dimensions = { 3, 9 })
public static class PermissionActionEditor extends InventoryMenuPage {
private PermissionAction base;
private Consumer<NPCShopAction> callback;
public PermissionActionEditor() {
}
public PermissionActionEditor(PermissionAction base, Consumer<NPCShopAction> callback) {
this.base = base;
this.callback = callback;
}
@Override
public void initialise(MenuContext ctx) {
for (int i = 0; i < 3 * 9; i++) {
final int idx = i;
ctx.getSlot(i).clear();
if (i < base.permissions.size()) {
ctx.getSlot(i).setItemStack(new ItemStack(Material.FEATHER), "<f>Set permission",
"Right click to remove\nCurrently: " + base.permissions.get(i));
}
ctx.getSlot(i).setClickHandler(event -> {
if (event.isRightClick()) {
event.setCancelled(true);
if (idx < base.permissions.size()) {
base.permissions.remove(idx);
ctx.getSlot(idx).setItemStack(null);
}
return;
}
ctx.getMenu().transition(InputMenus.stringSetter(
() -> idx < base.permissions.size() ? base.permissions.get(idx) : "", (res) -> {
if (res == null) {
if (idx < base.permissions.size()) {
base.permissions.remove(idx);
}
return;
}
if (idx < base.permissions.size()) {
base.permissions.set(idx, res);
} else {
base.permissions.add(res);
}
}));
});
}
}
@Override
public void onClose(HumanEntity player) {
callback.accept(base.permissions.isEmpty() ? null : base);
}
}
public static class PermissionActionGUI implements GUI {
private Boolean supported;
@Override
public InventoryMenuPage createEditor(NPCShopAction previous, Consumer<NPCShopAction> callback) {
return new PermissionActionEditor(previous == null ? new PermissionAction() : (PermissionAction) previous,
callback);
}
@Override
public ItemStack createMenuItem(NPCShopAction previous) {
if (supported == null) {
try {
supported = Bukkit.getServicesManager().getRegistration(Permission.class).getProvider() != null;
} catch (Throwable t) {
supported = false;
}
}
if (!supported) {
return null;
}
String description = null;
if (previous != null) {
PermissionAction old = (PermissionAction) previous;
description = old.describe();
}
return Util.createItem(Material.PAPER, "Permission", description);
}
@Override
public boolean manages(NPCShopAction action) {
return action instanceof PermissionAction;
}
}
}