-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More remote fixes, legacy remote import (pastebin) now working
- Loading branch information
Showing
8 changed files
with
136 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 59 additions & 1 deletion
60
src/main/java/me/desht/pneumaticcraft/common/util/legacyconv/ActionWidgetLegacyConv.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,71 @@ | ||
package me.desht.pneumaticcraft.common.util.legacyconv; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
import me.desht.pneumaticcraft.client.gui.remote.RemoteLayout; | ||
import me.desht.pneumaticcraft.lib.Log; | ||
|
||
import static me.desht.pneumaticcraft.common.util.legacyconv.ConversionUtil.*; | ||
|
||
public class ActionWidgetLegacyConv { | ||
public static int determineVersion(JsonObject json) { | ||
return 1; | ||
if (json.has("version")) { | ||
// 1.20.6 and newer | ||
return json.get("version").getAsInt(); | ||
} else if (json.has("main")) { | ||
return 1; | ||
} else { | ||
throw new JsonSyntaxException("can't determine saved remote layout version!"); | ||
} | ||
} | ||
|
||
public static void convertLegacy(JsonObject json, int oldVersion) { | ||
if (oldVersion == 1) { | ||
convertFromV1(json); | ||
} | ||
} | ||
|
||
private static void convertFromV1(JsonObject json) { | ||
JsonArray entries = json.getAsJsonObject("main").getAsJsonArray("value"); | ||
|
||
JsonArray newDoc = new JsonArray(); | ||
|
||
for (JsonElement entry : entries) { | ||
JsonObject oldEntry = entry.getAsJsonObject(); | ||
|
||
JsonObject newEntry = new JsonObject(); | ||
JsonObject base = new JsonObject(); | ||
JsonObject widget = new JsonObject(); | ||
|
||
getString(oldEntry, "enableVariable").ifPresent(val -> base.addProperty("enable_var", prefixVar(val))); | ||
convXYZ(oldEntry, base, "enabling", "enable_pos"); | ||
|
||
getInt(oldEntry, "x").ifPresent(val -> widget.addProperty("x", val)); | ||
getInt(oldEntry, "y").ifPresent(val -> widget.addProperty("y", val + 8)); | ||
getInt(oldEntry, "width").ifPresent(val -> widget.addProperty("width", val)); | ||
getInt(oldEntry, "height").ifPresent(val -> widget.addProperty("height", val)); | ||
getString(oldEntry, "text").ifPresent(val -> widget.addProperty("title", val)); | ||
getString(oldEntry, "tooltip").ifPresent(val -> widget.addProperty("tooltip", val)); | ||
|
||
newEntry.add("base", base); | ||
newEntry.add("widget", widget); | ||
|
||
getString(oldEntry, "id").ifPresent(val -> newEntry.addProperty("type", val)); | ||
getString(oldEntry, "variableName").ifPresent(val -> newEntry.addProperty("var_name", prefixVar(val))); | ||
convXYZ(oldEntry, newEntry, "setting", "set_pos"); | ||
getString(oldEntry, "dropDownElements").ifPresent(val -> newEntry.addProperty("elements", val)); | ||
getBool(oldEntry, "sorted").ifPresent(val -> newEntry.addProperty("sorted", val)); | ||
|
||
newDoc.add(newEntry); | ||
} | ||
|
||
json.add("action_widgets", newDoc); | ||
json.remove("main"); | ||
json.addProperty("version", RemoteLayout.JSON_VERSION); | ||
|
||
Log.info("Pastebin Remote import (V1 -> V2): converted {} legacy widgets", newDoc.size()); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/me/desht/pneumaticcraft/common/util/legacyconv/ConversionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package me.desht.pneumaticcraft.common.util.legacyconv; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
import net.minecraft.Util; | ||
import net.minecraft.nbt.Tag; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.OptionalInt; | ||
|
||
public class ConversionUtil { | ||
static OptionalInt getInt(JsonObject json, String fieldName) { | ||
if (json.has(fieldName)) { | ||
JsonObject sub = json.get(fieldName).getAsJsonObject(); | ||
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_INT) { | ||
return OptionalInt.of(sub.get("value").getAsInt()); | ||
} | ||
} | ||
return OptionalInt.empty(); | ||
} | ||
|
||
static Optional<String> getString(JsonObject json, String fieldName) { | ||
if (json.has(fieldName)) { | ||
JsonObject sub = json.get(fieldName).getAsJsonObject(); | ||
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_STRING) { | ||
return Optional.of(sub.get("value").getAsString()); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static Optional<Boolean> getBool(JsonObject json, String fieldName) { | ||
if (json.has(fieldName)) { | ||
JsonObject sub = json.get(fieldName).getAsJsonObject(); | ||
if (sub.has("type") && sub.get("type").getAsInt() == Tag.TAG_BYTE) { | ||
return Optional.of(sub.get("value").getAsBoolean()); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static void convXYZ(JsonObject from, JsonObject to, String prefix, String newKey) { | ||
int sx = getInt(from, prefix + "X").orElse(0); | ||
int sy = getInt(from, prefix + "Y").orElse(0); | ||
int sz = getInt(from, prefix + "Z").orElse(0); | ||
to.add(newKey, makeArray(sx, sy, sz)); | ||
} | ||
|
||
static String prefixVar(String varName) { | ||
return varName.isEmpty() || varName.startsWith("%") || varName.startsWith("#") ? varName : "#" + varName; | ||
} | ||
|
||
static JsonArray makeArray(int... vals) { | ||
return Util.make(new JsonArray(), a -> Arrays.stream(vals).forEach(a::add)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters