From ba294a623d14ab8f40380a8eb4307e0397efe3ed Mon Sep 17 00:00:00 2001 From: quat1024 Date: Wed, 25 Oct 2023 22:35:25 -0400 Subject: [PATCH] Move MiscUtil.massRegistryGet and take a few unused things out of MiscUtil --- .../vazkii/quark/base/handler/MiscUtil.java | 47 ------------------- .../base/handler/RecipeCrawlHandler.java | 8 ++-- .../module/EnchantmentPredicatesModule.java | 18 ++++--- .../module/AutomaticToolRestockModule.java | 5 +- .../ExpandedItemInteractionsModule.java | 3 +- .../tweaks/module/DiamondRepairModule.java | 4 +- .../quark/integration/jei/QuarkJeiPlugin.java | 4 +- .../java/vazkii/zeta/util/RegistryUtil.java | 14 ++++++ 8 files changed, 38 insertions(+), 65 deletions(-) create mode 100644 src/main/java/vazkii/zeta/util/RegistryUtil.java diff --git a/src/main/java/vazkii/quark/base/handler/MiscUtil.java b/src/main/java/vazkii/quark/base/handler/MiscUtil.java index 566be02d4f..dca3c22738 100644 --- a/src/main/java/vazkii/quark/base/handler/MiscUtil.java +++ b/src/main/java/vazkii/quark/base/handler/MiscUtil.java @@ -1,9 +1,5 @@ package vazkii.quark.base.handler; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSyntaxException; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.brigadier.StringReader; @@ -20,7 +16,6 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.tags.TagKey; -import net.minecraft.util.GsonHelper; import net.minecraft.util.RandomSource; import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; @@ -61,14 +56,12 @@ import net.minecraftforge.items.wrapper.InvWrapper; import net.minecraftforge.items.wrapper.SidedInvWrapper; import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.IForgeRegistry; import vazkii.quark.base.Quark; import vazkii.zeta.client.config.screen.ZetaScreen; import vazkii.quark.content.experimental.module.EnchantmentsBegoneModule; import javax.annotation.Nonnull; import java.util.*; -import java.util.stream.Collectors; public class MiscUtil { @@ -201,10 +194,6 @@ public static boolean validSpawnLocation(@Nonnull EntityType type return state.getMaterial() == Material.STONE && state.isValidSpawn(world, below, type); } - public static List massRegistryGet(Collection coll, IForgeRegistry registry) { - return coll.stream().map(ResourceLocation::new).map(registry::getValue).filter(Objects::nonNull).collect(Collectors.toList()); - } - public static void syncTE(BlockEntity tile) { Packet packet = tile.getUpdatePacket(); @@ -247,42 +236,6 @@ public static List getTagValues(RegistryAccess access, TagKey tag) { return holderSet.stream().map(Holder::value).toList(); } - public static String toColorString(int color) { - String colorString = Integer.toHexString(color); - int targetLength = colorString.length() > 6 ? 8 : 6; - String zeroes = colorString.length() < targetLength ? "0".repeat(targetLength - colorString.length()) : ""; - return "#" + zeroes + colorString; - } - - public static int getAsColor(JsonObject object, String key) { - if (object.has(key)) { - return convertToColor(object.get(key), key); - } else { - throw new JsonSyntaxException("Missing " + key + ", expected to find an item"); - } - } - - public static int convertToColor(JsonElement element, String key) { - if (element.isJsonPrimitive()) { - JsonPrimitive primitive = element.getAsJsonPrimitive(); - if (primitive.isNumber()) - return primitive.getAsInt(); - else if (primitive.isString()) { - String s = element.getAsString(); - if (s.matches("#[0-9a-f]{6}")) { - try { - return Integer.parseInt(s.substring(1), 16); - } catch (NumberFormatException e) { - // NO-OP, should be impossible to reach, but fall through to below if so - } - } - } - - } - - throw new JsonSyntaxException("Expected " + key + " to be a color, was " + GsonHelper.getType(element)); - } - public static BlockState fromString(String key) { try { BlockResult result = BlockStateParser.parseForBlock(Registry.BLOCK, new StringReader(key), false); diff --git a/src/main/java/vazkii/quark/base/handler/RecipeCrawlHandler.java b/src/main/java/vazkii/quark/base/handler/RecipeCrawlHandler.java index 8bacdfd8aa..922821ebd1 100644 --- a/src/main/java/vazkii/quark/base/handler/RecipeCrawlHandler.java +++ b/src/main/java/vazkii/quark/base/handler/RecipeCrawlHandler.java @@ -2,6 +2,7 @@ import com.google.common.collect.*; import net.minecraft.core.NonNullList; +import net.minecraft.core.Registry; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.*; @@ -14,6 +15,7 @@ import vazkii.quark.api.event.RecipeCrawlEvent; import vazkii.quark.api.event.RecipeCrawlEvent.Visit; import vazkii.quark.base.Quark; +import vazkii.zeta.util.RegistryUtil; import javax.annotation.Nullable; import java.util.ArrayList; @@ -138,9 +140,9 @@ private static void digest(Recipe recipe) { */ public static void recursivelyFindCraftedItemsFromStrings(@Nullable Collection derivationList, @Nullable Collection whitelist, @Nullable Collection blacklist, Consumer callback) { - List parsedDerivationList = derivationList == null ? null : MiscUtil.massRegistryGet(derivationList, ForgeRegistries.ITEMS); - List parsedWhitelist = whitelist == null ? null : MiscUtil.massRegistryGet(whitelist, ForgeRegistries.ITEMS); - List parsedBlacklist = blacklist == null ? null : MiscUtil.massRegistryGet(blacklist, ForgeRegistries.ITEMS); + List parsedDerivationList = derivationList == null ? null : RegistryUtil.massRegistryGet(derivationList, Registry.ITEM); + List parsedWhitelist = whitelist == null ? null : RegistryUtil.massRegistryGet(whitelist, Registry.ITEM); + List parsedBlacklist = blacklist == null ? null : RegistryUtil.massRegistryGet(blacklist, Registry.ITEM); recursivelyFindCraftedItems(parsedDerivationList, parsedWhitelist, parsedBlacklist, callback); } diff --git a/src/main/java/vazkii/quark/content/experimental/module/EnchantmentPredicatesModule.java b/src/main/java/vazkii/quark/content/experimental/module/EnchantmentPredicatesModule.java index 84cc3cda44..d7c7b29c4d 100644 --- a/src/main/java/vazkii/quark/content/experimental/module/EnchantmentPredicatesModule.java +++ b/src/main/java/vazkii/quark/content/experimental/module/EnchantmentPredicatesModule.java @@ -3,24 +3,23 @@ import com.google.common.collect.Lists; import net.minecraft.client.renderer.item.ItemProperties; import net.minecraft.client.renderer.item.ItemPropertyFunction; +import net.minecraft.core.Registry; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.EnchantmentHelper; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; -import net.minecraftforge.registries.ForgeRegistries; import vazkii.quark.base.Quark; -import vazkii.quark.base.handler.MiscUtil; -import vazkii.quark.base.module.LoadModule; +import vazkii.zeta.module.ModuleSide; +import vazkii.zeta.module.ZetaLoadModule; import vazkii.zeta.module.ZetaModule; import vazkii.quark.base.module.config.Config; import vazkii.zeta.event.bus.LoadEvent; import vazkii.zeta.client.event.ZClientSetup; +import vazkii.zeta.util.RegistryUtil; import java.util.List; -@LoadModule(category = "experimental", enabledByDefault = false, +@ZetaLoadModule(category = "experimental", enabledByDefault = false, side = ModuleSide.CLIENT_ONLY, description = "This feature generates Resource Pack Item Model predicates on the items defined in 'Items to Change'\n" + "for the Enchantments defined in 'Enchantments to Register'.\n\n" + "Example: if 'minecraft:silk_touch' is added to 'Enchantments to Register', and 'minecraft:netherite_pickaxe'\n" @@ -35,15 +34,14 @@ public class EnchantmentPredicatesModule extends ZetaModule { public static List enchantmentsToRegister = Lists.newArrayList(); @LoadEvent - @OnlyIn(Dist.CLIENT) public void clientSetup(ZClientSetup e) { if(enabled) { e.enqueueWork(() -> { - List items = MiscUtil.massRegistryGet(itemsToChange, ForgeRegistries.ITEMS); - List enchants = MiscUtil.massRegistryGet(enchantmentsToRegister, ForgeRegistries.ENCHANTMENTS); + List items = RegistryUtil.massRegistryGet(itemsToChange, Registry.ITEM); + List enchants = RegistryUtil.massRegistryGet(enchantmentsToRegister, Registry.ENCHANTMENT); for(Enchantment enchant : enchants) { - ResourceLocation enchantRes = ForgeRegistries.ENCHANTMENTS.getKey(enchant); + ResourceLocation enchantRes = Registry.ENCHANTMENT.getKey(enchant); ResourceLocation name = new ResourceLocation(Quark.MOD_ID + "_has_enchant_" + enchantRes.getNamespace() + "_" + enchantRes.getPath()); ItemPropertyFunction fun = (stack, level, entity, i) -> EnchantmentHelper.getTagEnchantmentLevel(enchant, stack); diff --git a/src/main/java/vazkii/quark/content/management/module/AutomaticToolRestockModule.java b/src/main/java/vazkii/quark/content/management/module/AutomaticToolRestockModule.java index c2e2fb031c..fd14827c22 100644 --- a/src/main/java/vazkii/quark/content/management/module/AutomaticToolRestockModule.java +++ b/src/main/java/vazkii/quark/content/management/module/AutomaticToolRestockModule.java @@ -31,6 +31,7 @@ import vazkii.quark.base.module.sync.SyncedFlagHandler; import vazkii.zeta.event.ZConfigChanged; import vazkii.zeta.event.bus.LoadEvent; +import vazkii.zeta.util.RegistryUtil; import java.util.*; import java.util.function.Predicate; @@ -83,8 +84,8 @@ public class AutomaticToolRestockModule extends ZetaModule { @LoadEvent public final void configChanged(ZConfigChanged event) { - importantEnchants = MiscUtil.massRegistryGet(enchantNames, ForgeRegistries.ENCHANTMENTS); - itemsToIgnore = MiscUtil.massRegistryGet(ignoredItems, ForgeRegistries.ITEMS); + importantEnchants = RegistryUtil.massRegistryGet(enchantNames, Registry.ENCHANTMENT); + itemsToIgnore = RegistryUtil.massRegistryGet(ignoredItems, Registry.ITEM); } @SubscribeEvent diff --git a/src/main/java/vazkii/quark/content/management/module/ExpandedItemInteractionsModule.java b/src/main/java/vazkii/quark/content/management/module/ExpandedItemInteractionsModule.java index 09d327e1c8..0edda4c70d 100644 --- a/src/main/java/vazkii/quark/content/management/module/ExpandedItemInteractionsModule.java +++ b/src/main/java/vazkii/quark/content/management/module/ExpandedItemInteractionsModule.java @@ -61,6 +61,7 @@ import vazkii.quark.content.management.client.screen.HeldShulkerBoxScreen; import vazkii.quark.content.management.inventory.HeldShulkerBoxContainer; import vazkii.quark.content.management.inventory.HeldShulkerBoxMenu; +import vazkii.zeta.util.RegistryUtil; import java.util.List; @@ -102,7 +103,7 @@ public final void clientSetup(ZClientSetup event) { public final void configChanged(ZConfigChanged event) { staticEnabled = configEnabled; - shulkers = MiscUtil.massRegistryGet(GeneralConfig.shulkerBoxes, ForgeRegistries.ITEMS); + shulkers = RegistryUtil.massRegistryGet(GeneralConfig.shulkerBoxes, Registry.ITEM); } public static boolean overrideStackedOnOther(ItemStack stack, Slot slot, ClickAction action, Player player) { diff --git a/src/main/java/vazkii/quark/content/tweaks/module/DiamondRepairModule.java b/src/main/java/vazkii/quark/content/tweaks/module/DiamondRepairModule.java index b4bdd5b138..c107184846 100644 --- a/src/main/java/vazkii/quark/content/tweaks/module/DiamondRepairModule.java +++ b/src/main/java/vazkii/quark/content/tweaks/module/DiamondRepairModule.java @@ -2,6 +2,7 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; +import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; @@ -16,6 +17,7 @@ import vazkii.zeta.event.ZGatherHints; import vazkii.zeta.event.bus.LoadEvent; import vazkii.zeta.event.bus.PlayEvent; +import vazkii.zeta.util.RegistryUtil; import java.util.Arrays; import java.util.Collection; @@ -76,7 +78,7 @@ public final void configChanged(ZConfigChanged event) { } } - unrepairableItems = MiscUtil.massRegistryGet(unrepairableItemsList, ForgeRegistries.ITEMS); + unrepairableItems = RegistryUtil.massRegistryGet(unrepairableItemsList, Registry.ITEM); } @PlayEvent diff --git a/src/main/java/vazkii/quark/integration/jei/QuarkJeiPlugin.java b/src/main/java/vazkii/quark/integration/jei/QuarkJeiPlugin.java index a6554e27bf..6082f259f8 100644 --- a/src/main/java/vazkii/quark/integration/jei/QuarkJeiPlugin.java +++ b/src/main/java/vazkii/quark/integration/jei/QuarkJeiPlugin.java @@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.Rect2i; import net.minecraft.core.NonNullList; +import net.minecraft.core.Registry; import net.minecraft.data.BuiltinRegistries; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; @@ -56,6 +57,7 @@ import vazkii.quark.content.tweaks.module.DiamondRepairModule; import vazkii.quark.content.tweaks.recipe.ElytraDuplicationRecipe; import vazkii.quark.content.tweaks.recipe.SlabToBlockRecipe; +import vazkii.zeta.util.RegistryUtil; import javax.annotation.Nonnull; import java.util.*; @@ -166,7 +168,7 @@ public void registerRecipes(@Nonnull IRecipeRegistration registration) { MutableComponent externalPreamble = Component.translatable("quark.jei.hint_preamble"); externalPreamble.setStyle(externalPreamble.getStyle().withColor(0x0b5d4b)); - List blacklist = MiscUtil.massRegistryGet(GeneralConfig.suppressedInfo, ForgeRegistries.ITEMS); + List blacklist = RegistryUtil.massRegistryGet(GeneralConfig.suppressedInfo, Registry.ITEM); //ZetaEventBus can't handle lambdas :( //noinspection Convert2Lambda diff --git a/src/main/java/vazkii/zeta/util/RegistryUtil.java b/src/main/java/vazkii/zeta/util/RegistryUtil.java new file mode 100644 index 0000000000..fc9ac6b223 --- /dev/null +++ b/src/main/java/vazkii/zeta/util/RegistryUtil.java @@ -0,0 +1,14 @@ +package vazkii.zeta.util; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; + +public class RegistryUtil { + public static List massRegistryGet(Collection coll, Registry reg) { + return coll.stream().map(ResourceLocation::new).map(reg::get).filter(Objects::nonNull).toList(); + } +}