Skip to content

Commit

Permalink
Fix brewing material choice block handling (#2592)
Browse files Browse the repository at this point in the history
* Fix brewing block material choice handler

* Remove `PaperAPITools#isDenizenMix`

* Minor formatting fix

* Minor naming cleanups
  • Loading branch information
tal5 committed Jan 30, 2024
1 parent dc5e6da commit d35dfdc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.util.Consumer;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PaperAPIToolsImpl extends PaperAPITools {
Expand Down Expand Up @@ -161,8 +162,8 @@ public void teleport(Entity entity, Location loc, PlayerTeleportEvent.TeleportCa
entity.teleport(loc, cause, teleportFlags.toArray(new TeleportFlag[0]));
}

record BrewingRecipeWithMatchers(PotionMix potionMix, String inputMatcher, String ingredientMatcher) {}
public static final Map<NamespacedKey, BrewingRecipeWithMatchers> potionMixes = new HashMap<>();
record BrewingRecipeMatchers(String inputMatcher, String ingredientMatcher) {}
public static final Map<NamespacedKey, BrewingRecipeMatchers> potionMixes = new HashMap<>();

@Override
public void registerBrewingRecipe(String keyName, ItemStack result, String input, String ingredient, ItemScriptContainer itemScriptContainer) {
Expand All @@ -178,9 +179,8 @@ public void registerBrewingRecipe(String keyName, ItemStack result, String input
return;
}
NamespacedKey key = new NamespacedKey(Denizen.getInstance(), keyName);
PotionMix mix = new PotionMix(key, result, inputChoice, ingredientChoice);
potionMixes.put(key, new BrewingRecipeWithMatchers(mix, input.startsWith("matcher:") ? input : null, ingredient.startsWith("matcher:") ? ingredient : null));
Bukkit.getPotionBrewer().addPotionMix(mix);
potionMixes.put(key, new BrewingRecipeMatchers(input.startsWith("matcher:") ? input : null, ingredient.startsWith("matcher:") ? ingredient : null));
Bukkit.getPotionBrewer().addPotionMix(new PotionMix(key, result, inputChoice, ingredientChoice));
}

@Override
Expand Down Expand Up @@ -219,16 +219,6 @@ public static RecipeChoice parseBrewingRecipeChoice(ItemScriptContainer containe
return new RecipeChoice.MaterialChoice(mats);
}

@Override
public boolean isDenizenMix(ItemStack currInput, ItemStack ingredient) {
for (BrewingRecipeWithMatchers brewing : potionMixes.values()) {
if (brewing.potionMix().getInput().test(currInput) && brewing.potionMix().getIngredient().test(ingredient)) {
return true;
}
}
return false;
}

@Override
public String getBrewingRecipeInputMatcher(NamespacedKey recipeId) {
return potionMixes.get(recipeId).inputMatcher();
Expand All @@ -239,6 +229,11 @@ public String getBrewingRecipeIngredientMatcher(NamespacedKey recipeId) {
return potionMixes.get(recipeId).ingredientMatcher();
}

@Override
public RecipeChoice createPredicateRecipeChoice(Predicate<ItemStack> predicate) {
return PotionMix.createPredicateChoice(predicate);
}

@Override
public String getDeathMessage(PlayerDeathEvent event) {
return PaperModule.stringifyComponent(event.deathMessage());
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.nms.interfaces.ItemHelper;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
Expand Down Expand Up @@ -595,17 +596,47 @@ public static boolean isAllowedToCraftWith(ItemStack item) {
@EventHandler(priority = EventPriority.LOW)
public void onBrewingStandBrews(BrewEvent event) {
ItemStack ingredient = event.getContents().getIngredient();
ItemStack currInput;
boolean ingredientBlockCraft = !isAllowedToCraftWith(ingredient);
List<ItemHelper.BrewingRecipe> potentialRecipes = null;
for (int i = 0; i < 3; i++) {
currInput = event.getContents().getItem(i);
if(!NMSHandler.itemHelper.isValidMix(currInput, ingredient) || !PaperAPITools.instance.isDenizenMix(currInput, ingredient)) {
if (!isAllowedToCraftWith(currInput)) {
event.setCancelled(true);
ItemStack currInput = event.getContents().getItem(i);
boolean inputBlockCraft = !isAllowedToCraftWith(currInput);
if (!inputBlockCraft && !ingredientBlockCraft) {
continue;
}
ItemHelper.BrewingRecipe customRecipe = null;
if (Denizen.supportsPaper && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
if (potentialRecipes == null) {
potentialRecipes = new ArrayList<>();
for (ItemHelper.BrewingRecipe recipe : NMSHandler.itemHelper.getCustomBrewingRecipes().values()) {
if (recipe.ingredient().test(ingredient)) {
potentialRecipes.add(recipe);
}
}
}
for (ItemHelper.BrewingRecipe recipe : potentialRecipes) {
if (currInput != null && recipe.input().test(currInput)) {
customRecipe = recipe;
break;
}
}
}
// If it's a vanilla mix and either the ingredient or input should be blocked (checked above)
if (customRecipe == null && NMSHandler.itemHelper.isValidMix(currInput, ingredient)) {
event.getResults().set(i, currInput);
continue;
}
// If it's a custom recipe and either the input or ingredient are material choices and should be blocked
if (customRecipe != null && (shouldBlockChoice(customRecipe.ingredient(), ingredientBlockCraft) || shouldBlockChoice(customRecipe.input(), inputBlockCraft))) {
event.getResults().set(i, currInput);
}
}
}

private boolean shouldBlockChoice(RecipeChoice choice, boolean blockCraft) {
return blockCraft && choice instanceof RecipeChoice.MaterialChoice;
}

@EventHandler(priority = EventPriority.LOW)
public void onBrewingStandFuel(BrewingStandFuelEvent event) {
if (!isAllowedToCraftWith(event.getFuel())) {
Expand Down
Expand Up @@ -281,13 +281,13 @@ else if (recipe instanceof SmithingRecipe smithingRecipe) {
addChoice.accept(smithingRecipe.getAddition());
}
else if (brewingRecipe != null) {
if (brewingRecipe.ingredient() != null) {
if (brewingRecipe.ingredient() instanceof RecipeChoice.ExactChoice || brewingRecipe.ingredient() instanceof RecipeChoice.MaterialChoice) {
addChoice.accept(brewingRecipe.ingredient());
}
else {
recipeItems.addObject(new ElementTag(PaperAPITools.instance.getBrewingRecipeIngredientMatcher(recipeKey), true));
}
if (brewingRecipe.input() != null) {
if (brewingRecipe.input() instanceof RecipeChoice.ExactChoice || brewingRecipe.input() instanceof RecipeChoice.MaterialChoice) {
addChoice.accept(brewingRecipe.input());
}
else {
Expand Down
Expand Up @@ -24,6 +24,7 @@

import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.function.Predicate;

public class PaperAPITools {

Expand Down Expand Up @@ -122,10 +123,6 @@ public void registerBrewingRecipe(String keyName, ItemStack result, String input
public void clearBrewingRecipes() {
}

public boolean isDenizenMix(ItemStack currInput, ItemStack ingredient) {
return false;
}

public String getBrewingRecipeInputMatcher(NamespacedKey recipeId) {
return null;
}
Expand All @@ -134,6 +131,10 @@ public String getBrewingRecipeIngredientMatcher(NamespacedKey recipeId) {
return null;
}

public RecipeChoice createPredicateRecipeChoice(Predicate<ItemStack> predicate) {
throw new UnsupportedOperationException();
}

public String getDeathMessage(PlayerDeathEvent event) {
return event.getDeathMessage();
}
Expand Down
Expand Up @@ -10,6 +10,7 @@
import com.denizenscript.denizen.nms.v1_20.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.PaperAPITools;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
Expand Down Expand Up @@ -508,18 +509,23 @@ public Map<NamespacedKey, BrewingRecipe> getCustomBrewingRecipes() {
if (PaperPotionMix_CLASS == null) {
PaperPotionMix_CLASS = paperMix.getClass();
}
Predicate<net.minecraft.world.item.ItemStack> ingredient = ReflectionHelper.getFieldValue(PaperPotionMix_CLASS, "ingredient", paperMix);
Predicate<net.minecraft.world.item.ItemStack> input = ReflectionHelper.getFieldValue(PaperPotionMix_CLASS, "input", paperMix);
// Not an instance of net.minecraft.world.item.crafting.Ingredient = a predicate recipe choice
RecipeChoice ingredientChoice = ingredient instanceof Ingredient nmsRecipeChoice ? CraftRecipe.toBukkit(nmsRecipeChoice) : null;
RecipeChoice inputChoice = input instanceof Ingredient nmsRecipeChoice ? CraftRecipe.toBukkit(nmsRecipeChoice) : null;
RecipeChoice ingredient = convertChoice(ReflectionHelper.getFieldValue(PaperPotionMix_CLASS, "ingredient", paperMix));
RecipeChoice input = convertChoice(ReflectionHelper.getFieldValue(PaperPotionMix_CLASS, "input", paperMix));
ItemStack result = CraftItemStack.asBukkitCopy(ReflectionHelper.getFieldValue(PaperPotionMix_CLASS, "result", paperMix));
return new BrewingRecipe(inputChoice, ingredientChoice, result);
return new BrewingRecipe(input, ingredient, result);
});
}
return customBrewingRecipes;
}

private RecipeChoice convertChoice(Predicate<net.minecraft.world.item.ItemStack> nmsPredicate) {
// Not an instance of net.minecraft.world.item.crafting.Ingredient = a predicate recipe choice
if (nmsPredicate instanceof Ingredient ingredient) {
return CraftRecipe.toBukkit(ingredient);
}
return PaperAPITools.instance.createPredicateRecipeChoice(item -> nmsPredicate.test(CraftItemStack.asNMSCopy(item)));
}

@Override
public byte[] renderMap(MapView mapView, Player player) {
return ((CraftMapView) mapView).render((CraftPlayer) player).buffer;
Expand Down

0 comments on commit d35dfdc

Please sign in to comment.