Solved Custom crafting:store recipes

Discussion in 'Plugin Development' started by Gorbit99, Nov 28, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Gorbit99

    I want to create a plugin, where the player can put three items in a GUI, and then if those items match a predefined list of items, then an item is crafted. My problem is, that I need multiple crafting recipes, and I need a way to store them in a config file. How should I go about that?
     
  2. Offline

    DoggyCode™

    Couldn't you just store ItemStacks?
    Code:
    items:
      theslayer:
        - STICK 1
        - GOLD_INGOT 2
      woodenslay:
        - STICK 1
        - WOOD 2
    Code:
    List list = File#.getStringList("items");
    Then cast that list to ItemStack[]?
     
  3. Offline

    Gorbit99

    This is how my code looks like:
    Code:
    java.util.List<String> recipes = plugin.getConfig().getStringList("Recipes");
            boolean[] foundItems = new boolean[]{false, false, false};
            boolean found = false;
            for (String string : recipes) {
                String[] recipe = string.split(",");
                for (int x = 0; x < 3; x++) {
                    found = true;
                    for (int n = 0; n < 3; n++) {
                        if (recipe[x] == items[n].getType().toString() && !foundItems[n]) {
                            found = true;
                            foundItems[n] = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    break;
                }
            }
    I store the recipes in a line like this:
    Code:
    Recipes:
      COAL,COAL,COAL:
        DIAMOND
      DIAMOND,DIAMOND,STICK:
        DIAMOND_SWORD
    But for some reason, the "recipes" variable is alway null
     
  4. Offline

    Zombie_Striker

    @Gorbit99
    Create a class that stores an itemstack (recipe) and an itemstack array(materials used), and store that class.
     
  5. Offline

    Gorbit99

    @Zombie_Striker How can I store a class, can you explain me that? (Sorry, I'm relatively new to bukkit)
     
  6. Offline

    Zombie_Striker

    @Gorbit99
    getConfig().set("PATH", The class instance);
     
  7. Offline

    Gorbit99

    I will try it, thanks for the quick reply

    @Zombie_Striker When I try to put the class into the config using "config.addDefault("Recipes", recipes)", where recipe is a list of the Recipe class, it gives me an error: "No javabean properties found in <packagename>.Recipe. I googled it, and I think it means, that it can't be turned into a String. How can I solve this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 28, 2015
  8. Offline

    Zombie_Striker

    @Gorbit99
    The reason why it can't be turned into a string is because you most likely did not make anything that would turn it into a string.

    Looking back at the previous post, I noticed hat is a terrible option to do actually (saving the whole class). Instead do the following;
    1. Create the recipe class with two constructors, one that takes in an Itemstack (what it gives), and an Itemstack Array (what it needs), and another constructor that takes in a String(the path in the config).
    2. The first constructor would set the recipe and the components needed.
    3. The second constructor would look in the config, and try to find what is at the path(E.g. the path for the recipe is Stuff.things.recipe and the contents are Stuff.things.components, You would input "Stuff.things" since those are the same, and when it tries to get the recipe, it adds ".recipe" to the string. When it wants the components, it adds"components")
    4. The above constructor would only be used when you would get the itemstacks from the config. #2 would be used when trying to make a new recipe.
    5. After you created the constructor, make a method that would save the recipes and components. Itemstacks already have a "component" that allows it to be turned into a String. So instead of saving the whole class, you would use config.set(path+".recipe",itemstack you want) and config.set(path+".components",the itemstack [] of itemstacks needed)
    If anything here is confusing, let me know and I'll explain it further.
     
    Gorbit99 likes this.
  9. Offline

    Gorbit99

    It worked great, another question before I'm gonna mark this as solved, how could I check, if the two arrays match, if the order is different?
     
  10. Offline

    Zombie_Striker

    @Gorbit99
    Well, you check if the contents are the same. Loop through the contents of both arrays, and check if there are any objects that are different.
     
  11. Offline

    Scimiguy

    In other words, compare if two arrays have all the same elements regardless of order?
    You'd have to loop.

    1. compare the sizes of both loops, make sure they're the same size.
    2. Run a foreach loop on each of the elements in one Array, checking ArrayList#contains() for that element on the other array.
    3. If the element doesn't exist, return from your function, or use a boolean to mark that you've found an anomaly
     
    Zombie_Striker likes this.
  12. Offline

    mcdorli

    Create two buffer arrays for each of the arrays, loop trough one, and for every element, check if the other buffer array contains it, if it does, then delete it form both of the arrays. This would be easier to implement with arraylists, because of the .contains() and the .remove() methods. If the arrays are empty, stop, they are the same.
     
  13. Offline

    Gorbit99

    @Zombie_Striker Sorry for posting in a solved thread, but how can I get back my array of ItemSatcks from the config?
     
    Last edited: Nov 29, 2015
  14. Offline

    Scimiguy

    Try and work it out yourself, then come back to us later with code if it doesn't work

    We can't tell you how to do everything
     
  15. Offline

    Gorbit99

    I actually have most of the code done. I created a getRecipe function inside the Recipe class, which takes in a path argument. This is what @Zombie_Striker said, and the only problem is, that I don't know, what type of getter should I use for getting the ingredients back. It's stored in the config as a List<Itemstack>, but every list-getter, that makes sense returns null. What is the way of getting that?
     
  16. Offline

    Scimiguy

    Show your code.
    Show your config.
     
  17. Offline

    Gorbit99

Thread Status:
Not open for further replies.

Share This Page