Skip to content

Commit

Permalink
refactor potion brewing. Add filter for removing.
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Jul 26, 2024
1 parent aad777c commit d11f4c4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
12 changes: 10 additions & 2 deletions example_scripts/potion_brewing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ MoreJS.registerPotionBrewing(event => {

// removePotionBrewing
MoreJS.registerPotionBrewing(event => {
event.removePotionBrewing("minecraft:glowstone_dust", "minecraft:harming", "minecraft:strong_harming");
event.removePotionBrewing({
ingredient: "minecraft:apple",
input: "minecraft:harming",
output: "minecraft:strong_harming",
})
});

// addCustomBrewing
Expand All @@ -26,5 +30,9 @@ MoreJS.registerPotionBrewing(event => {
// removeCustomBrewing
MoreJS.registerPotionBrewing(event => {
event.addCustomBrewing("minecraft:emerald", "minecraft:nether_star", "minecraft:diamond");
event.removeCustomBrewing("minecraft:emerald", "minecraft:nether_star", "minecraft:diamond");
event.removeCustomBrewing({
ingredient: "minecraft:emerald",
input: "minecraft:nether_star",
output: "minecraft:diamond",
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.almostreliable.morejs.features.potion;

import com.almostreliable.morejs.util.Utils;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.brewing.BrewingRecipe;

import java.util.Optional;
import java.util.function.Predicate;

public record CustomBrewingFilter(Optional<Ingredient> ingredient, Optional<Ingredient> input,
Optional<Ingredient> output) implements Predicate<BrewingRecipe> {

@Override
public boolean test(BrewingRecipe brewingRecipe) {
if (input.filter(input -> Utils.matchesIngredient(input, brewingRecipe.getInput())).isEmpty()) {
return false;
}

if (output.filter(output -> output.test(brewingRecipe.getOutput())).isEmpty()) {
return false;
}

return ingredient
.filter(ingredient -> Utils.matchesIngredient(ingredient, brewingRecipe.getIngredient()))
.isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.almostreliable.morejs.features.potion;

import com.almostreliable.morejs.util.Utils;
import net.minecraft.core.HolderSet;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionBrewing;
import net.minecraft.world.item.crafting.Ingredient;

import java.util.Optional;
import java.util.function.Predicate;

public record PotionBrewingFilter(Optional<Ingredient> ingredient, Optional<HolderSet<Potion>> input,
Optional<HolderSet<Potion>> output) implements Predicate<PotionBrewing.Mix<Potion>> {

@Override
public boolean test(PotionBrewing.Mix<Potion> potionMix) {
if (input.filter(input -> input.contains(potionMix.from())).isEmpty()) {
return false;
}

if (output.filter(output -> output.contains(potionMix.to())).isEmpty()) {
return false;
}
return ingredient
.filter(ingredient -> Utils.matchesIngredient(ingredient, potionMix.ingredient()))
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import net.neoforged.neoforge.common.brewing.IBrewingRecipe;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Predicate;

public class PotionBrewingRegisterEvent implements KubeEvent {

Expand Down Expand Up @@ -67,20 +66,18 @@ public void addPotionBrewing(Ingredient ingredient, Potion output) {
addPotionBrewing(ingredient, Potions.WATER.value(), output);
}

public void removePotionBrewing(@Nullable Ingredient ingredient, @Nullable Potion input, @Nullable Potion output) {
public void removePotionBrewing(PotionBrewingFilter filter) {
potionBrewingAccessor.morejs$getPotionMixes().removeIf(mix -> {
boolean matchesInput = input == null || mix.from().value() == input;
boolean matchesIngredient = ingredient == null || Utils.matchesIngredient(ingredient, mix.ingredient());
boolean matchesOutput = output == null || mix.to().value() == output;
boolean matches = matchesInput && matchesIngredient && matchesOutput;
if (matches) {
if (filter.test(mix)) {
ConsoleJS.STARTUP.info(
"Removed potion brewing recipe: " +
mix.from() + " + " +
StringUtils.abbreviate(mix.ingredient().toString(), 64) + " -> " +
mix.to());
return true;
}
return matches;

return false;
});
}

Expand Down Expand Up @@ -128,20 +125,15 @@ public void addContainerRecipe(Ingredient ingredient, Item input, Item output) {
potionBrewingAccessor.morejs$getContainerMixes().add(mix);
}

public void removeCustomBrewing(@Nullable Ingredient ingredient, @Nullable Ingredient input, @Nullable Ingredient output) {
public void removeCustomBrewing(CustomBrewingFilter filter) {
ListIterator<IBrewingRecipe> it = potionBrewingAccessor.morejs$getRecipes().listIterator();
while (it.hasNext()) {
IBrewingRecipe recipe = it.next();
if (!(recipe instanceof BrewingRecipe br)) { // BrewingRecipe is the vanilla one
continue;
}

boolean matchesInput = ingredient == null || Utils.matchesIngredient(ingredient, br.getIngredient());
boolean matchesIngredient = input == null || Utils.matchesIngredient(input, br.getInput());
boolean matchesOutput = output == null || output.test(br.getOutput());


if (matchesInput && matchesIngredient && matchesOutput) {
if (filter.test(br)) {
String s = String.format("Removing custom brewing recipe: [Input: %s][Ingredient: %s][Output: %s]",
br.getInput(),
br.getIngredient(),
Expand All @@ -152,18 +144,7 @@ public void removeCustomBrewing(@Nullable Ingredient ingredient, @Nullable Ingre
}
}

public void removeCustomBrewing(Predicate<IBrewingRecipe> predicate) {
ListIterator<IBrewingRecipe> it = potionBrewingAccessor.morejs$getRecipes().listIterator();
while (it.hasNext()) {
IBrewingRecipe recipe = it.next();
if (recipe instanceof BrewingRecipe) {
continue;
}

if (predicate.test(recipe)) {
ConsoleJS.STARTUP.info("Removing custom brewing recipe: " + recipe);
it.remove();
}
}
public List<IBrewingRecipe> getCustomBrewingRecipes() {
return potionBrewingAccessor.morejs$getRecipes();
}
}

0 comments on commit d11f4c4

Please sign in to comment.