diff --git a/pom.xml b/pom.xml index abf264d..1f83c4b 100755 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.forkzone.mc CustomCrafting jar - 0.0.3-ALPHA + 0.1.0-ALPHA CustomCrafting diff --git a/src/main/java/org/forkzone/mc/customcrafting/CommandManager.java b/src/main/java/org/forkzone/mc/customcrafting/CommandManager.java new file mode 100755 index 0000000..19f0207 --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/CommandManager.java @@ -0,0 +1,23 @@ +package org.forkzone.mc.customcrafting; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class CommandManager +{ + CustomCrafting plugin; + CommandManager(CustomCrafting plugin, ConfigManager config, RecipeManager recipe) + { + this.plugin = plugin; + } + + public boolean command(CommandSender sender, Command cmd, String label, String[] args) + { + return false; + } + + public void destroy() + { + + } +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/ConfigManager.java b/src/main/java/org/forkzone/mc/customcrafting/ConfigManager.java new file mode 100755 index 0000000..226ae20 --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/ConfigManager.java @@ -0,0 +1,204 @@ +package org.forkzone.mc.customcrafting; + +import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.forkzone.mc.customcrafting.recipe.CCFurnaceRecipe; +import org.forkzone.mc.customcrafting.recipe.CCRecipe; +import org.forkzone.mc.customcrafting.recipe.CCShapedRecipe; +import org.forkzone.mc.customcrafting.recipe.CCShapelessRecipe; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +public class ConfigManager +{ + public static final int SHAPELESS = 0x00; + public static final int SHAPED = 0x01; + public static final int FURNACE = 0x02; + + private CustomCrafting plugin; + private List> configurations; + + private List message = new ArrayList(); + + ConfigManager(CustomCrafting plugin) + { + this.plugin = plugin; + configurations = new ArrayList>(); + configurations.add(SHAPELESS, new HashMap()); + configurations.add(SHAPED, new HashMap()); + configurations.add(FURNACE, new HashMap()); + } + + private String getNameWithoutExtension(String name) + { + int pos = name.lastIndexOf("."); + + if(pos != -1) + return name.substring(0, pos); + + return null; + } + + private boolean configFurnace(String name, JSONObject jObject) + { + try + { + configurations.get(FURNACE).put(name, CCFurnaceRecipe.interpret(jObject)); + return true; + } + catch(Exception e) + { + return false; + } + /* + { + "name":"XYZ", + "input":{ + "item":"COAL", + "data":null, + "nbt":null + }, + "output":"DIAMOND", + "data":null, + "nbt":null, + "amount":1 + "xp":0.5 + } + */ + } + + private boolean configShaped(String name, JSONObject jObject) + { + try + { + configurations.get(SHAPED).put(name, CCShapedRecipe.interpret(jObject)); + return true; + } + catch(Exception e) + { + return false; + } + /* + { + "name":"No_Space_In_Name_Please", + "shape":"D DDCDD D", + "input":{ + "D":{ + "item":"DIRT", + "data":null, + "nbt":null + }, + "C":{ + "item":"COAL", + "data":null, + "nbt":null + } + }, + "output":"DIAMOND", + "data":null, + "nbt":"{display:{Name:\"Dirty Diamond\"}}", + "amount":1 + } + */ + } + + private boolean configShapeless(String name, JSONObject jObject) + { + try + { + configurations.get(SHAPELESS).put(name, CCShapelessRecipe.interpret(jObject)); + return true; + } + catch(Exception e) + { + return false; + } + /* + { + "name":"quartz_block_to_quartz_item", + "input":[ + { + "item":"quartz_block", + "data":-1, + "nbt":null, + "amount":1 + } + ], + "output":"quartz", + "data":null, + "nbt":null, + "amount":1 + } + */ + } + + public boolean loadConfig(File filepath, int type) + { + JSONParser parser = new JSONParser(); + Object object = null; + + try + { + object = parser.parse(new FileReader(filepath)); + } + catch(Exception ex) + { + plugin.getLogger().warning(CustomCrafting.PREFIX + "Could not load: \"" + filepath.getName() + "\""); + return false; + } + + JSONObject jObject = (JSONObject) object; + String name = getNameWithoutExtension(filepath.getName()); + + switch(type) + { + case SHAPELESS: + return configShapeless(name, jObject); + case SHAPED: + return configShaped(name, jObject); + case FURNACE: + return configFurnace(name, jObject); + } + + return false; + } + + public boolean loadAllConfigs(File folder, int type) + { + if(!folder.exists()) + folder.mkdirs(); + + boolean status = true; + + for(File path : folder.listFiles()) + { + if(!loadConfig(path, type)) + { + status = false; + message.add("ERROR: Could not load \"" + path + "\""); + } + } + + return status; + } + + public List getMessages() + { + return message; + } + + public void clearMessages() + { + message.clear(); + } + + public void destroy() + { + + } +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/CustomCrafting.java b/src/main/java/org/forkzone/mc/customcrafting/CustomCrafting.java index 11b40c2..b09b066 100755 --- a/src/main/java/org/forkzone/mc/customcrafting/CustomCrafting.java +++ b/src/main/java/org/forkzone/mc/customcrafting/CustomCrafting.java @@ -1,301 +1,64 @@ package org.forkzone.mc.customcrafting; +import java.io.File; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.inventory.FurnaceRecipe; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.ShapelessRecipe; -import org.bukkit.material.MaterialData; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; - -/** - * CustomCrafting Plugin for Bukkit by basma93 - * - *

Allows to add custom crafting recipes for the workbench or furnace - * - * @author basman93 - * @version 0.0.3 - * - */ public final class CustomCrafting extends JavaPlugin { - /** Prefix {@value} for chat messages */ - private static final String PREFIX = "[CustomCrafting] "; - - /** Version number to check if the configfile is uptodate */ - private static final int CONFIG_VERSION = 1; - - /** - * @param shape (required) raw shape string - * @return returns the raw shape as an array:
- * first level are the rows of the recipe,
- * second level are the collums of the recipe,
- * third level are the item ID's and Data values. - */ - private String[][][] getShapeArray(String shape) - { - String[][][] shape_array = new String[3][3][2]; - String[] buffer = shape.split(";"); - - for(int i = 0; i < buffer.length; ++i) - { - String[] buffer_ = buffer[i].split(","); - for(int j = 0; j < buffer_.length; ++j) - { - String[] buffer__ = buffer_[j].split(":"); - - shape_array[i][j][0] = buffer__[0].toUpperCase(); - shape_array[i][j][1] = buffer__.length > 1 ? buffer__[1] : "0"; - } - } - - return shape_array; - } - - /** - * @param shape (required) raw shape string - * @return returns the raw shape as an array:
- * first level are the different items,
- * second level are the different attribute of the item:
- *

    - *
  1. Item ID
  2. - *
  3. Item data value
  4. - *
  5. Item amount
  6. - *
- */ - private String[][] getShapeArraySL(String shape) - { - ArrayList shape_array = new ArrayList(); - - String[] buffer = shape.split(","); - for(String buffer_ : buffer) - { - shape_array.add(getItemArray(buffer_)); - } - - return shape_array.toArray(new String[shape_array.size()][3]); - } - - /** - * @param item (required) raw item string - * @return returns the raw shape as an array:
- *
    - *
  1. Item ID
  2. - *
  3. Item data value
  4. - *
  5. Item amount
  6. - *
- */ - private String[] getItemArray(String item) - { - String[] item_array = new String[3]; - String[] buffer_item = item.split("#"); - - String[] buffer_item_ = buffer_item[0].split(":"); - item_array[0] = buffer_item_[0].toUpperCase(); - item_array[1] = buffer_item_.length > 1 ? buffer_item_[1] : "0"; - item_array[2] = buffer_item.length > 1 ? buffer_item[1] : "1"; - - return item_array; - } - - /** parse a string to a int */ - private int toInt(String string) - { - return Integer.parseInt(string); - } - - /** parse a string to a short */ - private short toShort(String string) - { - return Short.parseShort(string); - } - - /** - * Adds a custom furnace recipe to bukkit - * @param name (required) the name of the recipe - * @param shape (required) the raw shape of the input item - * @param item (required) the raw shape of the output item - * @param xp (required) the xp the player will get for this - */ - @SuppressWarnings("deprecation") - private void addNewFurnaceRecipe(String name, String shape, String item, float xp) - { - String[] item_array = getItemArray(item); - if(Material.getMaterial(item_array[0]) == null) - { - getLogger().warning("Could not add \"" + name + "\""); - getLogger().warning("Unknown: " + item_array[0]); - return; - } - - String[] shape_array = getItemArray(shape); - if(Material.getMaterial(shape_array[0]) == null) - { - getLogger().warning("Could not add \"" + name + "\""); - getLogger().warning("Unknown: " + shape_array[0]); - return; - } - - FurnaceRecipe recipe = new FurnaceRecipe(new ItemStack(Material.getMaterial(item_array[0]), toInt(item_array[2]), toShort(item_array[1])), new MaterialData(Material.getMaterial(shape_array[0]), (byte) 0), xp); - if(!Bukkit.getServer().addRecipe(recipe)) - getLogger().warning("Something went wrong! Cannot add \"" + name + "\""); - else - getLogger().info("Add Furnace Recipe \"" + name + "\""); - } - - /** - * Adds a custom crafting recipe to bukkit - * @param name (required) the name of the recipe - * @param shape (required) the raw shape of the input item - * @param item (required) the raw shape of the output item - * @param shapeless (required) if the recipe is shapeless - */ - @SuppressWarnings("deprecation") - private void addNewRecipe(NamespacedKey name, String shape, String item, boolean shapeless) - { - String[] item_array = getItemArray(item); - if(Material.getMaterial(item_array[0]) == null) - { - getLogger().warning("Could not add \"" + name.getKey() + "\""); - getLogger().warning("Unknown: " + item_array[0]); - return; - } - else if(shapeless) - { - String[][] shape_array = getShapeArraySL(shape); - ShapelessRecipe recipe = new ShapelessRecipe(name, new ItemStack(Material.getMaterial(item_array[0]), toInt(item_array[2]), toShort(item_array[1]))); - - for(String[] ingredient_array : shape_array) - { - recipe.addIngredient(toInt(ingredient_array[2]), Material.getMaterial(ingredient_array[0]), toInt(ingredient_array[1])); - } - - if(!Bukkit.getServer().addRecipe(recipe)) - getLogger().warning("Something went wrong! Cannot add \"" + name.getKey() + "\""); - else - getLogger().info("Add Recipe \"" + name.getKey() + "\""); - } - else - { - String[][][] shape_array = getShapeArray(shape); - ShapedRecipe recipe = new ShapedRecipe(name, new ItemStack(Material.getMaterial(item_array[0]), toInt(item_array[2]), toShort(item_array[1]))); - Map map = new HashMap(); - int counter = 0; - String[] recipe_shape = new String[3]; - for(int i = 0; i < shape_array.length; ++i) - { - recipe_shape[i] = ""; - for(int j = 0; j < shape_array[i].length; ++j) - { - if(!map.containsKey(shape_array[i][j][0] + ":" + shape_array[i][j][1])) - { - if(shape_array[i][j][0].equalsIgnoreCase(" ") || shape_array[i][j][0].equalsIgnoreCase("0") || shape_array[i][j][0].equalsIgnoreCase("air")) - map.put(shape_array[i][j][0] + ":" + shape_array[i][j][1], ' '); - else - { - map.put(shape_array[i][j][0] + ":" + shape_array[i][j][1], (char)('a' + counter)); - ++counter; - } - } - recipe_shape[i] += map.get(shape_array[i][j][0] + ":" + shape_array[i][j][1]); - } - } - recipe.shape(recipe_shape); - for (Map.Entry entry : map.entrySet()) - { - if(!entry.getValue().equals(' ')) - { - String[] buffer = entry.getKey().split(":"); - recipe.setIngredient(entry.getValue(), Material.getMaterial(buffer[0]), toInt(buffer[1])); - } - } - if(!Bukkit.getServer().addRecipe(recipe)) - getLogger().warning("Something went wrong! Cannot add \"" + name.getKey() + "\""); - else - getLogger().info("Add Recipe \"" + name.getKey() + "\""); - } - } - - /** - * reads the configfile and adds every recipe found in it. - */ - private void readConfigAndAddRecipe() - { - for(String key : getConfig().getConfigurationSection("recipes").getKeys(false)) - { - NamespacedKey nkey = new NamespacedKey(this, key); - addNewRecipe(nkey, getConfig().getString("recipes." + key + ".shape"), getConfig().getString("recipes." + key + ".item"), getConfig().getBoolean("recipes." + key + ".shapeless")); - } - - for(String key : getConfig().getConfigurationSection("furnace_recipes").getKeys(false)) - { - addNewFurnaceRecipe(key, getConfig().getString("furnace_recipes." + key + ".shape"), getConfig().getString("furnace_recipes." + key + ".item"), (float) getConfig().getDouble("furnace_recipes." + key + ".xp")); - } - } - - /** - * removes the custom recipes from bukkit. - * At this moment its only possible to reset the recipes list to default. - */ - private void removeRecipe() - { - Bukkit.getServer().resetRecipes(); - } - + private RecipeManager recipemanager; + private CommandManager commandmanager; + private ConfigManager configmanager; + + protected static final String PREFIX = "[CC] "; + @Override public void onEnable() { - saveDefaultConfig(); - reloadConfig(); - - // Checks if the configfile is the required version, give out a warning otherwise. - if(getConfig().getInt("version", 0) < CONFIG_VERSION) - getLogger().warning("Please delete your configfile. Your configfile is outdated!"); - - readConfigAndAddRecipe(); + load(); } @Override public void onDisable() { - removeRecipe(); + unload(); } @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if(cmd.getName().equalsIgnoreCase("customcrafting")) - { - if(args.length == 1 && args[0].equalsIgnoreCase("reload") && sender.hasPermission("customcrafting.reload")) - { - removeRecipe(); - saveDefaultConfig(); - reloadConfig(); - readConfigAndAddRecipe(); - sender.sendMessage(PREFIX + "reloaded!"); - } - else if(sender.hasPermission("customcrafting.infolite") || sender.hasPermission("customcrafting.info")) - { - sender.sendMessage(PREFIX + "Plugin Information"); - sender.sendMessage(PREFIX + "Name: " + getDescription().getName()); - sender.sendMessage(PREFIX + "Author: " + String.join(", ", getDescription().getAuthors())); - if(sender.hasPermission("customcrafting.info")) - { - sender.sendMessage(PREFIX + "Version: " + getDescription().getVersion()); - sender.sendMessage(PREFIX + "Arguments: reload"); - } - } - return true; - } - return false; + return commandmanager.command(sender, cmd, label, args); + } + + private void load() + { + configmanager = new ConfigManager(this); + recipemanager = new RecipeManager(this, configmanager); + commandmanager = new CommandManager(this, configmanager, recipemanager); + + configmanager.loadAllConfigs(new File(getDataFolder() + "/recipe/shapeless"), ConfigManager.SHAPELESS); + configmanager.loadAllConfigs(new File(getDataFolder() + "/recipe/shaped"), ConfigManager.SHAPED); + configmanager.loadAllConfigs(new File(getDataFolder() + "/recipe/furnace"), ConfigManager.FURNACE); + } + + private void unload() + { + commandmanager.destroy(); + recipemanager.destroy(); + configmanager.destroy(); + + commandmanager = null; + recipemanager = null; + configmanager = null; + } + + private void reload() + { + unload(); + load(); } } diff --git a/src/main/java/org/forkzone/mc/customcrafting/RecipeManager.java b/src/main/java/org/forkzone/mc/customcrafting/RecipeManager.java new file mode 100755 index 0000000..71523cd --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/RecipeManager.java @@ -0,0 +1,16 @@ +package org.forkzone.mc.customcrafting; + +public class RecipeManager +{ + CustomCrafting plugin; + + RecipeManager(CustomCrafting plugin, ConfigManager config) + { + this.plugin = plugin; + } + + public void destroy() + { + + } +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/recipe/CCFurnaceRecipe.java b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCFurnaceRecipe.java new file mode 100755 index 0000000..5ed4ef3 --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCFurnaceRecipe.java @@ -0,0 +1,12 @@ +package org.forkzone.mc.customcrafting.recipe; + +import org.json.simple.JSONObject; + +public class CCFurnaceRecipe extends CCRecipe { + + public static CCRecipe interpret(JSONObject jObject) throws Exception { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/recipe/CCRecipe.java b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCRecipe.java new file mode 100755 index 0000000..0bb0df7 --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCRecipe.java @@ -0,0 +1,9 @@ +package org.forkzone.mc.customcrafting.recipe; + +public class CCRecipe +{ + public CCShapelessRecipe asShapeless() + { + return (CCShapelessRecipe) this; + } +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapedRecipe.java b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapedRecipe.java new file mode 100755 index 0000000..7eb55cf --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapedRecipe.java @@ -0,0 +1,12 @@ +package org.forkzone.mc.customcrafting.recipe; + +import org.json.simple.JSONObject; + +public class CCShapedRecipe extends CCRecipe { + + public static CCRecipe interpret(JSONObject jObject) throws Exception { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapelessRecipe.java b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapelessRecipe.java new file mode 100755 index 0000000..8325243 --- /dev/null +++ b/src/main/java/org/forkzone/mc/customcrafting/recipe/CCShapelessRecipe.java @@ -0,0 +1,12 @@ +package org.forkzone.mc.customcrafting.recipe; + +import org.json.simple.JSONObject; + +public class CCShapelessRecipe extends CCRecipe { + public String test="X"; + + public static CCRecipe interpret(JSONObject jObject) throws Exception { + // TODO Auto-generated method stub + return null; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 61102e0..ea428f7 100755 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,25 +1 @@ -#Do not edit the version number! -#INFO: xp by furnace_recipes don't work at this moment -version: 1 -recipes: - dirtyDiamond: - shapeless: false - shape: "dirt,dirt,dirt;dirt,coal,dirt;dirt,dirt,dirt" - item: "diamond:0#1" - quarzblocktoitem: - shapeless: true - shape: "quartz_block:-1#1" - item: "quartz:0#4" - notchapple: - shapeless: false - shape: "gold_block,gold_block,gold_block;gold_block,apple,gold_block;gold_block,gold_block,gold_block" - item: "golden_apple:1" -furnace_recipes: - diamond_recycle: - shape: "diamond_sword" - item: "diamond:0#1" - xp: 0 - breadfromwheat: - shape: "HAY_BLOCK" - item: "BREAD:0#3" - xp: 0 \ No newline at end of file +version: 2 \ No newline at end of file