Skip to content

Commit

Permalink
Fixed Automatic Crafter & Custom Named recipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew authored and Matthew committed Feb 9, 2013
1 parent 6a8d69f commit 490ab25
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
Expand Up @@ -28,6 +28,7 @@
import com.sk89q.craftbook.circuits.ic.IC;
import com.sk89q.craftbook.circuits.ic.ICFactory;
import com.sk89q.craftbook.circuits.ic.PipeInputIC;
import com.sk89q.craftbook.mech.crafting.CustomCrafting;
import com.sk89q.craftbook.util.ItemUtil;
import com.sk89q.craftbook.util.SignUtil;
import com.sk89q.worldedit.BlockWorldVector;
Expand Down Expand Up @@ -116,7 +117,13 @@ public boolean craft(Dispenser disp) {
PistonBaseMaterial p = (PistonBaseMaterial) b.getState().getData();
if (p.getFacing() == ((org.bukkit.material.Dispenser) disp.getData()).getFacing().getOppositeFace()) {
List<ItemStack> items = new ArrayList<ItemStack>();
items.add(recipe.getResult().clone());
for(Recipe rec : CustomCrafting.advancedRecipes.keySet()) {

if(CustomCrafting.checkRecipes(rec, recipe)) {
items.add(CustomCrafting.applyAdvancedEffects(recipe.getResult().clone(),rec));
break;
}
}
if (CircuitCore.inst().getPipeFactory() != null)
if (CircuitCore.inst().getPipeFactory()
.detect(BukkitUtil.toWorldVector(b), items) != null) {
Expand All @@ -127,7 +134,13 @@ public boolean craft(Dispenser disp) {
}

if (!pipes) {
disp.getInventory().addItem(recipe.getResult().clone());
for(Recipe rec : CustomCrafting.advancedRecipes.keySet()) {

if(CustomCrafting.checkRecipes(rec, recipe)) {
disp.getInventory().addItem(CustomCrafting.applyAdvancedEffects(recipe.getResult().clone(),rec));
break;
}
}
for(int i = 0; i < recipe.getResult().getAmount(); i++)
disp.dispense();
}
Expand Down
70 changes: 41 additions & 29 deletions src/main/java/com/sk89q/craftbook/mech/crafting/CustomCrafting.java
Expand Up @@ -35,7 +35,7 @@ public class CustomCrafting implements Listener {
protected final RecipeManager recipes;
protected final CraftBookPlugin plugin = CraftBookPlugin.inst();

private HashMap<Recipe, RecipeManager.Recipe> advancedRecipes = new HashMap<Recipe, RecipeManager.Recipe>();
public static HashMap<Recipe, RecipeManager.Recipe> advancedRecipes = new HashMap<Recipe, RecipeManager.Recipe>();

public CustomCrafting() {

Expand Down Expand Up @@ -94,36 +94,48 @@ public void onCraft(CraftItemEvent event) {

for(Recipe rec : advancedRecipes.keySet()) {

check: {
if(ItemUtil.areItemsIdentical(rec.getResult(), event.getRecipe().getResult())) {
if(rec instanceof ShapedRecipe && event.getRecipe() instanceof ShapedRecipe || rec instanceof ShapelessRecipe && event.getRecipe() instanceof ShapelessRecipe) {
if(rec instanceof ShapedRecipe && event.getRecipe() instanceof ShapedRecipe) {
if(((ShapedRecipe) rec).getShape().length != ((ShapedRecipe) event.getRecipe()).getShape().length)
break check;
}
else if(rec instanceof ShapelessRecipe && event.getRecipe() instanceof ShapelessRecipe) {
if(((ShapelessRecipe) rec).getIngredientList().size() != ((ShapelessRecipe) event.getRecipe()).getIngredientList().size())
break check;

List<ItemStack> test = new ArrayList<ItemStack>();
test.addAll(((ShapelessRecipe) rec).getIngredientList());
if(!test.removeAll(((ShapelessRecipe) event.getRecipe()).getIngredientList()))
break check;
if(test.size() > 0)
break check;
}
RecipeManager.Recipe recipe = advancedRecipes.get(rec);
if(recipe.getResult().hasAdvancedData("name")) {
ItemStack res = event.getCurrentItem();
ItemMeta meta = res.getItemMeta();
meta.setDisplayName(ChatColor.RESET + (String) recipe.getResult().getAdvancedData("name"));
res.setItemMeta(meta);
event.setCurrentItem(res);
}
break;
}
if(checkRecipes(rec, event.getRecipe())) {
event.setCurrentItem(applyAdvancedEffects(event.getCurrentItem(),rec));
break;
}
}
}

public static ItemStack applyAdvancedEffects(ItemStack stack, Recipe rep) {
RecipeManager.Recipe recipe = advancedRecipes.get(rep);
ItemStack res = stack.clone();
if(recipe.getResult().hasAdvancedData("name")) {
ItemMeta meta = res.getItemMeta();
meta.setDisplayName(ChatColor.RESET + (String) recipe.getResult().getAdvancedData("name"));
res.setItemMeta(meta);
}
return res;
}

public static boolean checkRecipes(Recipe rec1, Recipe rec2) {

if(ItemUtil.areItemsIdentical(rec1.getResult(), rec2.getResult())) {
if(rec1 instanceof ShapedRecipe && rec2 instanceof ShapedRecipe || rec1 instanceof ShapelessRecipe && rec2 instanceof ShapelessRecipe) {
if(rec1 instanceof ShapedRecipe && rec2 instanceof ShapedRecipe) {
if(((ShapedRecipe) rec1).getShape().length != ((ShapedRecipe) rec2).getShape().length)
return false;
}
else if(rec1 instanceof ShapelessRecipe && rec2 instanceof ShapelessRecipe) {
if(((ShapelessRecipe) rec1).getIngredientList().size() != ((ShapelessRecipe) rec2).getIngredientList().size())
return false;

List<ItemStack> test = new ArrayList<ItemStack>();
test.addAll(((ShapelessRecipe) rec1).getIngredientList());
if(!test.removeAll(((ShapelessRecipe) rec2).getIngredientList()))
return false;
if(test.size() > 0)
return false;
}

return true;
}
}

return false;
}
}

0 comments on commit 490ab25

Please sign in to comment.