From 810f77cad847849694225f1d1a0981563928129b Mon Sep 17 00:00:00 2001 From: Minecraftschurli Date: Tue, 6 Sep 2022 15:05:56 +0200 Subject: [PATCH] support custom effects and custom colors in the potion bundle recipe --- changelog.md | 6 ++-- gradle.properties | 2 +- .../potionbundles/AbstractPotionBundle.java | 13 ++++--- .../potionbundles/PotionBundleRecipe.java | 35 +++++++++++++------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/changelog.md b/changelog.md index ea3c5fb..42f9f90 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,5 @@ -# Version 1.18.1-1.4 +# Version 1.18.1-1.5 -## Fixes +## Additions -- Fix dupe bug for bundle string +- Added the ability to use potions based on custom effects in bundles. diff --git a/gradle.properties b/gradle.properties index f06125f..c65398c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.daemon=false mod_group=com.github.ichhabehunger54 mod_id=potionbundles mod_name=Potion Bundles -mod_version=1.4 +mod_version=1.5 mod_url=https://www.curseforge.com/minecraft/mc-mods/potion-bundles mod_authors=IchHabeHunger54, Minecraftschurli mod_description=Potions can now be bound into bundles, to save inventory space. diff --git a/src/main/java/com/github/ichhabehunger54/potionbundles/AbstractPotionBundle.java b/src/main/java/com/github/ichhabehunger54/potionbundles/AbstractPotionBundle.java index 6fb2997..895be7f 100644 --- a/src/main/java/com/github/ichhabehunger54/potionbundles/AbstractPotionBundle.java +++ b/src/main/java/com/github/ichhabehunger54/potionbundles/AbstractPotionBundle.java @@ -1,9 +1,9 @@ package com.github.ichhabehunger54.potionbundles; -import net.minecraft.Util; import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.item.*; import net.minecraft.world.item.alchemy.Potion; import net.minecraft.world.item.alchemy.PotionUtils; @@ -48,7 +48,7 @@ public boolean isBarVisible(ItemStack stack) { @Nonnull @Override public Component getName(@Nonnull ItemStack stack) { - return new TranslatableComponent(getDescriptionId(), new TranslatableComponent(PotionUtils.getPotion(stack).getName(Util.makeDescriptionId("item", Items.POTION.getRegistryName()) + ".effect."))); + return new TranslatableComponent(getDescriptionId(), Items.POTION.getName(stack)); } @Override @@ -56,15 +56,18 @@ public void fillItemCategory(@Nonnull CreativeModeTab group, @Nonnull NonNullLis if (allowdedIn(group)) { for (Potion potion : ForgeRegistries.POTIONS) { if (potion == Potions.EMPTY) continue; - ItemStack stack = createStack(new ItemStack(Items.STRING), potion); + ItemStack stack = createStack(new ItemStack(Items.STRING), potion, List.of(), null); if (!stack.isEmpty()) items.add(stack); } } } @Nonnull - protected ItemStack createStack(@Nonnull ItemStack string, @Nonnull Potion potion) { - ItemStack stack = PotionUtils.setPotion(new ItemStack(this), potion); + protected ItemStack createStack(@Nonnull ItemStack string, @Nonnull Potion potion, @Nonnull List customEffects, @Nullable Integer customColor) { + ItemStack stack = new ItemStack(this); + PotionUtils.setPotion(stack, potion); + PotionUtils.setCustomEffects(stack, customEffects); + if (customColor != null) stack.getOrCreateTag().putInt("CustomPotionColor", customColor); PotionBundleUtils.setUses(stack, getMaxUses()); PotionBundleUtils.setString(stack, string); return stack; diff --git a/src/main/java/com/github/ichhabehunger54/potionbundles/PotionBundleRecipe.java b/src/main/java/com/github/ichhabehunger54/potionbundles/PotionBundleRecipe.java index 3bc7beb..b1e4538 100644 --- a/src/main/java/com/github/ichhabehunger54/potionbundles/PotionBundleRecipe.java +++ b/src/main/java/com/github/ichhabehunger54/potionbundles/PotionBundleRecipe.java @@ -4,6 +4,7 @@ import com.google.gson.JsonParseException; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; @@ -17,17 +18,15 @@ import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistryEntry; +import java.util.List; import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class PotionBundleRecipe extends CustomRecipe { - private @Nonnull - final Ingredient string; - private @Nonnull - final Item potion; - private @Nonnull - final AbstractPotionBundle bundle; + private final Ingredient string; + private final Item potion; + private final AbstractPotionBundle bundle; public PotionBundleRecipe(ResourceLocation id, @Nonnull Ingredient string, @Nonnull Item potion, @Nonnull AbstractPotionBundle bundle) { super(id); @@ -41,21 +40,31 @@ public boolean matches(CraftingContainer inv, @Nonnull Level world) { int potions = 0; boolean string = false; Potion potion = Potions.EMPTY; + List customEffects = null; + int color = 0; for (int i = 0; i < inv.getContainerSize(); i++) { ItemStack is = inv.getItem(i); if (this.string.test(is)) { if (string) return false; string = true; - } else if (is.getItem() == this.potion) { + continue; + } + if (is.getItem() == this.potion) { if (potions == 0) { + color = PotionUtils.getColor(is); potion = PotionUtils.getPotion(is); + customEffects = PotionUtils.getCustomEffects(is); potions++; } else if (potions > 0) { + if (PotionUtils.getColor(is) != color) return false; if (PotionUtils.getPotion(is) != potion) return false; + if (!PotionUtils.getCustomEffects(is).equals(customEffects)) return false; potions++; } if (potions > this.bundle.getMaxUses()) return false; - } else if (!is.isEmpty()) return false; + continue; + } + if (!is.isEmpty()) return false; } return potions == this.bundle.getMaxUses() && string; } @@ -64,12 +73,18 @@ public boolean matches(CraftingContainer inv, @Nonnull Level world) { @Override public ItemStack assemble(CraftingContainer inv) { Potion potion = null; + List customEffects = null; ItemStack string = null; + Integer customColor = null; for (int i = 0; i < inv.getContainerSize(); i++) { ItemStack is = inv.getItem(i); - if (potion == null && is.getItem() == this.potion) potion = PotionUtils.getPotion(is); + if (potion == null && is.is(this.potion)) { + potion = PotionUtils.getPotion(is); + customEffects = PotionUtils.getCustomEffects(is); + if (is.getOrCreateTag().contains("CustomPotionColor", 99)) customColor = PotionUtils.getColor(is); + } if (string == null && this.string.test(is)) string = is.copy().split(1); - if (potion != null && string != null) return this.bundle.createStack(string, potion); + if (potion != null && string != null) return this.bundle.createStack(string, potion, customEffects, customColor); } return ItemStack.EMPTY; }