Skip to content

Commit

Permalink
Tab completion yay.
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityfox committed Jan 4, 2016
1 parent ea68601 commit 49f4526
Show file tree
Hide file tree
Showing 29 changed files with 1,055 additions and 189 deletions.
2 changes: 1 addition & 1 deletion FoxCore
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

plugins {
id 'java'
// id 'com.github.johnrengelman.shadow' version '1.2.2'
Expand All @@ -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'

Expand All @@ -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'
}
Expand Down
4 changes: 2 additions & 2 deletions permissionslist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ foxguard {
}
}
link {
add {
link {
own
others
}
remove {
unlink {
own
others
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

public final class FGManager {

public static final String[] TYPES = {"region", "handler"};

private static FGManager instance;
private final Map<World, List<IRegion>> regions;
private final List<IHandler> handlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Map<String, String>, Function<String, Consumer<String>>> 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);
}
};
Expand All @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -143,6 +147,46 @@ public CommandResult process(CommandSource source, String arguments) throws Comm

@Override
public List<String> 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();
}

Expand All @@ -163,6 +207,6 @@ public Optional<? extends Text> getHelp(CommandSource source) {

@Override
public Text getUsage(CommandSource source) {
return Text.of("create <region [--w:<world>] | handler> <name> [--priority:<num>] <type> [parse.args...]");
return Text.of("create <region [--w:<world>] | handler> <name> [--priority:<num>] <type> [args...]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -53,7 +57,8 @@
public class CommandDelete implements CommandCallable {

private static final Function<Map<String, String>, Function<String, Consumer<String>>> 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);
}
};
Expand All @@ -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;
Expand All @@ -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!"));
Expand All @@ -105,6 +110,61 @@ public CommandResult process(CommandSource source, String arguments) throws Comm

@Override
public List<String> 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<World> 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();
}

Expand Down
Loading

0 comments on commit 49f4526

Please sign in to comment.