aboutsummaryrefslogtreecommitdiff
path: root/fabric/src/main/java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2022-06-26 03:33:14 +0800
committershedaniel <daniel@shedaniel.me>2022-06-26 03:33:14 +0800
commit81b82416abb2a94c12638fdbb08df2ac0f83254f (patch)
tree31379d04269c77a764d127685621a7cc6ac7136f /fabric/src/main/java
parent685dde03d71bbf18442564575e11a8247f401c0d (diff)
downloadRoughlyEnoughItems-81b82416abb2a94c12638fdbb08df2ac0f83254f.tar.gz
RoughlyEnoughItems-81b82416abb2a94c12638fdbb08df2ac0f83254f.tar.bz2
RoughlyEnoughItems-81b82416abb2a94c12638fdbb08df2ac0f83254f.zip
WIP on Tag Category, add Overflow Widget
Diffstat (limited to 'fabric/src/main/java')
-rw-r--r--fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagBuilder.java89
-rw-r--r--fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagLoader.java98
-rw-r--r--fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagManager.java53
3 files changed, 240 insertions, 0 deletions
diff --git a/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagBuilder.java b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagBuilder.java
new file mode 100644
index 000000000..5fbc343e8
--- /dev/null
+++ b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagBuilder.java
@@ -0,0 +1,89 @@
+/*
+ * This file is licensed under the MIT License, part of Roughly Enough Items.
+ * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.shedaniel.rei.mixin.fabric;
+
+import com.mojang.datafixers.util.Either;
+import me.shedaniel.rei.plugin.common.displays.tag.TagNodes;
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.tags.Tag;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+@Mixin(Tag.Builder.class)
+public class MixinTagBuilder<T> {
+ @Shadow @Final private List<Tag.BuilderEntry> entries;
+
+ @Inject(method = "build", at = @At("RETURN"))
+ private void load(Function<ResourceLocation, Tag<T>> tagResolver, Function<ResourceLocation, T> valueResolver, CallbackInfoReturnable<Either<Collection<Tag.BuilderEntry>, Tag<T>>> cir) {
+ Tag<T> tag = cir.getReturnValue().right().orElse(null);
+ if (tag != null) {
+ String currentTagDirectory = TagNodes.CURRENT_TAG_DIR.get();
+ if (currentTagDirectory == null) return;
+ ResourceKey<? extends Registry<?>> resourceKey = TagNodes.TAG_DIR_MAP.get(currentTagDirectory);
+ if (resourceKey == null) return;
+ Map<Tag<?>, TagNodes.RawTagData> dataMap = TagNodes.RAW_TAG_DATA_MAP.get(currentTagDirectory);
+ if (dataMap == null) return;
+ List<ResourceLocation> otherElements = new ArrayList<>();
+ List<ResourceLocation> otherTags = new ArrayList<>();
+
+ for (Tag.BuilderEntry builderEntry : this.entries) {
+ if (builderEntry.entry() instanceof Tag.OptionalTagEntry tagEntry) {
+ Tag<T> apply = tagResolver.apply(tagEntry.id);
+ if (apply != null) {
+ otherTags.add(tagEntry.id);
+ }
+ } else if (builderEntry.entry() instanceof Tag.TagEntry tagEntry) {
+ Tag<T> apply = tagResolver.apply(tagEntry.id);
+ if (apply != null) {
+ otherTags.add(tagEntry.id);
+ }
+ } else if (builderEntry.entry() instanceof Tag.OptionalElementEntry tagEntry) {
+ T apply = valueResolver.apply(tagEntry.id);
+ if (apply != null) {
+ otherElements.add(tagEntry.id);
+ }
+ } else if (builderEntry.entry() instanceof Tag.ElementEntry tagEntry) {
+ T apply = valueResolver.apply(tagEntry.id);
+ if (apply != null) {
+ otherElements.add(tagEntry.id);
+ }
+ }
+ }
+
+ dataMap.put(tag, new TagNodes.RawTagData(otherElements, otherTags));
+ }
+ }
+}
diff --git a/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagLoader.java b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagLoader.java
new file mode 100644
index 000000000..493361c56
--- /dev/null
+++ b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagLoader.java
@@ -0,0 +1,98 @@
+/*
+ * This file is licensed under the MIT License, part of Roughly Enough Items.
+ * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.shedaniel.rei.mixin.fabric;
+
+import com.google.common.base.Stopwatch;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import it.unimi.dsi.fastutil.ints.IntList;
+import me.shedaniel.rei.RoughlyEnoughItemsCore;
+import me.shedaniel.rei.plugin.common.displays.tag.TagNode;
+import me.shedaniel.rei.plugin.common.displays.tag.TagNodes;
+import net.minecraft.core.Registry;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.tags.Tag;
+import net.minecraft.tags.TagKey;
+import net.minecraft.tags.TagLoader;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import java.util.*;
+
+@Mixin(TagLoader.class)
+public class MixinTagLoader<T> {
+ @Shadow @Final private String directory;
+
+ @Inject(method = "build", at = @At("HEAD"))
+ private void load(Map<ResourceLocation, Tag.Builder> map, CallbackInfoReturnable<Map<ResourceLocation, Tag<T>>> cir) {
+ TagNodes.RAW_TAG_DATA_MAP.put(directory, new HashMap<>());
+ TagNodes.CURRENT_TAG_DIR.set(directory);
+ }
+
+ @Inject(method = "build", at = @At("RETURN"))
+ private void loadPost(Map<ResourceLocation, Tag.Builder> map, CallbackInfoReturnable<Map<ResourceLocation, Tag<T>>> cir) {
+ HashBiMap<ResourceLocation, Tag<T>> biMap = HashBiMap.create(cir.getReturnValue());
+ ResourceKey<? extends Registry<?>> resourceKey = TagNodes.TAG_DIR_MAP.get(directory);
+ if (resourceKey == null) return;
+ TagNodes.TAG_DATA_MAP.put(resourceKey, new HashMap<>());
+ Map<ResourceLocation, TagNodes.TagData> tagDataMap = TagNodes.TAG_DATA_MAP.get(resourceKey);
+ Registry<T> registry = ((Registry<Registry<T>>) Registry.REGISTRY).get((ResourceKey<Registry<T>>) resourceKey);
+ Stopwatch stopwatch = Stopwatch.createStarted();
+
+ Iterator<Map.Entry<Tag<?>, TagNodes.RawTagData>> entryIterator = TagNodes.RAW_TAG_DATA_MAP.getOrDefault(directory, Collections.emptyMap())
+ .entrySet().iterator();
+
+ if (!entryIterator.hasNext()) return;
+
+ while (entryIterator.hasNext()) {
+ Map.Entry<Tag<?>, TagNodes.RawTagData> entry = entryIterator.next();
+ Tag<?> tag = entry.getKey();
+ entryIterator.remove();
+
+ if (registry != null) {
+ ResourceLocation tagLoc = biMap.inverse().get(tag);
+
+ if (tagLoc != null) {
+ TagNodes.RawTagData rawTagData = entry.getValue();
+ IntList elements = new IntArrayList();
+ for (ResourceLocation element : rawTagData.otherElements()) {
+ T t = registry.get(element);
+ if (t != null) {
+ elements.add(registry.getId(t));
+ }
+ }
+ tagDataMap.put(tagLoc, new TagNodes.TagData(elements, rawTagData.otherTags()));
+ }
+ }
+ }
+
+ RoughlyEnoughItemsCore.LOGGER.info("Processed %d tags in %s for %s", tagDataMap.size(), stopwatch.stop(), resourceKey.location());
+ }
+}
diff --git a/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagManager.java b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagManager.java
new file mode 100644
index 000000000..9e3b22bd7
--- /dev/null
+++ b/fabric/src/main/java/me/shedaniel/rei/mixin/fabric/MixinTagManager.java
@@ -0,0 +1,53 @@
+/*
+ * This file is licensed under the MIT License, part of Roughly Enough Items.
+ * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package me.shedaniel.rei.mixin.fabric;
+
+import me.shedaniel.rei.plugin.common.displays.tag.TagNodes;
+import net.minecraft.core.Registry;
+import net.minecraft.core.RegistryAccess;
+import net.minecraft.resources.ResourceKey;
+import net.minecraft.server.packs.resources.ResourceManager;
+import net.minecraft.tags.TagManager;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+
+@Mixin(TagManager.class)
+public abstract class MixinTagManager<T> {
+ @Shadow
+ public static String getTagDir(ResourceKey<? extends Registry<?>> resourceKey) {
+ return null;
+ }
+
+ @Inject(method = "createLoader", at = @At("HEAD"))
+ private void load(ResourceManager resourceManager, Executor executor, RegistryAccess.RegistryEntry<T> registryEntry, CallbackInfoReturnable<CompletableFuture<TagManager.LoadResult<T>>> cir) {
+ ResourceKey<? extends Registry<T>> resourceKey = registryEntry.key();
+ TagNodes.TAG_DIR_MAP.put(getTagDir(resourceKey), resourceKey);
+ }
+}