-
Notifications
You must be signed in to change notification settings - Fork 484
Entry Removals in json tag files #4721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.21.9
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ); | ||
| } | ||
cputnam-a11y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FabricTagEntryImplbut it doesnt implementFabricTagEntry?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?