Java Code Examples for org.bukkit.inventory.ShapedRecipe#shape()

The following examples show how to use org.bukkit.inventory.ShapedRecipe#shape() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Craft.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private void register(){
	ShapedRecipe craftRecipe = VersionUtils.getVersionUtils().createShapedRecipe(craft, UUID.randomUUID().toString());
	
	craftRecipe.shape("abc","def","ghi");
	
	List<Character> symbols = Arrays.asList('a','b','c','d','e','f','g','h','i');
	for(int i=0 ; i<9 ; i++){
		if(!recipe.get(i).getType().equals(Material.AIR)){
			Material material = recipe.get(i).getType();
			MaterialData data = recipe.get(i).getData();
			if (data != null && data.getItemType() == material) {
				craftRecipe.setIngredient(symbols.get(i), data);
			}else {
				craftRecipe.setIngredient(symbols.get(i), material);
			}
		}
	}
	
	Bukkit.getLogger().info("[UhcCore] "+name+" custom craft registered");
	Bukkit.getServer().addRecipe(craftRecipe);
}
 
Example 2
Source File: CraftsManager.java    From UhcCore with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static void registerGoldenHeadCraft(){
	ItemStack goldenHead = UhcItems.createGoldenHead();
	ShapedRecipe headRecipe = VersionUtils.getVersionUtils().createShapedRecipe(goldenHead, "golden_head");

	headRecipe.shape("GGG", "GHG", "GGG");

	Material material = UniversalMaterial.PLAYER_HEAD.getType();
	MaterialData data = UniversalMaterial.PLAYER_HEAD.getStack().getData();

	headRecipe.setIngredient('G', Material.GOLD_INGOT);

	if (data != null && data.getItemType() == material) {
		headRecipe.setIngredient('H', data);
	}else {
		headRecipe.setIngredient('H', material);
	}

	Bukkit.getServer().addRecipe(headRecipe);
}
 
Example 3
Source File: ItemService.java    From Transport-Pipes with MIT License 5 votes vote down vote up
public ShapedRecipe createShapedRecipe(TransportPipes transportPipes, String recipeKey, ItemStack resultItem, String[] shape, Object... ingredientMap) {
    ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(transportPipes, recipeKey), resultItem);
    recipe.shape(shape);
    for (int i = 0; i < ingredientMap.length; i += 2) {
        char c = (char) ingredientMap[i];
        if (ingredientMap[i + 1] instanceof Material) {
            recipe.setIngredient(c, (Material) ingredientMap[i + 1]);
        } else {
            recipe.setIngredient(c, new RecipeChoice.MaterialChoice(((Collection<Material>) ingredientMap[i + 1]).stream().collect(Collectors.toList())));
        }
    }
    return recipe;
}
 
Example 4
Source File: UpsideDownCraftsListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
private ShapedRecipe getUpsideDownRecipeFor(ShapedRecipe recipe){
    ShapedRecipe upsideDown = VersionUtils.getVersionUtils().createShapedRecipe(recipe.getResult(), UUID.randomUUID().toString());
    upsideDown.shape(getUpsideDownShape(recipe.getShape()));

    Map<Character, RecipeChoice> recipeChoiceMap = recipe.getChoiceMap();
    for (char c : recipeChoiceMap.keySet()){
        upsideDown.setIngredient(c, recipeChoiceMap.get(c));
    }

    return upsideDown;
}
 
Example 5
Source File: RandomizedCraftsListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
private ShapedRecipe cloneRecipeWithResult(ShapedRecipe recipe, ItemStack result){
    ShapedRecipe clone = VersionUtils.getVersionUtils().createShapedRecipe(result, UUID.randomUUID().toString());
    clone.shape(recipe.getShape());

    Map<Character, RecipeChoice> recipeChoiceMap = recipe.getChoiceMap();
    for (char c : recipeChoiceMap.keySet()){
        clone.setIngredient(c, recipeChoiceMap.get(c));
    }

    return clone;
}
 
Example 6
Source File: CraftingModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
public Recipe parseShapedRecipe(MapFactory factory, Element elRecipe)
    throws InvalidXMLException {
  ShapedRecipe recipe = new ShapedRecipe(parseRecipeResult(factory, elRecipe));

  Element elShape = XMLUtils.getRequiredUniqueChild(elRecipe, "shape");
  List<String> rows = new ArrayList<>(3);

  for (Element elRow : elShape.getChildren("row")) {
    String row = elRow.getTextNormalize();

    if (rows.size() >= 3) {
      throw new InvalidXMLException(
          "Shape must have no more than 3 rows (" + row + ")", elShape);
    }

    if (rows.isEmpty()) {
      if (row.length() > 3) {
        throw new InvalidXMLException(
            "Shape must have no more than 3 columns (" + row + ")", elShape);
      }
    } else if (row.length() != rows.get(0).length()) {
      throw new InvalidXMLException("All rows must be the same width", elShape);
    }

    rows.add(row);
  }

  if (rows.isEmpty()) {
    throw new InvalidXMLException("Shape must have at least one row", elShape);
  }

  recipe.shape(rows.toArray(new String[rows.size()]));
  Set<Character> keys =
      recipe
          .getIngredientMap()
          .keySet(); // All shape symbols are present and mapped to null at this point

  for (Element elIngredient : elRecipe.getChildren("ingredient")) {
    SingleMaterialMatcher item = XMLUtils.parseMaterialPattern(elIngredient);
    Attribute attrSymbol = XMLUtils.getRequiredAttribute(elIngredient, "symbol");
    String symbol = attrSymbol.getValue();

    if (symbol.length() != 1) {
      throw new InvalidXMLException(
          "Ingredient key must be a single character from the recipe shape", attrSymbol);
    }

    char key = symbol.charAt(0);
    if (!keys.contains(key)) {
      throw new InvalidXMLException(
          "Ingredient key '" + key + "' does not appear in the recipe shape", attrSymbol);
    }

    if (item.dataMatters()) {
      recipe.setIngredient(key, item.getMaterialData());
    } else {
      recipe.setIngredient(key, item.getMaterial());
    }
  }

  if (recipe.getIngredientMap().isEmpty()) {
    throw new InvalidXMLException(
        "Crafting recipe must have at least one ingredient", elRecipe);
  }

  return recipe;
}
 
Example 7
Source File: Modifier.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
protected void registerCraftingRecipe() {
	if (!hasRecipe()) {
		return;
	}

	FileConfiguration config = getConfig();
	try {
		NamespacedKey nkey = new NamespacedKey(MineTinker.getPlugin(), "Modifier_" + getKey());
		ShapedRecipe newRecipe = new ShapedRecipe(nkey, this.getModItem()); //reload recipe
		String top = config.getString("Recipe.Top");
		String middle = config.getString("Recipe.Middle");
		String bottom = config.getString("Recipe.Bottom");
		ConfigurationSection materials = config.getConfigurationSection("Recipe.Materials");

		newRecipe.shape(top, middle, bottom); //makes recipe

		if (materials != null) {
			for (String key : materials.getKeys(false)) {
				String materialName = materials.getString(key);

				if (materialName == null) {
					ChatWriter.logInfo(LanguageManager.getString("Modifier.MaterialEntryNotFound"));
					return;
				}

				Material material = Material.getMaterial(materialName);

				if (material == null) {
					ChatWriter.log(false, "Material [" + materialName + "] is null for mod [" + this.name + "]");
					return;
				} else {
					newRecipe.setIngredient(key.charAt(0), material);
				}
			}
		} else {
			ChatWriter.logError("Could not register recipe for the " + this.name + "-Modifier!"); //executes if the recipe could not initialize
			ChatWriter.logError("Cause: Malformed recipe config.");

			return;
		}

		MineTinker.getPlugin().getServer().addRecipe(newRecipe); //adds recipe
		ChatWriter.log(false, "Registered recipe for the " + this.name + "-Modifier!");
		ModManager.instance().recipe_Namespaces.add(nkey);
	} catch (Exception e) {
		ChatWriter.logError("Could not register recipe for the " + this.name + "-Modifier!"); //executes if the recipe could not initialize
	}
}
 
Example 8
Source File: CraftingModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public Recipe parseShapedRecipe(MapModuleContext context, Element elRecipe) throws InvalidXMLException {
    ShapedRecipe recipe = new ShapedRecipe(parseRecipeResult(context, elRecipe));

    Element elShape = XMLUtils.getRequiredUniqueChild(elRecipe, "shape");
    List<String> rows = new ArrayList<>(3);

    for(Element elRow : elShape.getChildren("row")) {
        String row = elRow.getTextNormalize();

        if(rows.size() >= 3) {
            throw new InvalidXMLException("Shape must have no more than 3 rows (" + row + ")", elShape);
        }

        if(rows.isEmpty()) {
            if(row.length() > 3) {
                throw new InvalidXMLException("Shape must have no more than 3 columns (" + row + ")", elShape);
            }
        } else if(row.length() != rows.get(0).length()) {
            throw new InvalidXMLException("All rows must be the same width", elShape);
        }

        rows.add(row);
    }

    if(rows.isEmpty()) {
        throw new InvalidXMLException("Shape must have at least one row", elShape);
    }

    recipe.shape(rows.toArray(new String[rows.size()]));
    Set<Character> keys = recipe.getIngredientMap().keySet(); // All shape symbols are present and mapped to null at this point

    for(Element elIngredient : elRecipe.getChildren("ingredient")) {
        MaterialPattern item = XMLUtils.parseMaterialPattern(elIngredient);
        Attribute attrSymbol = XMLUtils.getRequiredAttribute(elIngredient, "symbol");
        String symbol = attrSymbol.getValue();

        if(symbol.length() != 1) {
            throw new InvalidXMLException("Ingredient key must be a single character from the recipe shape", attrSymbol);
        }

        char key = symbol.charAt(0);
        if(!keys.contains(key)) {
            throw new InvalidXMLException("Ingredient key '" + key + "' does not appear in the recipe shape", attrSymbol);
        }

        if(item.dataMatters()) {
            recipe.setIngredient(key, item.getMaterialData());
        } else {
            recipe.setIngredient(key, item.getMaterial());
        }
    }

    if(recipe.getIngredientMap().isEmpty()) {
        throw new InvalidXMLException("Crafting recipe must have at least one ingredient", elRecipe);
    }

    return recipe;
}