Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Faboslav committed Oct 3, 2024
1 parent e25287b commit 8015266
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.faboslav.friendsandfoes.common.init.registry.RegistryEntry;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

import java.util.function.Supplier;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.fabricmc.fabric.api.registry.FabricBrewingRecipeRegistryBuilder;
import net.fabricmc.fabric.api.registry.FlammableBlockRegistry;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.block.BlockSetType;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnRestriction;
import net.minecraft.entity.mob.MobEntity;
Expand All @@ -31,7 +30,6 @@
import net.minecraft.resource.ResourceType;
import net.minecraft.village.TradeOffers;
import net.minecraft.world.dimension.DimensionTypes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,73 @@
import java.util.concurrent.CompletableFuture;

// Source: https://github.com/BluSunrize/ImmersiveEngineering/blob/1.20.1/src/datagen/java/blusunrize/immersiveengineering/data/StructureUpdater.java
public class StructureNbtUpdater implements DataProvider {
private final String basePath;
private final String modid;
private final DataOutput output;
private final LifecycledResourceManagerImpl resources;
public class StructureNbtUpdater implements DataProvider
{
private final String basePath;
private final String modid;
private final DataOutput output;
private final LifecycledResourceManagerImpl resources;

public StructureNbtUpdater(String basePath, String modid, ExistingFileHelper helper, DataOutput output) {
this.basePath = basePath;
this.modid = modid;
this.output = output;
public StructureNbtUpdater(String basePath, String modid, ExistingFileHelper helper, DataOutput output) {
this.basePath = basePath;
this.modid = modid;
this.output = output;

try {
Field serverData = ExistingFileHelper.class.getDeclaredField("serverData");
serverData.setAccessible(true);
resources = (LifecycledResourceManagerImpl) serverData.get(helper);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
try {
Field serverData = ExistingFileHelper.class.getDeclaredField("serverData");
serverData.setAccessible(true);
resources = (LifecycledResourceManagerImpl) serverData.get(helper);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Override
public @NotNull CompletableFuture<?> run(@Nonnull DataWriter cache) {
try {
for (var entry : resources.findResources(basePath, $ -> true).entrySet()) {
if (entry.getKey().getNamespace().equals(modid)) {
process(entry.getKey(), entry.getValue(), cache);
}
}
return CompletableFuture.completedFuture(null);
} catch (IOException x) {
return CompletableFuture.failedFuture(x);
}
}
@Override
public @NotNull CompletableFuture<?> run(@Nonnull DataWriter cache) {
try {
for (var entry : resources.findResources(basePath, $ -> true).entrySet()) {
if (entry.getKey().getNamespace().equals(modid)) {
process(entry.getKey(), entry.getValue(), cache);
}
}
return CompletableFuture.completedFuture(null);
} catch (IOException x) {
return CompletableFuture.failedFuture(x);
}
}

private void process(Identifier loc, Resource resource, DataWriter cache) throws IOException {
NbtCompound inputNBT = NbtIo.readCompressed(resource.getInputStream(), NbtSizeTracker.ofUnlimitedBytes());
NbtCompound converted = updateNBT(inputNBT);
if (!converted.equals(inputNBT)) {
Class<? extends DataFixer> fixerClass = Schemas.getFixer().getClass();
if (!fixerClass.equals(DataFixerUpper.class)) {
throw new RuntimeException("Structures are not up to date, but unknown data fixer is in use: " + fixerClass.getName());
}
writeNBTTo(loc, converted, cache);
}
}
private void process(Identifier loc, Resource resource, DataWriter cache) throws IOException {
NbtCompound inputNBT = NbtIo.readCompressed(resource.getInputStream(), NbtSizeTracker.ofUnlimitedBytes());
NbtCompound converted = updateNBT(inputNBT);
if (!converted.equals(inputNBT)) {
Class<? extends DataFixer> fixerClass = Schemas.getFixer().getClass();
if (!fixerClass.equals(DataFixerUpper.class)) {
throw new RuntimeException("Structures are not up to date, but unknown data fixer is in use: " + fixerClass.getName());
}
writeNBTTo(loc, converted, cache);
}
}

private void writeNBTTo(Identifier loc, NbtCompound data, DataWriter cache) throws IOException {
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
NbtIo.writeCompressed(data, bytearrayoutputstream);
byte[] bytes = bytearrayoutputstream.toByteArray();
Path outputPath = output.getPath().resolve("data/" + loc.getNamespace() + "/" + loc.getPath());
cache.write(outputPath, bytes, Hashing.sha1().hashBytes(bytes));
}
private void writeNBTTo(Identifier loc, NbtCompound data, DataWriter cache) throws IOException {
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
NbtIo.writeCompressed(data, bytearrayoutputstream);
byte[] bytes = bytearrayoutputstream.toByteArray();
Path outputPath = output.getPath().resolve("data/" + loc.getNamespace() + "/" + loc.getPath());
cache.write(outputPath, bytes, Hashing.sha1().hashBytes(bytes));
}

private static NbtCompound updateNBT(NbtCompound nbt) {
final NbtCompound updatedNBT = DataFixTypes.STRUCTURE.update(
Schemas.getFixer(), nbt, nbt.getInt("DataVersion")
);
StructureTemplate template = new StructureTemplate();
template.readNbt(Registries.BLOCK.getReadOnlyWrapper(), updatedNBT);
return template.writeNbt(new NbtCompound());
}
private static NbtCompound updateNBT(NbtCompound nbt) {
final NbtCompound updatedNBT = DataFixTypes.STRUCTURE.update(
Schemas.getFixer(), nbt, nbt.getInt("DataVersion")
);
StructureTemplate template = new StructureTemplate();
template.readNbt(Registries.BLOCK.getReadOnlyWrapper(), updatedNBT);
return template.writeNbt(new NbtCompound());
}

@Nonnull
@Override
public String getName() {
return "Update structure files in " + basePath;
}
@Nonnull
@Override
public String getName() {
return "Update structure files in " + basePath;
}
}

0 comments on commit 8015266

Please sign in to comment.