sender.sendMessage("file creation failed. bad filename?");
}
}
}
} else if (p.first.equals("s") || p.first.equals("select")) {
final Node n = node.getNode(p.second);
if (n == null)
sender.sendMessage("invalid node in '" + p.second + "'");
else
node = n;
} else if (p.first.equals("save")) {
save = true;
} else if (p.first.equals("a") || p.first.equals("accept")) {
answered = true;
answer = true;
continue;
} else if (p.first.matches("v+")) {
v = Verbosity.values()[Math.max(p.first.length(), Verbosity.values().length - 1)];
}
answered = false;// --a will only skip the question(s) of the next option or the main action if it's the last option.
answer = false;
}
// --- ACTIONS ---
if (action != null) {
if (action.equals("e") || action.equals("enable")
|| action.equals("d") || action.equals("disable")) {
final boolean enable = action.startsWith("e");
final String prefix = (enable ? "en" : "dis");
if (node.getParent() == null) {
if (enable ^ !config.isEnabled()) {
sender.sendMessage("file is already " + prefix + "abled");
return;
}
if (!answered)
sender.sendMessage(prefix + "abling the file will rename the file on the disk. Do you want to continue?");
if (waitForAnswer()) {
if (config.setEnabled(enable)) {
sender.sendMessage("file " + prefix + "abled");
} else {
if (new File(config.getFile(), enable ? config.getFileName().substring(1) : "-" + config.getFileName()).exists()) {
sender.sendMessage("could not " + prefix + "able the file because a file with that name already exists");
} else {
sender.sendMessage("could not " + prefix + "able the file.");
}
}
}
} else {
if (node instanceof EntryNode) {
node.rename(node.getName().startsWith("-") ? node.getName().substring(1) : "-" + node.getName());
sender.sendMessage("node " + prefix + "abled");
}
}
} else if (action.equals("s") || action.equals("save")) {
save = true;
} else if (action.equals("a") || action.equals("add")
|| action.equals("n") || action.equals("new")) {
final String[] params = actionParams.split(":", 2);
if (params.length < 2) {
sender.sendMessage("usage: /s n key:value|group:");
return;
}
if (!(node instanceof SectionNode)) {
sender.sendMessage("adding node to parent of selected node.");
node = node.getParent();
}
if (params[1].isEmpty()) {
((SectionNode) node).getNodeList().add(node = new SectionNode(params[0], (SectionNode) node));
} else {
((SectionNode) node).getNodeList().add(node = new EntryNode(params[0], params[1], (SectionNode) node));
}
sender.sendMessage("created & selected " + params[0]);
} else if (action.equals("r") || action.equals("rename")) {
if (actionParams.isEmpty()) {
sender.sendMessage("usage: /s r new name");
return;
}
final String oldname = node.getName();
node.rename(actionParams);
sender.sendMessage("renamed " + oldname + " to " + node.getName());
} else if (action.equals("d") || action.equals("delete")) {
if (!answered)
sender.sendMessage("do you really want to delete this node?");
if (waitForAnswer()) {
node.delete();
}
} else if (action.equals("m") || action.equals("move")) {
if (actionParams.isEmpty()) {
sender.sendMessage("usage: /s m +move down|-move up|index to insert after|parent node");
}
if (actionParams.matches("-[0-9]+")) {
node.getParent().moveDelta(Integer.parseInt(actionParams));
} else if (actionParams.matches("\\+[0-9]+")) {
node.getParent().moveDelta(Integer.parseInt(actionParams.substring(1)));
} else if (actionParams.matches("[0-9]+")) {
node.getParent().move(Integer.parseInt(actionParams));
} else {
final Node n = node.getNode(actionParams, true);
if (n == null) {
sender.sendMessage("invalid path");
return;
}
if (!(n instanceof SectionNode)) {
node.move((SectionNode) n);
} else {
node.move(n.getParent());
node.move(n.getParent().getNodeList().indexOf(n) + 1);
}
}
} else if (action.equals("l") || action.equals("list")) {
if (!(node instanceof SectionNode)) {
sender.sendMessage("selected node is not a section, switching to parent node");
node = node.getParent();
}
int page = 1;
try {
page = Math.min((int) Math.ceil(((SectionNode) node).getNodeList().size() / CommandHandler.linesPerPage), Math.max(1, Integer.parseInt(actionParams)));
} catch (final NumberFormatException e) {}
sender.sendMessage("&8== subnodes of "
+ node.getName()
+ " (page "
+ page
+ " of "
+ Math.ceil((((SectionNode) node).getNodeList().size() - (v.compareTo(Verbosity.HIGH) >= 0 ? ((SectionNode) node).getNumVoidNodes() : 0))
/ CommandHandler.linesPerPage) + ")");
int j = 0;
for (int i = (page - 1) * CommandHandler.linesPerPage; j < CommandHandler.linesPerPage; i++) {
final Node n = ((SectionNode) node).getNodeList().get(i);
if (v.compareTo(Verbosity.HIGH) >= 0 && n instanceof VoidNode) {
continue;
}
j++;
String s;
if (n.isVoid()) {
s = n.getOrig();
} else {
s = n.getName();
if (v.compareTo(Verbosity.NORMAL) >= 0) {
if (node instanceof EntryNode)
s += ": " + ((EntryNode) n).getValue();
else if (node instanceof SectionNode)
s += " [" + ((SectionNode) n).getNodeList().size() + " subnodes]";