Skip to content

Commit

Permalink
Merge branch 'master' into 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Nov 6, 2024
2 parents 7c6cc3f + d62e607 commit 9a7aacf
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 189 deletions.
9 changes: 1 addition & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,4 @@ 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 structure upgrading not saving anything.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
min_loader_version=0.16.7

# Mod Properties
mod_version = 1.9.3
mod_version = 1.9.4
maven_group = net.frozenblock
archives_base_name = FrozenLib

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/frozenblock/lib/FrozenMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/frozenblock/lib/file/nbt/NbtFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

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.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;

public class StructureUpgradeCommand {
private StructureUpgradeCommand() {}

public static void register(@NotNull CommandDispatcher<CommandSourceStack> 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<ResourceLocation> 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<ResourceLocation, CompoundTag> savedTemplates = new Object2ObjectLinkedOpenHashMap<>();

foundPieces.forEach((resourceLocation) -> {
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()
.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;
}
}

This file was deleted.

10 changes: 2 additions & 8 deletions src/main/resources/assets/frozenlib/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9a7aacf

Please sign in to comment.