diff --git a/gradle.properties b/gradle.properties index fccec1b..7dc3d2a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,7 +18,7 @@ loader_version_range=[2,) mod_id=l2core mod_name=L2Core mod_license=LGPL-2.1 -mod_version=3.0.7+33 +mod_version=3.0.7+42 mod_group_id=dev.xkmc mod_authors=lcy0x1 mod_description=Core Library mod for all L2 mods diff --git a/src/main/java/dev/xkmc/l2core/compat/jei/BaseRecipeCategory.java b/src/main/java/dev/xkmc/l2core/compat/jei/BaseRecipeCategory.java index 370b221..4d8c498 100644 --- a/src/main/java/dev/xkmc/l2core/compat/jei/BaseRecipeCategory.java +++ b/src/main/java/dev/xkmc/l2core/compat/jei/BaseRecipeCategory.java @@ -6,9 +6,14 @@ import mezz.jei.api.recipe.IFocusGroup; import mezz.jei.api.recipe.RecipeType; import mezz.jei.api.recipe.category.IRecipeCategory; import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.RecipeInput; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.List; @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault @@ -27,6 +32,13 @@ public abstract class BaseRecipeCategory> this.type = new RecipeType<>(name, cls); } + public , I extends RecipeInput> List getAll(net.minecraft.world.item.crafting.RecipeType type) { + var level = Minecraft.getInstance().level; + if (level == null) return List.of(); + return level.getRecipeManager().getAllRecipesFor(type) + .stream().map(RecipeHolder::value).toList(); + } + @SuppressWarnings("unchecked") public final C getThis() { return (C) this; diff --git a/src/main/java/dev/xkmc/l2core/init/reg/registrate/L2Registrate.java b/src/main/java/dev/xkmc/l2core/init/reg/registrate/L2Registrate.java index 3fca142..1784fcd 100644 --- a/src/main/java/dev/xkmc/l2core/init/reg/registrate/L2Registrate.java +++ b/src/main/java/dev/xkmc/l2core/init/reg/registrate/L2Registrate.java @@ -101,6 +101,10 @@ public class L2Registrate extends AbstractRegistrate { } public SimpleEntry potion(String name, NonNullSupplier sup) { + return potion(name, RegistrateLangProvider.toEnglishName(name), sup); + } + + public SimpleEntry potion(String name, String desc, NonNullSupplier sup) { RegistryEntry ans = entry(name, (cb) -> new NoConfigBuilder<>(this, this, name, cb, Registries.POTION, sup)).register(); if (doDataGen.get()) { @@ -111,7 +115,7 @@ public class L2Registrate extends AbstractRegistrate { String str = item.getDescriptionId() + ".effect." + name; String pref_name = RegistrateLangProvider.toEnglishName(prefs[prefs.length - 1]); if (item == Items.TIPPED_ARROW) pref_name = "Arrow"; - addRawLang(str, pref_name + " of " + RegistrateLangProvider.toEnglishName(name)); + addRawLang(str, pref_name + " of " + desc); } } return new SimpleEntry<>(ans); diff --git a/src/main/java/dev/xkmc/l2core/init/reg/registrate/PotionBuilder.java b/src/main/java/dev/xkmc/l2core/init/reg/registrate/PotionBuilder.java new file mode 100644 index 0000000..4adf080 --- /dev/null +++ b/src/main/java/dev/xkmc/l2core/init/reg/registrate/PotionBuilder.java @@ -0,0 +1,141 @@ +package dev.xkmc.l2core.init.reg.registrate; + +import com.google.common.collect.ListMultimap; +import com.google.common.collect.Multimaps; +import com.tterrag.registrate.providers.RegistrateLangProvider; +import net.minecraft.core.Holder; +import net.minecraft.resources.ResourceKey; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.alchemy.Potion; +import net.minecraft.world.item.alchemy.PotionBrewing; +import net.minecraft.world.item.alchemy.PotionContents; +import net.minecraft.world.item.alchemy.Potions; +import net.minecraft.world.level.ItemLike; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.brewing.RegisterBrewingRecipesEvent; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.TreeSet; +import java.util.function.Consumer; +import java.util.stream.Stream; + +public class PotionBuilder { + + public static class TabHolder { + + private final ListMultimap> potions; + private final String modid; + + public TabHolder(String modid) { + potions = Multimaps.newListMultimap(new LinkedHashMap<>(), ArrayList::new); + this.modid = modid; + } + + public Stream> stream() { + List ans = new ArrayList<>(); + ans.add(modid); + var set = new TreeSet<>(potions.keySet()); + set.remove(modid); + ans.addAll(set); + return ans.stream().flatMap(e -> potions.get(e).stream()); + } + + public synchronized void add(String modid, SimpleEntry ans) { + potions.put(modid, ans); + } + + } + + private final List> recipes = new ArrayList<>(); + private final TabHolder tab; + + private final L2Registrate reg; + + private PotionBuilder(L2Registrate reg, TabHolder tab) { + this.reg = reg; + this.tab = tab; + NeoForge.EVENT_BUS.addListener(this::registerBrewingRecipe); + } + + public PotionBuilder(L2Registrate reg, PotionBuilder parent) { + this(reg, parent.tab); + } + + + public PotionBuilder(L2Registrate reg) { + this(reg, new TabHolder(reg.getModid())); + } + + private void registerBrewingRecipe(RegisterBrewingRecipesEvent event) { + var builder = event.getBuilder(); + recipes.forEach(e -> e.accept(builder)); + } + + public void addMix(Holder source, ItemLike item, Holder potion) { + recipes.add(e -> e.addMix(source, item.asItem(), potion)); + } + + + public Holder regPotion(String id, String name, Holder sup, int dur, int amp) { + var ans = reg.potion(id, RegistrateLangProvider.toEnglishName(name), () -> new Potion(new MobEffectInstance(sup, dur, amp))); + tab.add(reg.getModid(), ans); + return ans; + } + + public Holder regPotion(String id, String nameId, Holder sup, Holder source, ItemLike item, int dur, int amp) { + var potion = regPotion(id, nameId, sup, dur, amp); + addMix(source, item, potion); + return potion; + } + + public void regPotion2(String id, Holder sup, ItemLike item, int dur, int durLong) { + var potion = regPotion(id, id, sup, Potions.AWKWARD, item, dur, 0); + regPotion("long_" + id, id, sup, potion, Items.REDSTONE, durLong, 0); + } + + public void regPotion3(String id, Holder sup, ItemLike item, int durStrong, int dur, int durLong, int amp, int ampStrong) { + var potion = regPotion(id, id, sup, Potions.AWKWARD, item, dur, amp); + regPotion("long_" + id, id, sup, potion, Items.REDSTONE, durLong, amp); + regPotion("strong_" + id, id, sup, potion, Items.GLOWSTONE_DUST, durStrong, ampStrong); + } + + public void regPotion3(String id, Holder sup, ItemLike item, int durStrong, int dur, int durLong, int amp, int ampStrong, ItemLike longItem, ItemLike strongItem) { + var potion = regPotion(id, id, sup, Potions.AWKWARD, item, dur, amp); + regPotion("long_" + id, id, sup, potion, longItem, durLong, amp); + regPotion("strong_" + id, id, sup, potion, strongItem, durStrong, ampStrong); + } + + public void interleave(String id, Holder sup, int durStrong, int dur, int durLong, int amp, int ampStrong, + ItemLike a, Holder ap, @Nullable Holder lap, @Nullable Holder sap, + ItemLike b, Holder bp, @Nullable Holder lbp, @Nullable Holder sbp) { + var potion = regPotion(id, id, sup, dur, amp); + var longPotion = regPotion("long_" + id, id, sup, potion, Items.REDSTONE, durLong, amp); + var strongPotion = regPotion("strong_" + id, id, sup, potion, Items.GLOWSTONE_DUST, durStrong, ampStrong); + addMix(ap, a, potion); + addMix(bp, b, potion); + if (lap != null) addMix(lap, a, longPotion); + if (lbp != null) addMix(lbp, b, longPotion); + if (sap != null) addMix(sap, a, strongPotion); + if (sbp != null) addMix(sbp, b, strongPotion); + } + + public void regTab(ResourceKey key) { + regTab(key, Items.POTION); + regTab(key, Items.SPLASH_POTION); + regTab(key, Items.LINGERING_POTION); + } + + private void regTab(ResourceKey key, Item potion) { + reg.modifyCreativeModeTab(key, m -> tab.stream().forEach(e -> + m.accept(PotionContents.createItemStack(potion, e), + CreativeModeTab.TabVisibility.PARENT_TAB_ONLY))); + } + +} diff --git a/src/main/java/dev/xkmc/l2core/serial/recipe/BaseRecipeCategory.java b/src/main/java/dev/xkmc/l2core/serial/recipe/BaseRecipeCategory.java index 107d2ef..7425261 100644 --- a/src/main/java/dev/xkmc/l2core/serial/recipe/BaseRecipeCategory.java +++ b/src/main/java/dev/xkmc/l2core/serial/recipe/BaseRecipeCategory.java @@ -5,8 +5,15 @@ import mezz.jei.api.gui.drawable.IDrawable; import mezz.jei.api.recipe.IFocusGroup; import mezz.jei.api.recipe.RecipeType; import mezz.jei.api.recipe.category.IRecipeCategory; +import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.RecipeInput; +import java.util.List; + +@Deprecated(forRemoval = true) public abstract class BaseRecipeCategory> implements IRecipeCategory { @SuppressWarnings("unchecked") diff --git a/src/main/java/dev/xkmc/l2core/util/Proxy.java b/src/main/java/dev/xkmc/l2core/util/Proxy.java index a84970f..509c57a 100644 --- a/src/main/java/dev/xkmc/l2core/util/Proxy.java +++ b/src/main/java/dev/xkmc/l2core/util/Proxy.java @@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.RegistryAccess; import net.minecraft.server.MinecraftServer; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.neoforged.api.distmarker.Dist; import net.neoforged.fml.loading.FMLEnvironment; @@ -11,16 +12,30 @@ import net.neoforged.neoforge.server.ServerLifecycleHooks; import javax.annotation.Nullable; import java.util.Optional; -import java.util.function.BiConsumer; public class Proxy { - @Deprecated + @Nullable + public static Player getPlayer() { + if (FMLEnvironment.dist == Dist.CLIENT) { + return Minecraft.getInstance().player; + } + return null; + } + + @Nullable public static RegistryAccess getRegistryAccess() { if (FMLEnvironment.dist == Dist.CLIENT) { - return Minecraft.getInstance().level.registryAccess(); + var level = Minecraft.getInstance().level; + if (level != null) { + return Minecraft.getInstance().level.registryAccess(); + } } - return ServerLifecycleHooks.getCurrentServer().registryAccess(); + var server = ServerLifecycleHooks.getCurrentServer(); + if (server != null) { + return server.registryAccess(); + } + return null; } @Nullable @@ -28,7 +43,11 @@ public class Proxy { if (FMLEnvironment.dist == Dist.CLIENT) { return Minecraft.getInstance().level; } - return ServerLifecycleHooks.getCurrentServer().overworld(); + var server = ServerLifecycleHooks.getCurrentServer(); + if (server != null) { + return server.overworld(); + } + return null; } public static Optional getServer() {