Skip to content

Commit

Permalink
refactor: add more edithome/warp perms
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Mar 31, 2024
1 parent 27713c7 commit 65cc90e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.regex.Pattern;

public class EditHomeCommand extends SavedPositionCommand<Home> {

public EditHomeCommand(@NotNull HuskHomes plugin) {
super("edithome", List.of(), Home.class, List.of("rename", "description", "relocate", "privacy"), plugin);
addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, e) -> m.put(e, false), HashMap::putAll));
}

@Override
Expand All @@ -60,14 +56,11 @@ public void execute(@NotNull CommandUser executor, @NotNull Home home, @NotNull
.forEach(executor::sendMessage);
return;
}

if (!arguments.contains(operation.get().toLowerCase())) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
if (isInvalidOperation(operation.get(), executor)) {
return;
}

switch (operation.get().toLowerCase()) {
switch (operation.get().toLowerCase(Locale.ENGLISH)) {
case "rename" -> setHomeName(executor, home, ownerEditing, args);
case "description" -> setHomeDescription(executor, home, ownerEditing, args);
case "relocate" -> setHomePosition(executor, home, ownerEditing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
Expand All @@ -40,7 +39,6 @@ public class EditWarpCommand extends SavedPositionCommand<Warp> {
public EditWarpCommand(@NotNull HuskHomes plugin) {
super("editwarp", List.of(), Warp.class, List.of("rename", "description", "relocate"), plugin);
setOperatorCommand(true);
addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, e) -> m.put(e, false), HashMap::putAll));
}

@Override
Expand All @@ -50,10 +48,7 @@ public void execute(@NotNull CommandUser executor, @NotNull Warp warp, @NotNull
getWarpEditorWindow(warp).forEach(executor::sendMessage);
return;
}

if (!arguments.contains(operation.get().toLowerCase())) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
if (isInvalidOperation(operation.get(), executor)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import net.william278.huskhomes.util.TransactionResolver;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

public abstract class SavedPositionCommand<T extends SavedPosition> extends Command implements TabProvider {

Expand All @@ -48,6 +45,7 @@ protected SavedPositionCommand(@NotNull String name, @NotNull List<String> alias
this.arguments = arguments;

addAdditionalPermissions(Map.of("other", true));
addAdditionalPermissions(arguments.stream().collect(HashMap::new, (m, s) -> m.put(s, false), HashMap::putAll));
}

@NotNull
Expand Down Expand Up @@ -171,38 +169,52 @@ protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable tel
.buildAndComplete(executor.equals(teleporter), teleporter.getUsername());
}

protected boolean isInvalidOperation(String operation, CommandUser executor) {
if (!arguments.contains(operation.toLowerCase())) {
plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
.ifPresent(executor::sendMessage);
return true;
}
if (!executor.hasPermission(getPermission(operation.toLowerCase(Locale.ENGLISH)))) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(executor::sendMessage);
return true;
}
return false;
}

@NotNull
private static String formatUsage(List<String> arguments) {
private static String formatUsage(Collection<String> arguments) {
return ((!arguments.isEmpty()) ? " [" + String.join("|", arguments) + "]" : "");
}

@Override
@NotNull
public List<String> suggest(@NotNull CommandUser executor, @NotNull String[] args) {
if (positionType == Home.class) {
return switch (args.length) {
case 0, 1 -> {
if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) {
if (executor.hasPermission(getOtherPermission())) {
yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args);
}
yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args);
}
if (executor instanceof OnlineUser user) {
yield filter(plugin.getManager().homes().getUserHomes().get(user.getUsername()), args);
}
yield filter(plugin.getManager().homes().getUserHomeIdentifiers(), args);
}
case 2 -> filter(arguments.stream().toList(), args);
default -> List.of();
};
} else {
if (positionType != Home.class) {
return switch (args.length) {
case 0, 1 -> filter(plugin.getManager().warps().getUsableWarps(executor), args);
case 2 -> filter(arguments.stream().toList(), args);
case 0, 1 -> plugin.getManager().warps().getUsableWarps(executor);
case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList();
default -> List.of();
};
}

return switch (args.length) {
case 0, 1 -> {
if (args.length == 1 && args[0].contains(Home.IDENTIFIER_DELIMITER)) {
if (executor.hasPermission(getOtherPermission())) {
yield plugin.getManager().homes().getUserHomeIdentifiers();
}
yield plugin.getManager().homes().getUserHomeIdentifiers();
}
if (executor instanceof OnlineUser user) {
yield plugin.getManager().homes().getUserHomes().get(user.getUsername());
}
yield plugin.getManager().homes().getUserHomeIdentifiers();
}
case 2 -> arguments.stream().filter(a -> executor.hasPermission(getPermission(a))).toList();
default -> List.of();
};
}

}
13 changes: 9 additions & 4 deletions docs/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,28 @@ This is a table of HuskHomes commands, how to use them, and their required permi
<td rowspan="6"><code>/edithome</code></td>
<td><code>/edithome &lt;name&gt;</code></td>
<td>View and edit information about a home</td>
<td rowspan="4"><code>huskhomes.command.edithome</code></td>
<td rowspan="4" align="center">✅</td>
<td><code>huskhomes.command.edithome</code></td>
<td rowspan="5" align="center">✅</td>
</tr>
<tr>
<td><code>/edithome &lt;name&gt; rename &lt;new_name&gt;</code></td>
<td>Rename a home</td>
<td><code>huskhomes.command.edithome.rename</code></td>
</tr>
<tr>
<td><code>/edithome &lt;name&gt; description &lt;text&gt;</code></td>
<td>Set a home's description</td>
<td><code>huskhomes.command.edithome.description</code></td>
</tr>
<tr>
<td><code>/edithome &lt;name&gt; relocate</code></td>
<td>Move a home to your current position</td>
<td><code>huskhomes.command.edithome.relocate</code></td>
</tr>
<tr>
<td><code>/edithome &lt;name&gt; privacy [public|private]</code></td>
<td>Set a home's privacy (make it public or private)</td>
<td><code>huskhomes.command.edithome.privacy</code></td>
<td align="center">✅</td>
</tr>
<tr>
<td><code>/edithome &lt;owner_name.home_name&gt; [args]</code></td>
Expand Down Expand Up @@ -172,20 +174,23 @@ This is a table of HuskHomes commands, how to use them, and their required permi
<td rowspan="4"><code>/editwarp</code></td>
<td><code>/editwarp &lt;name&gt;</code></td>
<td>View and edit information about a warp</td>
<td rowspan="4"><code>huskhomes.command.editwarp</code></td>
<td><code>huskhomes.command.editwarp</code></td>
<td rowspan="4" align="center">❌</td>
</tr>
<tr>
<td><code>/editwarp &lt;name&gt; rename &lt;new_name&gt;</code></td>
<td>Rename a warp</td>
<td><code>huskhomes.command.editwarp.rename</code></td>
</tr>
<tr>
<td><code>/editwarp &lt;name&gt; description &lt;text&gt;</code></td>
<td>Set a warp's description</td>
<td><code>huskhomes.command.editwarp.description</code></td>
</tr>
<tr>
<td><code>/editwarp &lt;name&gt; relocate</code></td>
<td>Move a warp to your current position</td>
<td><code>huskhomes.command.editwarp.relocate</code></td>
</tr>
<!-- /spawn command -->
<tr><th colspan="5">Spawn commands</th></tr>
Expand Down

0 comments on commit 65cc90e

Please sign in to comment.