Files
CatServer/patches/net/minecraft/item/crafting/CraftingManager.java.patch
2018-07-15 13:26:53 +08:00

154 lines
6.0 KiB
Diff

--- ../src-base/minecraft/net/minecraft/item/crafting/CraftingManager.java
+++ ../src-work/minecraft/net/minecraft/item/crafting/CraftingManager.java
@@ -2,11 +2,12 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
+
+import java.util.*;
import javax.annotation.Nullable;
+
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+
import net.minecraft.block.Block;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.BlockStone;
@@ -24,14 +25,18 @@
public class CraftingManager
{
private static final CraftingManager INSTANCE = new CraftingManager();
- private final List<IRecipe> recipes = Lists.<IRecipe>newArrayList();
+ public List<IRecipe> recipes = Lists.<IRecipe>newArrayList(); //CB - final -> non-final
+ // CraftBukkit start
+ public IRecipe lastRecipe;
+ public org.bukkit.inventory.InventoryView lastCraftView;
+ // CraftBukkit end
public static CraftingManager getInstance()
{
return INSTANCE;
}
- private CraftingManager()
+ public CraftingManager()
{
(new RecipesTools()).addRecipes(this);
(new RecipesWeapons()).addRecipes(this);
@@ -191,12 +196,20 @@
this.addRecipe(new ItemStack(Items.ARMOR_STAND, 1), new Object[] {"///", " / ", "/_/", '/', Items.STICK, '_', new ItemStack(Blocks.STONE_SLAB, 1, BlockStoneSlab.EnumType.STONE.getMetadata())});
this.addRecipe(new ItemStack(Blocks.END_ROD, 4), new Object[] {"/", "#", '/', Items.BLAZE_ROD, '#', Items.CHORUS_FRUIT_POPPED});
this.addRecipe(new ItemStack(Blocks.BONE_BLOCK, 1), new Object[] {"XXX", "XXX", "XXX", 'X', new ItemStack(Items.DYE, 1, EnumDyeColor.WHITE.getDyeDamage())});
- Collections.sort(this.recipes, new Comparator<IRecipe>()
- {
+ sort();
+ }
+
+ // CraftBukkit start
+ public void sort() {
+ Collections.sort(this.recipes, new Comparator() {
public int compare(IRecipe p_compare_1_, IRecipe p_compare_2_)
{
return p_compare_1_ instanceof ShapelessRecipes && p_compare_2_ instanceof ShapedRecipes ? 1 : (p_compare_2_ instanceof ShapelessRecipes && p_compare_1_ instanceof ShapedRecipes ? -1 : (p_compare_2_.getRecipeSize() < p_compare_1_.getRecipeSize() ? -1 : (p_compare_2_.getRecipeSize() > p_compare_1_.getRecipeSize() ? 1 : 0)));
}
+
+ public int compare(Object object, Object object1) {
+ return this.compare((IRecipe) object, (IRecipe) object1);
+ }
});
}
@@ -309,15 +322,85 @@
@Nullable
public ItemStack findMatchingRecipe(InventoryCrafting craftMatrix, World worldIn)
{
- for (IRecipe irecipe : this.recipes)
+ int i = 0;
+ ItemStack itemstack = null;
+ ItemStack itemstack1 = null;
+ int j;
+
+ for (j = 0; j < craftMatrix.getSizeInventory(); ++j)
{
- if (irecipe.matches(craftMatrix, worldIn))
+ ItemStack itemstack2 = craftMatrix.getStackInSlot(j);
+
+ if (itemstack2 != null)
{
- return irecipe.getCraftingResult(craftMatrix);
+ if (i == 0)
+ {
+ itemstack = itemstack2;
+ }
+
+ if (i == 1)
+ {
+ itemstack1 = itemstack2;
+ }
+
+ ++i;
}
}
+ if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable())
+ {
+ Item item = itemstack.getItem();
+ int j1 = item.getMaxDamage() - itemstack.getItemDamage();
+ int k = item.getMaxDamage() - itemstack1.getItemDamage();
+ int l = j1 + k + item.getMaxDamage() * 5 / 100;
+ int i1 = item.getMaxDamage() - l;
- return null;
+ if (i1 < 0)
+ {
+ i1 = 0;
+ }
+
+ // Cauldron start - vanilla compatibility
+ if (craftMatrix.resultInventory == null)
+ {
+ return new ItemStack(itemstack.getItem(), 1, i1);
+ }
+ // Cauldron end
+ // CraftBukkit start - Construct a dummy repair recipe
+ ItemStack result = new ItemStack(itemstack.getItem(), 1, i1);
+ List<ItemStack> ingredients = new ArrayList<ItemStack>();
+ ingredients.add(itemstack.copy());
+ ingredients.add(itemstack1.copy());
+ ShapelessRecipes recipe = new ShapelessRecipes(result.copy(), ingredients);
+ craftMatrix.currentRecipe = recipe;
+ result = CraftEventFactory.callPreCraftEvent(craftMatrix, result, lastCraftView, true);
+ return result;
+ // CraftBukkit end
+ }
+ else
+ {
+ for (j = 0; j < this.recipes.size(); ++j)
+ {
+ IRecipe irecipe = (IRecipe)this.recipes.get(j);
+
+ if (irecipe.matches(craftMatrix, worldIn) && craftMatrix.resultInventory != null) // Cauldron - add null check for vanilla compatibility
+ {
+ // CraftBukkit start - INVENTORY_PRE_CRAFT event
+ craftMatrix.currentRecipe = irecipe;
+ ItemStack result = irecipe.getCraftingResult(craftMatrix);
+ return CraftEventFactory.callPreCraftEvent(craftMatrix, result, lastCraftView, false);
+ // CraftBukkit end
+ }
+ // Cauldron start - vanilla
+ else if (irecipe.matches(craftMatrix, worldIn))
+ {
+ return irecipe.getCraftingResult(craftMatrix);
+ }
+ // Cauldron end
+ }
+
+ craftMatrix.currentRecipe = null; // CraftBukkit - Clear recipe when no recipe is found
+ return null;
+ }
}
public ItemStack[] getRemainingItems(InventoryCrafting craftMatrix, World worldIn)