Add support for permissions in the Things API.
This commit introduces the PermissionThing and associated parser. The parser determines the value (grant/revoke) of the permission by looking at the first character of the input string - if it is a minus (-) or caret (^), the value is false (revoke), otherwise it is true (grant). To distinguish permissions from other things, the parser requires a prefix of "perm:".
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.garbagemule.MobArena.things;
|
||||
|
||||
import com.garbagemule.MobArena.MobArena;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
|
||||
public class PermissionThing implements Thing {
|
||||
private final String perm;
|
||||
private final boolean value;
|
||||
private final MobArena plugin;
|
||||
|
||||
public PermissionThing(String perm, boolean value, MobArena plugin) {
|
||||
this.perm = perm;
|
||||
this.value = value;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean giveTo(Player player) {
|
||||
removeExistingAttachment(player);
|
||||
PermissionAttachment attachment = player.addAttachment(plugin);
|
||||
attachment.setPermission(perm, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean takeFrom(Player player) {
|
||||
removeExistingAttachment(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void removeExistingAttachment(Player player) {
|
||||
player.getEffectivePermissions().stream()
|
||||
.filter(info -> info.getAttachment() != null)
|
||||
.filter(info -> info.getAttachment().getPlugin().equals(plugin))
|
||||
.filter(info -> info.getPermission().equals(perm))
|
||||
.map(PermissionAttachmentInfo::getAttachment)
|
||||
.findAny()
|
||||
.ifPresent(PermissionAttachment::remove);
|
||||
}
|
||||
|
||||
String getPermission() {
|
||||
return perm;
|
||||
}
|
||||
|
||||
boolean getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean heldBy(Player player) {
|
||||
return player.hasPermission(perm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (value) {
|
||||
return perm;
|
||||
}
|
||||
return "-" + perm;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.garbagemule.MobArena.things;
|
||||
|
||||
import com.garbagemule.MobArena.MobArena;
|
||||
|
||||
class PermissionThingParser implements ThingParser {
|
||||
private static final String PREFIX = "perm:";
|
||||
|
||||
private MobArena plugin;
|
||||
|
||||
PermissionThingParser(MobArena plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionThing parse(String s) {
|
||||
String value = trimPrefix(s);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value.startsWith("-") || value.startsWith("^")) {
|
||||
return new PermissionThing(value.substring(1), false, plugin);
|
||||
}
|
||||
return new PermissionThing(value, true, plugin);
|
||||
}
|
||||
|
||||
private String trimPrefix(String s) {
|
||||
if (s.startsWith(PREFIX)) {
|
||||
return s.substring(PREFIX.length());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ public class ThingManager implements ThingParser {
|
||||
parsers = new ArrayList<>();
|
||||
parsers.add(new CommandThingParser());
|
||||
parsers.add(new MoneyThingParser(plugin));
|
||||
parsers.add(new PermissionThingParser(plugin));
|
||||
parsers.add(new PotionEffectThingParser());
|
||||
items = parser;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user