mirror of
https://github.com/TotalFreedomMC/TF-LibsDisguises.git
synced 2024-05-28 23:31:19 +00:00
It will now compile on Java 16
This commit is contained in:
parent
d525f8edcb
commit
5a340341b0
2
pom.xml
2
pom.xml
|
@ -69,7 +69,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<asm.version>9.0</asm.version>
|
||||
<lombok.version>1.18.16</lombok.version>
|
||||
<lombok.version>1.18.20</lombok.version>
|
||||
<protocollib.version>master-SNAPSHOT</protocollib.version>
|
||||
<spigot.version>[1.16,]</spigot.version>
|
||||
<junit.version>4.13.1</junit.version>
|
||||
|
|
|
@ -224,11 +224,9 @@ public class LibsDisguises extends JavaPlugin {
|
|||
registerCommand("libsdisguises", new LibsDisguisesCommand());
|
||||
registerCommand("disguise", new DisguiseCommand());
|
||||
registerCommand("undisguise", new UndisguiseCommand());
|
||||
registerCommand("disguiseplayer", new DisguisePlayerCommand());
|
||||
registerCommand("undisguiseplayer", new UndisguisePlayerCommand());
|
||||
registerCommand("undisguiseentity", new UndisguiseEntityCommand());
|
||||
registerCommand("disguiseentity", new DisguiseEntityCommand());
|
||||
registerCommand("disguiseradius", new DisguiseRadiusCommand());
|
||||
registerCommand("undisguiseradius", new UndisguiseRadiusCommand());
|
||||
registerCommand("disguisehelp", new DisguiseHelpCommand());
|
||||
registerCommand("disguiseclone", new DisguiseCloneCommand());
|
||||
|
@ -236,8 +234,6 @@ public class LibsDisguises extends JavaPlugin {
|
|||
registerCommand("disguiseviewbar", new DisguiseViewBarCommand());
|
||||
registerCommand("disguisemodify", new DisguiseModifyCommand());
|
||||
registerCommand("disguisemodifyentity", new DisguiseModifyEntityCommand());
|
||||
registerCommand("disguisemodifyplayer", new DisguiseModifyPlayerCommand());
|
||||
registerCommand("disguisemodifyradius", new DisguiseModifyRadiusCommand());
|
||||
registerCommand("copydisguise", new CopyDisguiseCommand());
|
||||
registerCommand("grabskin", new GrabSkinCommand());
|
||||
registerCommand("savedisguise", new SaveDisguiseCommand());
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package me.libraryaddict.disguise.commands;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import me.libraryaddict.disguise.BlockedDisguises;
|
||||
import me.libraryaddict.disguise.DisguiseConfig;
|
||||
import me.libraryaddict.disguise.commands.disguise.DisguiseCommand;
|
||||
|
|
|
@ -24,4 +24,4 @@ public class WatcherMethod {
|
|||
return "WatcherMethod{" + "watcherClass=" + watcherClass + ", method=" + method + ", name='" + name + '\'' + ", returnType=" + returnType + ", param=" +
|
||||
param + ", randomDefault=" + randomDefault + '}';
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2030,4 +2030,4 @@ public class ReflectionManager {
|
|||
public static String toReadable(String string, String joiner) {
|
||||
return StringUtils.join(splitReadable(string), joiner);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -26,4 +26,4 @@ public class WatcherInfo {
|
|||
|
||||
return getRemoved() < 0 || removed > ReflectionManager.getVersion().ordinal();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,113 +0,0 @@
|
|||
package me.libraryaddict.disguise.disguisetypes;
|
||||
|
||||
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
|
||||
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
|
||||
import me.libraryaddict.disguise.utilities.parser.WatcherMethod;
|
||||
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 19/01/2020.
|
||||
*/
|
||||
public class DisguiseCloneTest {
|
||||
|
||||
/**
|
||||
* MetaIndex needs ProtocolLib to have initialized so.
|
||||
*/
|
||||
// @Test
|
||||
public void testCloneDisguise() {
|
||||
try {
|
||||
ReflectionManager.registerValues();
|
||||
DisguiseParser.createDefaultMethods();
|
||||
DisguiseUtilities.init();
|
||||
|
||||
for (DisguiseType type : DisguiseType.values()) {
|
||||
Disguise disguise;
|
||||
|
||||
if (type.isPlayer()) {
|
||||
disguise = new PlayerDisguise("libraryaddict");
|
||||
} else if (type.isMob()) {
|
||||
disguise = new MobDisguise(type);
|
||||
} else {
|
||||
disguise = new MiscDisguise(type);
|
||||
}
|
||||
|
||||
for (Map.Entry<WatcherMethod, Map.Entry<WatcherMethod, Object>> entry : DisguiseParser.getMethodDefaults().entrySet()) {
|
||||
Object dValue = entry.getValue().getValue();
|
||||
|
||||
if (dValue instanceof String) {
|
||||
dValue = "NewString";
|
||||
} else if (dValue instanceof Float) {
|
||||
dValue = ((float) dValue) + 1;
|
||||
} else if (dValue instanceof Double) {
|
||||
dValue = ((double) dValue) + 1;
|
||||
} else if (dValue instanceof Long) {
|
||||
dValue = ((long) dValue) + 1;
|
||||
} else if (dValue instanceof Integer) {
|
||||
dValue = ((int) dValue) + 1;
|
||||
} else if (dValue instanceof Byte) {
|
||||
dValue = ((byte) dValue) + 1;
|
||||
} else if (dValue instanceof Short) {
|
||||
dValue = ((short) dValue) + 1;
|
||||
} else if (dValue instanceof ItemStack) {
|
||||
dValue = new ItemStack(Material.DIAMOND_BLOCK);
|
||||
} else if (dValue instanceof Boolean) {
|
||||
dValue = !((Boolean) dValue);
|
||||
} else if (dValue instanceof Enum) {
|
||||
Object[] vals = dValue.getClass().getEnumConstants();
|
||||
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
if (vals[i] == dValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dValue = vals[i];
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
WatcherMethod m = entry.getKey();
|
||||
|
||||
Object invokeWith = disguise;
|
||||
|
||||
if (FlagWatcher.class.isAssignableFrom(entry.getKey().getWatcherClass())) {
|
||||
invokeWith = disguise.getWatcher();
|
||||
}
|
||||
|
||||
try {
|
||||
MethodHandle handle = entry.getKey().getMethod();
|
||||
handle.bindTo(invokeWith);
|
||||
handle.invoke(dValue);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Disguise cloned = disguise.clone();
|
||||
String dString = DisguiseUtilities.getGson().toJson(disguise);
|
||||
String cString = DisguiseUtilities.getGson().toJson(cloned);
|
||||
|
||||
if (!dString.equals(cString)) {
|
||||
System.err.println(dString);
|
||||
System.err.println(cString);
|
||||
Assert.fail("Cloned disguise is not the same!");
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
if (ex.getCause() != null) {
|
||||
ex.getCause().printStackTrace();
|
||||
}
|
||||
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 4/05/2019.
|
||||
*/
|
||||
public class DisguiseTypesTest {
|
||||
@Test
|
||||
public void testDisguiseTypes() {
|
||||
for (EntityType entityType : EntityType.values()) {
|
||||
if (entityType == EntityType.LIGHTNING) {
|
||||
continue;
|
||||
} else if (entityType == EntityType.UNKNOWN) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DisguiseType disguiseType = DisguiseType.getType(entityType);
|
||||
|
||||
Assert.assertSame(entityType.name() + " has no DisguiseType registered!", disguiseType.name(),
|
||||
entityType.name());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,150 +0,0 @@
|
|||
package me.libraryaddict.disguise.utilities;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 25/10/2018.
|
||||
*/
|
||||
public class DisguiseUtilitiesTest {
|
||||
@Test
|
||||
public void testNewlineSplitter() {
|
||||
Assert.assertArrayEquals(new String[]{"Name 1", "Name 2"}, DisguiseUtilities.splitNewLine("Name 1\nName 2"));
|
||||
Assert.assertArrayEquals(new String[]{"Name 1", "Name 2"}, DisguiseUtilities.splitNewLine("Name 1\\nName 2"));
|
||||
Assert.assertArrayEquals(new String[]{"Name 1\\", "Name 2"},
|
||||
DisguiseUtilities.splitNewLine("Name 1\\\nName 2"));
|
||||
Assert.assertArrayEquals(new String[]{"Name 1\\nName 2"}, DisguiseUtilities.splitNewLine("Name 1\\\\nName 2"));
|
||||
Assert.assertArrayEquals(new String[]{"Name 1\\", "Name 2"},
|
||||
DisguiseUtilities.splitNewLine("Name 1\\\\\\nName 2"));
|
||||
Assert.assertArrayEquals(new String[]{"Name 1\\\\nName 2"},
|
||||
DisguiseUtilities.splitNewLine("Name 1\\\\\\\\nName 2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuoteSplitter() {
|
||||
// Test if splits are correct
|
||||
Assert.assertArrayEquals(new String[]{"A", "simple", "string"}, DisguiseUtilities.split("A simple string"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A quoted string"}, DisguiseUtilities.split("\"A quoted string\""));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"\"A double quoted string\""},
|
||||
DisguiseUtilities.split("\"\"A double quoted string\"\""));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string", "containing a", "quote"},
|
||||
DisguiseUtilities.split("A string \"containing a\" quote"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string", "fully", "split"},
|
||||
DisguiseUtilities.split("\"A\" string fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string", "fully", "split"},
|
||||
DisguiseUtilities.split("\"A\" \"string\" fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string", "fully", "split"},
|
||||
DisguiseUtilities.split("A \"string\" fully split"));
|
||||
|
||||
// Test if quotes are ignored properly and included in result
|
||||
Assert.assertArrayEquals(new String[]{"A", "\"string", "fully", "split"},
|
||||
DisguiseUtilities.split("A \"string fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "\"string", "\"fully", "split"},
|
||||
DisguiseUtilities.split("A \"string \"fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"\"A", "\"string", "\"fully", "split"},
|
||||
DisguiseUtilities.split("\"A \"string \"fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string\"", "fully", "split"},
|
||||
DisguiseUtilities.split("A string\" fully split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string\"", "fully\"", "split"},
|
||||
DisguiseUtilities.split("A string\" fully\" split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A", "string", "fully\"", "split"},
|
||||
DisguiseUtilities.split("A \"string\" fully\" split"));
|
||||
|
||||
Assert.assertArrayEquals(new String[]{"A \"string", "with", "four", "splits"},
|
||||
DisguiseUtilities.split("\"A \"string\" with four splits"));
|
||||
|
||||
// Test for quotes inside words
|
||||
Assert.assertArrayEquals(new String[]{"Fully", "split", "\"", "message"},
|
||||
DisguiseUtilities.split("Fully split \"\"\" message"));
|
||||
|
||||
// Test to make sure space can be quoted, with an empty quote at the end
|
||||
Assert.assertArrayEquals(new String[]{" ", "\""}, DisguiseUtilities.split("\" \" \""));
|
||||
|
||||
// Test to make sure empty quotes, are still quotes
|
||||
Assert.assertArrayEquals(new String[]{"Three", "", "split"}, DisguiseUtilities.split("Three \"\" split"));
|
||||
|
||||
// Test to ensure single quotes, are still not quotes
|
||||
Assert.assertArrayEquals(new String[]{"'Three", "split", "message'"},
|
||||
DisguiseUtilities.split("'Three split message'"));
|
||||
|
||||
// There is a quoted message inside the quoted message, however it was not escaped
|
||||
Assert.assertArrayEquals(new String[]{"A", "quoted message \"inside a quoted message\""},
|
||||
DisguiseUtilities.split("A \"quoted message \"inside a quoted message\"\""));
|
||||
|
||||
// Now test for escaped quotes, however as escaped quotes look different inside editors, I'll be replacing \
|
||||
// with / and " with '
|
||||
|
||||
// Test for escaped quotes, they should be ignored
|
||||
splitEquals("/'Escaped quotes/'", "'Escaped", "quotes'");
|
||||
|
||||
// Test with one quote escaped
|
||||
splitEquals("'Escaped quotes/'", "'Escaped", "quotes'");
|
||||
|
||||
// Test with no quotes escaped, where the escape was escaped
|
||||
splitEquals("'Unescaped quotes/'", "'Unescaped", "quotes'");
|
||||
|
||||
// Test with three escaped slashes, then unescaped quote
|
||||
splitEquals("'Unescaped quotes//////'", "Unescaped quotes///");
|
||||
|
||||
// Test with three escaped slashes, then escaped quote
|
||||
splitEquals("'Escaped quotes///////'", "'Escaped", "quotes///'");
|
||||
|
||||
// Test with strings of escapes and quotes only
|
||||
splitEquals("////", "////");
|
||||
|
||||
splitEquals("////'", "//'");
|
||||
|
||||
splitEquals("'////'", "//");
|
||||
|
||||
splitEquals("'/////'", "'//'");
|
||||
|
||||
splitEquals("'// //'", "// /");
|
||||
|
||||
splitEquals("'//// ////'", "//// //");
|
||||
|
||||
splitEquals(
|
||||
"Foobar is not 'Foo Bar' but is a single word 'foobar' or as some quote it, /'foobar/' and again, " +
|
||||
"not /'foo bar/' - It is 'foobar'!",
|
||||
|
||||
"Foobar", "is", "not", "Foo Bar", "but", "is", "a", "single", "word", "foobar", "or", "as", "some",
|
||||
"quote", "it,", "'foobar'", "and", "again,", "not", "'foo", "bar'", "-", "It", "is", "'foobar'!");
|
||||
|
||||
splitAndBack("Hi \" bye");
|
||||
splitAndBack("Hi\\\" I'm Sam");
|
||||
splitAndBack("\"Hi\\\" I'm Sam");
|
||||
splitAndBack("\"Hi\\\\\" I'm Sam");
|
||||
splitAndBack("\"Hi\\\\\\\" I'm Sam");
|
||||
splitAndBack("\"Hi\\\\\\\" \"I'm Sam");
|
||||
}
|
||||
|
||||
private void splitAndBack(String string) {
|
||||
String quoted = DisguiseUtilities.quote(string);
|
||||
String[] split = DisguiseUtilities.split(quoted);
|
||||
|
||||
Assert.assertEquals(1, split.length);
|
||||
Assert.assertEquals(string, split[0]);
|
||||
}
|
||||
|
||||
private void splitEquals(String toSplit, String... expected) {
|
||||
String[] splitted = DisguiseUtilities.split(toSplit.replace("/", "\\").replace("'", "\""));
|
||||
String[] expect = Arrays.stream(expected).map(string -> string.replace("/", "\\").replace("'", "\""))
|
||||
.toArray(String[]::new);
|
||||
|
||||
Assert.assertArrayEquals(expect, splitted);
|
||||
|
||||
splitAndBack(toSplit);
|
||||
}
|
||||
}
|
|
@ -1,355 +0,0 @@
|
|||
package me.libraryaddict.disguise.utilities.parser;
|
||||
|
||||
import me.libraryaddict.disguise.DisguiseConfig;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by libraryaddict on 21/10/2018.
|
||||
*/
|
||||
public class DisguisePermissionsTest {
|
||||
@Test
|
||||
public void testDisguisesExist() {
|
||||
Assert.assertNull("There should not be a reindeer disguise", DisguiseParser.getDisguisePerm("Reindeer"));
|
||||
|
||||
Assert.assertNotNull("There should be a cow disguise", DisguiseParser.getDisguisePerm("Cow"));
|
||||
|
||||
Assert.assertNotNull("There should be a firework disguise", DisguiseParser.getDisguisePerm("Firework"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPermissionNames() {
|
||||
Assert.assertFalse("There should not be permissions", createPermissions("Disguise", false).hasPermissions());
|
||||
|
||||
Assert.assertFalse("The commands should not match",
|
||||
createPermissions("Disguise", false, "libsdisguises.disguiseentity.cow").hasPermissions());
|
||||
|
||||
Assert.assertFalse("The commands should not match",
|
||||
createPermissions("Disguised", false, "libsdisguises.disguise.cow").hasPermissions());
|
||||
|
||||
Assert.assertTrue("There should be permissions",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.animal").hasPermissions());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperatorPermissions() {
|
||||
DisguisePermissions permissions = createPermissions("Disguise", true, "-libsdisguises.disguise.sheep",
|
||||
"-libsdisguises.disguise.horse.setBaby");
|
||||
|
||||
Assert.assertTrue("There should be permissions", permissions.hasPermissions());
|
||||
|
||||
Assert.assertTrue("The disguise cow should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow")));
|
||||
|
||||
Assert.assertFalse("The disguise sheep should not be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Sheep")));
|
||||
|
||||
Assert.assertTrue("The disguise horse should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Horse")));
|
||||
|
||||
Assert.assertFalse("The disguise horse should not be allowed with setBaby", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Horse"), Collections.singletonList("setBaby")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWildcardsPermissions() {
|
||||
Assert.assertTrue("The cow disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.animal")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow")));
|
||||
|
||||
Assert.assertFalse("The firework disguise should not be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.animal")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertTrue("The firework disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.*")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertTrue("The firework disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.disguise.*")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertTrue("The firework disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.Firework")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertFalse("The firework disguise should not be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.*.*", "-libsdisguises.*.misc")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertTrue("The firework disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.disguise.*", "-libsdisguises.*.*")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
|
||||
Assert.assertTrue("The firework disguise should be allowed",
|
||||
createPermissions("Disguise", false, "libsdisguises.disguise.firework", "-libsdisguises.disguise.misc")
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInheritedPermissions() {
|
||||
testInheritedPermissions(createPermissions("Disguise", false, "libsdisguises.disguise.animal.setBaby",
|
||||
"-libsdisguises.disguise.sheep.setBaby"));
|
||||
|
||||
testInheritedPermissions(createPermissions("Disguise", false, "libsdisguises.disguise.animal.setBaby",
|
||||
"libsdisguises.disguise.sheep.-setBaby"));
|
||||
}
|
||||
|
||||
private void testInheritedPermissions(DisguisePermissions permissions) {
|
||||
Assert.assertTrue("The sheep disguise should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Sheep")));
|
||||
|
||||
Assert.assertTrue("The cow disguise should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow")));
|
||||
|
||||
Assert.assertTrue("The cow disguise should be allowed with setBaby", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setBaby")));
|
||||
|
||||
Assert.assertFalse("The sheep disguise should not be allowed with setBaby", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Sheep"), Collections.singletonList("setBaby")));
|
||||
|
||||
Assert.assertFalse("The firework disguise should not be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Firework")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegatedPermissions() {
|
||||
DisguisePermissions permissions = createPermissions("Disguise", false, "libsdisguises.disguise.sheep",
|
||||
"-libsdisguises.disguise.cow.setSprinting", "-libsdisguises.disguise.donkey",
|
||||
"-libsdisguises.disguise.horse.setRearing", "libsdisguises.disguise.horse");
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow")));
|
||||
|
||||
Assert.assertTrue("The sheep disguise should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Sheep")));
|
||||
|
||||
Assert.assertFalse("The donkey disguise should not be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Donkey")));
|
||||
|
||||
Assert.assertTrue("The horse disguise should be allowed",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Horse")));
|
||||
|
||||
Assert.assertTrue("The horse disguise should be allowed with options", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Horse"), Collections.singletonList("setBaby")));
|
||||
|
||||
Assert.assertFalse("The horse disguise should not be allowed setRearing", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Horse"), Collections.singletonList("setRearing")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiDisguises() {
|
||||
DisguisePermissions permissions = createPermissions("Disguise", false, "libsdisguises.disguise.cow.setBaby",
|
||||
"libsdisguises.disguise.cow.setHealth", "libsdisguises.disguise.cow.-setBurning");
|
||||
|
||||
Assert.assertTrue("The cow disguise should be able to use setBaby", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setBaby")));
|
||||
|
||||
Assert.assertTrue("The cow disguise should be able to use setHealth", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setHealth")));
|
||||
|
||||
Assert.assertTrue("The cow disguise should be able to use setBaby and setHealth", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Arrays.asList("setBaby", "setHealth")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be able to use setSprinting", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setSprinting")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be able to use setSprinting with setBaby", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Arrays.asList("setSprinting", "setBaby")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOptions() {
|
||||
Assert.assertFalse("The disguise should not be valid",
|
||||
createPermissions("Disguise", false, "libsdisguises.disguise.cow", "-libsdisguises.disguise.cow")
|
||||
.hasPermissions());
|
||||
|
||||
DisguisePermissions permissions = createPermissions("Disguise", false, "libsdisguises.disguise.cow",
|
||||
"libsdisguises.disguise.sheep.setColor.setSprinting", "libsdisguises.disguise.animal.-setSprinting");
|
||||
|
||||
Assert.assertTrue("There should be a valid disguise", permissions.hasPermissions());
|
||||
|
||||
DisguisePerm cow = DisguiseParser.getDisguisePerm("Cow");
|
||||
|
||||
Assert.assertTrue("The cow disguise should be allowed", permissions.isAllowedDisguise(cow));
|
||||
|
||||
Assert.assertTrue("The cow disguise should be allowed with options",
|
||||
permissions.isAllowedDisguise(cow, Arrays.asList("setBaby", "setBurning")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be allowed with options setSprinting",
|
||||
permissions.isAllowedDisguise(cow, Arrays.asList("setBaby", "setSprinting")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be allowed with options",
|
||||
permissions.isAllowedDisguise(cow, Collections.singletonList("setSprinting")));
|
||||
|
||||
DisguisePerm sheep = DisguiseParser.getDisguisePerm("Sheep");
|
||||
|
||||
Assert.assertFalse("The sheep disguise should not be allowed with options",
|
||||
permissions.isAllowedDisguise(sheep, Arrays.asList("setBaby", "setBurning")));
|
||||
|
||||
Assert.assertTrue("The sheep disguise should be allowed setColor",
|
||||
permissions.isAllowedDisguise(sheep, Collections.singletonList("setColor")));
|
||||
|
||||
Assert.assertTrue("The sheep disguise should be allowed setSprinting",
|
||||
permissions.isAllowedDisguise(sheep, Collections.singletonList("setSprinting")));
|
||||
|
||||
Assert.assertFalse("The sheep disguise should not be allowed setColor and setBaby",
|
||||
permissions.isAllowedDisguise(sheep, Arrays.asList("setColor", "setBaby")));
|
||||
|
||||
DisguisePerm firework = DisguiseParser.getDisguisePerm("Firework");
|
||||
|
||||
Assert.assertFalse("The firework disguise should not be allowed", permissions.isAllowedDisguise(firework));
|
||||
|
||||
Assert.assertFalse("The disguise should not be allowed even with options",
|
||||
permissions.isAllowedDisguise(firework, Arrays.asList("setBaby", "setBurning")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplictPermissions() {
|
||||
DisguiseConfig.setExplicitDisguisePermissions(true);
|
||||
|
||||
DisguisePermissions permissions = createPermissions("Disguise", false, "libsdisguises.disguise.animal",
|
||||
"libsdisguises.disguise.zombie", "libsdisguises.disguise.skeleton.*",
|
||||
"libsdisguises.disguise.wither.setburning", "libsdisguises.disguise.silverfish.-setburning");
|
||||
|
||||
Assert.assertTrue("The cow disguise should be usable",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow")));
|
||||
|
||||
Assert.assertFalse("The cow disguise should not be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Cow"), Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertTrue("The zombie disguise should be usable",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Zombie")));
|
||||
|
||||
Assert.assertFalse("The zombie disguise should not be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Zombie"), Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertTrue("The skeleton disguise should be usable",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Skeleton")));
|
||||
|
||||
Assert.assertTrue("The skeleton disguise should be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Skeleton"),
|
||||
Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertTrue("The wither disguise should be usable",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Wither")));
|
||||
|
||||
Assert.assertTrue("The wither disguise should be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Wither"), Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertFalse("The wither disguise should not be able to use setSprinting", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Wither"),
|
||||
Collections.singletonList("setSprinting")));
|
||||
|
||||
Assert.assertTrue("The silverfish disguise should be usable",
|
||||
permissions.isAllowedDisguise(DisguiseParser.getDisguisePerm("Silverfish")));
|
||||
|
||||
Assert.assertFalse("The silverfish disguise should not be able to use setBurning", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Silverfish"),
|
||||
Collections.singletonList("setBurning")));
|
||||
|
||||
Assert.assertTrue("The silverfish disguise should be able to use setSprinting", permissions
|
||||
.isAllowedDisguise(DisguiseParser.getDisguisePerm("Silverfish"),
|
||||
Collections.singletonList("setSprinting")));
|
||||
|
||||
DisguiseConfig.setExplicitDisguisePermissions(false);
|
||||
}
|
||||
|
||||
private DisguisePermissions createPermissions(String commandName, boolean isOp, String... perms) {
|
||||
List<String> permitted = new ArrayList<>();
|
||||
List<String> negated = new ArrayList<>();
|
||||
Set<PermissionAttachmentInfo> attachments = new HashSet<>();
|
||||
|
||||
Permissible permissible = new Permissible() {
|
||||
@Override
|
||||
public boolean isPermissionSet(String s) {
|
||||
return permitted.contains(s) || negated.contains(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermissionSet(Permission permission) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String s) {
|
||||
return permitted.contains(s) || (isOp() && !negated.contains(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Permission permission) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionAttachment addAttachment(Plugin plugin) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b, int i) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PermissionAttachment addAttachment(Plugin plugin, int i) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttachment(PermissionAttachment permissionAttachment) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions() {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
||||
return attachments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOp() {
|
||||
return isOp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOp(boolean b) {
|
||||
throw new UnsupportedOperationException("Not Supported");
|
||||
}
|
||||
};
|
||||
|
||||
// If permission starts with a - then it was negated
|
||||
Arrays.stream(perms).forEach(perm -> {
|
||||
boolean setTrue = !perm.startsWith("-");
|
||||
|
||||
if (setTrue) {
|
||||
permitted.add(perm);
|
||||
} else {
|
||||
negated.add(perm = perm.substring(1));
|
||||
}
|
||||
|
||||
attachments.add(new PermissionAttachmentInfo(permissible, perm, null, setTrue));
|
||||
});
|
||||
|
||||
return new DisguisePermissions(permissible, commandName);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue