Skip to content

Commit

Permalink
Move MiscUtil.massRegistryGet and take a few unused things out of Mis…
Browse files Browse the repository at this point in the history
…cUtil
  • Loading branch information
quat1024 committed Oct 26, 2023
1 parent 76d5549 commit ba294a6
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 65 deletions.
47 changes: 0 additions & 47 deletions src/main/java/vazkii/quark/base/handler/MiscUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -201,10 +194,6 @@ public static boolean validSpawnLocation(@Nonnull EntityType<? extends Mob> type
return state.getMaterial() == Material.STONE && state.isValidSpawn(world, below, type);
}

public static <T> List<T> massRegistryGet(Collection<String> coll, IForgeRegistry<T> registry) {
return coll.stream().map(ResourceLocation::new).map(registry::getValue).filter(Objects::nonNull).collect(Collectors.toList());
}

public static void syncTE(BlockEntity tile) {
Packet<ClientGamePacketListener> packet = tile.getUpdatePacket();

Expand Down Expand Up @@ -247,42 +236,6 @@ public static <T> List<T> getTagValues(RegistryAccess access, TagKey<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -138,9 +140,9 @@ private static void digest(Recipe<?> recipe) {
*/

public static void recursivelyFindCraftedItemsFromStrings(@Nullable Collection<String> derivationList, @Nullable Collection<String> whitelist, @Nullable Collection<String> blacklist, Consumer<Item> callback) {
List<Item> parsedDerivationList = derivationList == null ? null : MiscUtil.massRegistryGet(derivationList, ForgeRegistries.ITEMS);
List<Item> parsedWhitelist = whitelist == null ? null : MiscUtil.massRegistryGet(whitelist, ForgeRegistries.ITEMS);
List<Item> parsedBlacklist = blacklist == null ? null : MiscUtil.massRegistryGet(blacklist, ForgeRegistries.ITEMS);
List<Item> parsedDerivationList = derivationList == null ? null : RegistryUtil.massRegistryGet(derivationList, Registry.ITEM);
List<Item> parsedWhitelist = whitelist == null ? null : RegistryUtil.massRegistryGet(whitelist, Registry.ITEM);
List<Item> parsedBlacklist = blacklist == null ? null : RegistryUtil.massRegistryGet(blacklist, Registry.ITEM);

recursivelyFindCraftedItems(parsedDerivationList, parsedWhitelist, parsedBlacklist, callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,15 +34,14 @@ public class EnchantmentPredicatesModule extends ZetaModule {
public static List<String> enchantmentsToRegister = Lists.newArrayList();

@LoadEvent
@OnlyIn(Dist.CLIENT)
public void clientSetup(ZClientSetup e) {
if(enabled) {
e.enqueueWork(() -> {
List<Item> items = MiscUtil.massRegistryGet(itemsToChange, ForgeRegistries.ITEMS);
List<Enchantment> enchants = MiscUtil.massRegistryGet(enchantmentsToRegister, ForgeRegistries.ENCHANTMENTS);
List<Item> items = RegistryUtil.massRegistryGet(itemsToChange, Registry.ITEM);
List<Enchantment> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -76,7 +78,7 @@ public final void configChanged(ZConfigChanged event) {
}
}

unrepairableItems = MiscUtil.massRegistryGet(unrepairableItemsList, ForgeRegistries.ITEMS);
unrepairableItems = RegistryUtil.massRegistryGet(unrepairableItemsList, Registry.ITEM);
}

@PlayEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Item> blacklist = MiscUtil.massRegistryGet(GeneralConfig.suppressedInfo, ForgeRegistries.ITEMS);
List<Item> blacklist = RegistryUtil.massRegistryGet(GeneralConfig.suppressedInfo, Registry.ITEM);

//ZetaEventBus can't handle lambdas :(
//noinspection Convert2Lambda
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/vazkii/zeta/util/RegistryUtil.java
Original file line number Diff line number Diff line change
@@ -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 <T> List<T> massRegistryGet(Collection<String> coll, Registry<T> reg) {
return coll.stream().map(ResourceLocation::new).map(reg::get).filter(Objects::nonNull).toList();
}
}

0 comments on commit ba294a6

Please sign in to comment.