potion builder

This commit is contained in:
lcy0x1
2024-09-12 17:14:46 +08:00
parent ca438aac32
commit af889f4808
6 changed files with 190 additions and 7 deletions

View File

@@ -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

View File

@@ -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<T, C extends BaseRecipeCategory<T, C>>
this.type = new RecipeType<>(name, cls);
}
public <R extends Recipe<I>, I extends RecipeInput> List<R> getAll(net.minecraft.world.item.crafting.RecipeType<R> 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;

View File

@@ -101,6 +101,10 @@ public class L2Registrate extends AbstractRegistrate<L2Registrate> {
}
public <T extends Potion> SimpleEntry<Potion> potion(String name, NonNullSupplier<T> sup) {
return potion(name, RegistrateLangProvider.toEnglishName(name), sup);
}
public <T extends Potion> SimpleEntry<Potion> potion(String name, String desc, NonNullSupplier<T> sup) {
RegistryEntry<Potion, T> 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<L2Registrate> {
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);

View File

@@ -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<String, Holder<Potion>> potions;
private final String modid;
public TabHolder(String modid) {
potions = Multimaps.newListMultimap(new LinkedHashMap<>(), ArrayList::new);
this.modid = modid;
}
public Stream<Holder<Potion>> stream() {
List<String> 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<Potion> ans) {
potions.put(modid, ans);
}
}
private final List<Consumer<PotionBrewing.Builder>> 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<Potion> source, ItemLike item, Holder<Potion> potion) {
recipes.add(e -> e.addMix(source, item.asItem(), potion));
}
public Holder<Potion> regPotion(String id, String name, Holder<MobEffect> 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<Potion> regPotion(String id, String nameId, Holder<MobEffect> sup, Holder<Potion> 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<MobEffect> 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<MobEffect> 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<MobEffect> 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<MobEffect> sup, int durStrong, int dur, int durLong, int amp, int ampStrong,
ItemLike a, Holder<Potion> ap, @Nullable Holder<Potion> lap, @Nullable Holder<Potion> sap,
ItemLike b, Holder<Potion> bp, @Nullable Holder<Potion> lbp, @Nullable Holder<Potion> 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<CreativeModeTab> key) {
regTab(key, Items.POTION);
regTab(key, Items.SPLASH_POTION);
regTab(key, Items.LINGERING_POTION);
}
private void regTab(ResourceKey<CreativeModeTab> key, Item potion) {
reg.modifyCreativeModeTab(key, m -> tab.stream().forEach(e ->
m.accept(PotionContents.createItemStack(potion, e),
CreativeModeTab.TabVisibility.PARENT_TAB_ONLY)));
}
}

View File

@@ -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<T, C extends BaseRecipeCategory<T, C>> implements IRecipeCategory<T> {
@SuppressWarnings("unchecked")

View File

@@ -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<MinecraftServer> getServer() {