我正在尝试使用bukkit创建手工配方,我希望该配方仅接受名称为“ Better Helmet”的皮革头盔。现在我有这个:

public static ItemStack lvl2Head = new ItemStack(Material.LEATHER_HELMET, 1);
{
    //removed unnessecary information
    lvl2HeadMeta.setDisplayName("Better Helmet 2");
}
public void lvl1ArmorHead() {
    ShapedRecipe recipe = new ShapedRecipe(lvl2Head);
    recipe.shape("AAA", "ABA", "AAA");
    recipe.setIngredient('A', Material.DIAMOND);
        //want it to check it under here in place of "LEATHER_HELMET"
    recipe.setIngredient('B', Material.LEATHER_HELMET);
    this.getServer().addRecipe(recipe);
}


我有什么办法可以做到这一点?我尝试将新的ItemStack的名称替换为Material.LEATHER_HELMET,但它希望MaterialData而不是ItemStack

更新

我仍然可以使用由jojodmo创建的常规皮头盔将物品从手工桌中拉出。

主类:

public static ShapedRecipe lvl1ArmorHeadRecipe() {
    ShapedRecipe recipe = new ShapedRecipe(lvl1Head);
    recipe.shape("AAA", "ABA", "AAA");
    recipe.setIngredient('A', Material.DIAMOND);
    recipe.setIngredient('B', Material.LEATHER_HELMET);
    return recipe;
}
public void lvl1ArmorHead(){
    this.getServer().addRecipe(lvl1ArmorHeadRecipe());
}


EventHandler类:

@EventHandler
    public void craft(CraftItemEvent e){
        if(e.getInventory() instanceof CraftingInventory){
        CraftingInventory inv = (CraftingInventory) e.getInventory();
        if(inv.getSize() != 4 && e.getRecipe().equals(Main.lvl1ArmorHeadRecipe())){
            org.bukkit.inventory.ItemStack helm = inv.getMatrix()[5];
            if(helm.hasItemMeta()){
                if(helm.getItemMeta().getDisplayName().equals("Better Helmet")){
                    //done.
                } else{
                    e.setCancelled(true);
                }
            } else {
                e.setCancelled(true);
            }
        }
    }
}


注意:这适用于Bukkit 1.7.2

最佳答案

我以前做过,花了我很长时间才弄清楚该怎么做!唯一的问题是,如果您使用普通的皮革头盔,结果仍然会显示出来,但并不能让您将结果从手工制作表中取出。

这是我的操作方式:

public ShapedRecipie lvl1ArmorHeadRecipie(){
  ShapedRecipe recipe = new ShapedRecipe(lvl2Head);
  recipe.shape("AAA", "ABA", "AAA");
  recipe.setIngredient('A', Material.DIAMOND);
  recipe.setIngredient('B', Material.LEATHER_HELMET);
  return recipie;
}

public void lvl1ArmorHead(){
  this.getServer().addRecipe(lvl1ArmorHeadRecipie());
  //do everything in here normally
}


接下来,使用它。确保使该类位于implement Listener

@EventHandler
public void craft(CraftItemEvent e){
  if(e.getInventory() instanceof CraftingInventory){
    CraftingInventory inv = (CraftingInventory) e.getInventory();
    if(inv.getSize() != 4 && e.getRecipe().equals(lvl1ArmorHelmetRecipe())){
      ItemStack helm = inv.getMatrix()[4];//get the middle item in the bench, which is the helmet
      if(helm.hasItemMeta()){//make sure the helmet has item meta
        if(helm.getItemMeta().getDisplayName().equals("Better Helmet")){//make sure the helmet's display name is 'Better Helmet'
          //you're done! It works! Do something like tell the player "you have crafted better armor" or something here.
          return;
        }
      }
      //the return; above would have been called if the crafting had succeeded. When it got called, the remainder of this method would not run (this part will not be run if the crafting has succeeded)
      //send the player a message to make it more realistic here. For my wizardry server I use: 'One of thee items used was incorrect, and unbalanced the energy. The death block hath been destroyed'
      e.setCanceled(true);
      e.setResult(new ItemStack(Material.AIR));
    }
  }
}

关于java - 带有特殊命名项目的异型食谱?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24103281/

10-09 22:36