Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.tfminecraft</groupId>
<artifactId>magic</artifactId>
Expand Down Expand Up @@ -130,6 +136,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.4</version>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions src/main/java/net/tfminecraft/dowsing/utils/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void loadNodes() {
}
setSlots(currentType, activePMs);
Node n = new Node(id, b, loc, g, isActive, level, cycleTime, currentType, timeLeft, inputCounter, efficiency);
if(json.containsKey(LastCycleResult.KEY)) {
n.setLastResult(LastCycleResult.fromJson(json.get(LastCycleResult.KEY)));
}
NodeManager.nodes.add(n);
} catch (Exception ex) {
ex.printStackTrace();
Expand Down Expand Up @@ -261,6 +264,7 @@ public void saveNode(Node n) {
}
defaults.put("active pms", pmArray);
defaults.put("efficiency", n.getEfficiency());
defaults.put(LastCycleResult.KEY, LastCycleResult.toJson(n.getLastResult()));
save(file, defaults);
} catch (Throwable ex) {
ex.printStackTrace();
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/net/tfminecraft/dowsing/utils/LastCycleResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.tfminecraft.dowsing.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;

public final class LastCycleResult {

public static final String KEY = "last result";

private LastCycleResult() {}

@SuppressWarnings("unchecked")
public static JSONObject toJson(Map<String, Integer> result) {
JSONObject object = new JSONObject();
if(result == null) {
return object;
}
for(Map.Entry<String, Integer> entry : result.entrySet()) {
if(entry.getKey() == null || entry.getKey().isBlank() || entry.getValue() == null || entry.getValue() <= 0) {
continue;
}
object.put(entry.getKey(), entry.getValue());
}
return object;
}

public static Map<String, Integer> fromJson(Object raw) {
Map<String, Integer> result = new HashMap<>();
if(!(raw instanceof JSONObject object)) {
return result;
}
for(Object key : object.keySet()) {
if(key == null) {
continue;
}
int amount = amountOf(object.get(key));
if(amount > 0) {
result.put(String.valueOf(key), amount);
}
}
return result;
}

static int amountOf(Object value) {
if(value instanceof Number number) {
return number.intValue();
}
if(value == null) {
return 0;
}
try {
return (int) Math.round(Double.parseDouble(value.toString()));
} catch (NumberFormatException ex) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.tfminecraft.dowsing.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.LinkedHashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.junit.jupiter.api.Test;

class LastCycleResultTest {

@Test
void roundTripsExtractedItems() {
Map<String, Integer> extracted = new LinkedHashMap<>();
extracted.put("m.materials.niter", 1);
extracted.put("m.materials.arcane_crystal", 3);

JSONObject stored = LastCycleResult.toJson(extracted);
Map<String, Integer> loaded = LastCycleResult.fromJson(stored);

assertEquals(extracted, loaded);
}

@SuppressWarnings("unchecked")
@Test
void readsNumericAmountsWrittenByTheJsonParser() {
JSONObject stored = new JSONObject();
stored.put("m.materials.niter", 1L);
stored.put("m.materials.arcane_crystal", 3.0d);

Map<String, Integer> loaded = LastCycleResult.fromJson(stored);

assertEquals(1, loaded.get("m.materials.niter"));
assertEquals(3, loaded.get("m.materials.arcane_crystal"));
}

@Test
void missingOrEmptyResultsStayEmpty() {
assertTrue(LastCycleResult.fromJson(null).isEmpty());
assertTrue(LastCycleResult.toJson(Map.of()).isEmpty());
assertTrue(LastCycleResult.toJson(null).isEmpty());
}
}