Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1. package net.minecraft.item.crafting;
  2.  
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParseException;
  7. import com.google.gson.JsonSyntaxException;
  8. import java.io.BufferedReader;
  9. import java.io.Closeable;
  10. import java.io.IOException;
  11. import java.io.Reader;
  12. import java.net.URI;
  13. import java.net.URISyntaxException;
  14. import java.net.URL;
  15. import java.nio.file.FileSystem;
  16. import java.nio.file.FileSystems;
  17. import java.nio.file.Files;
  18. import java.nio.file.Path;
  19. import java.nio.file.Paths;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import javax.annotation.Nullable;
  23. import net.minecraft.inventory.InventoryCrafting;
  24. import net.minecraft.item.ItemStack;
  25. import net.minecraft.util.JsonUtils;
  26. import net.minecraft.util.NonNullList;
  27. import net.minecraft.util.ResourceLocation;
  28. import net.minecraft.util.registry.RegistryNamespaced;
  29. import net.minecraft.world.World;
  30. import org.apache.commons.io.FilenameUtils;
  31. import org.apache.commons.io.IOUtils;
  32. import org.apache.logging.log4j.LogManager;
  33. import org.apache.logging.log4j.Logger;
  34.  
  35. public class CraftingManager
  36. {
  37.     private static final Logger field_192422_a = LogManager.getLogger();
  38.     private static int field_193381_c;
  39.     public static final RegistryNamespaced<ResourceLocation, IRecipe> field_193380_a = new RegistryNamespaced<ResourceLocation, IRecipe>();
  40.  
  41.     public static boolean func_193377_a()
  42.     {
  43.         try
  44.         {
  45.             func_193379_a("armordye", new RecipesArmorDyes());
  46.             func_193379_a("bookcloning", new RecipeBookCloning());
  47.             func_193379_a("mapcloning", new RecipesMapCloning());
  48.             func_193379_a("mapextending", new RecipesMapExtending());
  49.             func_193379_a("fireworks", new RecipeFireworks());
  50.             func_193379_a("repairitem", new RecipeRepairItem());
  51.             func_193379_a("tippedarrow", new RecipeTippedArrow());
  52.             func_193379_a("bannerduplicate", new RecipesBanners.RecipeDuplicatePattern());
  53.             func_193379_a("banneraddpattern", new RecipesBanners.RecipeAddPattern());
  54.             func_193379_a("shielddecoration", new ShieldRecipes.Decoration());
  55.             func_193379_a("shulkerboxcoloring", new ShulkerBoxRecipes.ShulkerBoxColoring());
  56.             return func_192420_c();
  57.         }
  58.         catch (Throwable var1)
  59.         {
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     private static boolean func_192420_c()
  65.     {
  66.         FileSystem filesystem = null;
  67.         Gson gson = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
  68.         boolean flag1;
  69.  
  70.         try
  71.         {
  72.             URL url = CraftingManager.class.getResource("/assets/.mcassetsroot");
  73.  
  74.             if (url != null)
  75.             {
  76.                 URI uri = url.toURI();
  77.                 Path path;
  78.  
  79.                 if ("file".equals(uri.getScheme()))
  80.                 {
  81.                     path = Paths.get(CraftingManager.class.getResource("/assets/minecraft/recipes").toURI());
  82.                 }
  83.                 else
  84.                 {
  85.                     if (!"jar".equals(uri.getScheme()))
  86.                     {
  87.                         field_192422_a.error("Unsupported scheme " + uri + " trying to list all recipes");
  88.                         boolean flag2 = false;
  89.                         return flag2;
  90.                     }
  91.  
  92.                     filesystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
  93.                     path = filesystem.getPath("/assets/minecraft/recipes");
  94.                 }
  95.  
  96.                 Iterator<Path> iterator = Files.walk(path).iterator();
  97.  
  98.                 while (iterator.hasNext())
  99.                 {
  100.                     Path path1 = iterator.next();
  101.  
  102.                     if ("json".equals(FilenameUtils.getExtension(path1.toString())))
  103.                     {
  104.                         Path path2 = path.relativize(path1);
  105.                         String s = FilenameUtils.removeExtension(path2.toString()).replaceAll("\\\\", "/");
  106.                         ResourceLocation resourcelocation = new ResourceLocation(s);
  107.                         BufferedReader bufferedreader = null;
  108.  
  109.                         try
  110.                         {
  111.                             boolean flag;
  112.  
  113.                             try
  114.                             {
  115.                                 bufferedreader = Files.newBufferedReader(path1);
  116.                                 func_193379_a(s, func_193376_a((JsonObject)JsonUtils.func_193839_a(gson, bufferedreader, JsonObject.class)));
  117.                             }
  118.                             catch (JsonParseException jsonparseexception)
  119.                             {
  120.                                 field_192422_a.error("Parsing error loading recipe " + resourcelocation, (Throwable)jsonparseexception);
  121.                                 flag = false;
  122.                                 return flag;
  123.                             }
  124.                             catch (IOException ioexception)
  125.                             {
  126.                                 field_192422_a.error("Couldn't read recipe " + resourcelocation + " from " + path1, (Throwable)ioexception);
  127.                                 flag = false;
  128.                                 return flag;
  129.                             }
  130.                         }
  131.                         finally
  132.                         {
  133.                             IOUtils.closeQuietly((Reader)bufferedreader);
  134.                         }
  135.                     }
  136.                 }
  137.  
  138.                 return true;
  139.             }
  140.  
  141.             field_192422_a.error("Couldn't find .mcassetsroot");
  142.             flag1 = false;
  143.         }
  144.         catch (IOException | URISyntaxException urisyntaxexception)
  145.         {
  146.             field_192422_a.error("Couldn't get a list of all recipe files", (Throwable)urisyntaxexception);
  147.             flag1 = false;
  148.             return flag1;
  149.         }
  150.         finally
  151.         {
  152.             IOUtils.closeQuietly((Closeable)filesystem);
  153.         }
  154.  
  155.         return flag1;
  156.     }
  157.  
  158.     private static IRecipe func_193376_a(JsonObject p_193376_0_)
  159.     {
  160.         String s = JsonUtils.getString(p_193376_0_, "type");
  161.  
  162.         if ("crafting_shaped".equals(s))
  163.         {
  164.             return ShapedRecipes.func_193362_a(p_193376_0_);
  165.         }
  166.         else if ("crafting_shapeless".equals(s))
  167.         {
  168.             return ShapelessRecipes.func_193363_a(p_193376_0_);
  169.         }
  170.         else
  171.         {
  172.             throw new JsonSyntaxException("Invalid or unsupported recipe type '" + s + "'");
  173.         }
  174.     }
  175.  
  176.     public static void func_193379_a(String p_193379_0_, IRecipe p_193379_1_)
  177.     {
  178.         func_193372_a(new ResourceLocation(p_193379_0_), p_193379_1_);
  179.     }
  180.  
  181.     public static void func_193372_a(ResourceLocation p_193372_0_, IRecipe p_193372_1_)
  182.     {
  183.         if (field_193380_a.containsKey(p_193372_0_))
  184.         {
  185.             throw new IllegalStateException("Duplicate recipe ignored with ID " + p_193372_0_);
  186.         }
  187.         else
  188.         {
  189.             field_193380_a.register(field_193381_c++, p_193372_0_, p_193372_1_);
  190.         }
  191.     }
  192.  
  193.     /**
  194.      * Retrieves an ItemStack that has multiple recipes for it.
  195.      */
  196.     public static ItemStack findMatchingRecipe(InventoryCrafting p_82787_0_, World craftMatrix)
  197.     {
  198.         for (IRecipe irecipe : field_193380_a)
  199.         {
  200.             if (irecipe.matches(p_82787_0_, craftMatrix))
  201.             {
  202.                 return irecipe.getCraftingResult(p_82787_0_);
  203.             }
  204.         }
  205.  
  206.         return ItemStack.field_190927_a;
  207.     }
  208.  
  209.     @Nullable
  210.     public static IRecipe func_192413_b(InventoryCrafting p_192413_0_, World p_192413_1_)
  211.     {
  212.         for (IRecipe irecipe : field_193380_a)
  213.         {
  214.             if (irecipe.matches(p_192413_0_, p_192413_1_))
  215.             {
  216.                 return irecipe;
  217.             }
  218.         }
  219.  
  220.         return null;
  221.     }
  222.  
  223.     public static NonNullList<ItemStack> getRemainingItems(InventoryCrafting p_180303_0_, World craftMatrix)
  224.     {
  225.         for (IRecipe irecipe : field_193380_a)
  226.         {
  227.             if (irecipe.matches(p_180303_0_, craftMatrix))
  228.             {
  229.                 return irecipe.getRemainingItems(p_180303_0_);
  230.             }
  231.         }
  232.  
  233.         NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>func_191197_a(p_180303_0_.getSizeInventory(), ItemStack.field_190927_a);
  234.  
  235.         for (int i = 0; i < nonnulllist.size(); ++i)
  236.         {
  237.             nonnulllist.set(i, p_180303_0_.getStackInSlot(i));
  238.         }
  239.  
  240.         return nonnulllist;
  241.     }
  242.  
  243.     @Nullable
  244.     public static IRecipe func_193373_a(ResourceLocation p_193373_0_)
  245.     {
  246.         return field_193380_a.getObject(p_193373_0_);
  247.     }
  248.  
  249.     public static int func_193375_a(IRecipe p_193375_0_)
  250.     {
  251.         return field_193380_a.getIDForObject(p_193375_0_);
  252.     }
  253.  
  254.     @Nullable
  255.     public static IRecipe func_193374_a(int p_193374_0_)
  256.     {
  257.         return field_193380_a.getObjectById(p_193374_0_);
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement