From cc5ddd79365b4c82e4c0094f6add7763adc9a06f Mon Sep 17 00:00:00 2001 From: DerFrZocker Date: Tue, 15 Feb 2022 09:36:00 +1100 Subject: [PATCH] #1010: Add PDC to World --- .../minecraft/server/level/WorldServer.patch | 2 +- .../world/level/storage/Convertable.patch | 19 +++++++++-- .../world/level/storage/WorldDataServer.patch | 33 +++++++++++++------ .../org/bukkit/craftbukkit/CraftWorld.java | 24 ++++++++++++++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/nms-patches/net/minecraft/server/level/WorldServer.patch b/nms-patches/net/minecraft/server/level/WorldServer.patch index cd644c49f..aabb6a3a6 100644 --- a/nms-patches/net/minecraft/server/level/WorldServer.patch +++ b/nms-patches/net/minecraft/server/level/WorldServer.patch @@ -70,7 +70,7 @@ - this.serverLevelData = iworlddataserver; + // CraftBukkit start + this.serverLevelData = (WorldDataServer) iworlddataserver; -+ serverLevelData.world = this; ++ serverLevelData.setWorld(this); + if (gen != null) { + chunkgenerator = new org.bukkit.craftbukkit.generator.CustomChunkGenerator(this, chunkgenerator, gen); + } diff --git a/nms-patches/net/minecraft/world/level/storage/Convertable.patch b/nms-patches/net/minecraft/world/level/storage/Convertable.patch index f8540f5f1..a06bdcce7 100644 --- a/nms-patches/net/minecraft/world/level/storage/Convertable.patch +++ b/nms-patches/net/minecraft/world/level/storage/Convertable.patch @@ -11,7 +11,20 @@ public class Convertable { static final Logger LOGGER = LogManager.getLogger(); -@@ -277,9 +281,23 @@ +@@ -216,7 +220,11 @@ + LevelVersion levelversion = LevelVersion.parse(dynamic); + WorldSettings worldsettings = WorldSettings.parse(dynamic, datapackconfiguration); + +- return WorldDataServer.parse(dynamic, datafixer, i, nbttagcompound2, worldsettings, levelversion, (GeneratorSettings) pair.getFirst(), (Lifecycle) pair.getSecond()); ++ // CraftBukkit start - Add PDC to world ++ WorldDataServer worldDataServer = WorldDataServer.parse(dynamic, datafixer, i, nbttagcompound2, worldsettings, levelversion, (GeneratorSettings) pair.getFirst(), (Lifecycle) pair.getSecond()); ++ worldDataServer.pdc = nbttagcompound1.get("BukkitValues"); ++ return worldDataServer; ++ // CraftBukkit end + } catch (Exception exception) { + Convertable.LOGGER.error("Exception reading {}", file, exception); + return null; +@@ -277,9 +285,23 @@ return this.backupDir; } @@ -37,7 +50,7 @@ public class ConversionSession implements AutoCloseable { -@@ -287,8 +305,12 @@ +@@ -287,8 +309,12 @@ public final Path levelPath; private final String levelId; private final Map resources = Maps.newHashMap(); @@ -51,7 +64,7 @@ this.levelId = s; this.levelPath = Convertable.this.baseDir.resolve(s); this.lock = SessionLock.create(this.levelPath); -@@ -305,7 +327,7 @@ +@@ -305,7 +331,7 @@ } public Path getDimensionPath(ResourceKey resourcekey) { diff --git a/nms-patches/net/minecraft/world/level/storage/WorldDataServer.patch b/nms-patches/net/minecraft/world/level/storage/WorldDataServer.patch index 3b4e8f1b4..0234dffaa 100644 --- a/nms-patches/net/minecraft/world/level/storage/WorldDataServer.patch +++ b/nms-patches/net/minecraft/world/level/storage/WorldDataServer.patch @@ -16,15 +16,27 @@ public class WorldDataServer implements IWorldDataServer, SaveData { private static final Logger LOGGER = LogManager.getLogger(); -@@ -78,6 +87,7 @@ +@@ -78,6 +87,19 @@ private final Set knownServerBrands; private boolean wasModded; private final CustomFunctionCallbackTimerQueue scheduledEvents; -+ public WorldServer world; // CraftBukkit ++ // CraftBukkit start - Add world and pdc ++ private WorldServer world; ++ protected NBTBase pdc; ++ ++ public void setWorld(WorldServer world) { ++ if (this.world != null) { ++ return; ++ } ++ this.world = world; ++ world.getWorld().readBukkitValues(pdc); ++ pdc = null; ++ } ++ // CraftBukkit end private WorldDataServer(@Nullable DataFixer datafixer, int i, @Nullable NBTTagCompound nbttagcompound, boolean flag, int j, int k, int l, float f, long i1, long j1, int k1, int l1, int i2, boolean flag1, int j2, boolean flag2, boolean flag3, boolean flag4, WorldBorder.c worldborder_c, int k2, int l2, @Nullable UUID uuid, Set set, CustomFunctionCallbackTimerQueue customfunctioncallbacktimerqueue, @Nullable NBTTagCompound nbttagcompound1, NBTTagCompound nbttagcompound2, WorldSettings worldsettings, GeneratorSettings generatorsettings, Lifecycle lifecycle) { this.fixerUpper = datafixer; -@@ -121,7 +131,8 @@ +@@ -121,7 +143,8 @@ return (NBTBase) dynamic.get("DimensionData").get("1").get("DragonFight").orElseEmptyMap().getValue(); }); @@ -34,7 +46,7 @@ return SystemUtils.toStream(dynamic1.asString().result()); }).collect(Collectors.toCollection(Sets::newLinkedHashSet)), new CustomFunctionCallbackTimerQueue<>(CustomFunctionCallbackTimers.SERVER_CALLBACKS, dynamic.get("ScheduledEvents").asStream()), (NBTTagCompound) dynamic.get("CustomBossEvents").orElseEmptyMap().getValue(), nbttagcompound1, worldsettings, generatorsettings, lifecycle); } -@@ -141,7 +152,7 @@ +@@ -141,7 +164,7 @@ private void setTagData(IRegistryCustom iregistrycustom, NBTTagCompound nbttagcompound, @Nullable NBTTagCompound nbttagcompound1) { NBTTagList nbttaglist = new NBTTagList(); @@ -43,7 +55,7 @@ Objects.requireNonNull(nbttaglist); stream.forEach(nbttaglist::add); -@@ -156,7 +167,7 @@ +@@ -156,7 +179,7 @@ nbttagcompound.put("Version", nbttagcompound2); nbttagcompound.putInt("DataVersion", SharedConstants.getCurrentVersion().getWorldVersion()); RegistryWriteOps registrywriteops = RegistryWriteOps.create(DynamicOpsNBT.INSTANCE, iregistrycustom); @@ -52,15 +64,16 @@ Logger logger = WorldDataServer.LOGGER; Objects.requireNonNull(logger); -@@ -204,6 +215,7 @@ +@@ -204,6 +227,8 @@ nbttagcompound.putUUID("WanderingTraderId", this.wanderingTraderId); } + nbttagcompound.putString("Bukkit.Version", Bukkit.getName() + "/" + Bukkit.getVersion() + "/" + Bukkit.getBukkitVersion()); // CraftBukkit ++ world.getWorld().storeBukkitValues(nbttagcompound); // CraftBukkit - add pdc } @Override -@@ -321,6 +333,20 @@ +@@ -321,6 +346,20 @@ @Override public void setThundering(boolean flag) { @@ -81,7 +94,7 @@ this.thundering = flag; } -@@ -341,6 +367,20 @@ +@@ -341,6 +380,20 @@ @Override public void setRaining(boolean flag) { @@ -102,7 +115,7 @@ this.raining = flag; } -@@ -407,6 +447,12 @@ +@@ -407,6 +460,12 @@ @Override public void setDifficulty(EnumDifficulty enumdifficulty) { this.settings = this.settings.withDifficulty(enumdifficulty); @@ -115,7 +128,7 @@ } @Override -@@ -527,4 +573,12 @@ +@@ -527,4 +586,12 @@ public WorldSettings getLevelSettings() { return this.settings.copy(); } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index dbdd93e97..d5a328df4 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -23,6 +23,8 @@ import java.util.UUID; import java.util.function.Predicate; import java.util.stream.Collectors; import net.minecraft.core.BlockPosition; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket; import net.minecraft.network.protocol.game.PacketPlayOutCustomSoundEffect; import net.minecraft.network.protocol.game.PacketPlayOutEntitySound; @@ -91,6 +93,8 @@ import org.bukkit.craftbukkit.entity.CraftEntity; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.metadata.BlockMetadataStore; +import org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer; +import org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry; import org.bukkit.craftbukkit.potion.CraftPotionUtil; import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftRayTraceResult; @@ -116,6 +120,7 @@ import org.bukkit.generator.ChunkGenerator; import org.bukkit.inventory.ItemStack; import org.bukkit.material.MaterialData; import org.bukkit.metadata.MetadataValue; +import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.messaging.StandardMessenger; import org.bukkit.potion.PotionData; @@ -127,6 +132,7 @@ import org.bukkit.util.Vector; public class CraftWorld extends CraftRegionAccessor implements World { public static final int CUSTOM_DIMENSION_OFFSET = 10; + private static final CraftPersistentDataTypeRegistry DATA_TYPE_REGISTRY = new CraftPersistentDataTypeRegistry(); private final WorldServer world; private WorldBorder worldBorder; @@ -137,6 +143,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { private final List populators = new ArrayList(); private final BlockMetadataStore blockMetadata = new BlockMetadataStore(this); private final Object2IntOpenHashMap spawnCategoryLimit = new Object2IntOpenHashMap<>(); + private final CraftPersistentDataContainer persistentDataContainer = new CraftPersistentDataContainer(DATA_TYPE_REGISTRY); private static final Random rand = new Random(); @@ -1791,4 +1798,21 @@ public class CraftWorld extends CraftRegionAccessor implements World { public DragonBattle getEnderDragonBattle() { return (getHandle().dragonFight() == null) ? null : new CraftDragonBattle(getHandle().dragonFight()); } + + @Override + public PersistentDataContainer getPersistentDataContainer() { + return persistentDataContainer; + } + + public void storeBukkitValues(NBTTagCompound c) { + if (!this.persistentDataContainer.isEmpty()) { + c.put("BukkitValues", this.persistentDataContainer.toTagCompound()); + } + } + + public void readBukkitValues(NBTBase c) { + if (c instanceof NBTTagCompound) { + this.persistentDataContainer.putAll((NBTTagCompound) c); + } + } }