Advertisement
HalestormXV

Untitled

Feb 10th, 2021
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. package com.halestormxv.numinous.common.recipes;
  2.  
  3. import com.google.gson.JsonObject;
  4. import com.halestormxv.numinous.NuminousMod;
  5. import com.halestormxv.numinous.common.items.MaterialPouch;
  6. import net.minecraft.inventory.CraftingInventory;
  7. import net.minecraft.item.DyeColor;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.item.crafting.IRecipeSerializer;
  10. import net.minecraft.item.crafting.Ingredient;
  11. import net.minecraft.item.crafting.SpecialRecipe;
  12. import net.minecraft.network.PacketBuffer;
  13. import net.minecraft.util.JSONUtils;
  14. import net.minecraft.util.ResourceLocation;
  15. import net.minecraft.world.World;
  16. import net.minecraftforge.common.Tags;
  17. import net.minecraftforge.common.crafting.CraftingHelper;
  18. import net.minecraftforge.registries.ForgeRegistryEntry;
  19.  
  20.  
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23.  
  24. public class RecolorPouchRecipe extends SpecialRecipe {
  25.     public static final ResourceLocation NAME = NuminousMod.getId("recolor_pouch");
  26.     public static final Serializer SERIALIZER = new Serializer();
  27.  
  28.     public RecolorPouchRecipe(ResourceLocation idIn) { super(idIn); }
  29.  
  30.     @Override
  31.     public boolean matches(CraftingInventory inv, World worldIn) {
  32.         int pouchCount = 0;
  33.         int dyeCount = 0;
  34.  
  35.         for (int i = 0; i < inv.getSizeInventory(); ++i) {
  36.             ItemStack stack = inv.getStackInSlot(i);
  37.             if (!stack.isEmpty()) {
  38.                 if (stack.getItem() instanceof MaterialPouch) {
  39.                     ++pouchCount;
  40.                 } else if (stack.getItem().isIn(Tags.Items.DYES)) {
  41.                     ++dyeCount;
  42.                 } else {
  43.                     return false;
  44.                 }
  45.             }
  46.         }
  47.  
  48.         return pouchCount == 1 && dyeCount > 0;
  49.     }
  50.  
  51.     @Override
  52.     public ItemStack getCraftingResult(CraftingInventory inv) {
  53.         ItemStack materialPouch = ItemStack.EMPTY;
  54.         Collection<ItemStack> dyes = new ArrayList<>();
  55.  
  56.         for (int i = 0; i < inv.getSizeInventory(); ++i) {
  57.             ItemStack stack = inv.getStackInSlot(i);
  58.             if (!stack.isEmpty()) {
  59.                 if (stack.getItem() instanceof MaterialPouch) {
  60.                     materialPouch = stack;
  61.                 } else if (stack.getItem().isIn(Tags.Items.DYES)) {
  62.                     dyes.add(stack);
  63.                 }
  64.             }
  65.         }
  66.  
  67.         ItemStack result = materialPouch.copy();
  68.         applyDyes(result, dyes);
  69.         return result;
  70.     }
  71.  
  72.     private static void applyDyes(ItemStack materialPouch, Collection<ItemStack> dyes) {
  73.         int[] componentSums = new int[3];
  74.         int maxColorSum = 0;
  75.         int colorCount = 0;
  76.  
  77.         int pouchColor = MaterialPouch.getPouchColor(materialPouch);
  78.         if (pouchColor != DyeColor.WHITE.getFireworkColor()) {
  79.             float r = (float) (pouchColor >> 16 & 255) / 255.0F;
  80.             float g = (float) (pouchColor >> 8 & 255) / 255.0F;
  81.             float b = (float) (pouchColor & 255) / 255.0F;
  82.             maxColorSum = (int) ((float) maxColorSum + Math.max(r, Math.max(g, b)) * 255.0F);
  83.             componentSums[0] = (int) ((float) componentSums[0] + r * 255.0F);
  84.             componentSums[1] = (int) ((float) componentSums[1] + g * 255.0F);
  85.             componentSums[2] = (int) ((float) componentSums[2] + b * 255.0F);
  86.             ++colorCount;
  87.         }
  88.  
  89.         for (ItemStack dye : dyes) {
  90.             DyeColor dyeColor = DyeColor.getColor(dye);
  91.             if (dyeColor != null) {
  92.                 float[] componentValues = dyeColor.getColorComponentValues();
  93.                 int r = (int) (componentValues[0] * 255.0F);
  94.                 int g = (int) (componentValues[1] * 255.0F);
  95.                 int b = (int) (componentValues[2] * 255.0F);
  96.                 maxColorSum += Math.max(r, Math.max(g, b));
  97.                 componentSums[0] += r;
  98.                 componentSums[1] += g;
  99.                 componentSums[2] += b;
  100.                 ++colorCount;
  101.             }
  102.         }
  103.  
  104.         if (colorCount > 0) {
  105.             int r = componentSums[0] / colorCount;
  106.             int g = componentSums[1] / colorCount;
  107.             int b = componentSums[2] / colorCount;
  108.             float maxAverage = (float) maxColorSum / (float) colorCount;
  109.             float max = (float) Math.max(r, Math.max(g, b));
  110.             r = (int) ((float) r * maxAverage / max);
  111.             g = (int) ((float) g * maxAverage / max);
  112.             b = (int) ((float) b * maxAverage / max);
  113.             int finalColor = (r << 8) + g;
  114.             finalColor = (finalColor << 8) + b;
  115.             MaterialPouch.setPouchColor(materialPouch, finalColor);
  116.         }
  117.     }
  118.  
  119.     @Override
  120.     public boolean canFit(int width, int height) {
  121.         return width * height >= 2;
  122.     }
  123.  
  124.     @Override
  125.     public IRecipeSerializer<?> getSerializer() {
  126.         return SERIALIZER;
  127.     }
  128.  
  129.     public static class Serializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<RecolorPouchRecipe> {
  130.         @Override
  131.         public RecolorPouchRecipe read(ResourceLocation recipeId, JsonObject json) {
  132.             return new RecolorPouchRecipe(recipeId);
  133.         }
  134.  
  135.         @Override
  136.         public RecolorPouchRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
  137.             return new RecolorPouchRecipe(recipeId);
  138.         }
  139.  
  140.         @Override
  141.         public void write(PacketBuffer buffer, RecolorPouchRecipe recipe) {
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement