Matching recipes (any size from 1x1 to 3x3 matrix) [solved]

Discussion in 'Plugin Development' started by Digi, Oct 13, 2011.

Thread Status:
Not open for further replies.
  1. I'm trying to match (compare) a recipe against a crafted recipe but It's difficult for shapes that are smaller than the grid and expecially those that need to contain air.

    Example of problematic recipe:
    Code:
     W
    WW
    W beeing wood and space air/null, let's say that makes stairs.
    It can be placed in the 2x2 grid from the inventory and the 3x3 workbench, but in the workbench it can be placed anywhere on the grid... and that's the problem, if I get something like in the onInventoryCrafted event's getInventory() or getRecipe():
    Code:
    NULL NULL NULL
    NULL NULL WOOD
    NULL WOOD WOOD
    or
    Code:
    NULL WOOD NULL
    WOOD WOOD NULL
    NULL NULL NULL
    How can I match both against a pre-stored recipe ?[/b]
     
  2. I suppose that's why we have a CraftingManager. You don't manually hook an event, but register a Recipe and the rest will be done for you.
    I've never worked with it, but the JavaDocs are possibly enough to understand it.

    That could be a beginning:
    Code:
    ShapedRecipe recipe = new ShapedRecipe(...);
    // configure your recipe
    {server}.addRecipe(recipe);
     
  3. I need to match (compare) them to restrict some recipes, I know how to create new ones :p
     
  4. Offline

    Shamebot

  5. I tried that code but I don't get anything out of it due to the fact that I don't understand what it does.
     
  6. Offline

    Shamebot

    It checks wether there's a recipe on the server matching the one you specified with .setItem(..),
    some minecraft names might be different in the new versions.
     
  7. Well, can it match a big list against one recipe that's currently beeing crafted in the onInventoryCraft event ?

    Apart from wanting to restrict recipes, I want to detect when custom recipes are used so I can return some items if the recipe specifies it ..
     
  8. Offline

    Afforess

    What changes to the event API for crafting would make matching the recipe easier? Shapeless list of items?
     
  9. Well, a dynamic size 2D array would do, I'm already storing recipes with air in them if they have spaces.
    The process for existing recipe 2D array would be to detect each horizontal and vertical line if they're all null and skip them from beeing added to the new array.

    I dunno, I'm a newbie at Java :}
     
  10. Offline

    Father Of Time

    I know nothing about this so I’m completely speculating, but I would imagine the built in recipe system has the ability to look up what a shape equals and return the DataValue correct? So for instance (pseudo code):

    Code:
    Recipe.Small.GetDataValue();
    Recipe.Large.GetDataValue();
    Which returns an items DataValue. Rather than trying to compare the recipes in the window could you tap into the “GetItemByShape” methods for the two different crafting windows then compare the Items DataValue they return?

    Code:
    SmallCraftingWindow = Recipe.Small.GetDataID();
    LargeCraftingWindow = Recipe.Large.GetDataID();
    
    If(SmallCraftingWindow == LargeCraftingWindow)
                Return true;
    
    Else return false;
    As I stated before, I have had no experience with crafting menus and I’m currently at work with no access to my programming software, so the information I’ve provided is simply food for though, not necessarily a solution to your problem.

    Regardless, I hope my ranting prove to be useful and not completely infeasible.
     
  11. There's no small or large recipe, the size can be anything between 1x1 and 3x3 and if they're smaller than 3x3 they can be placed anywhere on the grid but getRecipe() returns their exact placement...

    Hmm, OH I got it, @Afforess , getRecipe() should return the original recipe's items, not the shape that the user placed them into, it will be great if you can make it return the exact size... I mean, if you created a 1x2 recipe, it should return that, but if you created it by surrounding it with air, it should return the full 3x3... if you get what I mean xD
    It should be fairly easy to get the recipe's original shape in spout's internals.
     
  12. *bump* :}

    So, anyone got an ideea how to move a shape in a 3x3 array in the top-left corner ?

    Something like:
    Code:
    A A A
    A A W
    A W W
    Let's say W is wood and A is air, that beeing a recipe someone entered in their recipe grid, I can store them in a 2D array but how do I move them so that they'll be aligned in the top-left corner like this:
    Code:
    A W A
    W W A
    A A A
    ?
     
  13. Well, I figured this out myself too... a acceptable code that does what I wanted in the above post:
    Code:
     // items is a 9-cell array that contains the items from the crafting grid
    
    	    	int slots[][] =
    		    {
    		    	{0,1,2,3,4,5,6,7,8},
    		    	{0,3,6,1,4,7,2,5,8}
    		    };
    	    	
    	    	int limit;
    	    	
    	    	for(int slot[] : slots)
    	    	{
    	        	limit = 0;
    	        	
    	        	while(items[slot[0]] == null && items[slot[1]] == null && items[slot[2]] == null && ++limit <= 2)
    	        	{
    	        		items[slot[0]] = items[slot[3]];
    	        		items[slot[1]] = items[slot[4]];
    	        		items[slot[2]] = items[slot[5]];
    	        		
    	        		items[slot[3]] = items[slot[6]];
    	        		items[slot[4]] = items[slot[7]];
    	        		items[slot[5]] = items[slot[8]];
    	        		
    	        		items[slot[6]] = null;
    	        		items[slot[7]] = null;
    	        		items[slot[8]] = null;
    	        	}
    	    	}
    
    // then I just loop through recipes and check if their items match the items from this code
    Tested relatively briefly, I'll update if I find anything wrong with it.
     
Thread Status:
Not open for further replies.

Share This Page