Skip to content

Commit

Permalink
Migrated permissions to String and added suggestions with sender
Browse files Browse the repository at this point in the history
  • Loading branch information
divios committed Feb 3, 2022
1 parent 3a2ef0c commit d6fbcd0
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'io.github.divios'
version = '1.7'
version = '1.7.3E'

sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/io/github/divios/jcommands/JCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JCommand {
private final String name;
private boolean isSubcommand = false;
private final List<String> aliases = new ArrayList<>();
private Permission permission = null;
private String permission = null;
private Consumer<CommandSender> invalidPermission = player -> {
};
private final LinkedList<Argument> arguments = new LinkedList<>();
Expand Down Expand Up @@ -49,21 +49,12 @@ public JCommand withAliases(String... aliases) {
this.aliases.addAll(Arrays.asList(aliases));
return this;
}

public JCommand assertPermission(String permissionStr) {
return assertPermission(new Permission(permissionStr));
}

public JCommand assertPermission(Permission permission) {
public JCommand assertPermission(String permission) {
return assertPermission(permission, sender -> {
});
}

public JCommand assertPermission(String permissionStr, Consumer<CommandSender> invalidPermission) {
return assertPermission(new Permission(permissionStr), invalidPermission);
}

public JCommand assertPermission(Permission permission, Consumer<CommandSender> invalidPermission) {
public JCommand assertPermission(String permission, Consumer<CommandSender> invalidPermission) {
this.permission = permission;
this.invalidPermission = invalidPermission;
return this;
Expand Down Expand Up @@ -135,7 +126,7 @@ public List<String> getAliases() {
return aliases;
}

public Permission getPermission() {
public String getPermission() {
return permission;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
for (Node child : node.getChildren()) {
if (!meetsCommandRequirements(sender, child)) continue;

for (String suggestion : child.getLabel().getSuggestions())
for (String suggestion : child.getLabel().getSuggestions(sender))
if (StringUtils.startsWithIgnoreCase(suggestion, (args[args.length - 1])))
tabCompletes.add(suggestion);
}
Expand All @@ -49,7 +49,7 @@ private boolean meetsCommandRequirements(CommandSender sender, Node node) {

private boolean meetsPerms(CommandSender sender, Node node) {
if (node.getPermissions() == null || node.getPermissions().isEmpty()) return true;
return node.getPermissions().stream().allMatch(sender::hasPermission);
return node.getPermissions().stream().filter(Objects::nonNull).allMatch(sender::hasPermission);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.github.divios.jcommands.arguments;

import org.bukkit.command.CommandSender;

import java.util.List;

public interface Argument {

String getName();
boolean isValidArgument(String o);
List<String> getSuggestions();
default List<String> getSuggestions() { return getSuggestions(null); }
List<String> getSuggestions(CommandSender sender);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.divios.jcommands.arguments.types;

import io.github.divios.jcommands.util.Primitives;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -20,8 +22,8 @@ protected boolean isValidArgumentAbstract(String s) {
}

@Override
public abstractArgument<Boolean> overrideSuggestions(@NotNull Supplier<List<Boolean>> booleanSuggestions, boolean imperative) {
super.setSuggestions(() -> booleanSuggestions.get().stream()
public abstractArgument<Boolean> overrideSuggestions(@NotNull Function<CommandSender, List<Boolean>> booleanSuggestions, boolean imperative) {
super.setSuggestions((sender) -> booleanSuggestions.apply(sender).stream()
.map(String::valueOf)
.collect(Collectors.toList()));
super.imperative = imperative;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.divios.jcommands.arguments.types;

import io.github.divios.jcommands.util.Primitives;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -20,8 +22,8 @@ protected boolean isValidArgumentAbstract(String s) {
}

@Override
public abstractArgument<Integer> overrideSuggestions(@NotNull Supplier<List<Integer>> integerSuggestions, boolean imperative) {
super.setSuggestions(() -> integerSuggestions.get().stream()
public abstractArgument<Integer> overrideSuggestions(@NotNull Function<CommandSender, List<Integer>> integerSuggestions, boolean imperative) {
super.setSuggestions((sender) -> integerSuggestions.apply(sender).stream()
.map(String::valueOf)
.collect(Collectors.toList()));
super.imperative = imperative;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import io.github.divios.jcommands.util.Primitives;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand All @@ -25,8 +27,8 @@ protected boolean isValidArgumentAbstract(String s) {
}

@Override
public abstractArgument<Player> overrideSuggestions(@NotNull Supplier<List<Player>> playerSuggestions, boolean imperative) {
super.setSuggestions(() -> playerSuggestions.get().stream()
public abstractArgument<Player> overrideSuggestions(@NotNull Function<CommandSender, List<Player>> playerSuggestions, boolean imperative) {
super.setSuggestions((sender) -> playerSuggestions.apply(sender).stream()
.map(Player::getName)
.collect(Collectors.toList())
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.divios.jcommands.arguments.types;

import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class StringArgument extends abstractArgument<String> {
Expand All @@ -18,7 +20,7 @@ protected boolean isValidArgumentAbstract(String o) {
}

@Override
public abstractArgument<String> overrideSuggestions(@NotNull Supplier<List<String>> stringSuggestions, boolean imperative) {
public abstractArgument<String> overrideSuggestions(@NotNull Function<CommandSender, List<String>> stringSuggestions, boolean imperative) {
super.setSuggestions(stringSuggestions);
super.imperative = imperative;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
package io.github.divios.jcommands.arguments.types;

import io.github.divios.jcommands.arguments.Argument;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public abstract class abstractArgument<T> implements Argument {

protected final String name;
protected Supplier<List<String>> suggestions;
protected Function<CommandSender, List<String>> suggestions;
protected boolean imperative = false;

abstractArgument(String name, Supplier<List<String>> suggestions) {
this.name = name;
this.suggestions = suggestions;
this.suggestions = sender -> suggestions.get();
}

@Override
public String getName() { return name; }
public String getName() {
return name;
}

public boolean isValidArgument(String s) {
if (imperative)
return isValidArgumentAbstract(s) && suggestions.get().stream().anyMatch(s1 -> s1.equalsIgnoreCase(s));
return isValidArgumentAbstract(s) && suggestions.apply(null).stream().anyMatch(s1 -> s1.equalsIgnoreCase(s));
else
return isValidArgumentAbstract(s);
}

protected abstract boolean isValidArgumentAbstract(String s);

public List<String> getSuggestions() {
return suggestions == null ? null : suggestions.get();
public List<String> getSuggestions(CommandSender sender) {
return suggestions == null ? null : suggestions.apply(sender);
}

void setSuggestions(@NotNull Supplier<List<String>> suggestions) {
this.suggestions = sender -> suggestions.get();
}

void setSuggestions(@NotNull Function<CommandSender, List<String>> suggestions) {
this.suggestions = suggestions;
}

public abstractArgument<T> overrideSuggestions(@NotNull Supplier<List<T>> suggestions) {
return overrideSuggestions(suggestions, false);
}

public abstract abstractArgument<T> overrideSuggestions(@NotNull Supplier<List<T>> suggestions, boolean imperative);
public abstractArgument<T> overrideSuggestions(@NotNull Supplier<List<T>> suggestions, boolean imperative) {
return overrideSuggestions(sender -> suggestions.get(), imperative);
}

public abstractArgument<T> overrideSuggestions(@NotNull Function<CommandSender, List<T>> suggestions) {
return overrideSuggestions(suggestions, false);
}

public abstract abstractArgument<T> overrideSuggestions(@NotNull Function<CommandSender, List<T>> suggestions, boolean imperative);

public abstractArgument<T> setAsImperative() {
imperative = true;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/github/divios/jcommands/maptree/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
import io.github.divios.jcommands.JCommand;
import io.github.divios.jcommands.arguments.Argument;

import org.bukkit.permissions.Permission;
import java.util.*;

@SuppressWarnings("unused")
public class Node {

private final Argument label;
private final JCommand command;
private Node parent;
private List<Node> children;
private final Set<Permission> permissions;
private final Set<String> permissions;

Node(Argument label, JCommand command) {
this.label = label;
this.command = command;
this.children = new ArrayList<>();
this.permissions = new HashSet<>();
permissions.add(command.getPermission());
}

Node(Argument label, JCommand command, Node parent) {
Expand All @@ -41,11 +42,11 @@ public void addChildren(Collection<Node> children) {
this.children.addAll(children);
}

public void addPermission(Permission permission) {
public void addPermission(String permission) {
permissions.add(permission);
}

public void addPermission(Collection<Permission> permissions) {
public void addPermission(Collection<String> permissions) {
this.permissions.addAll(permissions);
}

Expand All @@ -61,7 +62,7 @@ public List<Node> getChildren() {
return children;
}

public Set<Permission> getPermissions() {
public Set<String> getPermissions() {
return permissions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ private void processChildren(Node node, JCommand command) {
Node child = new Node(new StringArgument(subCommand.getName())
.overrideSuggestions(() -> Collections.singletonList(subCommand.getName()), true),
subCommand);
child.addPermission(node.getPermissions()); // Add parent perms
node.addChildren(child);
processChildren(child, subCommand);
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/maptree/mapTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,32 @@ public void testAliases() {

}

@Test
public void testPermissions() {
TreeMap treeMap = new TreeMap();
JCommand test = new JCommand("test")
.assertPermission("test.perm")
.withSubcommands(
JCommand.create("restock")
.assertPermission("test.perm.restock")
.withArguments(new StringArgument("shop")
.overrideSuggestions(() -> Arrays.asList("blocks", "drops", "--all", "farm"))
)
.executes((sender, valueMap) -> System.out.println(valueMap.get("shop").getAsString()))
).withSubcommands(
JCommand.create("sub2")
.assertPermission("test.perm.sub2")
);

Assert.assertEquals("test.perm", test.getPermission());
treeMap.put(test);

Node node = treeMap.search("test", new String[]{"restock", "shop"});
Assert.assertNotNull(node);
Assert.assertEquals(new HashSet<>(Arrays.asList("test.perm.restock", "test.perm")), node.getPermissions());

}

private ValueMap wrapArgs(JCommand command, String[] args) {

Map<String, Value> valueMap = new LinkedHashMap<>();
Expand Down

0 comments on commit d6fbcd0

Please sign in to comment.