Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.winx64.test;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import org.bukkit.Material;
- import org.bukkit.inventory.ItemStack;
- import net.minecraft.server.v1_8_R3.Item;
- import net.minecraft.server.v1_8_R3.ItemPotion;
- import net.minecraft.server.v1_8_R3.Items;
- import net.minecraft.server.v1_8_R3.MobEffect;
- import net.minecraft.server.v1_8_R3.PotionBrewer;
- /**
- * Utility class to provide the different recipes for each potion in the game.
- *
- * @author WinX64
- *
- */
- public final class PotionRecipes {
- private static final Map<Integer, Set<List<Material>>> MAPPED_RECIPES = new HashMap<>();
- private static final Map<Material, String> MODIFIERS = new HashMap<>();
- static {
- Item.REGISTRY.forEach(item -> {
- if (item.l(new net.minecraft.server.v1_8_R3.ItemStack(item))) {
- @SuppressWarnings("deprecation")
- Material material = Material.getMaterial(Item.getId(item));
- MODIFIERS.put(material, item.j(null));
- }
- });
- mapPotions(0, new ArrayList<>());
- }
- private PotionRecipes() {}
- /**
- * Gets every recipe the leads to the specified potion
- *
- * @param potion
- * The potion
- * @return The recipes for the specified potion
- */
- public static Set<List<Material>> getRecipes(ItemStack potion) {
- return getRecipes(potion.getDurability());
- }
- /**
- * Gets every recipe that leads to the specified potion ID
- *
- * @param potionId
- * The potion ID
- * @return The recipes for the specified potion ID
- */
- public static Set<List<Material>> getRecipes(int potionId) {
- return Collections.unmodifiableSet(MAPPED_RECIPES.getOrDefault(potionId, Collections.emptySet()));
- }
- /**
- * Gets the first recipe that leads to the specified potion
- *
- * @param potion
- * The potion
- * @return The first recipe for the specified potion
- */
- public static List<Material> getFirstRecipe(ItemStack potion) {
- Set<List<Material>> recipes = getRecipes(potion);
- return recipes.size() < 1 ? null : Collections.unmodifiableList(recipes.iterator().next());
- }
- /**
- * Gets the first recipe that leads to the specified potion ID
- *
- * @param potionId
- * The potion ID
- * @return The first recipe for the specified potion ID
- */
- public static List<Material> getFirstRecipe(int potionId) {
- Set<List<Material>> recipes = getRecipes(potionId);
- return recipes.size() < 1 ? null : Collections.unmodifiableList(recipes.iterator().next());
- }
- private static void mapPotions(int potionId, List<Material> craftingChain) {
- MODIFIERS.forEach((key, value) -> {
- int newPotionId = calculateNewPotion(potionId, value);
- if (newPotionId == -1) {
- return;
- }
- MAPPED_RECIPES.putIfAbsent(newPotionId, new HashSet<>());
- MAPPED_RECIPES.get(newPotionId).forEach(existingChain -> {
- if (Collections.indexOfSubList(craftingChain, existingChain) != -1) {
- return;
- }
- });
- List<Material> newCraftingChain = new ArrayList<Material>(craftingChain);
- newCraftingChain.add(key);
- MAPPED_RECIPES.get(newPotionId).add(newCraftingChain);
- mapPotions(newPotionId, newCraftingChain);
- });
- }
- private static int calculateNewPotion(int potionId, String itemUsed) {
- int newPotionId = PotionBrewer.a(potionId, itemUsed);
- List<MobEffect> effects = Items.POTION.e(potionId);
- List<MobEffect> newEffects = Items.POTION.e(newPotionId);
- if (potionId > 0 && effects == newEffects
- || effects != null && (effects.equals(newEffects) || newEffects == null)) {
- if (!ItemPotion.f(potionId) && ItemPotion.f(newPotionId)) {
- return newPotionId;
- }
- } else if (potionId != newPotionId) {
- return newPotionId;
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement