Skip to content

Instantly share code, notes, and snippets.

@kennytv
Last active November 18, 2021 21:10
Show Gist options
  • Save kennytv/1ee95cd3b8bb57dc8ee8cb71d5a4883e to your computer and use it in GitHub Desktop.
Save kennytv/1ee95cd3b8bb57dc8ee8cb71d5a4883e to your computer and use it in GitHub Desktop.
Via Mapping Utilities
import com.google.gson.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.Map;
public final class MappingsGenerator {
public static void main(final String[] args) throws IOException {
new File("generated").delete();
net.minecraft.data.Main.main(new String[]{"--reports"});
String content = new String(Files.readAllBytes(new File("generated/reports/blocks.json").toPath()));
final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
JsonObject object = gson.fromJson(content, JsonObject.class);
final JsonObject viaMappings = new JsonObject();
final JsonObject blockstates = new JsonObject();
final JsonObject blocks = new JsonObject();
viaMappings.add("blockstates", blockstates);
viaMappings.add("blocks", blocks);
String lastBlock = "";
int id = 0;
for (final Map.Entry<String, JsonElement> blocksEntry : object.entrySet()) {
final JsonObject block = blocksEntry.getValue().getAsJsonObject();
final JsonArray states = block.getAsJsonArray("states");
for (final JsonElement state : states) {
final StringBuilder value = new StringBuilder(blocksEntry.getKey());
if (!lastBlock.equals(blocksEntry.getKey())) {
lastBlock = blocksEntry.getKey();
blocks.add(Integer.toString(id++), new JsonPrimitive(lastBlock.replace("minecraft:", "")));
}
if (state.getAsJsonObject().has("properties")) {
value.append("[");
final JsonObject properties = state.getAsJsonObject().getAsJsonObject("properties");
boolean first = true;
for (final Map.Entry<String, JsonElement> propertyEntry : properties.entrySet()) {
if (first) {
first = false;
} else {
value.append(',');
}
value.append(propertyEntry.getKey()).append('=').append(propertyEntry.getValue().getAsJsonPrimitive().getAsString());
}
value.append("]");
}
blockstates.add(state.getAsJsonObject().get("id").getAsString(), new JsonPrimitive(value.toString()));
}
}
content = new String(Files.readAllBytes(new File("generated/reports/registries.json").toPath()));
object = gson.fromJson(content, JsonObject.class);
final JsonObject items = new JsonObject();
viaMappings.add("items", items);
for (final Map.Entry<String, JsonElement> itemsEntry : object.getAsJsonObject("minecraft:item").getAsJsonObject("entries").entrySet()) {
items.add(String.valueOf(itemsEntry.getValue().getAsJsonObject().getAsJsonPrimitive("protocol_id").getAsInt()), new JsonPrimitive(itemsEntry.getKey()));
}
final JsonArray sounds = new JsonArray();
viaMappings.add("sounds", sounds);
int i = 0;
for (final Map.Entry<String, JsonElement> soundEntry : object.getAsJsonObject("minecraft:sound_event").getAsJsonObject("entries").entrySet()) {
if (soundEntry.getValue().getAsJsonObject().getAsJsonPrimitive("protocol_id").getAsInt() != i) {
throw new IllegalStateException();
}
sounds.add(new JsonPrimitive(soundEntry.getKey().replace("minecraft:", "")));
i++;
}
try (final PrintWriter out = new PrintWriter("mapping-1.16.json")) {
out.print(gson.toJson(viaMappings));
}
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.File;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public final class ViaItemMapHelper {
public static void main(final String[] args) throws Exception {
final Gson gson = new GsonBuilder().create();
final String oldVer = "1.15";
final String newVer = "1.16";
// requires mapping files (shipped with ViaVersion)
final JsonObject oldMapping = gson.fromJson(Files.readString(new File("mapping-" + oldVer + ".json").toPath()), JsonObject.class);
final JsonObject newMapping = gson.fromJson(Files.readString(new File("mapping-" + newVer + ".json").toPath()), JsonObject.class);
final JsonObject missing = new JsonObject();
final Set<String> oldItems = new HashSet<>();
for (final Map.Entry<String, JsonElement> entry : oldMapping.getAsJsonObject("items").entrySet()) {
oldItems.add(entry.getValue().getAsString());
}
for (final Map.Entry<String, JsonElement> entry : newMapping.getAsJsonObject("items").entrySet()) {
final String namespacedKey = entry.getValue().getAsString();
if (oldItems.contains(namespacedKey)) continue;
final JsonObject object = new JsonObject();
object.addProperty("id", ""); // to be added manually case by case
object.addProperty("name", newVer + " " + createNiceString(namespacedKey));
missing.add(namespacedKey, object);
}
System.out.println(missing);
}
public static String createNiceString(String s) {
s = s.replace("minecraft:", "");
String result = "";
for (final String split : s.split("_")) {
result = result + " " + Character.toUpperCase(split.charAt(0)) + split.substring(1);
}
return result.substring(1);
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.File;
import java.nio.file.Files;
import java.util.LinkedHashMap;
import java.util.Map;
public final class ViaTranslation {
public static void main(final String[] args) throws Exception {
final String oldVer = "15.2";
final String newVer = "16";
final Gson gson = new GsonBuilder().setLenient().create();
final Map<String, String> newTranslations = new LinkedHashMap<>();
final JsonObject object = gson.fromJson(Files.readString(new File("en_us" + newVer + ".json").toPath()), JsonObject.class);
for (final Map.Entry<String, JsonElement> entry : object.entrySet()) {
newTranslations.put(entry.getKey(), entry.getValue().getAsString());
}
final Map<String, String> oldTranslations = new LinkedHashMap<>();
final JsonObject oldObject = gson.fromJson(Files.readString(new File("en_us" + oldVer + ".json").toPath()), JsonObject.class);
for (final Map.Entry<String, JsonElement> entry : oldObject.entrySet()) {
oldTranslations.put(entry.getKey(), entry.getValue().getAsString());
}
final JsonObject diff = new JsonObject();
for (final Map.Entry<String, String> entry : newTranslations.entrySet()) {
if (!oldTranslations.containsKey(entry.getKey())) {
diff.addProperty(entry.getKey(), entry.getValue());
}
}
System.out.println(diff);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment