Skip to content

Commit

Permalink
Add Version display to Litematics in File Browser
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Sep 13, 2024
1 parent c94feb7 commit f04937d
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ author = masa
mod_file_name = litematica-fabric

# Current mod version
mod_version = 0.19.3-sakura.2
mod_version = 0.19.3-sakura.3

# Required malilib version
malilib_version = 0.20.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

import fi.dy.masa.litematica.schematic.SchematicSchema;
import fi.dy.masa.litematica.util.DataFixerMode;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.tuple.Pair;

Expand All @@ -32,6 +35,7 @@ public class WidgetSchematicBrowser extends WidgetFileBrowserBase
protected static final FileFilter SCHEMATIC_FILTER = new FileFilterSchematics();

protected final Map<File, SchematicMetadata> cachedMetadata = new HashMap<>();
protected final Map<File, SchematicSchema> cachedVersion = new HashMap<>();
protected final Map<File, Pair<Identifier, NativeImageBackedTexture>> cachedPreviewImages = new HashMap<>();
protected final GuiSchematicBrowserBase parent;
protected final int infoWidth;
Expand Down Expand Up @@ -93,7 +97,20 @@ protected void drawSelectedSchematicInfo(@Nullable DirectoryEntry entry, DrawCon
return;
}

SchematicMetadata meta = this.getSchematicMetadata(entry);
Pair<SchematicSchema, SchematicMetadata> metaPair = this.getSchematicVersionAndMetadata(entry);
SchematicMetadata meta;
SchematicSchema version;

if (metaPair != null)
{
meta = metaPair.getRight();
version = metaPair.getLeft();
}
else
{
return;
}


if (meta != null)
{
Expand Down Expand Up @@ -164,6 +181,19 @@ protected void drawSelectedSchematicInfo(@Nullable DirectoryEntry entry, DrawCon
y += 12;
}

if (version != null)
{
str = StringUtils.translate("litematica.gui.label.schematic_info.version", version.litematicVersion());
this.drawString(drawContext, str, x, y, textColor);
y += 12;

DataFixerMode.Schema schema = DataFixerMode.getSchemaByVersion(version.minecraftDataVersion());

str = StringUtils.translate("litematica.gui.label.schematic_info.schema", schema.getString(), version.minecraftDataVersion());
this.drawString(drawContext, str, x, y, textColor);
y += 12;
}

/*
str = StringUtils.translate("litematica.gui.label.schematic_info.description");
this.drawString(x, y, textColor, str);
Expand Down Expand Up @@ -198,6 +228,7 @@ public void clearSchematicMetadataCache()
this.clearPreviewImages();
this.cachedMetadata.clear();
this.cachedPreviewImages.clear();
this.cachedVersion.clear();
}

@Nullable
Expand All @@ -224,6 +255,35 @@ protected SchematicMetadata getSchematicMetadata(DirectoryEntry entry)
return meta;
}

@Nullable
protected Pair<SchematicSchema, SchematicMetadata> getSchematicVersionAndMetadata(DirectoryEntry entry)
{
File file = new File(entry.getDirectory(), entry.getName());
SchematicMetadata meta = this.cachedMetadata.get(file);
SchematicSchema version = this.cachedVersion.get(file);

if (meta == null && this.cachedMetadata.containsKey(file) == false)
{
if (entry.getName().endsWith(LitematicaSchematic.FILE_EXTENSION))
{
Pair<SchematicSchema, SchematicMetadata> pair = LitematicaSchematic.readMetadataAndVersionFromFile(entry.getDirectory(), entry.getName());

if (pair != null)
{
meta = pair.getRight();
version = pair.getLeft();

this.createPreviewImage(file, meta);
}
}

this.cachedMetadata.put(file, meta);
this.cachedVersion.put(file, version);
}

return Pair.of(version, meta);
}

private void clearPreviewImages()
{
for (Pair<Identifier, NativeImageBackedTexture> pair : this.cachedPreviewImages.values())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import fi.dy.masa.litematica.util.PositionUtils;
import fi.dy.masa.litematica.util.WorldUtils;
import fi.dy.masa.litematica.util.*;
import org.apache.commons.lang3.tuple.Pair;

public class LitematicaSchematic
{
Expand Down Expand Up @@ -2342,6 +2343,54 @@ public static SchematicMetadata readMetadataFromFile(File dir, String fileName)
return null;
}

@Nullable
public static Pair<SchematicSchema, SchematicMetadata> readMetadataAndVersionFromFile(File dir, String fileName)
{
NbtCompound nbt = readNbtFromFile(fileFromDirAndName(dir, fileName, FileType.LITEMATICA_SCHEMATIC));

if (nbt != null)
{
SchematicMetadata metadata = new SchematicMetadata();

if (nbt.contains("Version", Constants.NBT.TAG_INT))
{
final int version = nbt.getInt("Version");
final int dataVersion = nbt.contains("MinecraftDataVersion") ? nbt.getInt("MinecraftDataVersion") : Configs.Generic.DATAFIXER_DEFAULT_SCHEMA.getIntegerValue();

if (version >= 1)
{
metadata.readFromNBT(nbt.getCompound("Metadata"));

return Pair.of(new SchematicSchema(version, dataVersion), metadata);
}
}
}

return null;
}

@Nullable
public static SchematicSchema readDataVersionFromFile(File dir, String fileName)
{
NbtCompound nbt = readNbtFromFile(fileFromDirAndName(dir, fileName, FileType.LITEMATICA_SCHEMATIC));

if (nbt != null)
{
if (nbt.contains("Version", Constants.NBT.TAG_INT))
{
final int version = nbt.getInt("Version");
final int dataVersion = nbt.contains("MinecraftDataVersion") ? nbt.getInt("MinecraftDataVersion") : Configs.Generic.DATAFIXER_DEFAULT_SCHEMA.getIntegerValue();

if (version >= 1)
{
return new SchematicSchema(version, dataVersion);
}
}
}

return null;
}

@Nullable
public static LitematicaSchematic createFromFile(File dir, String fileName)
{
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/fi/dy/masa/litematica/schematic/SchematicSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fi.dy.masa.litematica.schematic;

public record SchematicSchema(int litematicVersion, int minecraftDataVersion)
{
@Override
public String toString()
{
return "V" + this.litematicVersion() + " / DataVersion " + this.minecraftDataVersion();
}
}
82 changes: 50 additions & 32 deletions src/main/java/fi/dy/masa/litematica/util/DataFixerMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,48 +177,66 @@ public static Schema getSchemaByVersion(int dataVersion)
public enum Schema
{
// Minecraft Data Versions
SCHEMA_24W37A (4065),
SCHEMA_1_21_01 (3955),
SCHEMA_1_21_00 (3953),
SCHEMA_1_20_05 (3837),
SCHEMA_1_20_04 (3700),
SCHEMA_1_20_02 (3578),
SCHEMA_1_20_01 (3465),
SCHEMA_1_20_00 (3463),
SCHEMA_1_19_04 (3337),
SCHEMA_1_19_00 (3105),
SCHEMA_1_18_02 (2975),
SCHEMA_1_18_00 (2860),
SCHEMA_1_17_01 (2730),
SCHEMA_1_17_00 (2724),
SCHEMA_1_16_05 (2586),
SCHEMA_1_16_00 (2566),
SCHEMA_1_15_02 (2230),
SCHEMA_1_15_00 (2225),
SCHEMA_1_14_04 (1976),
SCHEMA_1_14_00 (1952),
SCHEMA_1_13_02 (1631),
SCHEMA_1_13_00 (1519),
SCHEMA_1_12_02 (1343),
SCHEMA_1_12_00 (1139),
SCHEMA_1_11_02 (922),
SCHEMA_1_11_00 (819),
SCHEMA_1_10_02 (512),
SCHEMA_1_10_00 (510),
SCHEMA_1_09_04 (184),
SCHEMA_1_09_00 (169),
SCHEMA_15W32A (100);
SCHEMA_24W37A (4065, "24w37a"),
SCHEMA_1_21_01 (3955, "1.21.1"),
SCHEMA_1_21_00 (3953, "1.21"),
SCHEMA_1_20_05 (3837, "1.20.5"),
SCHEMA_24W10A (3821, "24w10a"),
SCHEMA_24W09A (3819, "24w09a"), // Data Components ( https://minecraft.wiki/w/Data_component_format )
SCHEMA_24W07A (3817, "24w07a"),
SCHEMA_1_20_04 (3700, "1.20.4"),
SCHEMA_1_20_02 (3578, "1.20.2"),
SCHEMA_1_20_01 (3465, "1.20.1"),
SCHEMA_1_20_00 (3463, "1.20"),
SCHEMA_1_19_04 (3337, "1.19.4"),
SCHEMA_1_19_00 (3105, "1.19"),
SCHEMA_1_18_02 (2975, "1.18.2"),
SCHEMA_1_18_00 (2860, "1.18"),
SCHEMA_1_17_01 (2730, "1.17.1"),
SCHEMA_1_17_00 (2724, "1.17"),
SCHEMA_1_16_05 (2586, "1.16.5"),
SCHEMA_1_16_00 (2566, "1.16"),
SCHEMA_1_15_02 (2230, "1.15.2"),
SCHEMA_1_15_00 (2225, "1.15"),
SCHEMA_1_14_04 (1976, "1.14.4"),
SCHEMA_1_14_00 (1952, "1.14"),
SCHEMA_1_13_02 (1631, "1.13.2"),
SCHEMA_1_13_00 (1519, "1.13"),
SCHEMA_17W47A (1451, "17w47a"), // The Flattening ( https://minecraft.wiki/w/Java_Edition_1.13/Flattening )
SCHEMA_17W46A (1449, "17w46a"),
SCHEMA_1_12_02 (1343, "1.12.2"),
SCHEMA_1_12_00 (1139, "1.12"),
SCHEMA_1_11_02 (922, "1.11.2"),
SCHEMA_1_11_00 (819, "1.11"),
SCHEMA_1_10_02 (512, "1.10.2"),
SCHEMA_1_10_00 (510, "1.10"),
SCHEMA_1_09_04 (184, "1.9.4"),
SCHEMA_1_09_00 (169, "1.9"),
SCHEMA_15W32A (100, "15w32a");

private final int schemaId;
private final String str;

Schema(int id)
Schema(int id, String ver)
{
this.schemaId = id;
this.str = ver;
}

public int getDataVersion()
{
return this.schemaId;
}

public String getString()
{
return this.str;
}

@Override
public String toString()
{
return "MC: "+this.getString()+" [Schema: "+this.getDataVersion()+"]";
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/litematica/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@
"litematica.gui.label.schematic_info.total_blocks": "Total blocks: §f%d§r",
"litematica.gui.label.schematic_info.total_blocks_and_volume": "Blocks/Volume: §f%d§r/§f%d§r",
"litematica.gui.label.schematic_info.total_volume": "Total volume: §f%d§r blocks",
"litematica.gui.label.schematic_info.version": "Litematic Version: §f%d§r",
"litematica.gui.label.schematic_info.schema": "Minecraft: §f%s§r [Schema §f%d§r]",

"litematica.gui.label.schematic_load.checkbox.create_placement": "Create a placement",
"litematica.gui.label.schematic_load.hoverinfo.create_placement": "Immediately create a new placement\nand select that placement",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/litematica/lang/it_it.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@
"litematica.gui.label.schematic_info.total_blocks": "Totale blocchi: §f%d§r",
"litematica.gui.label.schematic_info.total_blocks_and_volume": "Blocchi/Volume: §f%d§r/§f%d§r",
"litematica.gui.label.schematic_info.total_volume": "Volume totale: §f%d§r blocchi",
"litematica.gui.label.schematic_info.version": "Versione Litematic: §f%d§r",
"litematica.gui.label.schematic_info.schema": "Minecraft: §f%s§r [Schema §f%d§r]",

"litematica.gui.label.schematic_load.checkbox.create_placement": "Crea una posizione",
"litematica.gui.label.schematic_load.hoverinfo.create_placement": "Crea immediatamente una nuova posizione\ned è selezionata quella posizione",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/litematica/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@
"litematica.gui.label.schematic_info.total_blocks": "方块总数: §f%d§r",
"litematica.gui.label.schematic_info.total_blocks_and_volume": "方块/体积: §f%d§r/§f%d§r",
"litematica.gui.label.schematic_info.total_volume": "总体积: §f%d§r 个方块",
"litematica.gui.label.schematic_info.version": "精简版: §f%d§r",
"litematica.gui.label.schematic_info.schema": "我的世界: §f%s§r [架构 §f%d§r]",

"litematica.gui.label.schematic_load.checkbox.create_placement": "创建放置",
"litematica.gui.label.schematic_load.hoverinfo.create_placement": "立即创建新位置并选择该位置",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/litematica/lang/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@
"litematica.gui.label.schematic_info.total_blocks": "總方塊數: §f%d§r",
"litematica.gui.label.schematic_info.total_blocks_and_volume": "方塊/體積: §f%d§r/§f%d§r",
"litematica.gui.label.schematic_info.total_volume": "總體積: §f%d§r 個方塊",
"litematica.gui.label.schematic_info.version": "精簡版: §f%d§r",
"litematica.gui.label.schematic_info.schema": "我的世界: §f%s§r [架构 §f%d§r]",

"litematica.gui.label.schematic_load.checkbox.create_placement": "創建放置",
"litematica.gui.label.schematic_load.hoverinfo.create_placement": "立即創建新位置\n並選擇該位置",
Expand Down

0 comments on commit f04937d

Please sign in to comment.