Skip to content

Commit

Permalink
fix parseInt not returning correct number / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Querz committed Aug 1, 2019
1 parent df8ec95 commit 60b6292
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/querz/mcaselector/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public static int getProcessThreads() {
}

public static void setProcessThreads(int processThreads) {
System.out.println("setting process threads to " + processThreads);
Config.processThreads = processThreads;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/querz/mcaselector/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
Future<Boolean> headless = new ParamExecutor(args).parseAndRun();
if (headless != null && headless.get()) {
// we already ran headless mode, so we exit here
System.out.println("exiting");
Debug.print("exiting");
System.exit(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.querz.mcaselector.changer.Field;
import net.querz.mcaselector.changer.FieldType;

import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.querz.mcaselector.headless;

import net.querz.mcaselector.util.Debug;
import net.querz.mcaselector.util.Progress;

import java.util.concurrent.atomic.AtomicInteger;

public class ConsoleProgress implements Progress {
Expand Down Expand Up @@ -46,14 +46,14 @@ public void incrementProgress(String msg, int progress) {

@Override
public void setMessage(String msg) {
System.out.println(msg);
Debug.print(msg);
}

public void onDone(Runnable doneAction) {
this.doneAction = doneAction;
}

private void printProgress(String msg) {
System.out.printf("%.2f%%\t%s\n", ((double) progress.get() / max * 100), msg);
Debug.printf("%.2f%%\t%s", ((double) progress.get() / max * 100), msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public GroupFilter parse() throws ParseException {

ptr.skipWhitespace();

//parse group
// parse group
if (ptr.currentChar() == '(') {
ptr.next();
GroupFilter child = parse();
Expand All @@ -51,7 +51,7 @@ public GroupFilter parse() throws ParseException {
}

private Filter<?> parseFilterType(Operator operator) throws ParseException {
//parse value
// parse value
String type = ptr.parseSimpleString();
FilterType t = FilterType.getByName(type);

Expand Down
63 changes: 34 additions & 29 deletions src/main/java/net/querz/mcaselector/headless/ParamExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.querz.mcaselector.io.SelectionExporter;
import net.querz.mcaselector.io.SelectionUtil;
import net.querz.mcaselector.util.DataProperty;
import net.querz.mcaselector.util.Debug;
import net.querz.mcaselector.util.Helper;
import net.querz.mcaselector.util.Point2i;
import java.io.File;
Expand All @@ -38,9 +39,11 @@ public Future<Boolean> parseAndRun() {

FutureTask<Boolean> future = new FutureTask<>(() -> {}, true);

DataProperty<Map<String, String>> params = new DataProperty<>();

try {
Map<String, String> params = new ParamParser(args).parse();
ParamInterpreter pi = new ParamInterpreter(params);
params.set(new ParamParser(args).parse());
ParamInterpreter pi = new ParamInterpreter(params.get());

// register parameter dependencies and restrictions
pi.registerDependencies("headless", null, new ActionKey("mode", null));
Expand Down Expand Up @@ -69,17 +72,17 @@ public Future<Boolean> parseAndRun() {
pi.registerDependencies("process-threads", null, new ActionKey("headless", null));
pi.registerDependencies("write-threads", null, new ActionKey("headless", null));

parseConfig(params);
parseConfig(params.get());

DataProperty<Boolean> isHeadless = new DataProperty<>();

pi.registerAction("headless", null, v -> runModeHeadless(isHeadless::set));
pi.registerAction("mode", "select", v -> runModeSelect(params, future));
pi.registerAction("mode", "export", v -> runModeExport(params, future));
pi.registerAction("mode", "import", v -> runModeImport(params, future));
pi.registerAction("mode", "delete", v -> runModeDelete(params, future));
pi.registerAction("mode", "change", v -> runModeChange(params, future));
pi.registerAction("mode", "cache", v -> runModeCache(params, future));
pi.registerAction("mode", "select", v -> runModeSelect(params.get(), future));
pi.registerAction("mode", "export", v -> runModeExport(params.get(), future));
pi.registerAction("mode", "import", v -> runModeImport(params.get(), future));
pi.registerAction("mode", "delete", v -> runModeDelete(params.get(), future));
pi.registerAction("mode", "change", v -> runModeChange(params.get(), future));
pi.registerAction("mode", "cache", v -> runModeCache(params.get(), future));

pi.execute();

Expand All @@ -88,8 +91,10 @@ public Future<Boolean> parseAndRun() {
}

} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Error: " + ex.getMessage());
Debug.error("Error: " + ex.getMessage());
if (params.get() != null && params.get().containsKey("debug")) {
ex.printStackTrace();
}
future.run();
return future;
}
Expand Down Expand Up @@ -118,6 +123,8 @@ private static void runModeCache(Map<String, String> params, FutureTask<Boolean>
checkDirectoryIsEmpty(output);
Config.setCacheDir(output);

printHeadlessSettings();

Integer zoomLevel = params.containsKey("zoom-level") ? parseInt(params.get("zoom-level")) : null;

ConsoleProgress progress = new ConsoleProgress();
Expand Down Expand Up @@ -152,16 +159,16 @@ private static void runModeDelete(Map<String, String> params, FutureTask<Boolean
checkDirectoryForFiles(world, Helper.MCA_FILE_PATTERN);
Config.setWorldDir(world);

printHeadlessSettings();

GroupFilter g = null;
if (params.containsKey("query")) {
g = new FilterParser(params.get("query")).parse();
System.out.println("filter set: " + g);
Debug.print("filter set: " + g);
}

Map<Point2i, Set<Point2i>> selection = loadSelection(params, "input");

printHeadlessSettings();

ConsoleProgress progress = new ConsoleProgress();
progress.onDone(future);

Expand Down Expand Up @@ -207,17 +214,17 @@ private static void runModeExport(Map<String, String> params, FutureTask<Boolean
createDirectoryIfNotExists(output);
checkDirectoryIsEmpty(output);

printHeadlessSettings();

GroupFilter g = null;
if (params.containsKey("query")) {
g = new FilterParser(params.get("query")).parse();
System.out.println("filter set: " + g);
Debug.print("filter set: " + g);
}

Map<Point2i, Set<Point2i>> selection = loadSelection(params, "input");

printHeadlessSettings();

System.out.println("exporting chunks...");
Debug.print("exporting chunks...");

ConsoleProgress progress = new ConsoleProgress();
progress.onDone(future);
Expand All @@ -239,15 +246,12 @@ private static void runModeSelect(Map<String, String> params, FutureTask<Boolean
File output = parseFile(params.get("output"), "csv");
createParentDirectoryIfNotExists(output);

GroupFilter g = new FilterParser(params.get("query")).parse();

printHeadlessSettings();

System.out.println("filter set: " + g);

System.out.println("selecting chunks...");
GroupFilter g = new FilterParser(params.get("query")).parse();

// select from filter
Debug.print("filter set: " + g);
Debug.print("selecting chunks...");

Map<Point2i, Set<Point2i>> selection = new HashMap<>();

Expand All @@ -261,9 +265,9 @@ private static void runModeSelect(Map<String, String> params, FutureTask<Boolean
}

private static void printHeadlessSettings() {
System.out.println("read threads: " + Config.getLoadThreads());
System.out.println("process threads: " + Config.getProcessThreads());
System.out.println("write threads: " + Config.getWriteThreads());
Debug.print("read threads: " + Config.getLoadThreads());
Debug.print("process threads: " + Config.getProcessThreads());
Debug.print("write threads: " + Config.getWriteThreads());
}

private static int parseInt(String value) throws ParseException {
Expand All @@ -284,6 +288,7 @@ private static int parsePositiveInt(String value) throws ParseException {
if (i <= 0) {
throw new ParseException("number cannot be negative: \"" + value + "\"");
}
return i;
} catch (NumberFormatException ex) {
throw new ParseException(ex.getMessage());
}
Expand All @@ -293,7 +298,7 @@ private static int parsePositiveInt(String value) throws ParseException {

private static Map<Point2i, Set<Point2i>> loadSelection(Map<String, String> params, String key) throws ParseException {
if (params.containsKey(key)) {
System.out.println("loading selection...");
Debug.print("loading selection...");

File input = parseFile(params.get("input"), "csv");
fileMustExist(input);
Expand All @@ -310,7 +315,7 @@ private static File parseFile(String value, String ending) throws ParseException
return file;
}

private static File parseDirectory(String value) throws ParseException {
private static File parseDirectory(String value) {
return new File(value);
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/querz/mcaselector/util/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.querz.mcaselector.Config;

import java.util.Arrays;

public class Debug {

public static void dump(Object... objects) {
Expand Down Expand Up @@ -31,4 +33,12 @@ public static void error(Object... objects) {
public static void errorf(String format, Object... objects) {
System.out.printf(format + "\n", objects);
}

public static void print(Object... objects) {
Arrays.stream(objects).forEach(System.out::println);
}

public static void printf(String format, Object... objects) {
System.out.printf(format + "\n", objects);
}
}

0 comments on commit 60b6292

Please sign in to comment.