Permalink
Browse files

Minor recipe cleanup. Also use NamespacedKeys for the recipes now.

  • Loading branch information...
me4502 committed Jun 25, 2017
1 parent 11063b9 commit 944b904430bc6d87e369ff42a7b03e6f36ac0696
@@ -1,6 +1,7 @@
package com.sk89q.craftbook.mechanics.crafting;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.inventory.ItemStack;
@@ -18,10 +19,6 @@
//Advanced data
private HashMap<String, Object> advancedData = new HashMap<>();
public HashMap<String, Object> getAllAdvancedData() {
return advancedData;
}
public boolean hasAdvancedData() {
return !advancedData.isEmpty();
}
@@ -46,7 +43,6 @@ public CraftingItemStack(ItemStack item) {
}
public ItemStack getItemStack() {
return item;
}
@@ -58,20 +54,17 @@ public CraftingItemStack add(CraftingItemStack stack) {
return this;
}
public boolean isSameType(CraftingItemStack stack) {
boolean isSameType(CraftingItemStack stack) {
return ItemUtil.areItemsIdentical(item, stack.item);
}
@Override
public int compareTo(CraftingItemStack stack) {
if (stack.item.getAmount() > item.getAmount()) return 1;
if (stack.item.getAmount() == item.getAmount()) return 0;
return -1;
return Integer.compare(stack.item.getAmount(), item.getAmount());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + item.hashCode();
@@ -81,16 +74,18 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
if (obj instanceof CraftingItemStack) {
CraftingItemStack stack = (CraftingItemStack) obj;
if(stack.advancedData.size() != advancedData.size())
return false;
if(stack.hasAdvancedData() != hasAdvancedData())
if(stack.advancedData.size() != advancedData.size()) {
return false;
for(String key : advancedData.keySet())
if(!stack.hasAdvancedData(key))
}
for(Map.Entry<String, Object> advancedDataEntries : advancedData.entrySet()) {
if (!stack.hasAdvancedData(advancedDataEntries.getKey())) {
return false;
} else if (!advancedDataEntries.getValue().equals(stack.getAdvancedData(advancedDataEntries.getKey()))){
return false;
}
}
return isSameType(stack) && stack.item.getAmount() == item.getAmount();
}
return false;
@@ -13,6 +13,7 @@
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@@ -33,11 +34,10 @@
public static CustomCrafting INSTANCE;
public static final Map<Recipe, RecipeManager.Recipe> advancedRecipes = new HashMap<>();
private static final Map<Recipe, RecipeManager.Recipe> advancedRecipes = new HashMap<>();
@Override
public boolean enable() {
INSTANCE = this;
CraftBookPlugin.inst().createDefaultConfiguration(new File(CraftBookPlugin.inst().getDataFolder(), "crafting-recipes.yml"), "crafting-recipes.yml");
new RecipeManager(new YAMLProcessor(new File(CraftBookPlugin.inst().getDataFolder(), "crafting-recipes.yml"), true, YAMLFormat.EXTENDED));
@@ -53,7 +53,6 @@ public boolean enable() {
@Override
public void disable () {
advancedRecipes.clear();
RecipeManager.INSTANCE = null;
INSTANCE = null;
@@ -67,11 +66,11 @@ public boolean addRecipe(RecipeManager.Recipe r) {
Recipe sh;
if (r.getType() == RecipeManager.RecipeType.SHAPELESS) {
sh = new ShapelessRecipe(r.getResult().getItemStack());
sh = new ShapelessRecipe(new NamespacedKey(CraftBookPlugin.inst(), r.getId()), r.getResult().getItemStack());
for (CraftingItemStack is : r.getIngredients())
((ShapelessRecipe) sh).addIngredient(is.getItemStack().getAmount(), is.getItemStack().getData());
} else if (r.getType() == RecipeManager.RecipeType.SHAPED) {
sh = new ShapedRecipe(r.getResult().getItemStack());
sh = new ShapedRecipe(new NamespacedKey(CraftBookPlugin.inst(), r.getId()), r.getResult().getItemStack());
((ShapedRecipe) sh).shape(r.getShape());
for (Entry<CraftingItemStack, Character> is : r.getShapedIngredients().entrySet())
((ShapedRecipe) sh).setIngredient(is.getValue(), is.getKey().getItemStack().getData());
@@ -103,7 +102,6 @@ public boolean addRecipe(RecipeManager.Recipe r) {
@EventHandler(priority = EventPriority.LOW)
public void prepareCraft(PrepareItemCraftEvent event) {
if(!EventUtil.passesFilter(event)) return;
ItemStack bits = null;
@@ -206,7 +204,6 @@ public void prepareCraft(PrepareItemCraftEvent event) {
@EventHandler(priority = EventPriority.LOW)
public void prepareFurnace(InventoryClickEvent event) {
if(!EventUtil.passesFilter(event)) return;
if(!(event.getInventory() instanceof FurnaceInventory)) return;
@@ -236,13 +233,11 @@ public void prepareFurnace(InventoryClickEvent event) {
@EventHandler(priority = EventPriority.LOW)
public void onFurnaceCook(FurnaceSmeltEvent event) {
if(!EventUtil.passesFilter(event)) return;
ItemStack bits = null;
CraftBookPlugin.logDebugMessage("Smelting has been initiated!", "advanced-data");
for(Entry<Recipe, RecipeManager.Recipe> recipeRecipeEntry : advancedRecipes.entrySet()) {
if(!(recipeRecipeEntry.getKey() instanceof FurnaceRecipe)) continue;
try {
if(checkFurnaceRecipes((FurnaceRecipe) recipeRecipeEntry.getKey(), event.getSource(), event.getResult())) {
@@ -356,7 +351,6 @@ public void applyPostData(RecipeManager.Recipe recipe, Player p, InventoryClickE
}
public static ItemStack craftItem(Recipe recipe) {
for(Recipe rec : advancedRecipes.keySet()) {
if(ItemUtil.areRecipesIdentical(rec, recipe))
return applyAdvancedEffects(recipe.getResult(),rec, null);
@@ -366,7 +360,6 @@ public static ItemStack craftItem(Recipe recipe) {
}
private static ItemStack applyAdvancedEffects(ItemStack stack, Recipe rep, Player player) {
RecipeManager.Recipe recipe = advancedRecipes.get(rep);
if(recipe == null)
@@ -379,7 +372,6 @@ private static ItemStack applyAdvancedEffects(ItemStack stack, Recipe rep, Playe
}
private static boolean checkFurnaceRecipes(FurnaceRecipe rec1, ItemStack source, ItemStack result) {
if(ItemUtil.areBaseItemsIdentical(rec1.getInput(), source))
if(ItemUtil.areBaseItemsIdentical(rec1.getResult(), result))
return true;
@@ -116,7 +116,6 @@ public boolean removeRecipe(String name) {
@Override
public boolean equals(Object o) {
if(o instanceof Recipe) {
if(shape != null)
if(shape.size() != ((Recipe)o).shape.size())
@@ -173,7 +172,6 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int ret = id.hashCode();
if(ingredients != null)
ret += ingredients.hashCode();
@@ -388,11 +386,12 @@ public Object getAdvancedData(String key) {
}
public void addAdvancedData(String key, Object data) {
CraftBookPlugin.logDebugMessage("Adding advanced data of type: " + key + " to an ItemStack!", "advanced-data.init");
CraftBookPlugin.logDebugMessage("Adding advanced data of type: " + key + " to an ItemStack! {" + String.valueOf(data) + '}',
"advanced-data.init");
advancedData.put(key, data);
}
public HashMap<String,Object> getAdvancedDataMap () {
public HashMap<String,Object> getAdvancedDataMap() {
return advancedData;
}
}

0 comments on commit 944b904

Please sign in to comment.