From ba65f7b82cf41324c2970b0fd155b3211e18b9ac Mon Sep 17 00:00:00 2001 From: AViewFromTheTop <87103914+AViewFromTheTop@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:01:48 -0500 Subject: [PATCH 1/4] likely fix upgrading --- .../java/net/frozenblock/lib/FrozenMain.java | 2 +- .../impl/upgrade/CommandStructureUpdater.java | 52 +++++++++++++++++++ .../StructureUpgradeCommand.java | 5 +- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java rename src/main/java/net/frozenblock/lib/worldgen/structure/impl/{command => upgrade}/StructureUpgradeCommand.java (97%) diff --git a/src/main/java/net/frozenblock/lib/FrozenMain.java b/src/main/java/net/frozenblock/lib/FrozenMain.java index 9197a9ee..718f6261 100644 --- a/src/main/java/net/frozenblock/lib/FrozenMain.java +++ b/src/main/java/net/frozenblock/lib/FrozenMain.java @@ -56,7 +56,7 @@ import net.frozenblock.lib.worldgen.feature.api.placementmodifier.FrozenPlacementModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenRuleBlockEntityModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenStructureProcessorTypes; -import net.frozenblock.lib.worldgen.structure.impl.command.StructureUpgradeCommand; +import net.frozenblock.lib.worldgen.structure.impl.upgrade.StructureUpgradeCommand; import net.frozenblock.lib.worldgen.surface.impl.BiomeTagConditionSource; import net.frozenblock.lib.worldgen.surface.impl.OptimizedBiomeTagConditionSource; import net.minecraft.commands.synchronization.ArgumentTypeInfos; diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java new file mode 100644 index 00000000..18becf65 --- /dev/null +++ b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2024 FrozenBlock + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.frozenblock.lib.worldgen.structure.impl.upgrade; + +import com.mojang.logging.LogUtils; +import net.minecraft.SharedConstants; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.data.structures.SnbtToNbt; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtUtils; +import net.minecraft.util.datafix.DataFixTypes; +import net.minecraft.util.datafix.DataFixers; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; + +public class CommandStructureUpdater implements SnbtToNbt.Filter { + private static final Logger LOGGER = LogUtils.getLogger(); + + @Override + public @NotNull CompoundTag apply(String string, CompoundTag compoundTag) { + return update(string, compoundTag); + } + + public static @NotNull CompoundTag update(String name, CompoundTag nbt) { + StructureTemplate structureTemplate = new StructureTemplate(); + int i = NbtUtils.getDataVersion(nbt, 500); + int currentVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion(); + if (i < currentVersion) { + LOGGER.warn("SNBT Too old, do not forget to update: {} < {}: {}", i, currentVersion, name); + } + + CompoundTag compoundTag = DataFixTypes.STRUCTURE.updateToCurrentVersion(DataFixers.getDataFixer(), nbt, i); + structureTemplate.load(BuiltInRegistries.BLOCK.asLookup(), compoundTag); + return structureTemplate.save(new CompoundTag()); + } +} diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java similarity index 97% rename from src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java rename to src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java index f140ca11..88f19bd3 100644 --- a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java +++ b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package net.frozenblock.lib.worldgen.structure.impl.command; +package net.frozenblock.lib.worldgen.structure.impl.upgrade; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; @@ -27,7 +27,6 @@ import net.minecraft.data.PackOutput; import net.minecraft.data.structures.NbtToSnbt; import net.minecraft.data.structures.SnbtToNbt; -import net.minecraft.data.structures.StructureUpdater; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.Resource; @@ -129,7 +128,7 @@ private static int exportSNBTToNBT(@NotNull CommandSourceStack source, String na outputPath.toFile().mkdirs(); PackOutput packOutput = new PackOutput(outputPath); - SnbtToNbt snbtToNbt = new SnbtToNbt(packOutput, Set.of(inputPath)).addFilter(new StructureUpdater()); + SnbtToNbt snbtToNbt = new SnbtToNbt(packOutput, Set.of(inputPath)).addFilter(new CommandStructureUpdater()); CompletableFuture completableFuture = snbtToNbt.run(CachedOutput.NO_CACHE); while (!completableFuture.isDone()) { From 1dc99f3e6dd77e4e87abcffa7bccad9f17574bd9 Mon Sep 17 00:00:00 2001 From: AViewFromTheTop <87103914+AViewFromTheTop@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:09:08 -0500 Subject: [PATCH 2/4] Revert "likely fix upgrading" This reverts commit ba65f7b82cf41324c2970b0fd155b3211e18b9ac. --- .../java/net/frozenblock/lib/FrozenMain.java | 2 +- .../StructureUpgradeCommand.java | 5 +- .../impl/upgrade/CommandStructureUpdater.java | 52 ------------------- 3 files changed, 4 insertions(+), 55 deletions(-) rename src/main/java/net/frozenblock/lib/worldgen/structure/impl/{upgrade => command}/StructureUpgradeCommand.java (97%) delete mode 100644 src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java diff --git a/src/main/java/net/frozenblock/lib/FrozenMain.java b/src/main/java/net/frozenblock/lib/FrozenMain.java index 718f6261..9197a9ee 100644 --- a/src/main/java/net/frozenblock/lib/FrozenMain.java +++ b/src/main/java/net/frozenblock/lib/FrozenMain.java @@ -56,7 +56,7 @@ import net.frozenblock.lib.worldgen.feature.api.placementmodifier.FrozenPlacementModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenRuleBlockEntityModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenStructureProcessorTypes; -import net.frozenblock.lib.worldgen.structure.impl.upgrade.StructureUpgradeCommand; +import net.frozenblock.lib.worldgen.structure.impl.command.StructureUpgradeCommand; import net.frozenblock.lib.worldgen.surface.impl.BiomeTagConditionSource; import net.frozenblock.lib.worldgen.surface.impl.OptimizedBiomeTagConditionSource; import net.minecraft.commands.synchronization.ArgumentTypeInfos; diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java similarity index 97% rename from src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java rename to src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java index 88f19bd3..f140ca11 100644 --- a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/StructureUpgradeCommand.java +++ b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package net.frozenblock.lib.worldgen.structure.impl.upgrade; +package net.frozenblock.lib.worldgen.structure.impl.command; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; @@ -27,6 +27,7 @@ import net.minecraft.data.PackOutput; import net.minecraft.data.structures.NbtToSnbt; import net.minecraft.data.structures.SnbtToNbt; +import net.minecraft.data.structures.StructureUpdater; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.Resource; @@ -128,7 +129,7 @@ private static int exportSNBTToNBT(@NotNull CommandSourceStack source, String na outputPath.toFile().mkdirs(); PackOutput packOutput = new PackOutput(outputPath); - SnbtToNbt snbtToNbt = new SnbtToNbt(packOutput, Set.of(inputPath)).addFilter(new CommandStructureUpdater()); + SnbtToNbt snbtToNbt = new SnbtToNbt(packOutput, Set.of(inputPath)).addFilter(new StructureUpdater()); CompletableFuture completableFuture = snbtToNbt.run(CachedOutput.NO_CACHE); while (!completableFuture.isDone()) { diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java deleted file mode 100644 index 18becf65..00000000 --- a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/upgrade/CommandStructureUpdater.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2024 FrozenBlock - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.frozenblock.lib.worldgen.structure.impl.upgrade; - -import com.mojang.logging.LogUtils; -import net.minecraft.SharedConstants; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.data.structures.SnbtToNbt; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.NbtUtils; -import net.minecraft.util.datafix.DataFixTypes; -import net.minecraft.util.datafix.DataFixers; -import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; - -public class CommandStructureUpdater implements SnbtToNbt.Filter { - private static final Logger LOGGER = LogUtils.getLogger(); - - @Override - public @NotNull CompoundTag apply(String string, CompoundTag compoundTag) { - return update(string, compoundTag); - } - - public static @NotNull CompoundTag update(String name, CompoundTag nbt) { - StructureTemplate structureTemplate = new StructureTemplate(); - int i = NbtUtils.getDataVersion(nbt, 500); - int currentVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion(); - if (i < currentVersion) { - LOGGER.warn("SNBT Too old, do not forget to update: {} < {}: {}", i, currentVersion, name); - } - - CompoundTag compoundTag = DataFixTypes.STRUCTURE.updateToCurrentVersion(DataFixers.getDataFixer(), nbt, i); - structureTemplate.load(BuiltInRegistries.BLOCK.asLookup(), compoundTag); - return structureTemplate.save(new CompoundTag()); - } -} From 56d5b904034e8f55bde175d4ec234faa0056889f Mon Sep 17 00:00:00 2001 From: AViewFromTheTop <87103914+AViewFromTheTop@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:09:10 -0500 Subject: [PATCH 3/4] Revert "update structure upgrading" This reverts commit db5bbf9e7d3517e6e382b3fc0c9828e75d661974. --- CHANGELOG.md | 12 +- .../java/net/frozenblock/lib/FrozenMain.java | 2 +- .../lib/file/nbt/NbtFileUtils.java | 4 +- .../impl/StructureUpgradeCommand.java | 90 ++++++++++ .../impl/command/StructureUpgradeCommand.java | 168 ------------------ .../assets/frozenlib/lang/en_us.json | 10 +- 6 files changed, 98 insertions(+), 188 deletions(-) create mode 100644 src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java delete mode 100644 src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java diff --git a/CHANGELOG.md b/CHANGELOG.md index dbfc6882..501a873a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,7 @@ Make sure to clear this after each release Put changelog here: ----------------- -- Revamped the Structure NBT upgrading system. - - First, you must place the original structure NBT into the `structure/nbt_input` folder in the root of your project. - - This file path should be: `structure` -> `nbt_input` -> MOD ID -> STRUCTURE NAME - - Next, run `/structure snbt export {MOD ID}`. - - This will export all inputted NBT files under the mod id to SNBT. - - Finally, run `/structure snbt upgrade {MOD ID}`. - - This will place the fixed contents into the `nbt_output` folder. - - Copy the contents and paste them back into your structure data folder. +- Fixed FrozenLib's capes causing severe performance issues when switching dimensions or respawning. +- Fixed `NbtFileUtils` creating a directory instead of a file. +- Added the `structure_upgrade` command, only accessible in development environments. + - This takes a string for the `namespace` parameter, and will grab all structure pieces under the given namespace then save them to `run/upgraded_structure,` upgraded to the latest DataVersion. diff --git a/src/main/java/net/frozenblock/lib/FrozenMain.java b/src/main/java/net/frozenblock/lib/FrozenMain.java index 9197a9ee..24c91078 100644 --- a/src/main/java/net/frozenblock/lib/FrozenMain.java +++ b/src/main/java/net/frozenblock/lib/FrozenMain.java @@ -56,7 +56,7 @@ import net.frozenblock.lib.worldgen.feature.api.placementmodifier.FrozenPlacementModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenRuleBlockEntityModifiers; import net.frozenblock.lib.worldgen.structure.impl.FrozenStructureProcessorTypes; -import net.frozenblock.lib.worldgen.structure.impl.command.StructureUpgradeCommand; +import net.frozenblock.lib.worldgen.structure.impl.StructureUpgradeCommand; import net.frozenblock.lib.worldgen.surface.impl.BiomeTagConditionSource; import net.frozenblock.lib.worldgen.surface.impl.OptimizedBiomeTagConditionSource; import net.minecraft.commands.synchronization.ArgumentTypeInfos; diff --git a/src/main/java/net/frozenblock/lib/file/nbt/NbtFileUtils.java b/src/main/java/net/frozenblock/lib/file/nbt/NbtFileUtils.java index 52ae442b..6b60d0ce 100644 --- a/src/main/java/net/frozenblock/lib/file/nbt/NbtFileUtils.java +++ b/src/main/java/net/frozenblock/lib/file/nbt/NbtFileUtils.java @@ -18,10 +18,8 @@ package net.frozenblock.lib.file.nbt; import com.mojang.logging.LogUtils; -import java.io.DataInputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtIo; @@ -40,7 +38,7 @@ public static void saveToConfigFile(CompoundTag compoundTag, String fileName) { } public static void saveToFile(CompoundTag compoundTag, @NotNull File file, String fileName) { - file.getParentFile().mkdirs(); + file.mkdirs(); File destFile = new File(file, withNBTExtension(fileName)); saveToFile(compoundTag, destFile); } diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java new file mode 100644 index 00000000..6d9ee8f5 --- /dev/null +++ b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2024 FrozenBlock + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.frozenblock.lib.worldgen.structure.impl; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; +import net.frozenblock.lib.file.nbt.NbtFileUtils; +import net.minecraft.SharedConstants; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.ResourceManager; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; +import org.jetbrains.annotations.NotNull; +import java.nio.file.Path; +import java.util.Map; +import java.util.Set; + +public class StructureUpgradeCommand { + private StructureUpgradeCommand() {} + + public static void register(@NotNull CommandDispatcher dispatcher) { + dispatcher.register(Commands.literal("structure_upgrade") + .then(Commands.argument("namespace", StringArgumentType.string()) + .executes(context -> upgradeAndExportPieces(context.getSource(), StringArgumentType.getString(context, "namespace"), false)) + .then(Commands.argument("log", BoolArgumentType.bool()) + .executes(context -> upgradeAndExportPieces(context.getSource(), StringArgumentType.getString(context, "namespace"), BoolArgumentType.getBool(context, "log"))) + ) + ) + ); + } + + private static int upgradeAndExportPieces(@NotNull CommandSourceStack source, String namespace, boolean log) { + ResourceManager resourceManager = source.getServer().getResourceManager(); + + Set foundPieces = resourceManager.listResources( + "structure", + resourceLocation -> resourceLocation.getPath().endsWith(".nbt") && resourceLocation.getNamespace().equals(namespace) + ).keySet(); + + if (log) { + foundPieces.forEach(resourceLocation -> System.out.println("Found piece: " + resourceLocation.toString())); + } + + StructureTemplateManager structureTemplateManager = source.getLevel().getStructureManager(); + Map savedTemplates = new Object2ObjectLinkedOpenHashMap<>(); + + foundPieces.forEach((resourceLocation) -> { + savedTemplates.put(resourceLocation, structureTemplateManager.getOrCreate(resourceLocation).save(new CompoundTag())); + }); + + Path outputPath = source.getServer().getServerDirectory() + .resolve("upgraded_structure/data_version_" + SharedConstants.getCurrentVersion().getDataVersion().getVersion()); + + savedTemplates.forEach((resourceLocation, compoundTag) -> { + NbtFileUtils.saveToFile( + compoundTag, + outputPath.resolve(resourceLocation.getNamespace()).toFile(), + resourceLocation.getPath().replace(".nbt", "") + ); + }); + + int templateCount = savedTemplates.size(); + if (templateCount > 0) { + source.sendSuccess(() -> Component.translatable("commands.structure_upgrade.success", templateCount, namespace), true); + } else { + source.sendSuccess(() -> Component.translatable("commands.structure_upgrade.failure", namespace), true); + } + return 1; + } +} diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java deleted file mode 100644 index f140ca11..00000000 --- a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/command/StructureUpgradeCommand.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2024 FrozenBlock - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.frozenblock.lib.worldgen.structure.impl.command; - -import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.StringArgumentType; -import net.frozenblock.lib.FrozenSharedConstants; -import net.minecraft.FileUtil; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.commands.Commands; -import net.minecraft.data.CachedOutput; -import net.minecraft.data.PackOutput; -import net.minecraft.data.structures.NbtToSnbt; -import net.minecraft.data.structures.SnbtToNbt; -import net.minecraft.data.structures.StructureUpdater; -import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.resources.Resource; -import net.minecraft.server.packs.resources.ResourceManager; -import org.jetbrains.annotations.NotNull; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CompletableFuture; - -public class StructureUpgradeCommand { - private StructureUpgradeCommand() {} - - public static void register(@NotNull CommandDispatcher dispatcher) { - dispatcher.register(Commands.literal("structure") - .then(Commands.literal("snbt") - .then(Commands.literal("export") - .then(Commands.argument("namespace", StringArgumentType.string()) - .executes(context -> exportPiecesToSNBT(context.getSource(), StringArgumentType.getString(context, "namespace"))) - ) - ) - .then(Commands.literal("upgrade") - .then(Commands.argument("namespace", StringArgumentType.string()) - .executes(context -> exportSNBTToNBT(context.getSource(), StringArgumentType.getString(context, "namespace"))) - ) - ) - ) - .then(Commands.literal("piece") - .then(Commands.literal("count") - .then(Commands.argument("namespace", StringArgumentType.string()) - .executes(context -> countPieces(context.getSource(), StringArgumentType.getString(context, "namespace"))) - ) - ) - ) - ); - } - - private static int exportPiecesToSNBT(@NotNull CommandSourceStack source, String namespace) { - try { - ResourceManager resourceManager = source.getServer().getResourceManager(); - - Set foundPieces = resourceManager.listResources( - "structure", - resourceLocation -> resourceLocation.getPath().endsWith(".nbt") && resourceLocation.getNamespace().equals(namespace) - ).keySet(); - - Path path = source.getServer().getServerDirectory(); - if (path.getParent() != null) path = path.getParent(); - path = path.resolve("structure"); - Path inputPath = path.resolve("nbt_input"); - Path outputPath = path.resolve("snbt"); - outputPath.toFile().delete(); - outputPath.toFile().mkdirs(); - - for (ResourceLocation resourceLocation : foundPieces) { - String putPath = resourceLocation.getNamespace() + "/" + resourceLocation.getPath().replaceFirst("structure/", ""); - Path exportedPath = NbtToSnbt.convertStructure(CachedOutput.NO_CACHE, inputPath.resolve(putPath), putPath.replace(".nbt", ""), outputPath); - if (exportedPath != null) { - try { - FileUtil.createDirectoriesSafe(exportedPath.getParent()); - } catch (IOException var7) { - FrozenSharedConstants.LOGGER.error("Could not create export folder", var7); - } - } else { - FrozenSharedConstants.LOGGER.error("Failed to export {}", resourceLocation); - } - } - long streamCount = Files.walk(outputPath).filter(p -> p.toString().endsWith(".snbt")).count(); - - if (streamCount > 0) { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_snbt.success", streamCount, foundPieces.size(), namespace), true); - } else { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_snbt.failure", namespace), true); - } - return 1; - } catch (Exception e) { - e.printStackTrace(); - } - return 0; - } - - private static int exportSNBTToNBT(@NotNull CommandSourceStack source, String namespace) { - try { - ResourceManager resourceManager = source.getServer().getResourceManager(); - - Map foundPieces = resourceManager.listResources( - "structure", - resourceLocation -> resourceLocation.getPath().endsWith(".nbt") && resourceLocation.getNamespace().equals(namespace) - ); - - Path path = source.getServer().getServerDirectory(); - if (path.getParent() != null) path = path.getParent(); - path = path.resolve("structure"); - Path inputPath = path.resolve("snbt"); - Path outputPath = path.resolve("nbt_output"); - outputPath.toFile().delete(); - outputPath.toFile().mkdirs(); - - PackOutput packOutput = new PackOutput(outputPath); - SnbtToNbt snbtToNbt = new SnbtToNbt(packOutput, Set.of(inputPath)).addFilter(new StructureUpdater()); - CompletableFuture completableFuture = snbtToNbt.run(CachedOutput.NO_CACHE); - - while (!completableFuture.isDone()) { - } - - long streamCount = Files.walk(outputPath).filter(p -> p.toString().endsWith(".nbt")).count(); - - if (streamCount > 0) { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_upgrade.success", streamCount, foundPieces.size(), namespace), true); - } else { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_upgrade.failure", namespace), true); - } - return 1; - } catch (Exception e) { - e.printStackTrace(); - } - return 0; - } - - private static int countPieces(@NotNull CommandSourceStack source, String namespace) { - ResourceManager resourceManager = source.getServer().getResourceManager(); - - Set foundPieces = resourceManager.listResources( - "structure", - resourceLocation -> resourceLocation.getPath().endsWith(".nbt") && resourceLocation.getNamespace().equals(namespace) - ).keySet(); - - int pieceCount = foundPieces.size(); - if (pieceCount > 0) { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_piece_count.success", pieceCount, namespace), true); - } else { - source.sendSuccess(() -> Component.translatable("commands.frozenlib.structure_piece_count.failure", namespace), true); - } - return 1; - } -} diff --git a/src/main/resources/assets/frozenlib/lang/en_us.json b/src/main/resources/assets/frozenlib/lang/en_us.json index 17c4bd3a..b2a81c1a 100644 --- a/src/main/resources/assets/frozenlib/lang/en_us.json +++ b/src/main/resources/assets/frozenlib/lang/en_us.json @@ -81,14 +81,8 @@ "commands.frozenlib.taglist.footer": "Listed %s tags in %s.", "commands.frozenlib.taglist.tag.invalid": "Invalid tag: %s", - "commands.frozenlib.structure_snbt.success": "Exported %s of %s structure pieces to SNBT for %s.", - "commands.frozenlib.structure_snbt.failure": "No structure pieces found to export for %s.\nPlease copy structure NBT to structure/nbt_input in the root of your project.", - - "commands.frozenlib.structure_upgrade.success": "Upgraded and saved %s of %s structure pieces for %s.", - "commands.frozenlib.structure_upgrade.failure": "No structure pieces found for %s.\nPlease run /structure snbt export first.", - - "commands.frozenlib.structure_piece_count.success": "Found %s structure pieces for %s.", - "commands.frozenlib.structure_piece_count.failure": "No structure pieces found for %s.", + "commands.structure_upgrade.success": "Upgraded and saved %s structure pieces for %s.", + "commands.structure_upgrade.failure": "No structure pieces found for %s.", "commands.warden_spawn_tracker.clear.success.multiple": "Cleared warden spawn warnings from %s players", "commands.warden_spawn_tracker.clear.success.single": "Cleared warden spawn warnings from %s", From d62e6074cce45ab45e777953125b579cd02df124 Mon Sep 17 00:00:00 2001 From: AViewFromTheTop <87103914+AViewFromTheTop@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:26:06 -0500 Subject: [PATCH 4/4] fix structure upgrading --- CHANGELOG.md | 5 +---- gradle.properties | 2 +- .../impl/StructureUpgradeCommand.java | 20 ++++++++++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 501a873a..ee72e33c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,4 @@ Make sure to clear this after each release Put changelog here: ----------------- -- Fixed FrozenLib's capes causing severe performance issues when switching dimensions or respawning. -- Fixed `NbtFileUtils` creating a directory instead of a file. -- Added the `structure_upgrade` command, only accessible in development environments. - - This takes a string for the `namespace` parameter, and will grab all structure pieces under the given namespace then save them to `run/upgraded_structure,` upgraded to the latest DataVersion. +- Fixed structure upgrading not saving anything. diff --git a/gradle.properties b/gradle.properties index d7c6fc55..03a1166d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,7 +15,7 @@ min_loader_version=0.16.0 # Mod Properties - mod_version = 1.9.3 + mod_version = 1.9.4 maven_group = net.frozenblock archives_base_name = FrozenLib diff --git a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java index 6d9ee8f5..0641226e 100644 --- a/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java +++ b/src/main/java/net/frozenblock/lib/worldgen/structure/impl/StructureUpgradeCommand.java @@ -26,11 +26,17 @@ import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtAccounter; +import net.minecraft.nbt.NbtIo; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; +import net.minecraft.util.FastBufferedInputStream; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; import org.jetbrains.annotations.NotNull; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Path; import java.util.Map; import java.util.Set; @@ -65,7 +71,19 @@ private static int upgradeAndExportPieces(@NotNull CommandSourceStack source, St Map savedTemplates = new Object2ObjectLinkedOpenHashMap<>(); foundPieces.forEach((resourceLocation) -> { - savedTemplates.put(resourceLocation, structureTemplateManager.getOrCreate(resourceLocation).save(new CompoundTag())); + try { + InputStream inputStream = resourceManager.getResourceOrThrow(resourceLocation).open(); + InputStream inputStream2 = new FastBufferedInputStream(inputStream); + CompoundTag compoundTag = NbtIo.readCompressed(inputStream2, NbtAccounter.unlimitedHeap()); + StructureTemplate structureTemplate = structureTemplateManager.readStructure(compoundTag); + + inputStream2.close(); + inputStream.close(); + + savedTemplates.put(resourceLocation, structureTemplate.save(new CompoundTag())); + } catch (IOException e) { + throw new RuntimeException(e); + } }); Path outputPath = source.getServer().getServerDirectory()