Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.ldtteam.structurize.blueprints.conversion;

import com.mojang.serialization.Dynamic;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.util.datafix.fixes.ChunkPalettedStorageFix;
import net.minecraft.world.item.DyeColor;

/**
* Block states whose pre-1.13 properties depended on neighboring blocks or block entities.
* Shared by the legacy format importer and older Sponge/Litematica exports.
*/
final class LegacyBlockStateFixer
{
private static final int FLATTENED_VERSION = 1519;

private LegacyBlockStateFixer()
{
}

static CompoundTag normalizeSkull(final CompoundTag input)
{
final String name = input.getString("Name");
if (!name.equals("%%FILTER_ME%%") && !name.equals("minecraft:skull") && !name.equals("skull"))
{
return input;
}
final String facing = input.getCompound("Properties").getString("facing");
final boolean wall = facing.equals("north") || facing.equals("south") || facing.equals("west") || facing.equals("east");
final CompoundTag result = new CompoundTag();
result.putString("Name", wall ? "minecraft:skeleton_wall_skull" : "minecraft:skeleton_skull");
final CompoundTag properties = new CompoundTag();
properties.putString(wall ? "facing" : "rotation", wall ? facing : "0");
result.put("Properties", properties);
return result;
}

static void fixMultiBlockStates(final SchematicConversionUtils result)
{
for (int y = 1; y < result.sizeY; y++)
{
for (int z = 0; z < result.sizeZ; z++)
{
for (int x = 0; x < result.sizeX; x++)
{
if (!result.stateAt(x, y, z).getCompound("Properties").getString("half").equals("upper"))
{
continue;
}
final CompoundTag upper = result.stateAt(x, y, z).copy();
final CompoundTag properties = upper.getCompound("Properties");
final CompoundTag lower = result.stateAt(x, y - 1, z).copy();
if (upper.getString("Name").startsWith("minecraft:") && upper.getString("Name").endsWith("_door")
&& upper.getString("Name").equals(lower.getString("Name")))
{
final CompoundTag lowerProperties = lower.getCompound("Properties");
properties.putString("facing", lowerProperties.getString("facing"));
properties.putString("open", lowerProperties.getString("open"));
lowerProperties.putString("hinge", properties.getString("hinge"));
lowerProperties.putString("powered", properties.getString("powered"));
result.setBlock(x, y - 1, z, result.addState(lower, SchematicConversionUtils.currentVersion()));
}
else if (isTallPlant(lower.getString("Name")) && isTallPlant(upper.getString("Name")))
{
upper.putString("Name", lower.getString("Name"));
}
else
{
continue;
}
result.setBlock(x, y, z, result.addState(upper, SchematicConversionUtils.currentVersion()));
}
}
}
}

private static boolean isTallPlant(final String name)
{
return switch (name)
{
case "minecraft:sunflower", "minecraft:lilac", "minecraft:tall_grass", "minecraft:large_fern", "minecraft:rose_bush", "minecraft:peony" -> true;
default -> false;
};
}

/**
* Some pre-flattening block properties lived in tile NBT instead of the numeric metadata.
* Reuse Minecraft's own conversion tables before updating the remaining block entity data.
*/
static boolean fixBlockEntityState(final SchematicConversionUtils result, final CompoundTag tile, final int[] pos)
{
final CompoundTag state = result.stateAt(pos[0], pos[1], pos[2]).copy();
final String name = state.getString("Name");
if (!name.startsWith("minecraft:"))
{
return false;
}
final CompoundTag properties = state.getCompound("Properties");
Dynamic<?> replacement = null;
boolean consumed = false;
if (name.equals("minecraft:note_block"))
{
replacement = ChunkPalettedStorageFix.NOTE_BLOCK_MAP.get(Boolean.toString(tile.getBoolean("powered")) + Math.clamp(tile.getInt("note"), 0, 24));
consumed = true;
}
else if (name.equals("minecraft:flower_pot") || name.startsWith("minecraft:potted_"))
{
String item = tile.getString("Item");
if (tile.contains("Item", Tag.TAG_ANY_NUMERIC))
{
item = switch (tile.getInt("Item"))
{
case 6 -> "sapling";
case 31 -> "tallgrass";
case 32 -> "deadbush";
case 37 -> "yellow_flower";
case 38 -> "red_flower";
case 39 -> "brown_mushroom";
case 40 -> "red_mushroom";
case 81 -> "cactus";
default -> "air";
};
}
if (!item.contains(":"))
{
item = "minecraft:" + item;
}
replacement = ChunkPalettedStorageFix.FLOWER_POT_MAP.getOrDefault(item + tile.getInt("Data"), ChunkPalettedStorageFix.FLOWER_POT_MAP.get("minecraft:air0"));
consumed = true;
}
else if (name.endsWith("_bed"))
{
state.putString("Name", "minecraft:" + DyeColor.byId(tile.contains("color") ? tile.getInt("color") : 14).getName() + "_bed");
result.setBlock(pos[0], pos[1], pos[2], result.addState(state, SchematicConversionUtils.currentVersion()));
}
else if (name.endsWith("_skull") || name.endsWith("_head"))
{
final boolean wall = name.contains("_wall_");
String skullName = switch (tile.getInt("SkullType"))
{
case 1 -> "wither_skeleton_skull";
case 2 -> "zombie_head";
case 3 -> "player_head";
case 4 -> "creeper_head";
case 5 -> "dragon_head";
default -> "skeleton_skull";
};
if (wall)
{
skullName = skullName.replace("_skull", "_wall_skull").replace("_head", "_wall_head");
}
else
{
properties.putString("rotation", Integer.toString(tile.getInt("Rot") & 15));
}
state.putString("Name", "minecraft:" + skullName);
result.setBlock(pos[0], pos[1], pos[2], result.addState(state, SchematicConversionUtils.currentVersion()));
}
else if (name.endsWith("_banner"))
{
final String color = DyeColor.byId(15 - (tile.contains("Base") ? tile.getInt("Base") : 15)).getName();
state.putString("Name", "minecraft:" + color + (name.contains("_wall_") ? "_wall_banner" : "_banner"));
result.setBlock(pos[0], pos[1], pos[2], result.addState(state, SchematicConversionUtils.currentVersion()));
}
if (replacement != null)
{
result.setBlock(pos[0], pos[1], pos[2], result.addState(asTag(replacement), FLATTENED_VERSION));
}
return consumed;
}

private static CompoundTag asTag(final Dynamic<?> dynamic)
{
return (CompoundTag) dynamic.convert(NbtOps.INSTANCE).getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.ldtteam.structurize.blueprints.conversion;

import com.mojang.serialization.Dynamic;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.util.datafix.fixes.BlockStateData;
import net.minecraft.util.datafix.fixes.EntityBlockStateFix;

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

/**
* Converts the legacy MCEdit/WorldEdit Alpha .schematic format (numeric block IDs and metadata).
* Vanilla IDs and vanilla entries in SchematicaMapping are supported. Modded numeric IDs cannot be
* translated to modern block states reliably and cause the import to fail instead of becoming air.
*/
public final class LegacySchematicImporter
{
private static final int FLATTENED_VERSION = 1519;

private LegacySchematicImporter()
{
}

/**
* Convert an Alpha schematic without modifying the input.
*
* @param input source NBT.
* @param name blueprint name.
* @return blueprint v1 NBT.
*/
public static CompoundTag convert(final CompoundTag input, final String name)
{
if (input.contains("Materials") && !input.getString("Materials").equals("Alpha"))
{
throw new IllegalArgumentException("Only Alpha legacy schematics are supported");
}
final int dataVersion = SchematicConversionUtils.dataVersion(input, "DataVersion", 100);
final SchematicConversionUtils result = new SchematicConversionUtils(
dimension(input, "Width"), dimension(input, "Height"), dimension(input, "Length"));
final int volume = result.sizeX * result.sizeY * result.sizeZ;
final byte[] ids = byteArray(input, "Blocks");
final byte[] metadata = byteArray(input, "Data");
final byte[] extra = input.contains("AddBlocks") ? byteArray(input, "AddBlocks") : new byte[0];
if (ids.length != volume || metadata.length != volume || extra.length > (volume + 1) / 2)
{
throw new IllegalArgumentException("Legacy schematic block arrays do not match its dimensions");
}
final Map<Integer, Integer> idMapping = readMapping(input);
final Map<Integer, Short> states = new HashMap<>();
int index = 0;
for (int y = 0; y < result.sizeY; y++)
{
for (int z = 0; z < result.sizeZ; z++)
{
for (int x = 0; x < result.sizeX; x++, index++)
{
int id = ids[index] & 0xFF;
if (index / 2 < extra.length)
{
id |= ((extra[index / 2] >>> ((index & 1) * 4)) & 0xF) << 8;
}
if (!idMapping.isEmpty() && (id != 0 || idMapping.containsKey(0)))
{
if (!idMapping.containsKey(id))
{
throw new IllegalArgumentException("Unmapped legacy block ID: " + id);
}
id = idMapping.get(id);
}
if (id < 0 || id > 255 || (id != 0 && BlockStateData.upgradeBlock(id << 4).equals("minecraft:air")))
{
throw new IllegalArgumentException("Unsupported legacy block ID: " + id + " (modded IDs require a modern schematic export)");
}
final int combined = (id << 4) | (metadata[index] & 0xF);
final short state = states.computeIfAbsent(combined,
key -> result.addState(LegacyBlockStateFixer.normalizeSkull(asTag(BlockStateData.getTag(key))), FLATTENED_VERSION));
result.setBlock(x, y, z, state);
}
}
}
LegacyBlockStateFixer.fixMultiBlockStates(result);
for (final Tag entry : SchematicConversionUtils.optionalList(input, "TileEntities"))
{
final CompoundTag tile = ((CompoundTag) entry).copy();
final int[] pos = SchematicConversionUtils.blockPosition(tile);
result.checkPosition(pos[0], pos[1], pos[2]);
result.addBlockEntity(tile, pos[0], pos[1], pos[2], dataVersion);
}
for (final Tag entry : SchematicConversionUtils.optionalList(input, "Entities"))
{
result.addEntity((CompoundTag) entry, 0, 0, 0, dataVersion);
}
BlockPos anchor = null;
if (input.contains("WEOffsetX", Tag.TAG_INT) && input.contains("WEOffsetY", Tag.TAG_INT) && input.contains("WEOffsetZ", Tag.TAG_INT))
{
anchor = result.anchor(-(long) input.getInt("WEOffsetX"), -(long) input.getInt("WEOffsetY"), -(long) input.getInt("WEOffsetZ"));
}
return result.toBlueprint(name, input.getString("Author"), anchor);
}

private static Map<Integer, Integer> readMapping(final CompoundTag input)
{
final Map<Integer, Integer> mapping = new HashMap<>();
if (input.contains("SchematicaMapping"))
{
final CompoundTag source = SchematicConversionUtils.compound(input, "SchematicaMapping");
for (final String name : source.getAllKeys())
{
if (!source.contains(name, Tag.TAG_ANY_NUMERIC))
{
throw new IllegalArgumentException("Invalid Schematica block mapping: " + name);
}
final String namespaced = name.contains(":") ? name : "minecraft:" + name;
final int vanillaId = EntityBlockStateFix.getBlockId(namespaced);
mapping.put(source.getInt(name), vanillaId == 0 && !namespaced.equals("minecraft:air") ? -1 : vanillaId);
}
}
return mapping;
}

private static CompoundTag asTag(final Dynamic<?> dynamic)
{
return (CompoundTag) dynamic.convert(NbtOps.INSTANCE).getValue();
}

private static int dimension(final CompoundTag input, final String key)
{
if (!input.contains(key, Tag.TAG_SHORT))
{
throw new IllegalArgumentException("Missing legacy schematic dimension: " + key);
}
return input.getShort(key) & 0xFFFF;
}

private static byte[] byteArray(final CompoundTag input, final String key)
{
if (!input.contains(key, Tag.TAG_BYTE_ARRAY))
{
throw new IllegalArgumentException("Missing legacy schematic array: " + key);
}
return input.getByteArray(key);
}
}
Loading
Loading