Advertisement
Guest User

PotionRecipes

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. package io.github.winx64.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Set;
  10.  
  11. import org.bukkit.Material;
  12. import org.bukkit.inventory.ItemStack;
  13.  
  14. import net.minecraft.server.v1_8_R3.Item;
  15. import net.minecraft.server.v1_8_R3.ItemPotion;
  16. import net.minecraft.server.v1_8_R3.Items;
  17. import net.minecraft.server.v1_8_R3.MobEffect;
  18. import net.minecraft.server.v1_8_R3.PotionBrewer;
  19.  
  20. /**
  21.  * Utility class to provide the different recipes for each potion in the game.
  22.  *
  23.  * @author WinX64
  24.  *
  25.  */
  26. public final class PotionRecipes {
  27.  
  28.     private static final Map<Integer, Set<List<Material>>> MAPPED_RECIPES = new HashMap<>();
  29.     private static final Map<Material, String> MODIFIERS = new HashMap<>();
  30.  
  31.     static {
  32.         Item.REGISTRY.forEach(item -> {
  33.             if (item.l(new net.minecraft.server.v1_8_R3.ItemStack(item))) {
  34.                 @SuppressWarnings("deprecation")
  35.                 Material material = Material.getMaterial(Item.getId(item));
  36.                 MODIFIERS.put(material, item.j(null));
  37.             }
  38.         });
  39.        
  40.         mapPotions(0, new ArrayList<>());
  41.     }
  42.  
  43.     private PotionRecipes() {}
  44.  
  45.     /**
  46.      * Gets every recipe the leads to the specified potion
  47.      *
  48.      * @param potion
  49.      *            The potion
  50.      * @return The recipes for the specified potion
  51.      */
  52.     public static Set<List<Material>> getRecipes(ItemStack potion) {
  53.         return getRecipes(potion.getDurability());
  54.     }
  55.  
  56.     /**
  57.      * Gets every recipe that leads to the specified potion ID
  58.      *
  59.      * @param potionId
  60.      *            The potion ID
  61.      * @return The recipes for the specified potion ID
  62.      */
  63.     public static Set<List<Material>> getRecipes(int potionId) {
  64.         return Collections.unmodifiableSet(MAPPED_RECIPES.getOrDefault(potionId, Collections.emptySet()));
  65.     }
  66.  
  67.     /**
  68.      * Gets the first recipe that leads to the specified potion
  69.      *
  70.      * @param potion
  71.      *            The potion
  72.      * @return The first recipe for the specified potion
  73.      */
  74.     public static List<Material> getFirstRecipe(ItemStack potion) {
  75.         Set<List<Material>> recipes = getRecipes(potion);
  76.  
  77.         return recipes.size() < 1 ? null : Collections.unmodifiableList(recipes.iterator().next());
  78.     }
  79.  
  80.     /**
  81.      * Gets the first recipe that leads to the specified potion ID
  82.      *
  83.      * @param potionId
  84.      *            The potion ID
  85.      * @return The first recipe for the specified potion ID
  86.      */
  87.     public static List<Material> getFirstRecipe(int potionId) {
  88.         Set<List<Material>> recipes = getRecipes(potionId);
  89.  
  90.         return recipes.size() < 1 ? null : Collections.unmodifiableList(recipes.iterator().next());
  91.     }
  92.  
  93.     private static void mapPotions(int potionId, List<Material> craftingChain) {
  94.         MODIFIERS.forEach((key, value) -> {
  95.             int newPotionId = calculateNewPotion(potionId, value);
  96.             if (newPotionId == -1) {
  97.                 return;
  98.             }
  99.  
  100.             MAPPED_RECIPES.putIfAbsent(newPotionId, new HashSet<>());
  101.             MAPPED_RECIPES.get(newPotionId).forEach(existingChain -> {
  102.                 if (Collections.indexOfSubList(craftingChain, existingChain) != -1) {
  103.                     return;
  104.                 }
  105.             });
  106.  
  107.             List<Material> newCraftingChain = new ArrayList<Material>(craftingChain);
  108.             newCraftingChain.add(key);
  109.             MAPPED_RECIPES.get(newPotionId).add(newCraftingChain);
  110.  
  111.             mapPotions(newPotionId, newCraftingChain);
  112.         });
  113.     }
  114.  
  115.     private static int calculateNewPotion(int potionId, String itemUsed) {
  116.         int newPotionId = PotionBrewer.a(potionId, itemUsed);
  117.         List<MobEffect> effects = Items.POTION.e(potionId);
  118.         List<MobEffect> newEffects = Items.POTION.e(newPotionId);
  119.         if (potionId > 0 && effects == newEffects
  120.                 || effects != null && (effects.equals(newEffects) || newEffects == null)) {
  121.             if (!ItemPotion.f(potionId) && ItemPotion.f(newPotionId)) {
  122.                 return newPotionId;
  123.             }
  124.         } else if (potionId != newPotionId) {
  125.             return newPotionId;
  126.         }
  127.         return -1;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement