Draconix_Dust

BrewingRecipe

Apr 14th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. package lekavar.lma.drinkbeer.recipes;
  2.  
  3. import com.google.common.collect.Lists;
  4. import com.google.gson.JsonArray;
  5. import com.google.gson.JsonObject;
  6. import lekavar.lma.drinkbeer.registries.RecipeRegistry;
  7. import net.minecraft.core.NonNullList;
  8. import net.minecraft.network.FriendlyByteBuf;
  9. import net.minecraft.resources.ResourceLocation;
  10. import net.minecraft.util.GsonHelper;
  11. import net.minecraft.world.item.ItemStack;
  12. import net.minecraft.world.item.crafting.Ingredient;
  13. import net.minecraft.world.item.crafting.Recipe;
  14. import net.minecraft.world.item.crafting.RecipeSerializer;
  15. import net.minecraft.world.item.crafting.RecipeType;
  16. import net.minecraft.world.level.Level;
  17. import net.minecraftforge.common.crafting.CraftingHelper;
  18. import net.minecraftforge.registries.ForgeRegistryEntry;
  19. import net.minecraftforge.common.brewing.BrewingRecipeRegistry;
  20. import org.jetbrains.annotations.NotNull;
  21.  
  22. import javax.annotation.Nullable;
  23. import java.util.List;
  24.  
  25. public class BrewingRecipe implements Recipe<IBrewingInventory> {
  26.     private final ResourceLocation id;
  27.     private final NonNullList<Ingredient> input;
  28.     private final ItemStack cup;
  29.     private final int brewingTime;
  30.     private final ItemStack result;
  31.  
  32.     public BrewingRecipe(ResourceLocation id, NonNullList<Ingredient> input, ItemStack cup, int brewingTime, ItemStack result) {
  33.         this.id = id;
  34.         this.input = input;
  35.         this.cup = cup;
  36.         this.brewingTime = brewingTime;
  37.         this.result = result;
  38.     }
  39.  
  40.     @Deprecated
  41.     public NonNullList<Ingredient> getIngredient(){
  42.         NonNullList<Ingredient> result = NonNullList.create();
  43.         result.addAll(input);
  44.         return result;
  45.     }
  46.  
  47.     @Override
  48.     public NonNullList<Ingredient> getIngredients(){
  49.         NonNullList<Ingredient> result = NonNullList.create();
  50.         result.addAll(input);
  51.         return result;
  52.     }
  53.  
  54.     @Deprecated
  55.     public ItemStack geBeerCup(){
  56.         return cup.copy();
  57.     }
  58.  
  59.     public ItemStack getBeerCup(){
  60.         return cup.copy();
  61.     }
  62.  
  63.     @Override
  64.     public boolean matches(@NotNull IBrewingInventory p_77569_1_, Level p_77569_2_) {
  65.         List<Ingredient> testTarget = Lists.newArrayList(input);
  66.         List<ItemStack> tested = p_77569_1_.getIngredients();
  67.         for (ItemStack itemStack : tested) {
  68.             int i = getLatestMatched(testTarget, itemStack);
  69.             if (i == -1) return false;
  70.             else testTarget.remove(i);
  71.         }
  72.         return testTarget.isEmpty();
  73.     }
  74.  
  75.     private int getLatestMatched(@NotNull List<Ingredient> testTarget, ItemStack tested) {
  76.         for (int i = 0; i < testTarget.size(); i++) {
  77.             if (testTarget.get(i).test(tested)) return i;
  78.         }
  79.         return -1;
  80.     }
  81.  
  82.     /**
  83.      * Returns an Item that is the result of this recipe
  84.      */
  85.     @Override
  86.     public ItemStack assemble(IBrewingInventory inventory) {
  87.         return result.copy();
  88.     }
  89.  
  90.     // Can Craft at any dimension
  91.     @Override
  92.     public boolean canCraftInDimensions(int p_194133_1_, int p_194133_2_) {
  93.         return true;
  94.     }
  95.  
  96.  
  97.     /**
  98.      * Get the result of this recipe, usually for display purposes (e.g. recipe book).
  99.      * If your recipe has more than one possible result (e.g. it's dynamic and depends on its inputs),
  100.      * then return an empty stack.
  101.      */
  102.     @Override
  103.     public ItemStack getResultItem() {
  104.         //For Safety, I use #copy
  105.         return result.copy();
  106.     }
  107.  
  108.     @Override
  109.     public boolean isSpecial() {
  110.         return true;
  111.     }
  112.  
  113.     @Override
  114.     public ResourceLocation getId() {
  115.         return this.id;
  116.     }
  117.  
  118.     @Override
  119.     public RecipeSerializer<?> getSerializer() {
  120.         return RecipeRegistry.BREWING.get();
  121.     }
  122.  
  123.     @Override
  124.     public RecipeType<?> getType() {
  125.         return RecipeRegistry.Type.BREWING;
  126.     }
  127.  
  128.     public int getRequiredCupCount() {
  129.         return cup.getCount();
  130.     }
  131.  
  132.     public boolean isCupQualified(@NotNull IBrewingInventory inventory) {
  133.         return inventory.getCup().getItem() == cup.getItem() && inventory.getCup().getCount() >= cup.getCount();
  134.     }
  135.  
  136.     public int getBrewingTime() {
  137.         return brewingTime;
  138.     }
  139.  
  140.     public static class Serializer extends ForgeRegistryEntry<RecipeSerializer<?>> implements RecipeSerializer<BrewingRecipe> {
  141.  
  142.         @Override
  143.         public BrewingRecipe fromJson(ResourceLocation resourceLocation, JsonObject jsonObject) {
  144.             NonNullList<Ingredient> ingredients = itemsFromJson(GsonHelper.getAsJsonArray(jsonObject, "ingredients"));
  145.             ItemStack cup = CraftingHelper.getItemStack(GsonHelper.getAsJsonObject(jsonObject, "cup"), true);
  146.             int brewing_time = GsonHelper.getAsInt(jsonObject, "brewing_time");
  147.             ItemStack result = CraftingHelper.getItemStack(GsonHelper.getAsJsonObject(jsonObject, "result"), true);
  148.             return new BrewingRecipe(resourceLocation, ingredients, cup, brewing_time, result);
  149.         }
  150.  
  151.         private static NonNullList<Ingredient> itemsFromJson(JsonArray jsonArray) {
  152.             NonNullList<Ingredient> ingredients = NonNullList.create();
  153.             for (int i = 0; i < jsonArray.size(); ++i) {
  154.                 Ingredient ingredient = Ingredient.fromJson(jsonArray.get(i));
  155.                 ingredients.add(ingredient);
  156.             }
  157.             return ingredients;
  158.         }
  159.  
  160.         @Nullable
  161.         @Override
  162.         public BrewingRecipe fromNetwork(ResourceLocation resourceLocation, FriendlyByteBuf packetBuffer) {
  163.             int i = packetBuffer.readVarInt();
  164.             NonNullList<Ingredient> ingredients = NonNullList.withSize(i, Ingredient.EMPTY);
  165.             for (int j = 0; j < ingredients.size(); ++j) {
  166.                 ingredients.set(j, Ingredient.fromNetwork(packetBuffer));
  167.             }
  168.             ItemStack cup = packetBuffer.readItem();
  169.             int brewingTime = packetBuffer.readVarInt();
  170.             ItemStack result = packetBuffer.readItem();
  171.             return new BrewingRecipe(resourceLocation, ingredients, cup, brewingTime, result);
  172.         }
  173.  
  174.         @Override
  175.         public void toNetwork(FriendlyByteBuf packetBuffer, BrewingRecipe brewingRecipe) {
  176.             packetBuffer.writeVarInt(brewingRecipe.input.size());
  177.             for (Ingredient ingredient : brewingRecipe.input) {
  178.                 ingredient.toNetwork(packetBuffer);
  179.             }
  180.             packetBuffer.writeItem(brewingRecipe.cup);
  181.             packetBuffer.writeVarInt(brewingRecipe.brewingTime);
  182.             packetBuffer.writeItem(brewingRecipe.result);
  183.  
  184.         }
  185.     }
  186. }
Add Comment
Please, Sign In to add comment