mirror of
https://github.com/garbagemule/MobArena.git
synced 2025-02-16 04:21:29 +01:00
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:
parent
19fb748e0e
commit
c1d1728144
@ -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;
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package com.garbagemule.MobArena.things;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import com.garbagemule.MobArena.MobArena;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PermissionThingParserTest {
|
||||
|
||||
private PermissionThingParser subject;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MobArena plugin = mock(MobArena.class);
|
||||
subject = new PermissionThingParser(plugin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noPrefixNoPerms() {
|
||||
String input = "mobarena.use.join";
|
||||
|
||||
PermissionThing result = subject.parse(input);
|
||||
|
||||
assertThat(result, is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void grant() {
|
||||
String perm = "mobarena.use.leave";
|
||||
String input = "perm:" + perm;
|
||||
|
||||
PermissionThing result = subject.parse(input);
|
||||
|
||||
assertThat(result.getPermission(), equalTo(perm));
|
||||
assertThat(result.getValue(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void denyMinus() {
|
||||
String perm = "mobarena.setup.addarena";
|
||||
String input = "perm:-" + perm;
|
||||
|
||||
PermissionThing result = subject.parse(input);
|
||||
|
||||
assertThat(result.getPermission(), equalTo(perm));
|
||||
assertThat(result.getValue(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void denyCaret() {
|
||||
String perm = "mobarena.use.join";
|
||||
String input = "perm:^" + perm;
|
||||
|
||||
PermissionThing result = subject.parse(input);
|
||||
|
||||
assertThat(result.getPermission(), equalTo(perm));
|
||||
assertThat(result.getValue(), equalTo(false));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user