From 5a5ce79035f7ed3d77974c4eb8bc94b2ffae54e7 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Fri, 15 Apr 2011 22:11:13 -0400 Subject: Implemented addRecipe method in CraftServer and associated recipe classes. --- .../java/org/bukkit/craftbukkit/CraftServer.java | 28 +++++++++++ .../craftbukkit/inventory/CraftFurnaceRecipe.java | 34 ++++++++++++++ .../bukkit/craftbukkit/inventory/CraftRecipe.java | 7 +++ .../craftbukkit/inventory/CraftShapedRecipe.java | 54 ++++++++++++++++++++++ .../inventory/CraftShapelessRecipe.java | 42 +++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java create mode 100644 src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java create mode 100644 src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java create mode 100644 src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java (limited to 'src/main/java/org/bukkit') diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index b699258d..fbf48c60 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -8,6 +8,10 @@ import net.minecraft.server.IWorldAccess; import org.bukkit.command.*; import org.bukkit.entity.Player; import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.inventory.FurnaceRecipe; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.ShapelessRecipe; import java.io.File; import java.util.ArrayList; @@ -37,6 +41,10 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.SimplePluginManager; import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.craftbukkit.inventory.CraftFurnaceRecipe; +import org.bukkit.craftbukkit.inventory.CraftRecipe; +import org.bukkit.craftbukkit.inventory.CraftShapedRecipe; +import org.bukkit.craftbukkit.inventory.CraftShapelessRecipe; import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.util.config.Configuration; @@ -420,4 +428,24 @@ public final class CraftServer implements Server { return this.prefix; } } + + @Override + public boolean addRecipe(Recipe recipe) { + CraftRecipe toAdd; + if(recipe instanceof CraftRecipe) { + toAdd = (CraftRecipe) recipe; + } else { + if (recipe instanceof ShapedRecipe) { + toAdd = CraftShapedRecipe.fromBukkitRecipe((ShapedRecipe) recipe); + } else if (recipe instanceof ShapelessRecipe) { + toAdd = CraftShapelessRecipe.fromBukkitRecipe((ShapelessRecipe) recipe); + } else if (recipe instanceof FurnaceRecipe) { + toAdd = CraftFurnaceRecipe.fromBukkitRecipe((FurnaceRecipe) recipe); + } else { + return false; + } + } + toAdd.addToCraftingManager(); + return true; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java new file mode 100644 index 00000000..0c28fd2f --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftFurnaceRecipe.java @@ -0,0 +1,34 @@ +package org.bukkit.craftbukkit.inventory; + +import net.minecraft.server.FurnaceRecipes; + +import org.bukkit.Material; +import org.bukkit.inventory.FurnaceRecipe; +import org.bukkit.inventory.ItemStack; +import org.bukkit.material.MaterialData; + +public class CraftFurnaceRecipe extends FurnaceRecipe implements CraftRecipe { + public CraftFurnaceRecipe(ItemStack result, Material source) { + super(result, source); + } + + public CraftFurnaceRecipe(ItemStack result, MaterialData source) { + super(result, source); + } + + public static CraftFurnaceRecipe fromBukkitRecipe(FurnaceRecipe recipe) { + if (recipe instanceof CraftFurnaceRecipe) { + return (CraftFurnaceRecipe) recipe; + } + return new CraftFurnaceRecipe(recipe.getResult(), recipe.getInput()); + } + + public void addToCraftingManager() { + ItemStack result = this.getResult(); + MaterialData input = this.getInput(); + int id = result.getTypeId(); + int amount = result.getAmount(); + int dmg = result.getDurability(); + FurnaceRecipes.a().a(input.getItemTypeId(), new net.minecraft.server.ItemStack(id, amount, dmg)); + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java new file mode 100644 index 00000000..d3e03e24 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java @@ -0,0 +1,7 @@ +package org.bukkit.craftbukkit.inventory; + +import org.bukkit.inventory.Recipe; + +public interface CraftRecipe extends Recipe { + void addToCraftingManager(); +} diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java new file mode 100644 index 00000000..f54cff78 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapedRecipe.java @@ -0,0 +1,54 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.HashMap; + +import net.minecraft.server.CraftingManager; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.material.MaterialData; + +public class CraftShapedRecipe extends ShapedRecipe implements CraftRecipe { + public CraftShapedRecipe(ItemStack result) { + super(result); + } + + public static CraftShapedRecipe fromBukkitRecipe(ShapedRecipe recipe) { + if (recipe instanceof CraftShapedRecipe) { + return (CraftShapedRecipe) recipe; + } + CraftShapedRecipe ret = new CraftShapedRecipe(recipe.getResult()); + String[] shape = recipe.getShape(); + ret.shape(shape); + for (char c : recipe.getIngredientMap().keySet()) { + ret.setIngredient(c, recipe.getIngredientMap().get(c)); + } + return ret; + } + + public void addToCraftingManager() { + Object[] data; + String[] shape = this.getShape(); + HashMap ingred = this.getIngredientMap(); + int datalen = shape.length; + datalen += ingred.size() * 2; + int i = 0; + data = new Object[datalen]; + for (; i < shape.length; i++) { + data[i] = shape[i]; + } + for (char c : ingred.keySet()) { + data[i] = c; + i++; + MaterialData mdata = ingred.get(c); + int id = mdata.getItemTypeId(); + byte dmg = mdata.getData(); + data[i] = new net.minecraft.server.ItemStack(id, 1, dmg); + i++; + } + int id = this.getResult().getTypeId(); + int amount = this.getResult().getAmount(); + short durability = this.getResult().getDurability(); + CraftingManager.a().a(new net.minecraft.server.ItemStack(id, amount, durability), data); + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java new file mode 100644 index 00000000..53f2a165 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftShapelessRecipe.java @@ -0,0 +1,42 @@ +package org.bukkit.craftbukkit.inventory; + +import java.util.ArrayList; + +import net.minecraft.server.CraftingManager; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapelessRecipe; +import org.bukkit.material.MaterialData; + +public class CraftShapelessRecipe extends ShapelessRecipe implements CraftRecipe { + public CraftShapelessRecipe(ItemStack result) { + super(result); + } + + public static CraftShapelessRecipe fromBukkitRecipe(ShapelessRecipe recipe) { + if (recipe instanceof CraftShapelessRecipe) { + return (CraftShapelessRecipe) recipe; + } + CraftShapelessRecipe ret = new CraftShapelessRecipe(recipe.getResult()); + for (MaterialData ingred : recipe.getIngredientList()) { + ret.addIngredient(ingred); + } + return ret; + } + + public void addToCraftingManager() { + ArrayList ingred = this.getIngredientList(); + Object[] data = new Object[ingred.size()]; + int i = 0; + for (MaterialData mdata : ingred) { + int id = mdata.getItemTypeId(); + byte dmg = mdata.getData(); + data[i] = new net.minecraft.server.ItemStack(id, 1, dmg); + i++; + } + int id = this.getResult().getTypeId(); + int amount = this.getResult().getAmount(); + short durability = this.getResult().getDurability(); + CraftingManager.a().b(new net.minecraft.server.ItemStack(id, amount, durability), data); + } +} -- cgit v1.2.3