Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SRF 1-8 -> 12 migration. #96

Merged
merged 16 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add migration from SRF 1-8
Not production ready, needs fixing.
  • Loading branch information
kyngs committed Jan 6, 2024
commit 1f93decd47114d3db692c89a01f892d54fe2d4e7
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.infernalsuite.aswm.api;

import com.flowpowered.nbt.CompoundMap;
import com.flowpowered.nbt.CompoundTag;
import com.infernalsuite.aswm.api.world.SlimeWorld;
import com.infernalsuite.aswm.api.world.SlimeWorldInstance;
import net.kyori.adventure.util.Services;
Expand Down Expand Up @@ -47,4 +48,6 @@ static class Holder {

}

CompoundTag convertChunkTo1_13(CompoundTag tag);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.infernalsuite.aswm.api.world.properties.SlimePropertyMap;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v11.v11WorldFormat;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v12.v12WorldFormat;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v19.v1_9WorldFormat;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9WorldFormat;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v10.v10WorldFormat;

import java.io.ByteArrayInputStream;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9;

public interface Upgrade {

void upgrade(v1_9SlimeWorld world);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.upgrade;

import com.flowpowered.nbt.CompoundTag;
import com.flowpowered.nbt.StringTag;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.Upgrade;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9SlimeChunk;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9SlimeWorld;

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

public class v1_11WorldUpgrade implements Upgrade {

private static Map<String, String> oldToNewMap = new HashMap<>();
private static Map<String, String> newToOldMap = new HashMap<>();

static {
rename("Furnace", "minecraft:furnace");
rename("Chest", "minecraft:chest");
rename("EnderChest", "minecraft:ender_chest");
rename("RecordPlayer", "minecraft:jukebox");
rename("Trap", "minecraft:dispenser");
rename("Dropper", "minecraft:dropper");
rename("Sign", "minecraft:sign");
rename("MobSpawner", "minecraft:mob_spawner");
rename("Music", "minecraft:noteblock");
rename("Piston", "minecraft:piston");
rename("Cauldron", "minecraft:brewing_stand");
rename("EnchantTable", "minecraft:enchanting_table");
rename("Airportal", "minecraft:end_portal");
rename("Beacon", "minecraft:beacon");
rename("Skull", "minecraft:skull");
rename("DLDetector", "minecraft:daylight_detector");
rename("Hopper", "minecraft:hopper");
rename("Comparator", "minecraft:comparator");
rename("FlowerPot", "minecraft:flower_pot");
rename("Banner", "minecraft:banner");
rename("Structure", "minecraft:structure_block");
rename("EndGateway", "minecraft:end_gateway");
rename("Control", "minecraft:command_block");
rename(null, "minecraft:bed"); // Patch for issue s#62
}

private static void rename(String oldName, String newName) {
if (oldName != null) {
oldToNewMap.put(oldName, newName);
}

newToOldMap.put(newName, oldName);
}

@Override
public void upgrade(v1_9SlimeWorld world) {
// 1.11 changed the way Tile Entities are named
for (v1_9SlimeChunk chunk : world.chunks.values()) {
for (CompoundTag entityTag : chunk.tileEntities) {
String oldType = entityTag.getAsStringTag("id").get().getValue();
String newType = oldToNewMap.get(oldType);

if (newType == null) {
if (newToOldMap.containsKey(oldType)) { // Maybe it's in the new format for some reason?
continue;
}

throw new IllegalStateException("Failed to find 1.11 upgrade for tile entity " + oldType);
}

entityTag.getValue().put("id", new StringTag("id", newType));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.upgrade;

import com.flowpowered.nbt.*;
import com.infernalsuite.aswm.api.SlimeNMSBridge;
import com.infernalsuite.aswm.api.utils.NibbleArray;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.Upgrade;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9SlimeChunk;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9SlimeChunkSection;
import com.infernalsuite.aswm.serialization.slime.reader.impl.v1_9.v1_9SlimeWorld;
import org.bukkit.ChatColor;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class v1_13WorldUpgrade implements Upgrade {

@Override
public void upgrade(v1_9SlimeWorld world) {
Logger.getLogger("v1_13WorldUpgrade").warning("Updating world to the 1.13 format. This may take a while.");

List<v1_9SlimeChunk> chunks = new ArrayList<>(world.chunks.values());
long lastMessage = -1;

for (int i = 0; i < chunks.size(); i++) {
v1_9SlimeChunk chunk = chunks.get(i);

// The world upgrade process is a very complex task, and there's already a
// built-in upgrade tool inside the server, so we can simply use it
CompoundTag globalTag = new CompoundTag("", new CompoundMap());
globalTag.getValue().put("DataVersion", new IntTag("DataVersion", 1343));

CompoundTag chunkTag = new CompoundTag("Level", new CompoundMap());

chunkTag.getValue().put("xPos", new IntTag("xPos", chunk.x));
chunkTag.getValue().put("zPos", new IntTag("zPos", chunk.z));
chunkTag.getValue().put("Sections", serializeSections(chunk.sections));
chunkTag.getValue().put("Entities", new ListTag<>("Entities", TagType.TAG_COMPOUND, chunk.entities));
chunkTag.getValue().put("TileEntities", new ListTag<>("TileEntities", TagType.TAG_COMPOUND, chunk.tileEntities));
chunkTag.getValue().put("TileTicks", new ListTag<>("TileTicks", TagType.TAG_COMPOUND, new ArrayList<>()));
chunkTag.getValue().put("TerrainPopulated", new ByteTag("TerrainPopulated", (byte) 1));
chunkTag.getValue().put("LightPopulated", new ByteTag("LightPopulated", (byte) 1));

globalTag.getValue().put("Level", chunkTag);

globalTag = SlimeNMSBridge.instance().convertChunkTo1_13(globalTag);
chunkTag = globalTag.getAsCompoundTag("Level").get();

// Chunk sections
v1_9SlimeChunkSection[] newSections = new v1_9SlimeChunkSection[16];
ListTag<CompoundTag> serializedSections = (ListTag<CompoundTag>) chunkTag.getAsListTag("Sections").get();

for (CompoundTag sectionTag : serializedSections.getValue()) {
ListTag<CompoundTag> palette = (ListTag<CompoundTag>) sectionTag.getAsListTag("Palette").get();
long[] blockStates = sectionTag.getLongArrayValue("BlockStates").get();

NibbleArray blockLight = new NibbleArray(sectionTag.getByteArrayValue("BlockLight").get());
NibbleArray skyLight = new NibbleArray(sectionTag.getByteArrayValue("SkyLight").get());

int index = sectionTag.getIntValue("Y").get();

v1_9SlimeChunkSection section = new v1_9SlimeChunkSection(null, null, palette, blockStates, null, null, blockLight, skyLight);
newSections[index] = section;
}

// Biomes
int[] newBiomes = new int[256];

for (int index = 0; index < chunk.biomes.length; index++) {
newBiomes[index] = chunk.biomes[index] & 255;
}

chunk.sections = newSections;
chunk.biomes = newBiomes;

int done = i + 1;
if (done == chunks.size()) {
Logger.getLogger("v1_13WorldUpgrade").info("World successfully converted to the 1.13 format!");
} else if (System.currentTimeMillis() - lastMessage > 1000) {
int percentage = (done * 100) / chunks.size();
Logger.getLogger("v1_13WorldUpgrade").info("Converting world... " + percentage + "%");
lastMessage = System.currentTimeMillis();
}
}
}

private ListTag<CompoundTag> serializeSections(v1_9SlimeChunkSection[] sections) {
ListTag<CompoundTag> sectionList = new ListTag<>("Sections", TagType.TAG_COMPOUND, new ArrayList<>());

for (int i = 0; i < sections.length; i++) {
v1_9SlimeChunkSection section = sections[i];

if (section != null) {
CompoundTag sectionTag = new CompoundTag(i + "", new CompoundMap());

sectionTag.getValue().put("Y", new IntTag("Y", i));
sectionTag.getValue().put("Blocks", new ByteArrayTag("Blocks", section.blocks));
sectionTag.getValue().put("Data", new ByteArrayTag("Data", section.data.getBacking()));
sectionTag.getValue().put("BlockLight", new ByteArrayTag("Data", section.blockLight.getBacking()));
sectionTag.getValue().put("SkyLight", new ByteArrayTag("Data", section.skyLight.getBacking()));

sectionList.getValue().add(sectionTag);
}
}

return sectionList;
}
}
Loading