Guest User

Untitled

a guest
Nov 10th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.82 KB | None | 0 0
  1. public class WorkbenchRecipeShaped implements IRecipe<IInventory> {
  2.    
  3.     public static final int                     recipeWidth  = 4;
  4.     public static final int                     recipeHeight = 4;
  5.     private final       NonNullList<Ingredient> recipeItems;
  6.     private final       ItemStack               result;
  7.     private final       ResourceLocation        recipeId;
  8.    
  9.     public WorkbenchRecipeShaped(ResourceLocation recipeId, NonNullList<Ingredient> recipeItemsIn, ItemStack result) {
  10.         this.recipeId    = recipeId;
  11.         this.recipeItems = recipeItemsIn;
  12.         this.result      = result;
  13.     }
  14.    
  15.     @Override public boolean matches(IInventory inv, World worldIn) {
  16.         for (int i = 0 ; i < recipeWidth ; i++) {
  17.             for (int j = 0 ; j < recipeHeight ; j++) {
  18.                 Ingredient ing = recipeItems.get(j + i * recipeWidth);
  19.                
  20.                 if (!ing.test(inv.getStackInSlot(j + i * recipeWidth + 1))) return false;
  21.             }
  22.         }
  23.         return true;
  24.     }
  25.    
  26.     @Override public ItemStack getCraftingResult(IInventory inv) {
  27.         return this.result.copy();
  28.     }
  29.    
  30.     @Override public boolean canFit(int width, int height) {
  31.         return width * height <= recipeHeight * recipeHeight;
  32.     }
  33.    
  34.     @Override public ItemStack getRecipeOutput() {
  35.         return this.result;
  36.     }
  37.    
  38.     @Override public ResourceLocation getId() {
  39.         return this.recipeId;
  40.     }
  41.    
  42.     @Override public ItemStack getIcon() {
  43.         return new ItemStack(ModBlocks.CELESTIAL_WORKBENCH_ITEM.get());
  44.     }
  45.     @Override public IRecipeSerializer<?> getSerializer() {
  46.         return ModRecipes.WORKBENCH_RECIPE_SHAPED_SERIALIZER.get();
  47.     }
  48.    
  49.     @Override public IRecipeType<?> getType() {
  50.         return ModRecipes.WORKBENCH_RECIPE_SHAPED_RECIPE_TYPE;
  51.     }
  52.    
  53.     private static NonNullList<Ingredient> deserializeIngredients(String[] pattern, Map<String, Ingredient> keys, int patternWidth, int patternHeight) {
  54.         NonNullList<Ingredient> nonnulllist = NonNullList.withSize(patternWidth * patternHeight, Ingredient.EMPTY);
  55.         Set<String>             set         = Sets.newHashSet(keys.keySet());
  56.         set.remove(" ");
  57.        
  58.         for (int i = 0 ; i < pattern.length ; ++i) {
  59.             for (int j = 0 ; j < pattern[i].length() ; ++j) {
  60.                 String     s          = pattern[i].substring(j, j + 1);
  61.                 Ingredient ingredient = keys.get(s);
  62.                 if (ingredient == null) {
  63.                     throw new JsonSyntaxException("Pattern references symbol '" + s + "' but it's not defined in the key");
  64.                 }
  65.                
  66.                 set.remove(s);
  67.                 nonnulllist.set(j + patternWidth * i, ingredient);
  68.             }
  69.         }
  70.        
  71.         if (!set.isEmpty()) {
  72.             throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + set);
  73.         }
  74.         else {
  75.             return nonnulllist;
  76.         }
  77.     }
  78.    
  79.     @VisibleForTesting
  80.     static String[] shrink(String... toShrink) {
  81.         int i = Integer.MAX_VALUE;
  82.         int j = 0;
  83.         int k = 0;
  84.         int l = 0;
  85.        
  86.         for (int i1 = 0 ; i1 < toShrink.length ; ++i1) {
  87.             String s = toShrink[i1];
  88.             i = Math.min(i, firstNonSpace(s));
  89.             int j1 = lastNonSpace(s);
  90.             j = Math.max(j, j1);
  91.             if (j1 < 0) {
  92.                 if (k == i1) {
  93.                     ++k;
  94.                 }
  95.                
  96.                 ++l;
  97.             }
  98.             else {
  99.                 l = 0;
  100.             }
  101.         }
  102.        
  103.         if (toShrink.length == l) {
  104.             return new String[0];
  105.         }
  106.         else {
  107.             String[] astring = new String[toShrink.length - l - k];
  108.            
  109.             for (int k1 = 0 ; k1 < astring.length ; ++k1) {
  110.                 astring[k1] = toShrink[k1 + k].substring(i, j + 1);
  111.             }
  112.            
  113.             return astring;
  114.         }
  115.     }
  116.    
  117.     private static int firstNonSpace(String str) {
  118.         int i;
  119.         for (i = 0; i < str.length() && str.charAt(i) == ' ' ; ++i) {
  120.         }
  121.        
  122.         return i;
  123.     }
  124.    
  125.     private static int lastNonSpace(String str) {
  126.         int i;
  127.         for (i = str.length() - 1; i >= 0 && str.charAt(i) == ' ' ; --i) {
  128.         }
  129.        
  130.         return i;
  131.     }
  132.    
  133.     private static String[] patternFromJson(JsonArray jsonArr) {
  134.         String[] astring = new String[jsonArr.size()];
  135.        
  136.         for (int i = 0 ; i < astring.length ; ++i) {
  137.             String s = JSONUtils.getString(jsonArr.get(i), "pattern[" + i + "]");
  138.             if (i > 0 && astring[0].length() != s.length()) {
  139.                 throw new JsonSyntaxException("Invalid pattern: each row must be the same width");
  140.             }
  141.            
  142.             astring[i] = s;
  143.         }
  144.        
  145.         return astring;
  146.     }
  147.    
  148.     private static Map<String, Ingredient> deserializeKey(JsonObject json) {
  149.         Map<String, Ingredient> map = Maps.newHashMap();
  150.        
  151.         for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
  152.             if (entry.getKey().length() != 1) {
  153.                 throw new JsonSyntaxException("Invalid key entry: '" + (String) entry.getKey() + "' is an invalid symbol (must be 1 character only).");
  154.             }
  155.            
  156.             if (" ".equals(entry.getKey())) {
  157.                 throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");
  158.             }
  159.            
  160.             map.put(entry.getKey(), Ingredient.deserialize(entry.getValue()));
  161.         }
  162.        
  163.         map.put(" ", Ingredient.EMPTY);
  164.         return map;
  165.     }
  166.    
  167.     public static ItemStack deserializeItem(JsonObject object) {
  168.         String s = JSONUtils.getString(object, "item");
  169.         Item item = Registry.ITEM.getOptional(new ResourceLocation(s)).orElseThrow(() -> {
  170.             return new JsonSyntaxException("Unknown item '" + s + "'");
  171.         });
  172.         if (object.has("data")) {
  173.             throw new JsonParseException("Disallowed data tag found");
  174.         }
  175.         else {
  176.             int i = JSONUtils.getInt(object, "count", 1);
  177.             return net.minecraftforge.common.crafting.CraftingHelper.getItemStack(object, true);
  178.         }
  179.     }
  180.    
  181.     public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<WorkbenchRecipeShaped> {
  182.        
  183.         private static final int width  = WorkbenchRecipeShaped.recipeWidth;
  184.         private static final int height = WorkbenchRecipeShaped.recipeHeight;
  185.        
  186.         @Override public WorkbenchRecipeShaped read(ResourceLocation recipeId, JsonObject json) {
  187.             Map<String, Ingredient> map     = WorkbenchRecipeShaped.deserializeKey(JSONUtils.getJsonObject(json, "key"));
  188.             String[]                pattern = WorkbenchRecipeShaped.shrink(WorkbenchRecipeShaped.patternFromJson(JSONUtils.getJsonArray(json, "pattern")));
  189.             NonNullList<Ingredient> inputs  = WorkbenchRecipeShaped.deserializeIngredients(pattern, map, width, height);
  190.             ItemStack               output  = WorkbenchRecipeShaped.deserializeItem(JSONUtils.getJsonObject(json, "result"));
  191.            
  192.             return new WorkbenchRecipeShaped(recipeId, inputs, output);
  193.         }
  194.        
  195.         @Nullable @Override public WorkbenchRecipeShaped read(ResourceLocation recipeId, PacketBuffer buffer) {
  196.             NonNullList<Ingredient> inputs = NonNullList.withSize(width * height, Ingredient.EMPTY);
  197.            
  198.             for (int i = 0 ; i < inputs.size() ; i++) {
  199.                 inputs.set(i, Ingredient.read(buffer));
  200.             }
  201.            
  202.             ItemStack result = buffer.readItemStack();
  203.             return new WorkbenchRecipeShaped(recipeId, inputs, result);
  204.         }
  205.        
  206.         @Override public void write(PacketBuffer buffer, WorkbenchRecipeShaped recipe) {
  207.             buffer.writeVarInt(width);
  208.             buffer.writeVarInt(height);
  209.            
  210.             for (Ingredient ing : recipe.getIngredients())
  211.                 ing.write(buffer);
  212.            
  213.             buffer.writeItemStack(recipe.result);
  214.         }
  215.        
  216.     }
  217.    
  218. }
Advertisement
Add Comment
Please, Sign In to add comment