From 78b84573ca1248d422ea58cd11790e8e133e709c Mon Sep 17 00:00:00 2001 From: gravityfox Date: Thu, 17 Nov 2016 18:17:49 -0800 Subject: [PATCH] Made improvements. Locatable command sources are now able to use relative coordinates and omit things like world parameters. Why you want to do this i don't know, but in theory this makes things more future proof. --- FoxCore | 2 +- .../sponge/foxguard/plugin/FoxGuardMain.java | 6 +- .../plugin/command/CommandCreate.java | 5 +- .../plugin/command/CommandDelete.java | 5 +- .../plugin/command/CommandDetail.java | 24 +- .../plugin/command/CommandEnableDisable.java | 9 +- .../foxguard/plugin/command/CommandHere.java | 18 +- .../foxguard/plugin/command/CommandLink.java | 7 +- .../plugin/command/CommandModify.java | 7 +- .../plugin/command/CommandRename.java | 7 +- .../plugin/command/link/LinkageParser.java | 7 +- .../foxguard/plugin/handler/BasicHandler.java | 137 ++-- .../foxguard/plugin/handler/GroupHandler.java | 123 +-- .../plugin/handler/PermissionHandler.java | 714 ++++++++++++++++++ .../plugin/handler/StaticHandler.java | 69 +- .../foxguard/plugin/handler/util/Entry.java | 25 +- .../plugin/handler/util/PermissionEntry.java | 71 ++ .../plugin/handler/util/TristateEntry.java | 70 ++ .../plugin/region/world/CuboidRegion.java | 11 +- .../plugin/region/world/ElevationRegion.java | 6 +- .../region/world/RectangularRegion.java | 8 +- .../plugin/state/RegionsStateField.java | 7 +- 22 files changed, 1102 insertions(+), 236 deletions(-) create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/PermissionEntry.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/TristateEntry.java diff --git a/FoxCore b/FoxCore index f870688..09ebca7 160000 --- a/FoxCore +++ b/FoxCore @@ -1 +1 @@ -Subproject commit f870688cb9d6a7527d66acb4e7afa3f648c4a66a +Subproject commit 09ebca7670ce634f91f2f1ec86db2315c2281a9d diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java index a5f5121..682520a 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java @@ -34,10 +34,7 @@ import net.foxdenstudio.sponge.foxguard.plugin.command.*; import net.foxdenstudio.sponge.foxguard.plugin.controller.LogicController; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; -import net.foxdenstudio.sponge.foxguard.plugin.handler.BasicHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.DebugHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.GroupHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.StaticHandler; +import net.foxdenstudio.sponge.foxguard.plugin.handler.*; import net.foxdenstudio.sponge.foxguard.plugin.listener.*; import net.foxdenstudio.sponge.foxguard.plugin.misc.FGContextCalculator; import net.foxdenstudio.sponge.foxguard.plugin.object.factory.FGFactoryManager; @@ -289,6 +286,7 @@ private void registerFactories() { manager.registerHandlerFactory(new StaticHandler.Factory()); manager.registerHandlerFactory(new BasicHandler.Factory()); manager.registerHandlerFactory(new GroupHandler.Factory()); + manager.registerHandlerFactory(new PermissionHandler.Factory()); manager.registerHandlerFactory(new DebugHandler.Factory()); //manager.registerControllerFactory(new MessageController.Factory()); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandCreate.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandCreate.java index f2c1a7a..7b0d719 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandCreate.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandCreate.java @@ -48,6 +48,7 @@ import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.util.Tristate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -97,7 +98,7 @@ public CommandResult process(@Nonnull CommandSource source, @Nonnull String argu String worldName = parse.flags.get("world"); World world = null; if (isWorldRegion) { - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -257,7 +258,7 @@ else if (parse.current.index == 1) { } else if (isIn(WORLDREGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDelete.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDelete.java index 54ff1b5..549ca26 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDelete.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDelete.java @@ -48,6 +48,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -90,7 +91,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -149,7 +150,7 @@ else if (parse.current.index == 1) { if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java index 2bd367f..ecdd18d 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandDetail.java @@ -30,6 +30,7 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser; import net.foxdenstudio.sponge.foxcore.plugin.command.util.FlagMapper; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; import net.foxdenstudio.sponge.foxguard.plugin.controller.IController; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; @@ -49,6 +50,7 @@ import org.spongepowered.api.text.format.TextStyles; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -95,7 +97,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -136,6 +138,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(objectDetails); } catch (Exception e) { builder.append(Text.of(TextColors.RED, TextStyles.ITALIC, "There was an error getting details for region \"" + region.getName() + "\".")); + FoxGuardMain.instance().getLogger().error( + (region instanceof IWorldRegion ? "Worldregion \"" : "Region \"") + + region.getName() + "\" of type \"" + region.getLongTypeName() + "\"" + + (region instanceof IWorldRegion ? " in world \"" + ((IWorldRegion) region).getWorld().getName() + "\"" : "") + + " threw an exception while getting details", e + ); } outboundLinks(builder, region, source); } else { @@ -148,6 +156,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(objectDetails); } catch (Exception e) { builder.append(Text.of(TextColors.RED, TextStyles.ITALIC, "There was an error getting details for region \"" + region.getName() + "\".")); + FoxGuardMain.instance().getLogger().error( + (region instanceof IWorldRegion ? "Worldregion \"" : "Region \"") + + region.getName() + "\" of type \"" + region.getLongTypeName() + "\"" + + (region instanceof IWorldRegion ? " in world \"" + ((IWorldRegion) region).getWorld().getName() + "\"" : "") + + " threw an exception while getting details", e + ); } } source.sendMessage(builder.build()); @@ -187,6 +201,9 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(objectDetails); } catch (Exception e) { builder.append(Text.of(TextColors.RED, TextStyles.ITALIC, "There was an error getting details for handler \"" + handler.getName() + "\".")); + FoxGuardMain.instance().getLogger().error( + "Handler \"" + handler.getName() + "\" of type \"" + handler.getLongTypeName() + "\" threw an exception while getting details", e + ); } builder.append(Text.of(TextColors.GREEN, "\n------- Inbound Links -------")); List controllerList = FGManager.getInstance().getControllers().stream() @@ -278,6 +295,9 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(objectDetails); } catch (Exception e) { builder.append(Text.of(TextColors.RED, TextStyles.ITALIC, "There was an error getting details for handler \"" + handler.getName() + "\".")); + FoxGuardMain.instance().getLogger().error( + "Handler \"" + handler.getName() + "\" of type \"" + handler.getLongTypeName() + "\" threw an exception while getting details", e + ); } } source.sendMessage(builder.build()); @@ -360,7 +380,7 @@ else if (parse.current.index == 1) { if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java index 821ebb3..861e80a 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java @@ -53,6 +53,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -127,7 +128,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } else if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -230,7 +231,7 @@ else if (parse.current.index > 0) { if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -297,8 +298,6 @@ public Optional getHelp(CommandSource source) { @Override public Text getUsage(CommandSource source) { - if (source instanceof Player) - return Text.of((this.enableState ? "enable" : "disable") + " ] | handler> [names]..."); - else return Text.of((this.enableState ? "enable" : "disable") + " | handler> [names]..."); + return Text.of((this.enableState ? "enable" : "disable") + " | handler> [names]..."); } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandHere.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandHere.java index e3f432e..3e229eb 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandHere.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandHere.java @@ -37,7 +37,6 @@ import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.listener.PlayerMoveListener; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; -import net.foxdenstudio.sponge.foxguard.plugin.region.world.IWorldRegion; import net.foxdenstudio.sponge.foxguard.plugin.util.FGUtil; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; @@ -50,6 +49,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -89,7 +89,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -102,8 +102,8 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (world == null) throw new CommandException(Text.of("Must specify a world!")); double x, y, z; Vector3d pPos = null; - if (source instanceof Player) - pPos = ((Player) source).getLocation().getPosition(); + if (source instanceof Locatable) + pPos = ((Locatable) source).getLocation().getPosition(); if (parse.args.length == 0) { if (pPos == null) throw new CommandException(Text.of("Must specify coordinates!")); @@ -154,16 +154,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (selectedRegions.contains(region)) { output.append(Text.of(TextColors.GRAY, "[+]")); output.append(Text.of(TextColors.RED, - TextActions.runCommand("/foxguard s r remove " + - (region instanceof IWorldRegion ? ("--w:" + ((IWorldRegion) region).getWorld() + " ") : "") + - region.getName()), + TextActions.runCommand("/foxguard s r remove " + FGUtil.genWorldFlag(region) + region.getName()), TextActions.showText(Text.of("Remove from state buffer")), "[-]")); } else { output.append(Text.of(TextColors.GREEN, - TextActions.runCommand("/foxguard s r add " + - (region instanceof IWorldRegion ? ("--w:" + ((IWorldRegion) region).getWorld().getName() + " ") : "") + - region.getName()), + TextActions.runCommand("/foxguard s r add " + FGUtil.genWorldFlag(region) + region.getName()), TextActions.showText(Text.of("Add to state buffer")), "[+]")); output.append(Text.of(TextColors.GRAY, "[-]")); @@ -171,7 +167,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm output.append(Text.of(" ")); } output.append(Text.of(FGUtil.getColorForObject(region), - TextActions.runCommand("/foxguard detail region" + (region instanceof IWorldRegion ? (" --w:" + ((IWorldRegion) region).getWorld().getName() + " ") : "") + region.getName()), + TextActions.runCommand("/foxguard detail region " + FGUtil.genWorldFlag(region) + region.getName()), TextActions.showText(Text.of("View details")), FGUtil.getRegionName(region, false))); if (regionListIterator.hasNext()) output.append(Text.NEW_LINE); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandLink.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandLink.java index eb483e6..32b6dbb 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandLink.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandLink.java @@ -48,6 +48,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -97,7 +98,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm World world = null; if (region == null) { String worldName = parse.flags.get("world"); - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -143,7 +144,7 @@ public List getSuggestions(CommandSource source, String arguments, @Null if (parse.current.index == 0) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -165,7 +166,7 @@ public List getSuggestions(CommandSource source, String arguments, @Null if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java index 0ebf07a..bed9b09 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java @@ -46,6 +46,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -90,7 +91,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -186,7 +187,7 @@ else if (parse.current.index == 1) { if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -232,7 +233,7 @@ else if (parse.current.type.equals(AdvCmdParser.CurrentElement.ElementType.LONGF if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandRename.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandRename.java index 6ce9410..efb5fff 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandRename.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandRename.java @@ -48,6 +48,7 @@ import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.util.Tristate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -88,7 +89,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm region = FGManager.getInstance().getRegion(parse.args[1]); if (region == null) { String worldName = parse.flags.get("world"); - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -167,7 +168,7 @@ else if (parse.current.index == 1) { if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -201,7 +202,7 @@ else if (parse.current.index == 1) { IRegion region = FGManager.getInstance().getRegion(parse.args[1]); if (region == null) { String worldName = parse.flags.get("world"); - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/link/LinkageParser.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/link/LinkageParser.java index 6ca54ab..a486f94 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/link/LinkageParser.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/link/LinkageParser.java @@ -42,6 +42,7 @@ import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.World; import java.util.*; @@ -69,7 +70,7 @@ public static List getSuggestions(String expressionString, CommandSource Matcher matcher = PATTERN.matcher(expressionString); boolean found = false; Stage stage = Stage.START; - World world = source instanceof Player ? ((Player) source).getWorld() : null; + World world = source instanceof Locatable ? ((Locatable) source).getWorld() : null; int parentheses = 0; String match = ""; while (matcher.find()) { @@ -147,8 +148,8 @@ else if (match.startsWith("%")) { } private LinkageParser(CommandSource source) { - if (source instanceof Player) { - currentWorld = ((Player) source).getWorld(); + if (source instanceof Locatable) { + currentWorld = ((Locatable) source).getWorld(); } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/BasicHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/BasicHandler.java index 97cc4db..0d99886 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/BasicHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/BasicHandler.java @@ -39,8 +39,8 @@ import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagBitSet; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Entry; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Operation; +import net.foxdenstudio.sponge.foxguard.plugin.handler.util.TristateEntry; import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import net.foxdenstudio.sponge.foxguard.plugin.object.factory.IHandlerFactory; import net.foxdenstudio.sponge.foxguard.plugin.util.EverythingSet; @@ -93,9 +93,9 @@ public class BasicHandler extends HandlerBase { }; private final List groups; - private final Map> groupPermissions; + private final Map> groupPermissions; private final Group defaultGroup; - private final List defaultPermissions; + private final List defaultPermissions; private final Map> groupPermCache; private final Map defaultPermCache; @@ -118,9 +118,9 @@ public BasicHandler(String name, int priority) { public BasicHandler(String name, boolean isEnabled, int priority, List groups, - Map> groupPermissions, + Map> groupPermissions, Group defaultGroup, - List defaultPermissions) { + List defaultPermissions) { super(name, priority, isEnabled); this.groups = groups; this.defaultGroup = defaultGroup; @@ -130,14 +130,14 @@ public BasicHandler(String name, boolean isEnabled, int priority, this.groupPermCache = new CacheMap<>((k1, m1) -> { if (k1 instanceof Group) { - List entries = BasicHandler.this.groupPermissions.get(k1); + List entries = BasicHandler.this.groupPermissions.get(k1); Map map = new CacheMap<>((k2, m2) -> { if (k2 instanceof FlagBitSet) { FlagBitSet flags = (FlagBitSet) k2; Tristate state = null; - for (Entry entry : entries) { + for (TristateEntry entry : entries) { if (flags.toFlagSet().containsAll(entry.set)) { - state = entry.state; + state = entry.tristate; break; } } @@ -153,9 +153,9 @@ public BasicHandler(String name, boolean isEnabled, int priority, if (k instanceof FlagBitSet) { FlagBitSet flags = (FlagBitSet) k; Tristate state = Tristate.UNDEFINED; - for (Entry entry : BasicHandler.this.defaultPermissions) { + for (TristateEntry entry : BasicHandler.this.defaultPermissions) { if (flags.toFlagSet().containsAll(entry.set)) { - state = entry.state; + state = entry.tristate; break; } } @@ -533,10 +533,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - List permissions = getGroupPermissions(group); - Entry entry = new Entry(flags, state); + List permissions = getGroupPermissions(group); + TristateEntry entry = new TristateEntry(flags, state); - for (Entry existing : permissions) { + for (TristateEntry existing : permissions) { if (existing.set.equals(entry.set)) return ProcessResult.of(false, Text.of("Entry already exists with this flag set!")); } @@ -559,7 +559,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "remove": { if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify flags or an index to remove!")); - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to remove in this group!"); try { int index = Integer.parseInt(parse.args[3]); if (index < 0) index = 0; @@ -577,8 +578,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } } - Entry entry = null; - for (Entry existing : permissions) { + TristateEntry entry = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(flags)) { entry = existing; break; @@ -592,7 +593,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "set": { if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify an index or flags and then a tristate value!")); - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to set in this group!"); try { int index = Integer.parseInt(parse.args[3]); if (index < 0) index = 0; @@ -647,10 +649,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null && !clear) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - Entry entry = new Entry(flags, state); + TristateEntry entry = new TristateEntry(flags, state); - Entry original = null; - for (Entry existing : permissions) { + TristateEntry original = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(entry.set)) { original = existing; break; @@ -683,9 +685,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } } case "move": { - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify flags or an index to move!")); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to move in this group!"); try { int from = Integer.parseInt(parse.args[3]); if (from < 0) from = 0; @@ -723,8 +726,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } if (!set) return ProcessResult.of(false, Text.of("Must specify a target index!")); - Entry entry = null; - for (Entry existing : permissions) { + TristateEntry entry = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(flags)) { entry = existing; break; @@ -1100,12 +1103,12 @@ public Text details(CommandSource source, String arguments) { TextActions.showText(Text.of("Click to add a flag entry")), group.displayName + ":\n")); int index = 0; - for (Entry entry : this.groupPermissions.get(group)) { + for (TristateEntry entry : this.groupPermissions.get(group)) { StringBuilder stringBuilder = new StringBuilder(); entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); Text.Builder entryBuilder = Text.builder(); entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")) - .append(FGUtil.readableTristateText(entry.state)) + .append(FGUtil.readableTristateText(entry.tristate)) .onHover(TextActions.showText(Text.of("Click to change this flag entry"))) .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " flags " + group.name + " set " + (index++) + " ")); builder.append(entryBuilder.build()).append(Text.NEW_LINE); @@ -1116,12 +1119,12 @@ public Text details(CommandSource source, String arguments) { TextActions.showText(Text.of("Click to add a flag entry")), this.defaultGroup.displayName + ":")); int index = 0; - for (Entry entry : this.defaultPermissions) { + for (TristateEntry entry : this.defaultPermissions) { StringBuilder stringBuilder = new StringBuilder(); entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); Text.Builder entryBuilder = Text.builder(); entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")) - .append(FGUtil.readableTristateText(entry.state)) + .append(FGUtil.readableTristateText(entry.tristate)) .onHover(TextActions.showText(Text.of("Click to change this flag entry"))) .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " flags default set " + (index++) + " ")); builder.append(Text.NEW_LINE).append(entryBuilder.build()); @@ -1147,11 +1150,11 @@ public void save(Path directory) { for (Group group : this.groups) { List stringEntries = flagMapDB.indexTreeList(group.name, Serializer.STRING).createOrOpen(); stringEntries.clear(); - stringEntries.addAll(this.groupPermissions.get(group).stream().map(Entry::serialize).collect(Collectors.toList())); + stringEntries.addAll(this.groupPermissions.get(group).stream().map(TristateEntry::serialize).collect(Collectors.toList())); } List stringEntries = flagMapDB.indexTreeList("default", Serializer.STRING).createOrOpen(); stringEntries.clear(); - stringEntries.addAll(this.defaultPermissions.stream().map(Entry::serialize).collect(Collectors.toList())); + stringEntries.addAll(this.defaultPermissions.stream().map(TristateEntry::serialize).collect(Collectors.toList())); } Path groupsDirectory = directory.resolve("groups"); storageManager.constructDirectory(groupsDirectory); @@ -1331,15 +1334,15 @@ public void clearUsers(Group group) { group.users.clear(); } - public boolean addFlagEntry(Group group, Entry entry) { + public boolean addFlagEntry(Group group, TristateEntry entry) { return addFlagEntry(group, 0, entry); } - public boolean addFlagEntry(Group group, int index, Entry entry) { - List groupEntries = getGroupPermissions(group); + public boolean addFlagEntry(Group group, int index, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); if (index < 0 || index > groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + groupEntries.size()); - for (Entry groupEntry : groupEntries) { + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) return false; } groupEntries.add(index, entry); @@ -1347,11 +1350,11 @@ public boolean addFlagEntry(Group group, int index, Entry entry) { return true; } - public void setFlagEntry(Group group, Entry entry) { - List groupEntries = getGroupPermissions(group); - for (Entry groupEntry : groupEntries) { + public void setFlagEntry(Group group, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) { - groupEntry.state = entry.state; + groupEntry.tristate = entry.tristate; return; } } @@ -1359,10 +1362,10 @@ public void setFlagEntry(Group group, Entry entry) { clearFlagCacheForGroup(group); } - public void setFlagEntry(Group group, int index, Entry entry) { - List groupEntries = getGroupPermissions(group); - Entry original = null; - for (Entry groupEntry : groupEntries) { + public void setFlagEntry(Group group, int index, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); + TristateEntry original = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) { original = groupEntry; break; @@ -1374,18 +1377,18 @@ public void setFlagEntry(Group group, int index, Entry entry) { } public void setFlagEntry(Group group, int index, Tristate state) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (index < 0 || index >= groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (groupEntries.size() - 1)); - Entry entry = groupEntries.get(index); - entry.state = state; + TristateEntry entry = groupEntries.get(index); + entry.tristate = state; clearFlagCacheForGroup(group); } public boolean removeFlagEntry(Group group, Set flags) { - List groupEntries = getGroupPermissions(group); - Entry toRemove = null; - for (Entry groupEntry : groupEntries) { + List groupEntries = getGroupPermissions(group); + TristateEntry toRemove = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(flags)) { toRemove = groupEntry; } @@ -1397,7 +1400,7 @@ public boolean removeFlagEntry(Group group, Set flags) { } public void removeFlagEntry(Group group, int index) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (index < 0 || index >= groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (groupEntries.size() - 1)); groupEntries.remove(index); @@ -1405,11 +1408,11 @@ public void removeFlagEntry(Group group, int index) { } public boolean moveFlagEntry(Group group, Set flags, int destination) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (destination < 0 || destination >= groupEntries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (groupEntries.size() - 1)); - Entry toMove = null; - for (Entry groupEntry : groupEntries) { + TristateEntry toMove = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(flags)) { toMove = groupEntry; } @@ -1421,12 +1424,12 @@ public boolean moveFlagEntry(Group group, Set flags, int destination) { } public void moveFlagEntry(Group group, int source, int destination) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (source < 0 || source >= groupEntries.size()) throw new IndexOutOfBoundsException("Source index out of bounds: " + source + " Range: 0-" + (groupEntries.size() - 1)); if (destination < 0 || destination >= groupEntries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (groupEntries.size() - 1)); - Entry entry = groupEntries.remove(source); + TristateEntry entry = groupEntries.remove(source); groupEntries.add(destination, entry); clearFlagCacheForGroup(group); } @@ -1479,7 +1482,7 @@ private void clearFlagCacheForGroup(Group group) { } - private List getGroupPermissions(Group group) { + private List getGroupPermissions(Group group) { if (group == this.defaultGroup) return this.defaultPermissions; else return this.groupPermissions.get(group); } @@ -1585,7 +1588,7 @@ public IHandler create(String name, int priority, String arguments, CommandSourc Group owners = handler.createGroup("owners").get(); owners.displayName = "Owners"; owners.color = TextColors.GOLD; - handler.addFlagEntry(owners, new Entry(ImmutableSet.of(DEBUFF), TRUE)); + handler.addFlagEntry(owners, new TristateEntry(ImmutableSet.of(DEBUFF), TRUE)); if (source instanceof Player) owners.users.add(((Player) source).getUniqueId()); Group members = handler.createGroup("members").get(); @@ -1606,7 +1609,7 @@ public IHandler create(String name, int priority, String arguments, CommandSourc Group owners = handler.createGroup("owners").get(); owners.displayName = "Owners"; owners.color = TextColors.GOLD; - handler.addFlagEntry(owners, new Entry(ImmutableSet.of(DEBUFF), TRUE)); + handler.addFlagEntry(owners, new TristateEntry(ImmutableSet.of(DEBUFF), TRUE)); if (source instanceof Player) owners.users.add(((Player) source).getUniqueId()); Group members = handler.createGroup("members").get(); @@ -1623,25 +1626,25 @@ public IHandler create(String name, int priority, String arguments, CommandSourc Group owners = handler.createGroup("owners").get(); owners.displayName = "Owners"; owners.color = TextColors.GOLD; - handler.addFlagEntry(owners, new Entry(ImmutableSet.of(DEBUFF), TRUE)); + handler.addFlagEntry(owners, new TristateEntry(ImmutableSet.of(DEBUFF), TRUE)); if (source instanceof Player) owners.users.add(((Player) source).getUniqueId()); Group members = handler.createGroup("members").get(); members.displayName = "Members"; members.color = TextColors.GREEN; - handler.addFlagEntry(members, new Entry(ImmutableSet.of(BLOCK), UNDEFINED)); + handler.addFlagEntry(members, new TristateEntry(ImmutableSet.of(BLOCK), UNDEFINED)); Group passive = handler.createGroup("passive").get(); passive.displayName = "Passive"; passive.color = TextColors.AQUA; - handler.addFlagEntry(passive, new Entry(ImmutableSet.of(SPAWN, HOSTILE), FALSE)); + handler.addFlagEntry(passive, new TristateEntry(ImmutableSet.of(SPAWN, HOSTILE), FALSE)); handler.setPassiveSetting(PassiveSetting.GROUP, passive); Group defaultG = handler.getDefaultGroup(); - handler.addFlagEntry(defaultG, new Entry(ImmutableSet.of(BLOCK, CHANGE), FALSE)); - handler.addFlagEntry(defaultG, new Entry(ImmutableSet.of(DAMAGE, ENTITY), FALSE)); - handler.addFlagEntry(defaultG, new Entry(ImmutableSet.of(DAMAGE, LIVING), UNDEFINED)); - handler.addFlagEntry(defaultG, new Entry(ImmutableSet.of(DAMAGE, PLAYER), FALSE)); + handler.addFlagEntry(defaultG, new TristateEntry(ImmutableSet.of(BLOCK, CHANGE), FALSE)); + handler.addFlagEntry(defaultG, new TristateEntry(ImmutableSet.of(DAMAGE, ENTITY), FALSE)); + handler.addFlagEntry(defaultG, new TristateEntry(ImmutableSet.of(DAMAGE, LIVING), UNDEFINED)); + handler.addFlagEntry(defaultG, new TristateEntry(ImmutableSet.of(DAMAGE, PLAYER), FALSE)); return handler; } else throw new CommandException(Text.of("\"" + parse.args[0] + "\" is not a valid template!")); @@ -1678,20 +1681,20 @@ public IHandler create(Path directory, String name, int priority, boolean isEnab TextColor color = Sponge.getRegistry().getType(TextColor.class, root.getNode("color").getString("white")).orElse(TextColors.WHITE); groups.add(new Group(groupName, members, color, displayName)); } - Map> groupPermissions = new HashMap<>(); - List defaultPermissions; + Map> groupPermissions = new HashMap<>(); + List defaultPermissions; try (DB flagMapDB = DBMaker.fileDB(directory.resolve("flags.foxdb").normalize().toString()).make()) { for (Group group : groups) { List stringEntries = flagMapDB.indexTreeList(group.name, Serializer.STRING).createOrOpen(); groupPermissions.put(group, stringEntries.stream() - .map(Entry::deserialize) + .map(TristateEntry::deserialize) .filter(entry -> !entry.set.isEmpty()) .distinct() .collect(Collectors.toList())); } List stringEntries = flagMapDB.indexTreeList("default", Serializer.STRING).createOrOpen(); defaultPermissions = stringEntries.stream() - .map(Entry::deserialize) + .map(TristateEntry::deserialize) .filter(entry -> !entry.set.isEmpty()) .distinct() .collect(Collectors.toList()); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java index 56445ab..7914c11 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GroupHandler.java @@ -37,8 +37,8 @@ import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagBitSet; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Entry; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Operation; +import net.foxdenstudio.sponge.foxguard.plugin.handler.util.TristateEntry; import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import net.foxdenstudio.sponge.foxguard.plugin.object.factory.IHandlerFactory; import net.foxdenstudio.sponge.foxguard.plugin.util.ExtraContext; @@ -90,9 +90,9 @@ public class GroupHandler extends HandlerBase { }; private final List groups; - private final Map> groupPermissions; + private final Map> groupPermissions; private final Group defaultGroup; - private final List defaultPermissions; + private final List defaultPermissions; private final Map> groupPermCache; private final Map defaultPermCache; @@ -108,9 +108,9 @@ public GroupHandler(String name, int priority) { public GroupHandler(String name, boolean isEnabled, int priority, List groups, - Map> groupPermissions, + Map> groupPermissions, Group defaultGroup, - List defaultPermissions) { + List defaultPermissions) { super(name, priority, isEnabled); this.groups = groups; this.defaultGroup = defaultGroup; @@ -120,14 +120,14 @@ public GroupHandler(String name, boolean isEnabled, int priority, this.groupPermCache = new CacheMap<>((k1, m1) -> { if (k1 instanceof Group) { - List entries = GroupHandler.this.groupPermissions.get(k1); + List entries = GroupHandler.this.groupPermissions.get(k1); Map map = new CacheMap<>((k2, m2) -> { if (k2 instanceof FlagBitSet) { FlagBitSet flags = (FlagBitSet) k2; Tristate state = null; - for (Entry entry : entries) { + for (TristateEntry entry : entries) { if (flags.toFlagSet().containsAll(entry.set)) { - state = entry.state; + state = entry.tristate; break; } } @@ -143,9 +143,9 @@ public GroupHandler(String name, boolean isEnabled, int priority, if (k instanceof FlagBitSet) { FlagBitSet flags = (FlagBitSet) k; Tristate state = Tristate.UNDEFINED; - for (Entry entry : GroupHandler.this.defaultPermissions) { + for (TristateEntry entry : GroupHandler.this.defaultPermissions) { if (flags.toFlagSet().containsAll(entry.set)) { - state = entry.state; + state = entry.tristate; break; } } @@ -405,10 +405,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - List permissions = getGroupPermissions(group); - Entry entry = new Entry(flags, state); + List permissions = getGroupPermissions(group); + TristateEntry entry = new TristateEntry(flags, state); - for (Entry existing : permissions) { + for (TristateEntry existing : permissions) { if (existing.set.equals(entry.set)) return ProcessResult.of(false, Text.of("Entry already exists with this flag set!")); } @@ -431,7 +431,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "remove": { if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify flags or an index to remove!")); - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to remove in this group!"); try { int index = Integer.parseInt(parse.args[3]); if (index < 0) index = 0; @@ -449,8 +450,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } } - Entry entry = null; - for (Entry existing : permissions) { + TristateEntry entry = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(flags)) { entry = existing; break; @@ -464,7 +465,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "set": { if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify an index or flags and then a tristate value!")); - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to set in this group!"); try { int index = Integer.parseInt(parse.args[3]); if (index < 0) index = 0; @@ -519,10 +521,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null && !clear) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - Entry entry = new Entry(flags, state); + TristateEntry entry = new TristateEntry(flags, state); - Entry original = null; - for (Entry existing : permissions) { + TristateEntry original = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(entry.set)) { original = existing; break; @@ -555,9 +557,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } } case "move": { - List permissions = getGroupPermissions(group); + List permissions = getGroupPermissions(group); if (parse.args.length < 4) return ProcessResult.of(false, Text.of("Must specify flags or an index to move!")); + if(permissions.isEmpty()) return ProcessResult.of(false, "There are no entries to move in this group!"); try { int from = Integer.parseInt(parse.args[3]); if (from < 0) from = 0; @@ -595,8 +598,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } if (!set) return ProcessResult.of(false, Text.of("Must specify a target index!")); - Entry entry = null; - for (Entry existing : permissions) { + TristateEntry entry = null; + for (TristateEntry existing : permissions) { if (existing.set.equals(flags)) { entry = existing; break; @@ -860,12 +863,12 @@ public Text details(CommandSource source, String arguments) { TextActions.showText(Text.of("Click to add a flag entry")), group.displayName + ":\n")); int index = 0; - for (Entry entry : this.groupPermissions.get(group)) { + for (TristateEntry entry : this.groupPermissions.get(group)) { StringBuilder stringBuilder = new StringBuilder(); entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); Text.Builder entryBuilder = Text.builder(); entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")) - .append(FGUtil.readableTristateText(entry.state)) + .append(FGUtil.readableTristateText(entry.tristate)) .onHover(TextActions.showText(Text.of("Click to change this flag entry"))) .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " flags " + group.name + " set " + (index++) + " ")); builder.append(entryBuilder.build()).append(Text.NEW_LINE); @@ -876,14 +879,12 @@ public Text details(CommandSource source, String arguments) { TextActions.showText(Text.of("Click to add a flag entry")), this.defaultGroup.displayName + ":")); int index = 0; - for (Entry entry : this.defaultPermissions) { + for (TristateEntry entry : this.defaultPermissions) { StringBuilder stringBuilder = new StringBuilder(); - for (Flag flag : entry.set) { - stringBuilder.append(flag.name).append(" "); - } + entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); Text.Builder entryBuilder = Text.builder(); entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")) - .append(FGUtil.readableTristateText(entry.state)) + .append(FGUtil.readableTristateText(entry.tristate)) .onHover(TextActions.showText(Text.of("Click to change this flag entry"))) .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " flags default set " + (index++) + " ")); builder.append(Text.NEW_LINE).append(entryBuilder.build()); @@ -907,11 +908,11 @@ public void save(Path directory) { for (Group group : this.groups) { List stringEntries = flagMapDB.indexTreeList(group.name, Serializer.STRING).createOrOpen(); stringEntries.clear(); - stringEntries.addAll(this.groupPermissions.get(group).stream().map(Entry::serialize).collect(Collectors.toList())); + stringEntries.addAll(this.groupPermissions.get(group).stream().map(TristateEntry::serialize).collect(Collectors.toList())); } List stringEntries = flagMapDB.indexTreeList("default", Serializer.STRING).createOrOpen(); stringEntries.clear(); - stringEntries.addAll(this.defaultPermissions.stream().map(Entry::serialize).collect(Collectors.toList())); + stringEntries.addAll(this.defaultPermissions.stream().map(TristateEntry::serialize).collect(Collectors.toList())); } { Path groupsFile = directory.resolve("groups.cfg"); @@ -1030,15 +1031,15 @@ public boolean renameGroup(Group group, String newName) { } else return false; } - public boolean addFlagEntry(Group group, Entry entry) { + public boolean addFlagEntry(Group group, TristateEntry entry) { return addFlagEntry(group, 0, entry); } - public boolean addFlagEntry(Group group, int index, Entry entry) { - List groupEntries = getGroupPermissions(group); + public boolean addFlagEntry(Group group, int index, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); if (index < 0 || index > groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + groupEntries.size()); - for (Entry groupEntry : groupEntries) { + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) return false; } groupEntries.add(index, entry); @@ -1046,11 +1047,11 @@ public boolean addFlagEntry(Group group, int index, Entry entry) { return true; } - public void setFlagEntry(Group group, Entry entry) { - List groupEntries = getGroupPermissions(group); - for (Entry groupEntry : groupEntries) { + public void setFlagEntry(Group group, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) { - groupEntry.state = entry.state; + groupEntry.tristate = entry.tristate; return; } } @@ -1058,10 +1059,10 @@ public void setFlagEntry(Group group, Entry entry) { clearFlagCacheForGroup(group); } - public void setFlagEntry(Group group, int index, Entry entry) { - List groupEntries = getGroupPermissions(group); - Entry original = null; - for (Entry groupEntry : groupEntries) { + public void setFlagEntry(Group group, int index, TristateEntry entry) { + List groupEntries = getGroupPermissions(group); + TristateEntry original = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(entry.set)) { original = groupEntry; break; @@ -1073,18 +1074,18 @@ public void setFlagEntry(Group group, int index, Entry entry) { } public void setFlagEntry(Group group, int index, Tristate state) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (index < 0 || index >= groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (groupEntries.size() - 1)); - Entry entry = groupEntries.get(index); - entry.state = state; + TristateEntry entry = groupEntries.get(index); + entry.tristate = state; clearFlagCacheForGroup(group); } public boolean removeFlagEntry(Group group, Set flags) { - List groupEntries = getGroupPermissions(group); - Entry toRemove = null; - for (Entry groupEntry : groupEntries) { + List groupEntries = getGroupPermissions(group); + TristateEntry toRemove = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(flags)) { toRemove = groupEntry; } @@ -1096,7 +1097,7 @@ public boolean removeFlagEntry(Group group, Set flags) { } public void removeFlagEntry(Group group, int index) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (index < 0 || index >= groupEntries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (groupEntries.size() - 1)); groupEntries.remove(index); @@ -1104,11 +1105,11 @@ public void removeFlagEntry(Group group, int index) { } public boolean moveFlagEntry(Group group, Set flags, int destination) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (destination < 0 || destination >= groupEntries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (groupEntries.size() - 1)); - Entry toMove = null; - for (Entry groupEntry : groupEntries) { + TristateEntry toMove = null; + for (TristateEntry groupEntry : groupEntries) { if (groupEntry.set.equals(flags)) { toMove = groupEntry; } @@ -1120,12 +1121,12 @@ public boolean moveFlagEntry(Group group, Set flags, int destination) { } public void moveFlagEntry(Group group, int source, int destination) { - List groupEntries = getGroupPermissions(group); + List groupEntries = getGroupPermissions(group); if (source < 0 || source >= groupEntries.size()) throw new IndexOutOfBoundsException("Source index out of bounds: " + source + " Range: 0-" + (groupEntries.size() - 1)); if (destination < 0 || destination >= groupEntries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (groupEntries.size() - 1)); - Entry entry = groupEntries.remove(source); + TristateEntry entry = groupEntries.remove(source); groupEntries.add(destination, entry); clearFlagCacheForGroup(group); } @@ -1146,7 +1147,7 @@ private void clearFlagCacheForGroup(Group group) { } - private List getGroupPermissions(Group group) { + private List getGroupPermissions(Group group) { if (group == this.defaultGroup) return this.defaultPermissions; else return this.groupPermissions.get(group); } @@ -1260,17 +1261,17 @@ public IHandler create(Path directory, String name, int priority, boolean isEnab String defaultDisplayName = defaultNode.getNode("displayname").getString("Default"); TextColor defaultColor = Sponge.getRegistry().getType(TextColor.class, defaultNode.getNode("color").getString("red")).orElse(TextColors.RED); - Map> groupPermissions = new HashMap<>(); - List defaultPermissions; + Map> groupPermissions = new HashMap<>(); + List defaultPermissions; try (DB flagMapDB = DBMaker.fileDB(directory.resolve("flags.foxdb").normalize().toString()).make()) { for (Group group : groups) { List stringEntries = flagMapDB.indexTreeList(group.name, Serializer.STRING).createOrOpen(); groupPermissions.put(group, stringEntries.stream() - .map(Entry::deserialize) + .map(TristateEntry::deserialize) .collect(Collectors.toList())); } List stringEntries = flagMapDB.indexTreeList("default", Serializer.STRING).createOrOpen(); - defaultPermissions = stringEntries.stream().map(Entry::deserialize).collect(Collectors.toList()); + defaultPermissions = stringEntries.stream().map(TristateEntry::deserialize).collect(Collectors.toList()); } return new GroupHandler(name, isEnabled, priority, diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java new file mode 100644 index 0000000..ea2c593 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java @@ -0,0 +1,714 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package net.foxdenstudio.sponge.foxguard.plugin.handler; + +import com.google.common.collect.ImmutableList; +import net.foxdenstudio.sponge.foxcore.common.util.CacheMap; +import net.foxdenstudio.sponge.foxcore.common.util.FCCUtil; +import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParser; +import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; +import net.foxdenstudio.sponge.foxcore.plugin.util.FCPUtil; +import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagBitSet; +import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; +import net.foxdenstudio.sponge.foxguard.plugin.handler.util.PermissionEntry; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; +import net.foxdenstudio.sponge.foxguard.plugin.object.factory.IHandlerFactory; +import net.foxdenstudio.sponge.foxguard.plugin.util.ExtraContext; +import ninja.leaping.configurate.commented.CommentedConfigurationNode; +import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; +import org.spongepowered.api.command.CommandException; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.entity.living.player.User; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.action.TextActions; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.*; +import java.util.stream.Collectors; + +import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.*; + +/** + * Created by Fox on 11/15/2016. + */ +public class PermissionHandler extends HandlerBase { + + private final List entries; + private String defaultPermission; + private final Map> permCache; + + public PermissionHandler(String name, int priority, boolean isEnabled) { + this(name, priority, isEnabled, + new ArrayList<>(), ""); + } + + public PermissionHandler(String name, int priority, boolean isEnabled, List entries, String defaultPermission) { + super(name, priority, isEnabled); + this.entries = entries; + this.defaultPermission = defaultPermission; + this.permCache = new CacheMap<>((k, m) -> { + if (k instanceof FlagBitSet) { + FlagBitSet flags = (FlagBitSet) k; + List perms = new ArrayList<>(); + final String prefix = "foxguard.handler." + this.name; + for (PermissionEntry entry : entries) { + if (flags.toFlagSet().containsAll(entry.set)) { + perms.add(expandPermission(entry.permission)); + } + } + perms.add(expandPermission(defaultPermission)); + m.put(flags, perms); + return perms; + } else return null; + }); + } + + @Override + public String getShortTypeName() { + return "Perm"; + } + + @Override + public String getLongTypeName() { + return "Permission"; + } + + @Override + public String getUniqueTypeString() { + return "permission"; + } + + @Override + public EventResult handle(@Nullable User user, FlagBitSet flags, ExtraContext extra) { + if (user == null) return EventResult.pass(); + for (String permission : this.permCache.get(flags)) { + if (user.hasPermission(permission + ".allow")) return EventResult.allow(); + if (user.hasPermission(permission + ".deny")) return EventResult.deny(); + if (user.hasPermission(permission + ".pass")) return EventResult.pass(); + } + return EventResult.pass(); + } + + @Override + public ProcessResult modify(CommandSource source, String arguments) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder().arguments(arguments).parse(); + + if (parse.args.length < 1) return ProcessResult.of(false, Text.of("Must specify a command!")); + + if (isIn(ENTRIES_ALIASES, parse.args[0]) || isIn(FLAGS_ALIASES, parse.args[0])) { + if (parse.args.length < 2) return ProcessResult.of(false, Text.of("Must specify an entry operation!")); + switch (parse.args[1].toLowerCase()) { + case "add": { + String perm = null; + Set flags = new HashSet<>(); + boolean areFlagsSet = false; + if (parse.args.length < 3) + return ProcessResult.of(false, Text.of("Must specify flags and a permission string!")); + for (int i = 2; i < parse.args.length; i++) { + String argument = parse.args[i]; + if (argument.startsWith("=")) { + argument = argument.substring(1); + if (checkPermissionString(argument)) { + perm = argument; + } else { + return ProcessResult.of(false, "\"" + argument + "\" is not a valid permission!"); + } + } else { + Optional flagOptional = FlagRegistry.getInstance().getFlag(argument); + if (flagOptional.isPresent()) { + flags.add(flagOptional.get()); + areFlagsSet = true; + } else { + return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); + } + } + } + if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); + if (perm == null) return ProcessResult.of(false, Text.of("Must specify a permission string!")); + PermissionEntry entry = new PermissionEntry(flags, perm); + + for (PermissionEntry existing : this.entries) { + if (existing.set.equals(entry.set)) + return ProcessResult.of(false, Text.of("Entry already exists with this flag set!")); + } + + int index = 0; + if (parse.flags.containsKey("index")) { + String number = parse.flags.get("index"); + if (!number.isEmpty()) { + try { + index = Integer.parseInt(number); + if (index < 0) index = 0; + else if (index > this.entries.size()) index = this.entries.size(); + } catch (NumberFormatException ignored) { + } + } + } + this.addFlagEntry(index, entry); + return ProcessResult.of(true, Text.of("Successfully added flag entry!")); + } + case "remove": { + if (parse.args.length < 3) + return ProcessResult.of(false, Text.of("Must specify flags or an index to remove!")); + if (this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to remove!"); + try { + int index = Integer.parseInt(parse.args[2]); + if (index < 0) index = 0; + else if (index >= this.entries.size()) index = this.entries.size() - 1; + removeFlagEntry(index); + return ProcessResult.of(true, Text.of("Successfully removed flag entry!")); + } catch (NumberFormatException ignored) { + Set flags = new HashSet<>(); + for (int i = 2; i < parse.args.length; i++) { + String argument = parse.args[i]; + Optional flagOptional = FlagRegistry.getInstance().getFlag(argument); + if (flagOptional.isPresent()) { + flags.add(flagOptional.get()); + } else { + return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); + } + } + PermissionEntry entry = null; + for (PermissionEntry existing : this.entries) { + if (existing.set.equals(flags)) { + entry = existing; + break; + } + } + if (entry == null) return ProcessResult.of(false, Text.of("No flag entry with these flags!")); + removeFlagEntry(this.entries.indexOf(entry)); + return ProcessResult.of(true, Text.of("Successfully removed flag entry!")); + } + } + case "set": { + if (parse.args.length < 3) + return ProcessResult.of(false, Text.of("Must specify an index or flags and then a tristate value!")); + if (this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to set!"); + try { + int index = Integer.parseInt(parse.args[2]); + if (index < 0) index = 0; + else if (index >= this.entries.size()) index = this.entries.size() - 1; + if (parse.args.length < 4) + return ProcessResult.of(false, Text.of("Must specify a permission string!")); + String perm = parse.args[3]; + if (perm.startsWith("=")) perm = perm.substring(1); + if (checkPermissionString(perm)) { + this.setFlagEntry(index, perm); + return ProcessResult.of(true, "Successfully updated entry!"); + } else { + return ProcessResult.of(false, "\"" + perm + "\" is not a valid permission!"); + } + } catch (NumberFormatException ignored) { + } + String perm = null; + Set flags = new HashSet<>(); + boolean areFlagsSet = false; + for (int i = 2; i < parse.args.length; i++) { + String argument = parse.args[i]; + if (argument.startsWith("=")) { + argument = argument.substring(1); + if (checkPermissionString(argument)) { + perm = argument; + } else { + return ProcessResult.of(false, "\"" + argument + "\" is not a valid permission!"); + } + } else { + Optional flagOptional = FlagRegistry.getInstance().getFlag(argument); + if (flagOptional.isPresent()) { + flags.add(flagOptional.get()); + areFlagsSet = true; + } else { + return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); + } + } + } + if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); + if (perm == null) return ProcessResult.of(false, Text.of("Must specify a permission string!")); + PermissionEntry entry = new PermissionEntry(flags, perm); + + int index = 0; + if (parse.flags.containsKey("index")) { + String number = parse.flags.get("index"); + if (!number.isEmpty()) { + try { + index = Integer.parseInt(number); + if (index < 0) index = 0; + else if (index > this.entries.size()) index = this.entries.size(); + } catch (NumberFormatException ignored) { + } + } + } + this.setFlagEntry(index, entry); + return ProcessResult.of(true, Text.of("Successfully set flag entry!")); + + } + case "move": { + if (parse.args.length < 2) { + return ProcessResult.of(false, Text.of("Must specify flags or an index to move!")); + } + if (this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to move!"); + try { + int from = Integer.parseInt(parse.args[1]); + if (from < 0) from = 0; + else if (from >= this.entries.size()) from = this.entries.size() - 1; + if (parse.args.length < 3) + return ProcessResult.of(false, Text.of("Must specify a target index to move to!")); + int to = FCCUtil.parseCoordinate(from, parse.args[2]); + if (to < 0) to = 0; + else if (to >= this.entries.size()) to = this.entries.size() - 1; + moveFlagEntry(from, to); + return ProcessResult.of(true, Text.of("Successfully moved flag entry!")); + } catch (NumberFormatException ignored) { + Set flags = new HashSet<>(); + int index = 0; + boolean set = false; + boolean relative = false; + for (int i = 1; i < parse.args.length; i++) { + String argument = parse.args[i]; + Optional flagOptional = FlagRegistry.getInstance().getFlag(argument); + if (flagOptional.isPresent()) { + flags.add(flagOptional.get()); + continue; + } + try { + String arg = argument; + if (arg.startsWith("~")) { + arg = arg.substring(1); + relative = true; + } + index = Integer.parseInt(arg); + set = true; + continue; + } catch (NumberFormatException ignored2) { + } + return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); + } + if (!set) return ProcessResult.of(false, Text.of("Must specify a target index!")); + PermissionEntry entry = null; + for (PermissionEntry existing : this.entries) { + if (existing.set.equals(flags)) { + entry = existing; + break; + } + } + + if (entry == null) return ProcessResult.of(false, Text.of("No flag entry with these flags!")); + if (relative) index += this.entries.indexOf(entry); + moveFlagEntry(entry.set, index); + } + return ProcessResult.of(true, Text.of("Successfully moved flag entry!")); + } + default: + return ProcessResult.of(false, Text.of("Not a valid flag operation!")); + } + } else if (parse.args[0].equalsIgnoreCase("default")) { + if (parse.args.length > 1) { + String perm = parse.args[1]; + if (perm.startsWith("=")) perm = perm.substring(1); + if (checkPermissionString(perm)) { + defaultPermission = perm; + return ProcessResult.of(true, Text.of(TextColors.GREEN, "Successfully set default permission to ", + TextColors.AQUA, "\"", + TextColors.RESET, expandPermission(perm), + TextColors.AQUA, "\"", + TextColors.GREEN, "!")); + } else { + return ProcessResult.of(false, "\"" + perm + "\" is not a valid permission!"); + } + } else { + defaultPermission = ""; + return ProcessResult.of(true, "Successfully reset default permission!"); + } + } else { + return ProcessResult.of(false, "Not a valid modification command!"); + } + } + + @Override + public List modifySuggestions(CommandSource source, String arguments, @Nullable Location targetPosition) throws CommandException { + AdvCmdParser.ParseResult parse = AdvCmdParser.builder() + .arguments(arguments) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + + if (parse.current.type == AdvCmdParser.CurrentElement.ElementType.ARGUMENT) { + if (parse.current.index == 0) { + return ImmutableList.of("entries", "default").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(ENTRIES_ALIASES, parse.args[0]) || isIn(FLAGS_ALIASES, parse.args[0])) { + if (parse.current.index == 1) { + return ImmutableList.of("add", "set", "remove", "move").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.index > 1) { + switch (parse.args[1].toLowerCase()) { + case "add": { + if (parse.current.token.startsWith("=")) { + String perm = parse.current.token.substring(1); + if (!perm.isEmpty()) { + if (checkPermissionString(perm)) { + source.sendMessage(Text.of(TextColors.GREEN, "Permission is valid!")); + } else { + source.sendMessage(Text.of(TextColors.RED, "Permission is not valid!")); + } + } + return ImmutableList.of(); + } else { + String[] flagArgs = Arrays.copyOfRange(parse.args, 2, parse.args.length); + return FlagRegistry.getInstance().getFlagList().stream() + .map(Flag::getName) + .filter(arg -> !isIn(flagArgs, arg)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + case "set": { + if (parse.current.index == 2) { + if (parse.current.token.startsWith("=")) { + String perm = parse.current.token.substring(1); + if (!perm.isEmpty()) { + if (checkPermissionString(perm)) { + source.sendMessage(Text.of(TextColors.GREEN, "Permission is valid!")); + } else { + source.sendMessage(Text.of(TextColors.RED, "Permission is not valid!")); + } + } + return ImmutableList.of(); + } else { + return FlagRegistry.getInstance().getFlagList().stream() + .map(Flag::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index == 3) try { + Integer.parseInt(parse.args[2]); + String perm = parse.current.token.substring(1); + if (!perm.isEmpty()) { + if (checkPermissionString(perm)) { + source.sendMessage(Text.of(TextColors.GREEN, "Permission is valid!")); + } else { + source.sendMessage(Text.of(TextColors.RED, "Permission is not valid!")); + } + } + return ImmutableList.of(); + } catch (NumberFormatException ignored) { + } + if (parse.current.token.startsWith("=")) { + String perm = parse.current.token.substring(1); + if (!perm.isEmpty()) { + if (checkPermissionString(perm)) { + source.sendMessage(Text.of(TextColors.GREEN, "Permission is valid!")); + } else { + source.sendMessage(Text.of(TextColors.RED, "Permission is not valid!")); + } + } + return ImmutableList.of(); + } else { + String[] flagArgs = Arrays.copyOfRange(parse.args, 2, parse.args.length); + return FlagRegistry.getInstance().getFlagList().stream() + .map(Flag::getName) + .filter(arg -> !isIn(flagArgs, arg)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + case "move": + case "remove": { + String[] flagArgs = Arrays.copyOfRange(parse.args, 2, parse.args.length); + return FlagRegistry.getInstance().getFlagList().stream() + .map(Flag::getName) + .filter(arg -> !isIn(flagArgs, arg)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } + } else if (parse.args[0].equalsIgnoreCase("default")) { + if (parse.current.index == 1 && !parse.current.token.isEmpty()) { + String perm = parse.current.token; + if (perm.startsWith("=")) perm = perm.substring(1); + if (checkPermissionString(perm)) { + source.sendMessage(Text.of(TextColors.GREEN, "Permission is valid!")); + } else { + source.sendMessage(Text.of(TextColors.RED, "Permission is not valid!")); + } + return ImmutableList.of(); + } + } + } else if (parse.current.type.equals(AdvCmdParser.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + + @Override + public Text details(CommandSource source, String arguments) { + Text.Builder builder = Text.builder(); + + Text prefix = Text.of("foxguard.handler.", TextColors.GOLD, this.name); + Text postfix = Text.of(".", + TextColors.AQUA, "<", + TextColors.GREEN, "allow", + TextColors.AQUA, "/", + TextColors.RED, "deny", + TextColors.AQUA, "/", + TextColors.YELLOW, "pass", + TextColors.AQUA, ">"); + builder.append(Text.of( + TextActions.showText(Text.of("Click to add a permission entry")), + TextActions.suggestCommand("/foxguard md h " + this.name + " entries add "), + TextColors.GREEN, "Entries:" + )); + int index = 0; + for (PermissionEntry entry : this.entries) { + StringBuilder stringBuilder = new StringBuilder(); + entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); + Text.Builder entryBuilder = Text.builder(); + entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")); + if (entry.permission.isEmpty()) { + entryBuilder.append(prefix); + } else if (entry.permission.startsWith(".")) { + entryBuilder.append(prefix); + entryBuilder.append(Text.of(entry.permission)); + } else { + entryBuilder.append(Text.of(entry.permission)); + } + entryBuilder.append(postfix) + .onHover(TextActions.showText(Text.of("Click to change this entry"))) + .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + "entries set " + (index++) + " ")); + builder.append(Text.NEW_LINE).append(entryBuilder.build()); + } + Text.Builder entryBuilder = Text.builder(); + entryBuilder.append(Text.of(TextColors.RED, "Default: ")); + if (defaultPermission.isEmpty()) { + entryBuilder.append(prefix); + } else if (defaultPermission.startsWith(".")) { + entryBuilder.append(prefix); + entryBuilder.append(Text.of(defaultPermission)); + } else { + entryBuilder.append(Text.of(defaultPermission)); + } + entryBuilder.append(postfix) + .onHover(TextActions.showText(Text.of("Click to change this entry"))) + .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " default ")); + builder.append(Text.NEW_LINE).append(entryBuilder.build()); + + return builder.build(); + } + + @Override + public List detailsSuggestions(CommandSource source, String + arguments, @Nullable Location targetPosition) { + return ImmutableList.of(); + } + + @Override + public void save(Path directory) { + Path permissionFile = directory.resolve("permissions.cfg"); + ConfigurationLoader loader = + HoconConfigurationLoader.builder().setPath(permissionFile).build(); + CommentedConfigurationNode root = FCPUtil.getHOCONConfiguration(permissionFile, loader); + root.getNode("default").setValue(defaultPermission); + root.getNode("entries").setValue(this.entries.stream() + .map(PermissionEntry::serialize) + .collect(Collectors.toList()) + ); + try { + loader.save(root); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean addFlagEntry(PermissionEntry entry) { + return addFlagEntry(0, entry); + } + + public boolean addFlagEntry(int index, PermissionEntry entry) { + if (index < 0 || index > this.entries.size()) + throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + this.entries.size()); + for (PermissionEntry groupEntry : this.entries) { + if (groupEntry.set.equals(entry.set)) return false; + } + this.entries.add(index, entry); + this.permCache.clear(); + return true; + } + + public void setFlagEntry(PermissionEntry entry) { + for (PermissionEntry groupEntry : this.entries) { + if (groupEntry.set.equals(entry.set)) { + groupEntry.permission = entry.permission; + return; + } + } + this.entries.add(entry); + this.permCache.clear(); + } + + public void setFlagEntry(int index, PermissionEntry entry) { + PermissionEntry original = null; + for (PermissionEntry groupEntry : this.entries) { + if (groupEntry.set.equals(entry.set)) { + original = groupEntry; + break; + } + } + if (original != null) this.entries.remove(original); + this.entries.add(index, entry); + this.permCache.clear(); + } + + public void setFlagEntry(int index, String permission) { + if (index < 0 || index >= this.entries.size()) + throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (this.entries.size() - 1)); + PermissionEntry entry = this.entries.get(index); + entry.permission = permission; + this.permCache.clear(); + } + + public boolean removeFlagEntry(Set flags) { + PermissionEntry toRemove = null; + for (PermissionEntry groupEntry : this.entries) { + if (groupEntry.set.equals(flags)) { + toRemove = groupEntry; + } + } + if (toRemove == null) return false; + this.entries.remove(toRemove); + this.permCache.clear(); + return true; + } + + public void removeFlagEntry(int index) { + if (index < 0 || index >= this.entries.size()) + throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (this.entries.size() - 1)); + this.entries.remove(index); + this.permCache.clear(); + } + + public boolean moveFlagEntry(Set flags, int destination) { + if (destination < 0 || destination >= this.entries.size()) + throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (this.entries.size() - 1)); + PermissionEntry toMove = null; + for (PermissionEntry groupEntry : this.entries) { + if (groupEntry.set.equals(flags)) { + toMove = groupEntry; + } + } + if (toMove == null) return false; + this.entries.remove(toMove); + this.permCache.clear(); + return true; + } + + public void moveFlagEntry(int source, int destination) { + if (source < 0 || source >= this.entries.size()) + throw new IndexOutOfBoundsException("Source index out of bounds: " + source + " Range: 0-" + (this.entries.size() - 1)); + if (destination < 0 || destination >= this.entries.size()) + throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (this.entries.size() - 1)); + PermissionEntry entry = this.entries.remove(source); + this.entries.add(destination, entry); + this.permCache.clear(); + } + + private boolean checkPermissionString(String perm) { + return !(!perm.isEmpty() && (!perm.matches("[\\w\\-.]+") || + perm.matches("^.*\\.\\..*$") || + perm.endsWith("."))); + } + + private String expandPermission(String perm) { + if (perm.isEmpty() || perm.startsWith(".")) perm = "foxguard.handler." + this.name + perm; + return perm; + } + + public static class Factory implements IHandlerFactory { + + public static final String[] ALIASES = {"perm", "perms", "permission", "permissions"}; + + @Override + public IHandler create(String name, int priority, String arguments, CommandSource source) throws CommandException { + return new PermissionHandler(name, priority, true); + } + + @Override + public IHandler create(Path directory, String name, int priority, boolean isEnabled) { + Path permissionsFile = directory.resolve("permissions.cfg"); + ConfigurationLoader loader = + HoconConfigurationLoader.builder().setPath(permissionsFile).build(); + CommentedConfigurationNode root = FCPUtil.getHOCONConfiguration(permissionsFile, loader); + List> optionalFlagsList = root.getNode("entries").getList(o -> { + if (o instanceof String) { + return Optional.of((String) o); + } else return Optional.empty(); + }); + List entries = optionalFlagsList.stream() + .filter(Optional::isPresent) + .map(Optional::get) + .map(PermissionEntry::deserialize) + .collect(Collectors.toList()); + String defaultPermission = root.getNode("default").getString(""); + return new PermissionHandler(name, priority, isEnabled, entries, defaultPermission); + } + + @Override + public String[] getAliases() { + return ALIASES; + } + + @Override + public String getType() { + return "permission"; + } + + @Override + public String getPrimaryAlias() { + return getType(); + } + + @Override + public List createSuggestions(CommandSource source, String arguments, String type, @Nullable Location targetPosition) throws CommandException { + return ImmutableList.of(); + } + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/StaticHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/StaticHandler.java index abf6cff..b199d20 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/StaticHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/StaticHandler.java @@ -36,7 +36,7 @@ import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagBitSet; import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Entry; +import net.foxdenstudio.sponge.foxguard.plugin.handler.util.TristateEntry; import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import net.foxdenstudio.sponge.foxguard.plugin.object.factory.IHandlerFactory; import net.foxdenstudio.sponge.foxguard.plugin.util.ExtraContext; @@ -74,7 +74,7 @@ public class StaticHandler extends HandlerBase { return true; }; - private final List entries; + private final List entries; private final Map permCache; public StaticHandler(String name, int priority) { @@ -88,9 +88,9 @@ public StaticHandler(String name, int priority, boolean isEnabled) { if (k instanceof FlagBitSet) { FlagBitSet flags = (FlagBitSet) k; Tristate state = Tristate.UNDEFINED; - for (Entry entry : StaticHandler.this.entries) { + for (TristateEntry entry : StaticHandler.this.entries) { if (flags.toFlagSet().containsAll(entry.set)) { - state = entry.state; + state = entry.tristate; break; } } @@ -162,9 +162,9 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - Entry entry = new Entry(flags, state); + TristateEntry entry = new TristateEntry(flags, state); - for (Entry existing : this.entries) { + for (TristateEntry existing : this.entries) { if (existing.set.equals(entry.set)) return ProcessResult.of(false, Text.of("Entry already exists with this flag set!")); } @@ -187,6 +187,7 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "remove": { if (parse.args.length < 2) return ProcessResult.of(false, Text.of("Must specify flags or an index to remove!")); + if(this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to remove!"); try { int index = Integer.parseInt(parse.args[1]); if (index < 0) index = 0; @@ -204,8 +205,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } } - Entry entry = null; - for (Entry existing : this.entries) { + TristateEntry entry = null; + for (TristateEntry existing : this.entries) { if (existing.set.equals(flags)) { entry = existing; break; @@ -219,6 +220,7 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "set": { if (parse.args.length < 2) return ProcessResult.of(false, Text.of("Must specify an index or flags and then a tristate value!")); + if (this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to set!"); try { int index = Integer.parseInt(parse.args[1]); if (index < 0) index = 0; @@ -272,10 +274,10 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma } if (!areFlagsSet) return ProcessResult.of(false, Text.of("Must specify flags!")); if (state == null && !clear) return ProcessResult.of(false, Text.of("Must specify a tristate value!")); - Entry entry = new Entry(flags, state); + TristateEntry entry = new TristateEntry(flags, state); - Entry original = null; - for (Entry existing : this.entries) { + TristateEntry original = null; + for (TristateEntry existing : this.entries) { if (existing.set.equals(entry.set)) { original = existing; break; @@ -309,6 +311,7 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma case "move": { if (parse.args.length < 2) return ProcessResult.of(false, Text.of("Must specify flags or an index to move!")); + if (this.entries.isEmpty()) return ProcessResult.of(false, "There are no entries to move!"); try { int from = Integer.parseInt(parse.args[1]); if (from < 0) from = 0; @@ -346,8 +349,8 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("\"" + argument + "\" is not a valid flag!")); } if (!set) return ProcessResult.of(false, Text.of("Must specify a target index!")); - Entry entry = null; - for (Entry existing : this.entries) { + TristateEntry entry = null; + for (TristateEntry existing : this.entries) { if (existing.set.equals(flags)) { entry = existing; break; @@ -458,12 +461,12 @@ public Text details(CommandSource source, String arguments) { TextActions.showText(Text.of("Click to add a flag entry")), "Flags:")); int index = 0; - for (Entry entry : this.entries) { + for (TristateEntry entry : this.entries) { StringBuilder stringBuilder = new StringBuilder(); entry.set.stream().sorted().forEach(flag -> stringBuilder.append(flag.name).append(" ")); Text.Builder entryBuilder = Text.builder(); entryBuilder.append(Text.of(" " + index + ": " + stringBuilder.toString(), TextColors.AQUA, ": ")) - .append(FGUtil.readableTristateText(entry.state)) + .append(FGUtil.readableTristateText(entry.tristate)) .onHover(TextActions.showText(Text.of("Click to change this flag entry"))) .onClick(TextActions.suggestCommand("/foxguard md h " + this.name + " set " + (index++) + " ")); builder.append(Text.NEW_LINE).append(entryBuilder.build()); @@ -483,7 +486,7 @@ public void save(Path directory) { HoconConfigurationLoader.builder().setPath(flagsFile).build(); CommentedConfigurationNode root = FCPUtil.getHOCONConfiguration(flagsFile, loader); root.getNode("flags").setValue(this.entries.stream() - .map(Entry::serialize) + .map(TristateEntry::serialize) .collect(Collectors.toList()) ); try { @@ -493,14 +496,14 @@ public void save(Path directory) { } } - public boolean addFlagEntry(Entry entry) { + public boolean addFlagEntry(TristateEntry entry) { return addFlagEntry(0, entry); } - public boolean addFlagEntry(int index, Entry entry) { + public boolean addFlagEntry(int index, TristateEntry entry) { if (index < 0 || index > this.entries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + this.entries.size()); - for (Entry groupEntry : this.entries) { + for (TristateEntry groupEntry : this.entries) { if (groupEntry.set.equals(entry.set)) return false; } this.entries.add(index, entry); @@ -508,10 +511,10 @@ public boolean addFlagEntry(int index, Entry entry) { return true; } - public void setFlagEntry(Entry entry) { - for (Entry groupEntry : this.entries) { + public void setFlagEntry(TristateEntry entry) { + for (TristateEntry groupEntry : this.entries) { if (groupEntry.set.equals(entry.set)) { - groupEntry.state = entry.state; + groupEntry.tristate = entry.tristate; return; } } @@ -519,9 +522,9 @@ public void setFlagEntry(Entry entry) { this.permCache.clear(); } - public void setFlagEntry(int index, Entry entry) { - Entry original = null; - for (Entry groupEntry : this.entries) { + public void setFlagEntry(int index, TristateEntry entry) { + TristateEntry original = null; + for (TristateEntry groupEntry : this.entries) { if (groupEntry.set.equals(entry.set)) { original = groupEntry; break; @@ -535,14 +538,14 @@ public void setFlagEntry(int index, Entry entry) { public void setFlagEntry(int index, Tristate state) { if (index < 0 || index >= this.entries.size()) throw new IndexOutOfBoundsException("Index out of bounds: " + index + " Range: 0-" + (this.entries.size() - 1)); - Entry entry = this.entries.get(index); - entry.state = state; + TristateEntry entry = this.entries.get(index); + entry.tristate = state; this.permCache.clear(); } public boolean removeFlagEntry(Set flags) { - Entry toRemove = null; - for (Entry groupEntry : this.entries) { + TristateEntry toRemove = null; + for (TristateEntry groupEntry : this.entries) { if (groupEntry.set.equals(flags)) { toRemove = groupEntry; } @@ -563,8 +566,8 @@ public void removeFlagEntry(int index) { public boolean moveFlagEntry(Set flags, int destination) { if (destination < 0 || destination >= this.entries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (this.entries.size() - 1)); - Entry toMove = null; - for (Entry groupEntry : this.entries) { + TristateEntry toMove = null; + for (TristateEntry groupEntry : this.entries) { if (groupEntry.set.equals(flags)) { toMove = groupEntry; } @@ -580,7 +583,7 @@ public void moveFlagEntry(int source, int destination) { throw new IndexOutOfBoundsException("Source index out of bounds: " + source + " Range: 0-" + (this.entries.size() - 1)); if (destination < 0 || destination >= this.entries.size()) throw new IndexOutOfBoundsException("Destination index out of bounds: " + destination + " Range: 0-" + (this.entries.size() - 1)); - Entry entry = this.entries.remove(source); + TristateEntry entry = this.entries.remove(source); this.entries.add(destination, entry); this.permCache.clear(); } @@ -599,7 +602,7 @@ public void load(Path directory) { optionalFlagsList.stream() .filter(Optional::isPresent) .map(Optional::get) - .map(Entry::deserialize) + .map(TristateEntry::deserialize) .forEach(this.entries::add); this.permCache.clear(); } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Entry.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Entry.java index 31d6f59..661ed29 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Entry.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Entry.java @@ -38,17 +38,14 @@ /** * Created by Fox on 7/8/2016. */ -public class Entry { +public abstract class Entry { public Set set; - public Tristate state; - public Entry(Set set, Tristate state) { + public Entry(Set set) { this.set = set; - this.state = state; } - public Entry(Tristate state, Flag... flags) { - this.state = state; + public Entry(Flag... flags) { this.set = ImmutableSet.copyOf(flags); } @@ -60,23 +57,11 @@ public String serialize() { if (iterator.hasNext()) builder.append(","); } builder.append(":"); - builder.append(state.name()); + builder.append(serializeValue()); return builder.toString(); } - public static Entry deserialize(String string) { - FlagRegistry registry = FlagRegistry.getInstance(); - String[] parts = string.split(":"); - String[] flags = parts[0].split(","); - Set flagSet = new HashSet<>(); - for (String flagName : flags) { - Optional flagOptional = registry.getFlag(flagName); - if (flagOptional.isPresent()) { - flagSet.add(flagOptional.get()); - } - } - return new Entry(flagSet, Tristate.valueOf(parts[1])); - } + public abstract String serializeValue(); @Override public boolean equals(Object o) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/PermissionEntry.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/PermissionEntry.java new file mode 100644 index 0000000..7ff21df --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/PermissionEntry.java @@ -0,0 +1,71 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package net.foxdenstudio.sponge.foxguard.plugin.handler.util; + +import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +/** + * Created by Fox on 11/15/2016. + */ +public class PermissionEntry extends Entry { + + public String permission; + public boolean relative; + + public PermissionEntry(Set set, String permission) { + super(set); + this.permission = permission; + } + + public PermissionEntry(String permission, Flag... flags) { + super(flags); + this.permission = permission; + } + + @Override + public String serializeValue() { + return permission; + } + + public static PermissionEntry deserialize(String string){ + FlagRegistry registry = FlagRegistry.getInstance(); + String[] parts = string.split(":"); + String[] flags = parts[0].split(",", 2); + Set flagSet = new HashSet<>(); + for (String flagName : flags) { + Optional flagOptional = registry.getFlag(flagName); + if (flagOptional.isPresent()) { + flagSet.add(flagOptional.get()); + } + } + return new PermissionEntry(flagSet, parts[1]); + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/TristateEntry.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/TristateEntry.java new file mode 100644 index 0000000..43d7f52 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/TristateEntry.java @@ -0,0 +1,70 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package net.foxdenstudio.sponge.foxguard.plugin.handler.util; + +import net.foxdenstudio.sponge.foxguard.plugin.flag.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.flag.FlagRegistry; +import org.spongepowered.api.util.Tristate; + +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +/** + * Created by Fox on 11/15/2016. + */ +public class TristateEntry extends Entry { + + public Tristate tristate; + + public TristateEntry(Set set, Tristate tristate) { + super(set); + this.tristate = tristate; + } + + public TristateEntry(Tristate tristate, Flag... flags) { + super(flags); + this.tristate = tristate; + } + + public String serializeValue(){ + return tristate.name(); + } + + public static TristateEntry deserialize(String string) { + FlagRegistry registry = FlagRegistry.getInstance(); + String[] parts = string.split(":"); + String[] flags = parts[0].split(","); + Set flagSet = new HashSet<>(); + for (String flagName : flags) { + Optional flagOptional = registry.getFlag(flagName); + if (flagOptional.isPresent()) { + flagSet.add(flagOptional.get()); + } + } + return new TristateEntry(flagSet, Tristate.valueOf(parts[1])); + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java index a48a7cd..4a2e4b5 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/CuboidRegion.java @@ -46,6 +46,7 @@ import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -71,25 +72,23 @@ public CuboidRegion(String name, List positions, String[] ar throws CommandException { super(name, true); List allPositions = new ArrayList<>(positions); + Vector3i sourcePos = source instanceof Locatable ? ((Locatable) source).getLocation().getBlockPosition() : Vector3i.ZERO; for (int i = 0; i < args.length - 2; i += 3) { int x, y, z; try { - x = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockX() : 0, args[i]); + x = FCCUtil.parseCoordinate(sourcePos.getX(), args[i]); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + args[i] + "\"!"), e, args[i], i); } try { - y = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockY() : 0, args[i + 1]); + y = FCCUtil.parseCoordinate(sourcePos.getY(), args[i + 1]); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + args[i + 1] + "\"!"), e, args[i + 1], i + 1); } try { - z = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockZ() : 0, args[i + 2]); + z = FCCUtil.parseCoordinate(sourcePos.getZ(), args[i + 2]); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + args[i + 2] + "\"!"), e, args[i + 2], i + 2); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java index 164c2be..b8d9edc 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/ElevationRegion.java @@ -39,9 +39,9 @@ import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; -import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -67,11 +67,11 @@ public ElevationRegion(String name, List positions, String[] throws CommandException { super(name, true); List allPositions = new ArrayList<>(positions); + int sourceY = source instanceof Locatable ? ((Locatable) source).getLocation().getBlockY() : 0; for (String arg : args) { int y; try { - y = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockY() : 0, arg); + y = FCCUtil.parseCoordinate(sourceY, arg); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + arg + "\"!"), e, arg, 1); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java index 9e704f7..ed282b2 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/world/RectangularRegion.java @@ -45,6 +45,7 @@ import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -70,18 +71,17 @@ public RectangularRegion(String name, List positions, String throws CommandException { super(name, true); List allPositions = new ArrayList<>(positions); + Vector3i sourcePos = source instanceof Locatable ? ((Locatable) source).getLocation().getBlockPosition() : Vector3i.ZERO; for (int i = 0; i < args.length - 1; i += 2) { int x, z; try { - x = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockX() : 0, args[i]); + x = FCCUtil.parseCoordinate(sourcePos.getX(), args[i]); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + args[i] + "\"!"), e, args[i], i); } try { - z = FCCUtil.parseCoordinate(source instanceof Player ? - ((Player) source).getLocation().getBlockZ() : 0, args[i + 1]); + z = FCCUtil.parseCoordinate(sourcePos.getZ(), args[i + 1]); } catch (NumberFormatException e) { throw new ArgumentParseException( Text.of("Unable to parse \"" + args[i + 1] + "\"!"), e, args[i + 1], i + 1); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/RegionsStateField.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/RegionsStateField.java index 6d8a6f0..a8d43b1 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/RegionsStateField.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/RegionsStateField.java @@ -45,6 +45,7 @@ import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.Locatable; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; @@ -139,7 +140,7 @@ public List modifySuggestions(CommandSource source, String arguments, @N } else if (parse.current.index == 1) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -222,7 +223,7 @@ public ProcessResult add(CommandSource source, String arguments) throws CommandE if (region == null) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) { @@ -262,7 +263,7 @@ public ProcessResult remove(CommandSource source, String arguments) throws Comma if (matchCount != 1) { String worldName = parse.flags.get("world"); World world = null; - if (source instanceof Player) world = ((Player) source).getWorld(); + if (source instanceof Locatable) world = ((Locatable) source).getWorld(); if (!worldName.isEmpty()) { Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); if (optWorld.isPresent()) {