Skip to content

Commit

Permalink
sync QSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 committed Aug 31, 2022
1 parent 6cedefd commit 85ac4bf
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2022 QuiltMC
* Modified to work on Fabric
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.frozenblock.datafixerupper.api;

import java.util.Map;
import java.util.function.Supplier;

import com.mojang.datafixers.DSL;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.types.templates.TypeTemplate;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import org.jetbrains.annotations.Range;

/**
* Represents an empty {@link Schema}, having no parent and containing no type definitions.
* <p>
* Modified to work on Fabric
*/
public final class EmptySchema extends FirstSchema {
/**
* Constructs an empty schema.
*
* @param versionKey the data version key
*/
public EmptySchema(@Range(from = 0, to = Integer.MAX_VALUE) int versionKey) {
super(versionKey);
}

// Ensure the schema stays empty.
@Override
public void registerType(boolean recursive, DSL.TypeReference type, Supplier<TypeTemplate> template) {
throw new UnsupportedOperationException();
}

@Override
protected Map<String, Type<?>> buildTypes() {
return Object2ObjectMaps.emptyMap();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2022 QuiltMC
* Modified to work on Fabric
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.frozenblock.datafixerupper.api;

import java.util.Map;
import java.util.function.Supplier;

import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.templates.TypeTemplate;
import org.jetbrains.annotations.Range;

/**
* Represents a {@link Schema} that has no parent.
* <p>
* Modified to work on Fabric
*/
public class FirstSchema extends Schema {
/**
* Creates a schema.
* @param versionKey the data version key
*/
public FirstSchema(@Range(from = 0, to = Integer.MAX_VALUE) int versionKey) {
super(versionKey, null);
}

// all of these methods refer to this.parent without checking if its null
@Override
public void registerTypes(Schema schema, Map<String, Supplier<TypeTemplate>> entityTypes,
Map<String, Supplier<TypeTemplate>> blockEntityTypes) {}

@Override
public Map<String, Supplier<TypeTemplate>> registerEntities(Schema schema) {
return Map.of();
}

@Override
public Map<String, Supplier<TypeTemplate>> registerBlockEntities(Schema schema) {
return Map.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

package org.quiltmc.qsl.frozenblock.datafixerupper.api;

import java.util.concurrent.Executor;
import java.util.function.Supplier;

import com.mojang.datafixers.DataFixer;
import com.mojang.datafixers.DataFixerBuilder;
import net.minecraft.SharedConstants;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;

import java.util.concurrent.Executor;
import java.util.function.Supplier;
import net.minecraft.SharedConstants;

/**
* An extended variant of the {@link DataFixerBuilder} class, which provides an extra method.
Expand Down Expand Up @@ -69,4 +70,4 @@ public int getDataVersion() {
case UNINITIALIZED_OPTIMIZED, INITIALIZED_OPTIMIZED -> this.buildOptimized(executorGetter.get());
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@

package org.quiltmc.qsl.frozenblock.datafixerupper.api;

import java.util.Optional;
import java.util.function.BiFunction;

import com.mojang.datafixers.DataFixer;
import com.mojang.datafixers.DataFixerBuilder;
import com.mojang.datafixers.schemas.Schema;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
import org.quiltmc.qsl.frozenblock.datafixerupper.impl.QuiltDataFixesInternals;

import java.util.Optional;
import java.util.function.BiFunction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.Util;

import net.fabricmc.loader.api.ModContainer;
import org.quiltmc.qsl.frozenblock.datafixerupper.impl.QuiltDataFixesInternals;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;


/**
* Provides methods to register custom {@link DataFixer}s.
* <p>
* Modified to work on Fabric.
* Modified to work on Fabric
*/
public final class QuiltDataFixes {
private QuiltDataFixes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@

package org.quiltmc.qsl.frozenblock.datafixerupper.api;

import static java.util.Objects.requireNonNull;

import java.util.Map;
import java.util.Objects;

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.DataFixerBuilder;
import com.mojang.datafixers.schemas.Schema;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import net.minecraft.util.datafix.fixes.RenameBiomesFix;
import net.minecraft.util.datafix.fixes.BlockRenameFix;
import net.minecraft.util.datafix.fixes.ItemRenameFix;
import net.minecraft.util.datafix.fixes.RenameBiomesFix;
import net.minecraft.util.datafix.schemas.NamespacedSchema;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Objects;

import static java.util.Objects.requireNonNull;
import net.minecraft.resources.ResourceLocation;

/**
* Provides methods to add common {@link DataFix}es to {@link DataFixerBuilder}s.
Expand Down Expand Up @@ -114,4 +115,4 @@ public static void addBiomeRenameFix(@NotNull DataFixerBuilder builder, @NotNull
}
builder.addFixer(new RenameBiomesFix(schema, false, name, mapBuilder.build()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
* @see org.quiltmc.qsl.frozenblock.datafixerupper.api.QuiltDataFixerBuilder
*/

package org.quiltmc.qsl.frozenblock.datafixerupper.api;
package org.quiltmc.qsl.frozenblock.datafixerupper.api;
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@

import com.mojang.datafixers.DataFixer;
import com.mojang.datafixers.schemas.Schema;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.datafix.DataFixTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.nbt.CompoundTag;

import org.quiltmc.qsl.frozenblock.datafixerupper.api.EmptySchema;

/**
* Modified to work on Fabric
*/
@ApiStatus.Internal
public final class NopQuiltDataFixesInternals extends QuiltDataFixesInternals {
public final class NoOpQuiltDataFixesInternals extends QuiltDataFixesInternals {
private final Schema schema;

private boolean frozen;

public NopQuiltDataFixesInternals() {
this.schema = new Schema(0, null);
public NoOpQuiltDataFixesInternals() {
this.schema = new EmptySchema(0);

this.frozen = false;
}

@Override
public void registerFixer(@NotNull String modId, @Range(from = 0, to = Integer.MAX_VALUE) int currentVersion,
@NotNull DataFixer dataFixer) {
}
@NotNull DataFixer dataFixer) {}

@Override
public @Nullable DataFixerEntry getFixerEntry(@NotNull String modId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.DataFixer;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.logging.LogUtils;
import org.jetbrains.annotations.*;
import org.slf4j.Logger;

import net.minecraft.SharedConstants;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.util.datafix.DataFixers;
import org.jetbrains.annotations.*;
import net.minecraft.nbt.CompoundTag;

/**
* Modified to work on Fabric
*/
@ApiStatus.Internal
public abstract class QuiltDataFixesInternals {
public record DataFixerEntry(DataFixer dataFixer, int currentVersion) {
}
private static final Logger LOGGER = LogUtils.getLogger();

public record DataFixerEntry(DataFixer dataFixer, int currentVersion) {}

@Contract(pure = true)
@Range(from = 0, to = Integer.MAX_VALUE)
Expand All @@ -53,9 +57,10 @@ public static int getModDataVersion(@NotNull CompoundTag compound, @NotNull Stri
}

if (latestVanillaSchema == null) {
// either this Minecraft version is horribly broken, or someone is suppressing DFU initialization
// use a no-op impl
instance = new NopQuiltDataFixesInternals();
LOGGER.warn("[Quilt DFU API] Failed to initialize! Either someone stopped DFU from initializing,");
LOGGER.warn("[Quilt DFU API] or this Minecraft build is hosed.");
LOGGER.warn("[Quilt DFU API] Using no-op implementation.");
instance = new NoOpQuiltDataFixesInternals();
} else {
instance = new QuiltDataFixesInternalsImpl(latestVanillaSchema);
}
Expand All @@ -64,19 +69,16 @@ public static int getModDataVersion(@NotNull CompoundTag compound, @NotNull Stri
return instance;
}

public abstract void registerFixer(@NotNull String modId,
@Range(from = 0, to = Integer.MAX_VALUE) int currentVersion,
public abstract void registerFixer(@NotNull String modId, @Range(from = 0, to = Integer.MAX_VALUE) int currentVersion,
@NotNull DataFixer dataFixer);

public abstract @Nullable DataFixerEntry getFixerEntry(@NotNull String modId);

@Contract(value = "-> new", pure = true)
public abstract @NotNull Schema createBaseSchema();

public abstract @NotNull CompoundTag updateWithAllFixers(@NotNull DataFixTypes dataFixTypes,
@NotNull CompoundTag compound);
public abstract @NotNull CompoundTag updateWithAllFixers(@NotNull DataFixTypes dataFixTypes, @NotNull CompoundTag compound);

@Contract("_ -> new")
public abstract @NotNull CompoundTag addModDataVersions(@NotNull CompoundTag compound);

public abstract void freeze();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

package org.quiltmc.qsl.frozenblock.datafixerupper.impl;

import java.util.Collections;
import java.util.Map;

import com.mojang.datafixers.DataFixer;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.serialization.Dynamic;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.util.datafix.DataFixTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

import java.util.Collections;
import java.util.Map;
import net.minecraft.util.datafix.DataFixTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;

/**
* Modified to work on Fabric
Expand All @@ -50,8 +51,7 @@ public QuiltDataFixesInternalsImpl(@NotNull Schema latestVanillaSchema) {
}

@Override
public void registerFixer(@NotNull String modId,
@Range(from = 0, to = Integer.MAX_VALUE) int currentVersion,
public void registerFixer(@NotNull String modId, @Range(from = 0, to = Integer.MAX_VALUE) int currentVersion,
@NotNull DataFixer dataFixer) {
if (this.modDataFixers.containsKey(modId)) {
throw new IllegalArgumentException("Mod '" + modId + "' already has a registered data fixer");
Expand All @@ -71,8 +71,7 @@ public void registerFixer(@NotNull String modId,
}

@Override
public @NotNull CompoundTag updateWithAllFixers(@NotNull DataFixTypes dataFixTypes,
@NotNull CompoundTag compound) {
public @NotNull CompoundTag updateWithAllFixers(@NotNull DataFixTypes dataFixTypes, @NotNull CompoundTag compound) {
var current = new Dynamic<>(NbtOps.INSTANCE, compound);

for (Map.Entry<String, DataFixerEntry> entry : this.modDataFixers.entrySet()) {
Expand Down
Loading

0 comments on commit 85ac4bf

Please sign in to comment.