Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.api.tag.v1;

public interface FabricTagEntry {
default boolean isRemoved() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.impl.tag;

import java.util.function.Supplier;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.DynamicOps;
import org.jetbrains.annotations.Nullable;

import net.minecraft.registry.tag.TagEntry;
import net.minecraft.util.Unit;

import net.fabricmc.fabric.impl.tag.util.WrapperCodec;

public final class FabricTagEntryImpl {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FabricTagEntryImpl but it doesnt implement FabricTagEntry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to call it, as it just contains the mixin context thread local and the codec we merge. It can implement FabricTagEntry, but then the mixin doesn't implement FabricTagEntryImpl, and if we make it an interface so the mixin can implement it, then we lose encapsulation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps FabricTagEntryInternals is clearer to what it actually does?

public static final Codec<TagEntry> REMOVED_ENTRY_CODEC = new WrapperCodec<>(
TagEntry.CODEC,
new WrapperCodec.Wrapper<>() {
@Override
public <T> DataResult<Pair<TagEntry, T>> decode(DynamicOps<T> ops, T input, Decoder<TagEntry> wrapped) {
return FabricTagEntryImpl.withRemovedValue(true, () -> wrapped.decode(ops, input));
}
}
);

/**
* A Fake Argument to the {@link TagEntry} constructor representing whether the entry will have it's {@code removed} flag set.
*/
private static final ThreadLocal<Unit> REMOVED = new ThreadLocal<>();

private FabricTagEntryImpl() {
throw new UnsupportedOperationException();
}

public static <T> T withRemovedValue(boolean value, Supplier<T> action) {
@Nullable
Unit initialValue = REMOVED.get();

try {
if (value) {
REMOVED.set(Unit.INSTANCE);
} else {
REMOVED.remove();
}

return action.get();
} finally {
if (initialValue == null) {
REMOVED.remove();
} else {
REMOVED.set(initialValue);
}
}
}

public static boolean getCurrentRemovedValue() {
return REMOVED.get() != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.impl.tag.util;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.Encoder;

public record WrapperCodec<A>(Codec<A> wrapped, Wrapper<A> wrapper) implements Codec<A> {
@Override
public <T> DataResult<Pair<A, T>> decode(DynamicOps<T> ops, T input) {
return wrapper.decode(ops, input, wrapped);
}

@Override
public <T> DataResult<T> encode(A input, DynamicOps<T> ops, T prefix) {
return wrapper.encode(input, ops, prefix, wrapped);
}

public interface Wrapper<A> {
default <T> DataResult<T> encode(final A input, final DynamicOps<T> ops, final T prefix, Encoder<A> wrapped) {
return wrapped.encode(input, ops, prefix);
}
default <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input, Decoder<A> wrapped) {
return wrapped.decode(ops, input);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.mixin.tag;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import net.minecraft.registry.tag.TagEntry;

import net.fabricmc.fabric.api.tag.v1.FabricTagEntry;
import net.fabricmc.fabric.impl.tag.FabricTagEntryImpl;

@Mixin(TagEntry.class)
public class TagEntryMixin implements FabricTagEntry {
@Shadow
@Final
private boolean required;

@Unique
private final boolean removed;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too keen on this implementation, as is breaks assumptions that other mods may use for the TagEntry class. Instead, maybe TagFile should maintain a separate list for c:remove, in a way that is closer to the actual file representation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason it's done like this is to avoid adding a field to TagFile. We'll have to discuss alternatives. I don't really like extending TagEntry, but maybe that is preferable. I considered doing more patching to fake the field better, but decided against it for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the concern with adding a Field to TagFile? is it the fact its a mixin? Its not a deal breaker if we are careful.


public TagEntryMixin() { }

{
removed = FabricTagEntryImpl.getCurrentRemovedValue();
required = required && !removed;
}

@Override
public boolean isRemoved() {
return this.removed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.mixin.tag;

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import com.google.common.collect.Streams;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.Encoder;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Slice;

import net.minecraft.registry.tag.TagEntry;
import net.minecraft.registry.tag.TagFile;

import net.fabricmc.fabric.api.tag.v1.FabricTagEntry;
import net.fabricmc.fabric.impl.tag.FabricTagEntryImpl;
import net.fabricmc.fabric.impl.tag.util.WrapperCodec;

@Mixin(TagFile.class)
public class TagFileMixin {
@Unique
private static final ThreadLocal<List<TagEntry>> REMOVE_ENTRIES = ThreadLocal.withInitial(List::of);

@Shadow
@Final
public static Codec<TagFile> CODEC;

@ModifyArg(
method = "method_43950",
at = @At(
value = "INVOKE",
target = "Lcom/mojang/datafixers/Products$P2;apply(Lcom/mojang/datafixers/kinds/Applicative;Ljava/util/function/BiFunction;)Lcom/mojang/datafixers/kinds/App;"
)
)
private static BiFunction<List<TagEntry>, Boolean, TagFile> modify(BiFunction<List<TagEntry>, Boolean, TagFile> instance) {
return (entries, replace) -> instance.apply(
Streams.concat(entries.stream(), REMOVE_ENTRIES.get().stream()).toList(),
replace
);
}

@ModifyArg(
method = "method_43950",
at = @At(
value = "INVOKE:FIRST",
target = "Lcom/mojang/serialization/MapCodec;forGetter(Ljava/util/function/Function;)Lcom/mojang/serialization/codecs/RecordCodecBuilder;"
),
slice = @Slice(from = @At(value = "CONSTANT", args = "stringValue=values"))
)
private static Function<TagFile, List<TagEntry>> wrapGetter(Function<TagFile, List<TagEntry>> getter) {
return getter.andThen(list -> list.stream().filter(Predicate.not(entry -> ((FabricTagEntry) entry).isRemoved())).toList());
}

static {
Codec<List<TagEntry>> removeEntryCodec = FabricTagEntryImpl.REMOVED_ENTRY_CODEC
.listOf()
.lenientOptionalFieldOf("c:remove", List.of())
.codec();

CODEC = new WrapperCodec<>(CODEC, new WrapperCodec.Wrapper<>() {
@Override
public <T> DataResult<T> encode(TagFile input, DynamicOps<T> ops, T prefix, Encoder<TagFile> wrapped) {
return wrapped.encode(input, ops, prefix).flatMap(
result -> removeEntryCodec.encode(
List.copyOf(
input.entries()
.stream()
.filter(entry -> ((FabricTagEntry) entry).isRemoved())
.toList()
),
ops,
result
)
);
}

@Override
public <T> DataResult<Pair<TagFile, T>> decode(DynamicOps<T> ops, T input, Decoder<TagFile> wrapped) {
return removeEntryCodec.decode(ops, input).flatMap(
result -> withRemovedEntries(result.getFirst(), () -> wrapped.decode(ops, input))
);
}
});
}

@Unique
private static <T> T withRemovedEntries(List<TagEntry> removed, Supplier<T> action) {
List<TagEntry> initialValue = REMOVE_ENTRIES.get();

try {
REMOVE_ENTRIES.set(removed);
return action.get();
} finally {
REMOVE_ENTRIES.set(initialValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.mixin.tag;

import java.util.SequencedSet;
import java.util.function.Consumer;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import net.minecraft.registry.tag.TagEntry;
import net.minecraft.registry.tag.TagGroupLoader;

import net.fabricmc.fabric.api.tag.v1.FabricTagEntry;

@Mixin(TagGroupLoader.class)
public class TagGroupLoaderMixin {
@WrapOperation(
method = "resolveAll",
at = @At(value = "INVOKE", target = "Lnet/minecraft/registry/tag/TagEntry;resolve(Lnet/minecraft/registry/tag/TagEntry$ValueGetter;Ljava/util/function/Consumer;)Z")
)
private <T> boolean swapRemovalIdConsumer(TagEntry instance, TagEntry.ValueGetter<T> valueGetter, Consumer<T> idConsumer, Operation<Boolean> original, @Local SequencedSet<T> sequencedSet) {
return original.call(
instance,
valueGetter,
((FabricTagEntry) instance).isRemoved()
? (Consumer<T>) sequencedSet::remove
: idConsumer
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
"DataPackContentsMixin",
"SimpleRegistryMixin",
"SimpleRegistry2Mixin",
"SimpleRegistry3Mixin",
"SimpleRegistryTagLookup2Accessor"
"SimpleRegistryMixin",
"SimpleRegistryTagLookup2Accessor",
"TagEntryMixin",
"TagFileMixin",
"TagGroupLoaderMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Loading
Loading