finish help command

This commit is contained in:
Flemmli97 2021-06-09 14:06:12 +02:00
parent 345fc2256f
commit 47380161c7
5 changed files with 111 additions and 8 deletions

View File

@ -110,7 +110,9 @@ public class CommandClaim {
.then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, true))
.then(CommandManager.argument("toggle", StringArgumentType.word())
.suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGroupPerm))))));
builder.then(CommandManager.literal("help").executes(ctx -> CommandHelp.helpMessage(ctx, 0, builder.getArguments())).then(CommandManager.argument("page", IntegerArgumentType.integer()).executes(ctx -> CommandHelp.helpMessage(ctx, builder.getArguments()))));
builder.then(CommandManager.literal("help").executes(ctx -> CommandHelp.helpMessage(ctx, 0, builder.getArguments()))
.then(CommandManager.argument("page", IntegerArgumentType.integer()).executes(ctx -> CommandHelp.helpMessage(ctx, builder.getArguments())))
.then(CommandManager.literal("cmd").then(CommandManager.argument("command", StringArgumentType.word()).suggests((ctx, sb)->CommandSource.suggestMatching(CommandHelp.registeredCommands(ctx, builder.getArguments()), sb)).executes(ctx -> CommandHelp.helpCmd(ctx)))));
dispatcher.register(builder);
}

View File

@ -2,6 +2,7 @@ package io.github.flemmli97.flan.commands;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.CommandNode;
import io.github.flemmli97.flan.claim.PermHelper;
@ -24,15 +25,17 @@ public class CommandHelp {
}
public static int helpMessage(CommandContext<ServerCommandSource> context, int page, Collection<CommandNode<ServerCommandSource>> nodes) {
List<String> subCommands = nodes.stream().filter(node -> node.canUse(context.getSource())).map(CommandNode::getName).collect(Collectors.toList());
List<String> subCommands = registeredCommands(context, nodes);
int max = subCommands.size() / 8;
if (page > max)
page = max;
context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.helpHeader, page), Formatting.GOLD), false);
for (int i = 8 * page; i < 8 * (page + 1); i++)
if (i < subCommands.size())
context.getSource().sendFeedback(PermHelper.simpleColoredText(" -" + subCommands.get(i), Formatting.GRAY), false);
MutableText pageText = PermHelper.simpleColoredText((page > 0 ? " " : "") + " ", Formatting.DARK_GREEN);
if (i < subCommands.size()) {
MutableText cmdText = PermHelper.simpleColoredText("- " + subCommands.get(i), Formatting.GRAY);
context.getSource().sendFeedback(cmdText.fillStyle(cmdText.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/flan help cmd " + subCommands.get(i)))), false);
}
MutableText pageText = PermHelper.simpleColoredText((page > 0 ? " " : "") + " ", Formatting.DARK_GREEN);
if (page > 0) {
MutableText pageTextBack = PermHelper.simpleColoredText("<<", Formatting.DARK_GREEN);
pageTextBack.fillStyle(pageTextBack.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/flan help " + (page - 1))));
@ -46,4 +49,24 @@ public class CommandHelp {
context.getSource().sendFeedback(pageText, false);
return Command.SINGLE_SUCCESS;
}
public static int helpCmd(CommandContext<ServerCommandSource> context) {
String command = StringArgumentType.getString(context, "command");
String[] cmdHelp = ConfigHandler.lang.cmdLang.getCommandHelp(command);
context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.helpCmdHeader, Formatting.DARK_GREEN), false);
for(int i = 0; i < cmdHelp.length; i++) {
if(i == 0) {
context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.helpCmdSyntax, cmdHelp[i]), Formatting.GOLD), false);
context.getSource().sendFeedback(PermHelper.simpleColoredText(""), false);
}
else {
context.getSource().sendFeedback(PermHelper.simpleColoredText(cmdHelp[i], Formatting.GOLD), false);
}
}
return Command.SINGLE_SUCCESS;
}
public static List<String> registeredCommands(CommandContext<ServerCommandSource> context, Collection<CommandNode<ServerCommandSource>> nodes) {
return nodes.stream().filter(node -> node.canUse(context.getSource())).map(CommandNode::getName).collect(Collectors.toList());
}
}

View File

@ -49,7 +49,7 @@ public class Config {
public int buyPrice = -1;
public boolean lockDrops = true;
public int dropTicks = 2400;
public int dropTicks = 6000;
public boolean log;
@ -233,13 +233,13 @@ public class Config {
return permMap == null ? GlobalType.NONE : permMap.getOrDefault(perm, GlobalType.NONE);
}
private <V, K> Map<V, K> createHashMap(Consumer<Map<V, K>> cons) {
public static <V, K> Map<V, K> createHashMap(Consumer<Map<V, K>> cons) {
Map<V, K> map = new HashMap<>();
cons.accept(map);
return map;
}
private <V, K> Map<V, K> createLinkedHashMap(Consumer<Map<V, K>> cons) {
public static <V, K> Map<V, K> createLinkedHashMap(Consumer<Map<V, K>> cons) {
Map<V, K> map = new LinkedHashMap<>();
cons.accept(map);
return map;

View File

@ -0,0 +1,68 @@
package io.github.flemmli97.flan.config;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.Map;
public class LangCommands {
private Map<String, String[]> commands = Config.createHashMap(map -> {
map.put("help", new String[]{"help <page> | (cmd <command>)", "Shows all available commands or info for the given command."});
map.put("menu", new String[]{"menu", "When standing in a claim you have permissions for opens the claim menu."});
map.put("claimInfo", new String[]{"claimInfo", "Prints infos about the claim you're standing in."});
map.put("delete", new String[]{"delete", "Deletes the current claim."});
map.put("deleteAll", new String[]{"deleteAll", "Deletes all your claims (you need to double type to confirm it so no accidents)."});
map.put("deleteSubClaim", new String[]{"deleteSubClaim", "Deletes the current subclaim."});
map.put("deleteAllSubClaims", new String[]{"deleteAllSubClaims", "Deletes all subclaim of the current claim."});
map.put("list", new String[]{"list", "Lists all claims you have. if op also gives ability to list other players claims."});
map.put("switchMode", new String[]{"switchMode", "Switch between normal and subclaim mode."});
map.put("group", new String[]{"group (add | remove <name>) | (players add | remove <player> [overwrite])", "Adds/removes the group with that name. Also editable via the claim menu.", " adds/remove a player to the group. if overwrite then will overwrite the players current group else does nothing. Also editable via the claim menu."});
map.put("transferClaim", new String[]{"transferClaim <player>", "Gives ownership of the claim to the specified player. Only works if you're the claim owner."});
map.put("addClaim", new String[]{"addClaim <x y z> <x y z>", "Creates a claim with the given positions. Same as using the claim tool."});
map.put("permission", new String[]{"permission {global | (group <name>) | (personal <name>)} <permission> true | false | default", " Sets global/group/personal permissions. Also editable via the claim menu (for group perm right click on the group in the menu)."});
map.put("personalGroups", new String[]{"personalGroups", "Opens the gui to edit personal groups."});
map.put("sellBlocks", new String[]{"sellBlocks <amount>", "Sells <amount> claimblocks. Needs gunpowder currency installed."});
map.put("buyBlocks", new String[]{"buyBlocks <amount>", "Buys <amount> claimblocks. Needs gunpowder currency installed."});
map.put("trapped", new String[]{"trapped", "If in a claim not owned by the player attempts to teleport the player out of it after 5 seconds."});
map.put("unlockDrops", new String[]{"unlockDrops", "Unlocks dropped items from death so other players can pick them up too."});
map.put("reload", new String[]{"reload", "Reloads the config ingame."});
map.put("adminMode", new String[]{"adminMode", "Switches to admin mode ignoring all claims."});
map.put("readGriefPrevention", new String[]{"readGriefPreventionData", "Parses data from the GriefPrevention plugin to Flan."});
map.put("setAdminClaim", new String[]{"setAdminClaim", "Sets a claim to an admin claim."});
map.put("listAdminClaims", new String[]{"listAdminClaim", "Lists all admin claims in the current world."});
map.put("adminDelete", new String[]{"adminDelete [all <player>]", "Force deletes the current claim or deletes all claims from the specified player."});
map.put("giveClaimBlocks", new String[]{"giveClaimBlocks <amount>", "Gives a player additional claim blocks."});
});
public void load(JsonObject obj) throws IllegalAccessException {
obj.entrySet().forEach(e -> {
String[] val;
if (e.getValue().isJsonArray()) {
JsonArray arr = e.getValue().getAsJsonArray();
val = new String[arr.size()];
for (int i = 0; i < arr.size(); i++)
val[i] = arr.get(i).getAsString();
} else {
val = new String[1];
val[0] = e.getValue().getAsString();
}
this.commands.put(e.getKey(), val);
});
}
public void save(JsonObject obj) throws IllegalAccessException {
this.commands.forEach((cmd, val) -> {
JsonArray arr = new JsonArray();
if (val != null)
for (String s : val)
arr.add(s);
obj.add(cmd, arr);
});
}
public String[] getCommandHelp(String command) {
return this.commands.getOrDefault(command, new String[0]);
}
}

View File

@ -14,6 +14,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
public class LangConfig {
@ -84,6 +85,8 @@ public class LangConfig {
public String claimGroupPerms = " Permissions: %s";
public String claimGroupPlayers = " Players: %s";
public String helpHeader = "Available subcommands are (page %d):";
public String helpCmdHeader = "====================";
public String helpCmdSyntax = "Syntax: %s";
public String screenEnableText = "Enabled: %s";
public String screenUneditable = "Non Editable!";
@ -123,6 +126,8 @@ public class LangConfig {
public String unlockDropsCmd = "Your deathitems are protected. Use %s to unlock them for other players";
public String unlockDrops = "Your deathitems are now unlocked for %s ticks";
public LangCommands cmdLang = new LangCommands();
public LangConfig(MinecraftServer server) {
File configDir = FabricLoader.getInstance().getConfigDir().resolve("flan").toFile();
//server.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile();
@ -163,6 +168,8 @@ public class LangConfig {
perm.desc = new String[]{pe.getAsString()};
}
}
JsonObject cmd = ConfigHandler.fromJson(obj, "commands");
this.cmdLang.load(cmd);
} catch (IOException | IllegalAccessException e) {
e.printStackTrace();
}
@ -187,6 +194,9 @@ public class LangConfig {
obj.add(perm.id + ".desc", arr);
}
}
JsonObject cmd = new JsonObject();
this.cmdLang.save(cmd);
obj.add("commands", cmd);
FileWriter writer = new FileWriter(this.config);
ConfigHandler.GSON.toJson(obj, writer);
writer.close();