[Bukkit] Remove nbt tricks from adapters

This commit is contained in:
Octavia Togami 2022-02-28 23:06:48 -08:00
parent ce13ed9979
commit 8c83ef80fa
2 changed files with 4 additions and 101 deletions

View File

@ -27,8 +27,6 @@
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.Lifecycle;
import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.ByteTag;
@ -83,11 +81,8 @@
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
import net.minecraft.resources.RegistryReadOps;
import net.minecraft.resources.RegistryWriteOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
@ -149,12 +144,12 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -608,7 +603,7 @@ private void doRegen(org.bukkit.World bukkitWorld, Region region, Extent extent,
long seed = options.getSeed().orElse(originalWorld.getSeed());
WorldGenSettings newOpts = options.getSeed().isPresent()
? replaceSeed(originalWorld, seed, originalOpts)
? originalOpts.withSeed(levelProperties.isHardcore(), OptionalLong.of(seed))
: originalOpts;
LevelSettings newWorldSettings = new LevelSettings(
@ -653,49 +648,6 @@ private void doRegen(org.bukkit.World bukkitWorld, Region region, Extent extent,
}
}
private WorldGenSettings replaceSeed(ServerLevel originalWorld, long seed, WorldGenSettings originalOpts) {
RegistryWriteOps<net.minecraft.nbt.Tag> nbtReadRegOps = RegistryWriteOps.create(
NbtOps.INSTANCE,
originalWorld.getServer().registryAccess()
);
RegistryReadOps<net.minecraft.nbt.Tag> nbtRegOps = RegistryReadOps.createAndLoad(
NbtOps.INSTANCE,
originalWorld.getServer().getResourceManager(),
originalWorld.getServer().registryAccess()
);
Codec<WorldGenSettings> dimCodec = WorldGenSettings.CODEC;
return dimCodec
.encodeStart(nbtReadRegOps, originalOpts)
.flatMap(tag ->
dimCodec.parse(
recursivelySetSeed(new Dynamic<>(nbtRegOps, tag), seed, new HashSet<>())
)
)
.get()
.map(
l -> l,
error -> {
throw new IllegalStateException("Unable to map GeneratorOptions: " + error.message());
}
);
}
@SuppressWarnings("unchecked")
private Dynamic<net.minecraft.nbt.Tag> recursivelySetSeed(Dynamic<net.minecraft.nbt.Tag> dynamic, long seed, Set<Dynamic<net.minecraft.nbt.Tag>> seen) {
if (!seen.add(dynamic)) {
return dynamic;
}
return dynamic.updateMapValues(pair -> {
if (pair.getFirst().asString("").equals("seed")) {
return pair.mapSecond(v -> v.createLong(seed));
}
if (pair.getSecond().getValue() instanceof net.minecraft.nbt.CompoundTag) {
return pair.mapSecond(v -> recursivelySetSeed((Dynamic<net.minecraft.nbt.Tag>) v, seed, seen));
}
return pair;
});
}
private BiomeType adapt(ServerLevel serverWorld, Biome origBiome) {
ResourceLocation key = serverWorld.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getKey(origBiome);
if (key == null) {

View File

@ -27,8 +27,6 @@
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.Lifecycle;
import com.sk89q.jnbt.ByteArrayTag;
import com.sk89q.jnbt.ByteTag;
@ -83,11 +81,8 @@
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
import net.minecraft.resources.RegistryReadOps;
import net.minecraft.resources.RegistryWriteOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
@ -118,7 +113,6 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.dimension.LevelStem;
import net.minecraft.world.level.levelgen.WorldGenSettings;
import net.minecraft.world.level.storage.LevelStorageSource;
@ -150,12 +144,12 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -602,7 +596,7 @@ private void doRegen(org.bukkit.World bukkitWorld, Region region, Extent extent,
long seed = options.getSeed().orElse(originalWorld.getSeed());
WorldGenSettings newOpts = options.getSeed().isPresent()
? replaceSeed(originalWorld, seed, originalOpts)
? originalOpts.withSeed(levelProperties.isHardcore(), OptionalLong.of(seed))
: originalOpts;
LevelSettings newWorldSettings = new LevelSettings(
@ -647,49 +641,6 @@ private void doRegen(org.bukkit.World bukkitWorld, Region region, Extent extent,
}
}
private WorldGenSettings replaceSeed(ServerLevel originalWorld, long seed, WorldGenSettings originalOpts) {
RegistryWriteOps<net.minecraft.nbt.Tag> nbtReadRegOps = RegistryWriteOps.create(
NbtOps.INSTANCE,
originalWorld.getServer().registryAccess()
);
RegistryReadOps<net.minecraft.nbt.Tag> nbtRegOps = RegistryReadOps.createAndLoad(
NbtOps.INSTANCE,
originalWorld.getServer().getResourceManager(),
originalWorld.getServer().registryAccess()
);
Codec<WorldGenSettings> dimCodec = WorldGenSettings.CODEC;
return dimCodec
.encodeStart(nbtReadRegOps, originalOpts)
.flatMap(tag ->
dimCodec.parse(
recursivelySetSeed(new Dynamic<>(nbtRegOps, tag), seed, new HashSet<>())
)
)
.get()
.map(
l -> l,
error -> {
throw new IllegalStateException("Unable to map GeneratorOptions: " + error.message());
}
);
}
@SuppressWarnings("unchecked")
private Dynamic<net.minecraft.nbt.Tag> recursivelySetSeed(Dynamic<net.minecraft.nbt.Tag> dynamic, long seed, Set<Dynamic<net.minecraft.nbt.Tag>> seen) {
if (!seen.add(dynamic)) {
return dynamic;
}
return dynamic.updateMapValues(pair -> {
if (pair.getFirst().asString("").equals("seed")) {
return pair.mapSecond(v -> v.createLong(seed));
}
if (pair.getSecond().getValue() instanceof net.minecraft.nbt.CompoundTag) {
return pair.mapSecond(v -> recursivelySetSeed((Dynamic<net.minecraft.nbt.Tag>) v, seed, seen));
}
return pair;
});
}
private BiomeType adapt(ServerLevel serverWorld, Biome origBiome) {
ResourceLocation key = serverWorld.registryAccess().registryOrThrow(Registry.BIOME_REGISTRY).getKey(origBiome);
if (key == null) {