Skip to content

Commit

Permalink
support custom effects and custom colors in the potion bundle recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Minecraftschurli committed Sep 6, 2022
1 parent eac4afd commit 810f77c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
6 changes: 3 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,23 +48,26 @@ 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
public void fillItemCategory(@Nonnull CreativeModeTab group, @Nonnull NonNullList<ItemStack> items) {
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<MobEffectInstance> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -41,21 +40,31 @@ public boolean matches(CraftingContainer inv, @Nonnull Level world) {
int potions = 0;
boolean string = false;
Potion potion = Potions.EMPTY;
List<MobEffectInstance> 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;
}
Expand All @@ -64,12 +73,18 @@ public boolean matches(CraftingContainer inv, @Nonnull Level world) {
@Override
public ItemStack assemble(CraftingContainer inv) {
Potion potion = null;
List<MobEffectInstance> 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;
}
Expand Down

0 comments on commit 810f77c

Please sign in to comment.