From 49f4526f9fa99e2485d2cfe3a44c3fa66814066f Mon Sep 17 00:00:00 2001 From: gravityfox Date: Mon, 4 Jan 2016 07:19:25 -0800 Subject: [PATCH] Tab completion yay. --- FoxCore | 2 +- build.gradle | 5 +- permissionslist.txt | 4 +- .../sponge/foxguard/plugin/FGManager.java | 2 + .../sponge/foxguard/plugin/FoxGuardMain.java | 2 +- .../plugin/command/CommandCreate.java | 56 ++++++- .../plugin/command/CommandDelete.java | 68 ++++++++- .../plugin/command/CommandDetail.java | 72 ++++++++- .../plugin/command/CommandEnableDisable.java | 67 ++++++++- .../foxguard/plugin/command/CommandHere.java | 43 +++++- .../foxguard/plugin/command/CommandLink.java | 7 +- .../foxguard/plugin/command/CommandList.java | 39 ++++- .../plugin/command/CommandModify.java | 101 +++++++++++-- .../plugin/command/CommandRename.java | 68 ++++++++- .../plugin/handler/GlobalHandler.java | 140 ++++++++++-------- .../plugin/handler/PassiveHandler.java | 102 ++++++++++++- .../plugin/handler/PermissionHandler.java | 10 +- .../plugin/handler/SimpleHandler.java | 138 ++++++++++++++--- .../foxguard/plugin/object/IFGObject.java | 5 +- .../object/factory/FGFactoryManager.java | 25 +++- .../object/factory/FGHandlerFactory.java | 11 +- .../object/factory/FGRegionFactory.java | 33 +++-- .../plugin/object/factory/IFGFactory.java | 1 + .../foxguard/plugin/region/CuboidRegion.java | 8 +- .../plugin/region/ElevationRegion.java | 9 +- .../foxguard/plugin/region/GlobalRegion.java | 10 +- .../plugin/region/RectangularRegion.java | 9 +- .../plugin/state/HandlersStateField.java | 108 ++++++++++++-- .../plugin/state/RegionsStateField.java | 99 ++++++++++++- 29 files changed, 1055 insertions(+), 189 deletions(-) diff --git a/FoxCore b/FoxCore index 3ceae4c..c55e5a5 160000 --- a/FoxCore +++ b/FoxCore @@ -1 +1 @@ -Subproject commit 3ceae4c28bacb9adab5de4ad60670b4dc20ef56d +Subproject commit c55e5a50163e0299dd874ed09ce6f09e5f137f7d diff --git a/build.gradle b/build.gradle index bd6044f..87638e7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,3 @@ - plugins { id 'java' // id 'com.github.johnrengelman.shadow' version '1.2.2' @@ -7,6 +6,8 @@ plugins { group 'net.foxdenstudio.sponge.foxguard' version 'ALPHA-SNAPSHOT' +apply from: project(":FoxCore").file("sponge.gradle") + sourceCompatibility = '1.8' targetCompatibility = '1.8' @@ -20,7 +21,7 @@ repositories { dependencies { compile project(':FoxCore') - compile 'org.spongepowered:spongeapi:2.1-SNAPSHOT' + compile "org.spongepowered:spongeapi:$spongeVersion" // compile 'org.mcstats.sponge:metrics:R8-SNAPSHOT' testCompile group: 'junit', name: 'junit', version: '4.11' } diff --git a/permissionslist.txt b/permissionslist.txt index ccc9252..48556b8 100644 --- a/permissionslist.txt +++ b/permissionslist.txt @@ -58,11 +58,11 @@ foxguard { } } link { - add { + link { own others } - remove { + unlink { own others } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java index f0d5f0b..2f3b707 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java @@ -44,6 +44,8 @@ public final class FGManager { + public static final String[] TYPES = {"region", "handler"}; + private static FGManager instance; private final Map> regions; private final List handlers; 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 1b26b30..c737128 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java @@ -85,7 +85,7 @@ public final class FoxGuardMain { /** * String object containing the current version of the plugin. */ - public static final String PLUGIN_VERSION = "0.13.0-SNAPSHOT";//VERSION + public static final String PLUGIN_VERSION = "0.14.3-SNAPSHOT";//VERSION /** * FoxGuardMain instance object. 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 42018c3..e03e7e7 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 @@ -42,8 +42,11 @@ 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.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; @@ -57,9 +60,10 @@ public class CommandCreate implements CommandCallable { private static final String[] PRIORITY_ALIASES = {"priority", "prio", "p", "order", "level", "rank"}; private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); - } else if (isAlias(PRIORITY_ALIASES, key) && !map.containsKey("priority")) { + } else if (isIn(PRIORITY_ALIASES, key) && !map.containsKey("priority")) { map.put("priority", value); } }; @@ -70,7 +74,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(3).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(3).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() @@ -79,7 +83,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm .build()); return CommandResult.empty(); //---------------------------------------------------------------------------------------------------------------------- - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); String worldName = parse.flagmap.get("world"); World world = null; @@ -111,7 +115,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.GREEN, "Region created successfully")); return CommandResult.success(); //---------------------------------------------------------------------------------------------------------------------- - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); if (parse.args[1].matches("^.*[^0-9a-zA-Z_$].*$")) throw new ArgumentParseException(Text.of("Name must be alphanumeric!"), parse.args[1], 1); @@ -143,6 +147,46 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(3) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index == 2) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + return FGFactoryManager.getInstance().getPrimaryRegionTypeAliases().stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGFactoryManager.getInstance().getPrimaryHandlerTypeAliases().stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world", "priority").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } @@ -163,6 +207,6 @@ public Optional getHelp(CommandSource source) { @Override public Text getUsage(CommandSource source) { - return Text.of("create ] | handler> [--priority:] [parse.args...]"); + return Text.of("create ] | handler> [--priority:] [args...]"); } } 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 71574a2..a754105 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 @@ -30,6 +30,7 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.GlobalRegion; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandCallable; @@ -40,8 +41,11 @@ 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.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; @@ -53,7 +57,8 @@ public class CommandDelete implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -64,14 +69,14 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() .append(Text.of(TextColors.GREEN, "Usage: ")) .append(getUsage(source)) .build()); return CommandResult.empty(); - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); String worldName = parse.flagmap.get("world"); World world = null; @@ -91,7 +96,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.GREEN, "Region deleted successfully!")); return CommandResult.success(); - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); if (parse.args[1].equalsIgnoreCase(GlobalHandler.NAME)) throw new CommandException(Text.of("You may not delete the global Handler!")); @@ -105,6 +110,61 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index == 1) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .filter(region -> !(region instanceof GlobalRegion)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGManager.getInstance().getHandlerListCopy().stream() + .filter(region -> !(region instanceof GlobalHandler)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } 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 1cd8d7e..2850277 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 @@ -29,6 +29,7 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; import net.foxdenstudio.sponge.foxguard.plugin.util.FGHelper; import org.spongepowered.api.Sponge; @@ -42,8 +43,11 @@ import org.spongepowered.api.text.action.TextActions; import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.text.format.TextStyles; +import org.spongepowered.api.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; @@ -55,7 +59,8 @@ public class CommandDetail implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -66,14 +71,14 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(2).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(2).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() .append(Text.of(TextColors.GREEN, "Usage: ")) .append(getUsage(source)) .build()); return CommandResult.empty(); - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); String worldName = parse.flagmap.get("world"); World world = null; @@ -89,7 +94,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm if (region == null) throw new CommandException(Text.of("No Region with name \"" + parse.args[1] + "\"!")); Text.Builder builder = Text.builder(); - builder.append(Text.of(TextColors.GOLD, "-----------------------------------------------------\n")); + builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); builder.append(Text.of(TextColors.GREEN, "---General---\n")); builder.append(Text.of(TextColors.GOLD, "Name: "), Text.of(TextColors.RESET, region.getName() + "\n")); builder.append(Text.of(TextColors.GOLD, "Type: "), Text.of(TextColors.RESET, region.getLongTypeName() + "\n")); @@ -113,14 +118,14 @@ public CommandResult process(CommandSource source, String arguments) throws Comm ))); source.sendMessage(builder.build()); return CommandResult.empty(); - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); IHandler handler = FGManager.getInstance().gethandler(parse.args[1]); if (handler == null) throw new CommandException(Text.of("No Handler with name \"" + parse.args[1] + "\"!")); Text.Builder builder = Text.builder(); - builder.append(Text.of(TextColors.GOLD, "-----------------------------------------------------\n")); + builder.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); builder.append(Text.of(TextColors.GREEN, "---General---\n")); builder.append(Text.of(TextColors.GOLD, "Name: "), Text.of(TextColors.RESET, handler.getName() + "\n")); builder.append(Text.of(TextColors.GOLD, "Type: "), Text.of(TextColors.RESET, handler.getLongTypeName() + "\n")); @@ -147,6 +152,59 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index == 1) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGManager.getInstance().getHandlerListCopy().stream() + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } @@ -168,6 +226,6 @@ public Optional getHelp(CommandSource source) { @Override public Text getUsage(CommandSource source) { - return Text.of("detail ] | handler> [parse.args...]"); + return Text.of("detail ] | handler> [args...]"); } } 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 53abfd9..696edb0 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 @@ -46,6 +46,8 @@ 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.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; import java.util.*; @@ -57,7 +59,8 @@ public class CommandEnableDisable implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -74,7 +77,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { if (FGHelper.getSelectedRegions(source).isEmpty() && FGHelper.getSelectedHandlers(source).isEmpty()) { source.sendMessage(Text.builder() @@ -110,7 +113,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm + (this.enableState ? "enabled." : "disabled."))); } } - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { String worldName = parse.flagmap.get("world"); World world = null; if (source instanceof Player) world = ((Player) source).getWorld(); @@ -156,7 +159,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm + (this.enableState ? "enabled." : "disabled."))); } - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); int successes = 0; int failures = 0; @@ -195,6 +198,62 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index > 0) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .filter(region -> region.isEnabled() != this.enableState && !(region instanceof GlobalRegion)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .filter(alias -> !isIn(parse.args, alias)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGManager.getInstance().getHandlerListCopy().stream() + .filter(handler -> handler.isEnabled() != this.enableState && !(handler instanceof GlobalHandler)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .filter(alias -> !isIn(parse.args, alias)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } 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 957562c..e7a9395 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 @@ -43,6 +43,8 @@ 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.World; import java.util.*; @@ -55,11 +57,12 @@ public class CommandHere implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(REGIONS_ALIASES, key) && !map.containsKey("region")) { + map.put(key, value); + if (isIn(REGIONS_ALIASES, key) && !map.containsKey("region")) { map.put("region", value); - } else if (isAlias(HANDLERS_ALIASES, key) && !map.containsKey("handler")) { + } else if (isIn(HANDLERS_ALIASES, key) && !map.containsKey("handler")) { map.put("handler", value); - } else if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + } else if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -70,8 +73,8 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); - + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); + String worldName = parse.flagmap.get("world"); World world = null; if (source instanceof Player) world = ((Player) source).getWorld(); @@ -120,7 +123,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm List regionList = FGManager.getInstance().getRegionsListCopy(world).stream() .filter(region -> region.isInRegion(x, y, z)) .collect(Collectors.toList()); - output.append(Text.of(TextColors.GOLD, "-----------------------------------------------------\n")); + output.append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")); output.append(Text.of(TextColors.AQUA, "----- Position: (" + String.format("%.1f, %.1f, %.1f", x, y, z) + ") -----\n")); if (!parse.flagmap.containsKey("handler") || parse.flagmap.containsKey("region")) { output.append(Text.of(TextColors.GREEN, "----- Regions Located Here -----\n")); @@ -158,6 +161,34 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT) && parse.current.index < 3 && parse.current.token.isEmpty()) { + return ImmutableList.of(parse.current.prefix + "~"); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.SHORTFLAG)) { + return ImmutableList.of("r", "h").stream() + .filter(flag -> !parse.flagmap.containsKey(flag)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } 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 06b09c5..0aa526d 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 @@ -50,12 +50,13 @@ import java.util.function.Function; import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.WORLD_ALIASES; -import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isAlias; +import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isIn; public class CommandLink implements CommandCallable { private static final Function, Function>> mapper = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -66,7 +67,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(mapper).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(mapper).parse(); if (parse.args.length == 0) { if (FGHelper.getSelectedRegions(source).size() == 0 && diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandList.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandList.java index 3699eb4..3b161db 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandList.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandList.java @@ -41,6 +41,8 @@ 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.World; import java.util.*; @@ -50,7 +52,8 @@ public class CommandList implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (Aliases.isAlias(Aliases.WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (Aliases.isIn(Aliases.WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -61,7 +64,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() @@ -85,7 +88,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } Text.Builder output = Text.builder() - .append(Text.of(TextColors.GOLD, "-----------------------------------------------------\n")) + .append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")) .append(Text.of(TextColors.GREEN, "---Regions" + (allFlag ? "" : (" for world: \"" + worldName + "\"")) + "---\n")); ListIterator regionListIterator = regionList.listIterator(); while (regionListIterator.hasNext()) { @@ -106,7 +109,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm }*/ Text.Builder output = Text.builder() - .append(Text.of(TextColors.GOLD, "-----------------------------------------------------\n")) + .append(Text.of(TextColors.GOLD, "\n-----------------------------------------------------\n")) .append(Text.of(TextColors.GREEN, "---Handlers---\n")); ListIterator handlerListIterator = handlerList.listIterator(); while (handlerListIterator.hasNext()) { @@ -138,6 +141,34 @@ private boolean contains(String[] aliases, String input) { @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } 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 afa6692..6240d74 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 @@ -29,9 +29,9 @@ import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandCallable; @@ -42,8 +42,11 @@ 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.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; @@ -55,7 +58,8 @@ public class CommandModify implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -66,14 +70,18 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).limit(2).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() .append(Text.of(TextColors.GREEN, "Usage: ")) .append(getUsage(source)) .build()); return CommandResult.empty(); - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); String worldName = parse.flagmap.get("world"); World world = null; @@ -88,8 +96,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm IRegion region = FGManager.getInstance().getRegion(world, parse.args[1]); if (region == null) throw new CommandException(Text.of("No Region with name \"" + parse.args[1] + "\"!")); - ProcessResult result = region.modify(parse.args.length < 3 ? "" : parse.args[2], - FCStateManager.instance().getStateMap().get(source), source); + ProcessResult result = region.modify(source, parse.args.length < 3 ? "" : parse.args[2]); if (result.isSuccess()) { if (result.getMessage().isPresent()) { @@ -112,13 +119,12 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "Modification Failed for Region!")); } } - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("Must specify a name!")); IHandler handler = FGManager.getInstance().gethandler(parse.args[1]); if (handler == null) throw new CommandException(Text.of("No Handler with name \"" + parse.args[1] + "\"!")); - ProcessResult result = handler.modify(parse.args.length < 3 ? "" : parse.args[2], - FCStateManager.instance().getStateMap().get(source), source); + ProcessResult result = handler.modify(source, parse.args.length < 3 ? "" : parse.args[2]); if (result.isSuccess()) { if (result.getMessage().isPresent()) { if (!FCHelper.hasColor(result.getMessage().get())) { @@ -148,6 +154,81 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .leaveFinalAsIs(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index == 1) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGManager.getInstance().getHandlerListCopy().stream() + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.FINAL)) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + IRegion region = FGManager.getInstance().getRegion(world, parse.args[1]); + if (region == null) return ImmutableList.of(); + return region.modifySuggestions(source, parse.current.token); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + if (parse.args.length < 2) return ImmutableList.of(); + IHandler handler = FGManager.getInstance().gethandler(parse.args[1]); + if (handler == null) return ImmutableList.of(); + return handler.modifySuggestions(source, parse.current.token); + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } @@ -168,6 +249,6 @@ public Optional getHelp(CommandSource source) { @Override public Text getUsage(CommandSource source) { - return Text.of("detail ] | handler> [parse.args...]"); + return Text.of("modify ] | handler> [args...]"); } } 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 ecdd1d8..f4e6743 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 @@ -29,6 +29,7 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.GlobalRegion; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandCallable; @@ -39,8 +40,11 @@ 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.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; @@ -52,7 +56,8 @@ public class CommandRename implements CommandCallable { private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -63,14 +68,14 @@ public CommandResult process(CommandSource source, String arguments) throws Comm source.sendMessage(Text.of(TextColors.RED, "You don't have permission to use this command!")); return CommandResult.empty(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length == 0) { source.sendMessage(Text.builder() .append(Text.of(TextColors.GREEN, "Usage: ")) .append(getUsage(source)) .build()); return CommandResult.empty(); - } else if (isAlias(REGIONS_ALIASES, parse.args[0])) { + } else if (isIn(REGIONS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("You must specify a name!")); String worldName = parse.flagmap.get("world"); World world = null; @@ -95,7 +100,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm throw new CommandException(Text.of("There is already a region with the name \"" + parse.args[2] + "\"!")); FGManager.getInstance().renameRegion(world, parse.args[1], parse.args[2]); source.sendMessage(Text.of(TextColors.GREEN, "Region \"" + parse.args[1] + "\" successfully renamed to \"" + parse.args[2] + "\"!")); - } else if (isAlias(HANDLERS_ALIASES, parse.args[0])) { + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { if (parse.args.length < 2) throw new CommandException(Text.of("You must specify a name!")); if (parse.args[1].equalsIgnoreCase(GlobalHandler.NAME)) throw new CommandException(Text.of("You may not rename the global Handler!")); @@ -116,6 +121,61 @@ public CommandResult process(CommandSource source, String arguments) throws Comm @Override public List getSuggestions(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) return ImmutableList.of(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .limit(2) + .flagMapper(MAPPER) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return Arrays.asList(FGManager.TYPES).stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.index == 1) { + if (isIn(REGIONS_ALIASES, parse.args[0])) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .filter(region -> !(region instanceof GlobalRegion)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(HANDLERS_ALIASES, parse.args[0])) { + return FGManager.getInstance().getHandlerListCopy().stream() + .filter(region -> !(region instanceof GlobalHandler)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); return ImmutableList.of(); } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java index 3d6b387..c97fb13 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java @@ -25,10 +25,10 @@ package net.foxdenstudio.sponge.foxguard.plugin.handler; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import org.spongepowered.api.command.CommandException; @@ -40,10 +40,14 @@ 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.util.Tristate; import javax.sql.DataSource; import java.sql.*; +import java.util.Arrays; +import java.util.List; import java.util.Map; import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.*; @@ -100,65 +104,90 @@ public Tristate handle(User user, Flag flag, Event event) { } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) throws CommandException { + public ProcessResult modify(CommandSource source, String arguments) throws CommandException { if (!source.hasPermission("foxguard.command.modify.objects.modify.handlers")) { if (source instanceof ProxySource) source = ((ProxySource) source).getOriginalSource(); if (source instanceof Player) return ProcessResult.failure(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse2(); - try { - this.lock.writeLock().lock(); - if (parse.args.length > 0) { - if (isAlias(SET_ALIASES, parse.args[0])) { - if (parse.args.length > 1) { - Flag flag; - if (parse.args[1].equalsIgnoreCase("all")) { - flag = null; + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse(); + if (parse.args.length > 0) { + if (isIn(SET_ALIASES, parse.args[0])) { + if (parse.args.length > 1) { + Flag flag; + if (parse.args[1].equalsIgnoreCase("all")) { + flag = null; + } else { + flag = Flag.flagFrom(parse.args[1]); + if (flag == null) { + return ProcessResult.of(false, Text.of("Not a valid flag!")); + } + } + if (isIn(CLEAR_ALIASES, parse.args[2])) { + if (flag == null) { + this.map.clear(); + return ProcessResult.of(true, Text.of("Successfully cleared flags!")); } else { - flag = Flag.flagFrom(parse.args[1]); - if (flag == null) { - return ProcessResult.of(false, Text.of("Not a valid flag!")); - } + this.map.remove(flag); + return ProcessResult.of(true, Text.of("Successfully cleared flag!")); + } + } else { + Tristate tristate = tristateFrom(parse.args[2]); + if (tristate == null) { + return ProcessResult.of(false, Text.of("Not a valid value!")); } - if (isAlias(CLEAR_ALIASES, parse.args[2])) { - if (flag == null) { - this.map.clear(); - return ProcessResult.of(true, Text.of("Successfully cleared flags!")); - } else { - this.map.remove(flag); - return ProcessResult.of(true, Text.of("Successfully cleared flag!")); + if (flag == null) { + for (Flag thatExist : Flag.values()) { + this.map.put(thatExist, tristate); } + return ProcessResult.of(true, Text.of("Successfully set flags!")); } else { - Tristate tristate = tristateFrom(parse.args[2]); - if (tristate == null) { - return ProcessResult.of(false, Text.of("Not a valid value!")); - } - if (flag == null) { - for (Flag thatExist : Flag.values()) { - this.map.put(thatExist, tristate); - } - return ProcessResult.of(true, Text.of("Successfully set flags!")); - } else { - this.map.put(flag, tristate); - return ProcessResult.of(true, Text.of("Successfully set flag!")); - } - + this.map.put(flag, tristate); + return ProcessResult.of(true, Text.of("Successfully set flag!")); } - } else { - return ProcessResult.of(false, Text.of("Must specify a flag!")); + } } else { - return ProcessResult.of(false, Text.of("Not a valid GlobalHandler command!")); + return ProcessResult.of(false, Text.of("Must specify a flag!")); } } else { - return ProcessResult.of(false, Text.of("Must specify a command!")); + return ProcessResult.of(false, Text.of("Not a valid GlobalHandler command!")); } - } finally { - this.lock.writeLock().unlock(); + } else { + return ProcessResult.of(false, Text.of("Must specify a command!")); } } + @Override + public List modifySuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) { + return ImmutableList.of("set").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.index == 1) { + return Arrays.stream(Flag.values()) + .map(Flag::flagName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.index == 2) { + return ImmutableList.of("true", "false", "passthrough").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + @Override public Text getDetails(String arguments) { Text.Builder builder = Text.builder(); @@ -166,20 +195,16 @@ public Text getDetails(String arguments) { TextActions.suggestCommand("/foxguard modify handler " + NAME + " set "), TextActions.showText(Text.of("Click to Set a Flag")), "Global Flags:\n")); - try { - this.lock.readLock().lock(); - for (Flag f : this.map.keySet()) { - builder.append( - Text.builder().append(Text.of(" " + f.toString() + ": ")) - .append(FCHelper.readableTristateText(map.get(f))) - .append(Text.of("\n")) - .onClick(TextActions.suggestCommand("/foxguard modify handler " + NAME + " set " + f.flagName() + " ")) - .onHover(TextActions.showText(Text.of("Click to Change This Flag"))) - .build() - ); - } - } finally { - this.lock.readLock().unlock(); + + for (Flag f : this.map.keySet()) { + builder.append( + Text.builder().append(Text.of(" " + f.toString() + ": ")) + .append(FCHelper.readableTristateText(map.get(f))) + .append(Text.of("\n")) + .onClick(TextActions.suggestCommand("/foxguard modify handler " + NAME + " set " + f.flagName() + " ")) + .onHover(TextActions.showText(Text.of("Click to Change This Flag"))) + .build() + ); } return builder.build(); } @@ -206,7 +231,6 @@ public void writeToDatabase(DataSource dataSource) throws SQLException { } public void loadFromDatabase(DataSource dataSource) throws SQLException { - this.lock.writeLock().lock(); try (Connection conn = dataSource.getConnection()) { CallbackHashMap flagMap = new CallbackHashMap<>((key, map) -> Tristate.UNDEFINED); try (Statement statement = conn.createStatement()) { @@ -223,8 +247,6 @@ public void loadFromDatabase(DataSource dataSource) throws SQLException { } } this.map = flagMap; - } finally { - this.lock.writeLock().unlock(); } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java index 996b288..29bf5ce 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java @@ -25,13 +25,14 @@ package net.foxdenstudio.sponge.foxguard.plugin.handler; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.source.ProxySource; @@ -41,6 +42,8 @@ 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.util.Tristate; import javax.annotation.Nullable; @@ -85,16 +88,16 @@ public Tristate handle(@Nullable User user, Flag flag, Event event) { } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) throws CommandException { + public ProcessResult modify(CommandSource source, String arguments) throws CommandException { if (!source.hasPermission("foxguard.command.modify.objects.modify.handlers")) { if (source instanceof ProxySource) source = ((ProxySource) source).getOriginalSource(); if (source instanceof Player && !this.ownerList.contains(source)) return ProcessResult.failure(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse(); try { this.lock.writeLock().lock(); if (parse.args.length > 0) { - if (isAlias(OWNER_GROUP_ALIASES, parse.args[0])) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[0])) { if (parse.args.length > 1) { UserOperations op; if (parse.args[1].equalsIgnoreCase("add")) { @@ -149,7 +152,7 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s return ProcessResult.of(false, Text.of("Must specify an operation!")); } - } else if (isAlias(SET_ALIASES, parse.args[0])) { + } else if (isIn(SET_ALIASES, parse.args[0])) { if (parse.args.length > 1) { Flag flag; if (parse.args[1].equalsIgnoreCase("all")) { @@ -160,7 +163,7 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s return ProcessResult.of(false, Text.of("Not a valid flag!")); } } - if (isAlias(CLEAR_ALIASES, parse.args[2])) { + if (isIn(CLEAR_ALIASES, parse.args[2])) { if (flag == null) { this.map.clear(); return ProcessResult.of(true, Text.of("Successfully cleared flags!")); @@ -199,6 +202,93 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s } + @Override + public List modifySuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) { + return ImmutableList.of("set", "owners").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.index == 1) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[0])) { + return ImmutableList.of("add", "remove", "set").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(SET_ALIASES, parse.args[0])) { + return Arrays.stream(Flag.values()) + .map(Flag::flagName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index == 2) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[0])) { + if(parse.args[1].equalsIgnoreCase("set")){ + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("add")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> !FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("remove")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (isIn(SET_ALIASES, parse.args[0])) { + return ImmutableList.of("true", "false", "passthrough").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index > 2) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[0])) { + if (parse.args[1].equalsIgnoreCase("set")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("add")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> !FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(alias -> !isIn(Arrays.copyOfRange(parse.args, 2, parse.args.length), alias)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("remove")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(alias -> !isIn(Arrays.copyOfRange(parse.args, 2, parse.args.length), alias)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + @Override public Text getDetails(String arguments) { Text.Builder builder = super.getDetails(arguments).toBuilder(); 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 index 13a254d..e55cda7 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java @@ -25,8 +25,8 @@ package net.foxdenstudio.sponge.foxguard.plugin.handler; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.User; @@ -38,6 +38,7 @@ import javax.annotation.Nullable; import javax.sql.DataSource; import java.sql.SQLException; +import java.util.List; public class PermissionHandler extends HandlerBase { @@ -102,8 +103,13 @@ public void writeToDatabase(DataSource dataSource) throws SQLException { } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) { + public ProcessResult modify(CommandSource source, String arguments) { return ProcessResult.of(false, Text.of(TextColors.WHITE, "Permission Handlers have no configurable parameters!")); } + @Override + public List modifySuggestions(CommandSource source, String arguments) { + return ImmutableList.of(); + } + } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java index e4e1ab9..f9d9868 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java @@ -25,14 +25,15 @@ package net.foxdenstudio.sponge.foxguard.plugin.handler; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import net.foxdenstudio.sponge.foxguard.plugin.object.IMembership; +import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.source.ProxySource; @@ -42,6 +43,8 @@ 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.util.Tristate; import javax.sql.DataSource; @@ -79,21 +82,21 @@ public SimpleHandler(String name, int priority, } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) throws CommandException { + public ProcessResult modify(CommandSource source, String arguments) throws CommandException { if (!source.hasPermission("foxguard.command.modify.objects.modify.handlers")) { if (source instanceof ProxySource) source = ((ProxySource) source).getOriginalSource(); if (source instanceof Player && !this.ownerList.contains(source)) return ProcessResult.failure(); } - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse(); try { this.lock.writeLock().lock(); if (parse.args.length > 0) { - if (isAlias(GROUPS_ALIASES, parse.args[0])) { + if (isIn(GROUPS_ALIASES, parse.args[0])) { if (parse.args.length > 1) { List list; - if (isAlias(OWNER_GROUP_ALIASES, parse.args[1])) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[1])) { list = this.ownerList; - } else if (isAlias(MEMBER_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(MEMBER_GROUP_ALIASES, parse.args[1])) { list = this.memberList; } else { return ProcessResult.of(false, Text.of(TextColors.RED, "Not a valid group!")); @@ -154,14 +157,14 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s } else { return ProcessResult.of(false, Text.of("Must specify a group!")); } - } else if (isAlias(SET_ALIASES, parse.args[0])) { + } else if (isIn(SET_ALIASES, parse.args[0])) { Map map; if (parse.args.length > 1) { - if (isAlias(OWNER_GROUP_ALIASES, parse.args[1])) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[1])) { map = ownerPermissions; - } else if (isAlias(MEMBER_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(MEMBER_GROUP_ALIASES, parse.args[1])) { map = memberPermissions; - } else if (isAlias(DEFAULT_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(DEFAULT_GROUP_ALIASES, parse.args[1])) { map = defaultPermissions; } else { @@ -181,7 +184,7 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s } } if (parse.args.length > 3) { - if (isAlias(CLEAR_ALIASES, parse.args[3])) { + if (isIn(CLEAR_ALIASES, parse.args[3])) { if (flag == null) { map.clear(); return ProcessResult.of(true, Text.of("Successfully cleared flags!")); @@ -210,24 +213,24 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s } else { return ProcessResult.of(false, Text.of("Must specify a flag!")); } - } else if (isAlias(PASSIVE_ALIASES, parse.args[0])) { + } else if (isIn(PASSIVE_ALIASES, parse.args[0])) { if (parse.args.length > 1) { - if (isAlias(TRUE_ALIASES, parse.args[1])) { + if (isIn(TRUE_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.ALLOW; return ProcessResult.of(true, Text.of("Successfully set passive option!")); - } else if (isAlias(FALSE_ALIASES, parse.args[1])) { + } else if (isIn(FALSE_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.DENY; return ProcessResult.of(true, Text.of("Successfully set passive option!")); - } else if (isAlias(PASSTHROUGH_ALIASES, parse.args[1])) { + } else if (isIn(PASSTHROUGH_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.PASSTHROUGH; return ProcessResult.of(true, Text.of("Successfully set passive option!")); - } else if (isAlias(OWNER_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(OWNER_GROUP_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.OWNER; return ProcessResult.of(true, Text.of("Successfully set passive option!")); - } else if (isAlias(MEMBER_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(MEMBER_GROUP_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.MEMBER; return ProcessResult.of(true, Text.of("Successfully set passive option!")); - } else if (isAlias(DEFAULT_GROUP_ALIASES, parse.args[1])) { + } else if (isIn(DEFAULT_GROUP_ALIASES, parse.args[1])) { this.passiveOption = PassiveOptions.DEFAULT; return ProcessResult.of(true, Text.of("Successfully set passive option!")); } else { @@ -247,6 +250,105 @@ public ProcessResult modify(String arguments, SourceState state, CommandSource s } } + @Override + public List modifySuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .excludeCurrent(true) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) { + return ImmutableList.of("set", "group", "passive").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.index == 1) { + if (isIn(GROUPS_ALIASES, parse.args[0]) || isIn(SET_ALIASES, parse.args[0])) { + return ImmutableList.of("owner", "member").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(PASSIVE_ALIASES, parse.args[0])) { + return ImmutableList.of("true", "false", "passthrough", "owner", "member", "default").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index == 2) { + if (isIn(OWNER_GROUP_ALIASES, parse.args[0])) { + return ImmutableList.of("add", "remove", "set").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (isIn(SET_ALIASES, parse.args[0])) { + return Arrays.stream(Flag.values()) + .map(Flag::flagName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index == 3) { + if (isIn(GROUPS_ALIASES, parse.args[0])) { + if (parse.args[1].equalsIgnoreCase("set")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("add")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> !FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("remove")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (isIn(SET_ALIASES, parse.args[0])) { + return ImmutableList.of("true", "false", "passthrough").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.index > 3) { + if (isIn(GROUPS_ALIASES, parse.args[0])) { + if (parse.args[1].equalsIgnoreCase("set")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .map(Player::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("add")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> !FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(alias -> !isIn(Arrays.copyOfRange(parse.args, 2, parse.args.length), alias)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.args[1].equalsIgnoreCase("remove")) { + return Sponge.getGame().getServer().getOnlinePlayers().stream() + .filter(player -> FCHelper.isUserOnList(this.ownerList, player)) + .map(Player::getName) + .filter(alias -> !isIn(Arrays.copyOfRange(parse.args, 2, parse.args.length), alias)) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + @Override public Tristate handle(User user, Flag flag, Event event) { try { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/IFGObject.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/IFGObject.java index efb855d..919c150 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/IFGObject.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/IFGObject.java @@ -36,6 +36,7 @@ import javax.sql.DataSource; import java.sql.SQLException; +import java.util.List; /** * Interface for all FoxGuard Objects. Inherited by {@link IRegion Regions} @@ -135,6 +136,8 @@ default boolean autoSave() { return true; } - ProcessResult modify(String arguments, SourceState state, CommandSource source) throws CommandException; + ProcessResult modify(CommandSource source, String arguments) throws CommandException; + + List modifySuggestions(CommandSource source, String arguments) throws CommandException; } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGFactoryManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGFactoryManager.java index e31584d..692a1d2 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGFactoryManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGFactoryManager.java @@ -36,6 +36,7 @@ import javax.sql.DataSource; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public final class FGFactoryManager { @@ -57,7 +58,7 @@ public static FGFactoryManager getInstance() { public IRegion createRegion(String name, String type, String args, SourceState state, World world, CommandSource source) throws CommandException { for (IRegionFactory rf : regionFactories) { - if (Aliases.isAlias(rf.getAliases(), type)) { + if (Aliases.isIn(rf.getAliases(), type)) { IRegion region = rf.createRegion(name, type, args, state, source); if (region != null) return region; } @@ -67,7 +68,7 @@ public IRegion createRegion(String name, String type, String args, SourceState s public IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException { for (IRegionFactory rf : regionFactories) { - if (Aliases.isAlias(rf.getTypes(), type)) { + if (Aliases.isIn(rf.getTypes(), type)) { IRegion region = rf.createRegion(source, name, type, isEnabled); if (region != null) return region; } @@ -78,7 +79,7 @@ public IRegion createRegion(DataSource source, String name, String type, boolean public IHandler createHandler(String name, String type, int priority, String args, SourceState state, CommandSource source) { for (IHandlerFactory fsf : handlerFactories) { - if (Aliases.isAlias(fsf.getAliases(), type)) { + if (Aliases.isIn(fsf.getAliases(), type)) { IHandler handler = fsf.createHandler(name, type, priority, args, state, source); if (handler != null) return handler; } @@ -88,7 +89,7 @@ public IHandler createHandler(String name, String type, int priority, String arg public IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException { for (IHandlerFactory fsf : handlerFactories) { - if (Aliases.isAlias(fsf.getTypes(), type)) { + if (Aliases.isIn(fsf.getTypes(), type)) { IHandler handler = fsf.createHandler(source, name, type, priority, isEnabled); if (handler != null) return handler; } @@ -115,4 +116,20 @@ else if (factory instanceof IHandlerFactory) return handlerFactories.remove(factory); return false; } + + public List getPrimaryHandlerTypeAliases() { + List aliases = new ArrayList<>(); + for (IFGFactory factory : handlerFactories) { + aliases.addAll(Arrays.asList(factory.getPrimaryAliases())); + } + return aliases; + } + + public List getPrimaryRegionTypeAliases() { + List aliases = new ArrayList<>(); + for (IFGFactory factory : regionFactories) { + aliases.addAll(Arrays.asList(factory.getPrimaryAliases())); + } + return aliases; + } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java index 5f629eb..0bd2de8 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java @@ -67,17 +67,22 @@ public String[] getTypes() { return types; } + @Override + public String[] getPrimaryAliases() { + return types; + } + @Override public IHandler createHandler(String name, String type, int priority, String arguments, SourceState state, CommandSource source) { - if (Aliases.isAlias(simpleAliases, type)) { + if (Aliases.isIn(simpleAliases, type)) { SimpleHandler handler = new SimpleHandler(name, priority); if (source instanceof Player) handler.addOwner((Player) source); return handler; - } else if (Aliases.isAlias(passiveAliases, type)) { + } else if (Aliases.isIn(passiveAliases, type)) { PassiveHandler handler = new PassiveHandler(name, priority); if (source instanceof Player) handler.addOwner((Player) source); return handler; - } else if (Aliases.isAlias(permissionAliases, type)) { + } else if (Aliases.isIn(permissionAliases, type)) { return new PermissionHandler(name, priority); } else return null; } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGRegionFactory.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGRegionFactory.java index 11152d1..755b6f7 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGRegionFactory.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGRegionFactory.java @@ -52,7 +52,7 @@ import java.util.Optional; import java.util.UUID; -import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isAlias; +import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isIn; public class FGRegionFactory implements IRegionFactory { @@ -61,22 +61,37 @@ public class FGRegionFactory implements IRegionFactory { private static final String[] elevAliases = {"elevation", "elev", "height", "y", "vertical", "vert", "level", "updown"}; private static final String[] types = {"rectangular", "cuboid", "elevation"}; + @Override + public String[] getAliases() { + return FCHelper.concatAll(rectAliases, cuboidAliases, elevAliases); + } + + @Override + public String[] getTypes() { + return types; + } + + @Override + public String[] getPrimaryAliases() { + return types; + } + @SuppressWarnings("unchecked") @Override public IRegion createRegion(String name, String type, String arguments, SourceState state, CommandSource source) throws CommandException { String[] args = {}; if (!arguments.isEmpty()) args = arguments.split(" +"); - if (isAlias(rectAliases, type)) { + if (isIn(rectAliases, type)) { if (source instanceof Player) return new RectangularRegion(name, ((PositionsStateField) state.get(PositionsStateField.ID)).getList(), args, source, (Player) source); else return new RectangularRegion(name, ((PositionsStateField) state.get(PositionsStateField.ID)).getList(), args, source); - } else if (isAlias(cuboidAliases, type)) { + } else if (isIn(cuboidAliases, type)) { if (source instanceof Player) return new CuboidRegion(name, ((PositionsStateField) state.get(PositionsStateField.ID)).getList(), args, source, (Player) source); else return new CuboidRegion(name, ((PositionsStateField) state.get(PositionsStateField.ID)).getList(), args, source); - } else if (isAlias(elevAliases, type)) { + } else if (isIn(elevAliases, type)) { if (source instanceof Player) return new ElevationRegion(name, ((PositionsStateField) state.get(PositionsStateField.ID)).getList(), args, source, (Player) source); else @@ -159,14 +174,4 @@ public IRegion createRegion(DataSource source, String name, String type, boolean return region; } else return null; } - - @Override - public String[] getAliases() { - return FCHelper.concatAll(rectAliases, cuboidAliases, elevAliases); - } - - @Override - public String[] getTypes() { - return types; - } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/IFGFactory.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/IFGFactory.java index d29c78c..d609598 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/IFGFactory.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/IFGFactory.java @@ -31,5 +31,6 @@ interface IFGFactory { String[] getTypes(); + String[] getPrimaryAliases(); } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/CuboidRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/CuboidRegion.java index 0b7b893..327ea9a 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/CuboidRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/CuboidRegion.java @@ -28,7 +28,6 @@ import com.flowpowered.math.vector.Vector3i; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxguard.plugin.region.util.BoundingBox3; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; @@ -105,10 +104,15 @@ public CuboidRegion(String name, List positions, String[] args, Comman } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) { + public ProcessResult modify(CommandSource source, String arguments) { return ProcessResult.failure(); } + @Override + public List modifySuggestions(CommandSource source, String arguments) { + return null; + } + @Override public boolean isInRegion(int x, int y, int z) { try { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java index dfc9995..c0c0d84 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java @@ -26,9 +26,9 @@ package net.foxdenstudio.sponge.foxguard.plugin.region; import com.flowpowered.math.vector.Vector3i; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; @@ -92,10 +92,15 @@ public ElevationRegion(String name, List positions, String[] args, Com } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) { + public ProcessResult modify(CommandSource source, String arguments) { return ProcessResult.failure(); } + @Override + public List modifySuggestions(CommandSource source, String arguments) { + return ImmutableList.of(); + } + @Override public boolean isInRegion(int x, int y, int z) { try { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java index 64bca8c..bc25737 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/GlobalRegion.java @@ -27,12 +27,13 @@ import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.text.Text; import javax.sql.DataSource; +import java.util.List; public class GlobalRegion extends RegionBase { @@ -82,10 +83,15 @@ public void setIsEnabled(boolean state) { } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) { + public ProcessResult modify(CommandSource source, String arguments) { return ProcessResult.failure(); } + @Override + public List modifySuggestions(CommandSource source, String arguments) { + return ImmutableList.of(); + } + public boolean isInRegion(int x, int y, int z) { return true; } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RectangularRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RectangularRegion.java index 10da2b0..d7e17a2 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RectangularRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/RectangularRegion.java @@ -26,9 +26,9 @@ package net.foxdenstudio.sponge.foxguard.plugin.region; import com.flowpowered.math.vector.Vector3i; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxcore.plugin.command.util.SourceState; import net.foxdenstudio.sponge.foxguard.plugin.region.util.BoundingBox2; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; @@ -98,10 +98,15 @@ public RectangularRegion(String name, List positions, String[] args, C } @Override - public ProcessResult modify(String arguments, SourceState state, CommandSource source) { + public ProcessResult modify(CommandSource source, String arguments) { return ProcessResult.failure(); } + @Override + public List modifySuggestions(CommandSource source, String arguments) { + return ImmutableList.of(); + } + @Override public boolean isInRegion(int x, int y, int z) { try { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/HandlersStateField.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/HandlersStateField.java index 4e94039..87b9e81 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/HandlersStateField.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/state/HandlersStateField.java @@ -25,18 +25,27 @@ package net.foxdenstudio.sponge.foxguard.plugin.state; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxcore.plugin.state.ListStateFieldBase; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.util.FGHelper; +import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; +import org.spongepowered.api.world.World; import java.util.Iterator; +import java.util.List; public class HandlersStateField extends ListStateFieldBase { @@ -60,7 +69,7 @@ public Text state() { @Override public ProcessResult add(CommandSource source, String arguments) throws CommandException { - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse(); if (parse.args.length < 1) throw new CommandException(Text.of("Must specify a name!")); IHandler handler = FGManager.getInstance().gethandler(parse.args[0]); @@ -73,26 +82,93 @@ public ProcessResult add(CommandSource source, String arguments) throws CommandE return ProcessResult.of(true, Text.of("Successfully added Handler to your state buffer!")); } + @Override + public List addSuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return FGManager.getInstance().getHandlerListCopy().stream() + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + @Override public ProcessResult subtract(CommandSource source, String arguments) throws CommandException { - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).parse(); if (parse.args.length < 1) throw new CommandException(Text.of("Must specify a name or a number!")); - IHandler handler; - try { - int index = Integer.parseInt(parse.args[0]); - handler = this.list.get(index - 1); - } catch (NumberFormatException e) { - handler = FGManager.getInstance().gethandler(parse.args[1]); - } catch (IndexOutOfBoundsException e) { - throw new ArgumentParseException(Text.of("Index out of bounds! (1 - " + this.list.size()), parse.args[0], 1); + if (parse.args.length == 1) { + IHandler handler; + try { + int index = Integer.parseInt(parse.args[0]); + handler = this.list.get(index - 1); + } catch (NumberFormatException e) { + handler = FGManager.getInstance().gethandler(parse.args[0]); + } catch (IndexOutOfBoundsException e) { + throw new ArgumentParseException(Text.of("Index out of bounds! (1 - " + this.list.size()), parse.args[0], 1); + } + if (handler == null) + throw new ArgumentParseException(Text.of("No Handlers with this name!"), parse.args[0], 1); + if (!this.list.contains(handler)) + throw new ArgumentParseException(Text.of("Handler is not in your state buffer!"), parse.args[0], 1); + this.list.remove(handler); + return ProcessResult.of(true, Text.of("Successfully removed Handler from your state buffer!")); + } else { + int successes = 0, failures = 0; + for (String arg : parse.args) { + IHandler handler; + try { + int index = Integer.parseInt(arg); + handler = this.list.get(index - 1); + } catch (NumberFormatException e) { + handler = FGManager.getInstance().gethandler(arg); + } catch (IndexOutOfBoundsException e) { + failures++; + continue; + } + if (handler == null) { + failures++; + continue; + } + if (!this.list.contains(handler)) { + failures++; + continue; + } + this.list.remove(handler); + successes++; + } + if (successes > 0) { + return ProcessResult.of(true, Text.of(TextColors.GREEN, "Successfully removed handlers handlers from your state buffer with " + + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); + } else { + return ProcessResult.of(false, Text.of(failures + " failures while trying to remove handlers from your state buffer. " + + "Check that their names or indices are valid.")); + } } - if (handler == null) - throw new ArgumentParseException(Text.of("No Handlers with this name!"), parse.args[0], 1); - if (!this.list.contains(handler)) - throw new ArgumentParseException(Text.of("Handler is not in your state buffer!"), parse.args[0], 1); - this.list.remove(handler); + } - return ProcessResult.of(true, Text.of("Successfully removed Handler from your state buffer!")); + @Override + public List subtractSuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) + return this.list.stream() + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); } } 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 5748a6e..2d6abeb 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 @@ -25,10 +25,12 @@ package net.foxdenstudio.sponge.foxguard.plugin.state; +import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxcore.plugin.state.ListStateFieldBase; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; import net.foxdenstudio.sponge.foxguard.plugin.util.FGHelper; import org.spongepowered.api.Sponge; @@ -36,22 +38,26 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; +import org.spongepowered.api.util.GuavaCollectors; +import org.spongepowered.api.util.StartsWithPredicate; import org.spongepowered.api.world.World; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Consumer; import java.util.function.Function; import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.WORLD_ALIASES; -import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isAlias; +import static net.foxdenstudio.sponge.foxcore.plugin.util.Aliases.isIn; public class RegionsStateField extends ListStateFieldBase { public static final String ID = "region"; private static final Function, Function>> MAPPER = map -> key -> value -> { - if (isAlias(WORLD_ALIASES, key) && !map.containsKey("world")) { + map.put(key, value); + if (isIn(WORLD_ALIASES, key) && !map.containsKey("world")) { map.put("world", value); } }; @@ -77,7 +83,7 @@ public Text state() { @Override public ProcessResult add(CommandSource source, String arguments) throws CommandException { System.out.println(arguments); - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length < 1) throw new CommandException(Text.of("Must specify a name!")); String worldName = parse.flagmap.get("world"); @@ -100,9 +106,50 @@ public ProcessResult add(CommandSource source, String arguments) throws CommandE return ProcessResult.of(true, Text.of("Successfully added Region to your state buffer!")); } + @Override + public List addSuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .flagMapper(MAPPER) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0){ + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + return FGManager.getInstance().getRegionListAsStream(world) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList());} + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } + @Override public ProcessResult subtract(CommandSource source, String arguments) throws CommandException { - AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse2(); + AdvCmdParse.ParseResult parse = AdvCmdParse.builder().arguments(arguments).flagMapper(MAPPER).parse(); if (parse.args.length < 1) throw new CommandException(Text.of("Must specify a name or a number!")); String worldName = parse.flagmap.get("world"); @@ -133,4 +180,48 @@ public ProcessResult subtract(CommandSource source, String arguments) throws Com return ProcessResult.of(true, Text.of("Successfully removed Region from your state buffer!")); } + + @Override + public List subtractSuggestions(CommandSource source, String arguments) throws CommandException { + AdvCmdParse.ParseResult parse = AdvCmdParse.builder() + .arguments(arguments) + .flagMapper(MAPPER) + .autoCloseQuotes(true) + .parse(); + if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.ARGUMENT)) { + if (parse.current.index == 0) { + String worldName = parse.flagmap.get("world"); + World world = null; + if (source instanceof Player) world = ((Player) source).getWorld(); + if (!worldName.isEmpty()) { + Optional optWorld = Sponge.getGame().getServer().getWorld(worldName); + if (optWorld.isPresent()) { + world = optWorld.get(); + } + } + if (world == null) return ImmutableList.of(); + final World finalWorld = world; + return this.list.stream() + .filter(region -> region.getWorld().equals(finalWorld)) + .map(IFGObject::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGKEY)) + return ImmutableList.of("world").stream() + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.LONGFLAGVALUE)) { + if (parse.current.key.equals("world")) + return Sponge.getGame().getServer().getWorlds().stream() + .map(World::getName) + .filter(new StartsWithPredicate(parse.current.token)) + .map(args -> parse.current.prefix + args) + .collect(GuavaCollectors.toImmutableList()); + } else if (parse.current.type.equals(AdvCmdParse.CurrentElement.ElementType.COMPLETE)) + return ImmutableList.of(parse.current.prefix + " "); + return ImmutableList.of(); + } }