具有特别命名项目的形状配方?

时间:2014-06-08 05:26:55

标签: java bukkit

我试图用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

2 个答案:

答案 0 :(得分: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));
    }
  }
}

答案 1 :(得分:1)

查看PrepareItemCraftEvent,我在bukkit论坛上回答了你的问题,看看我的帖子:http://forums.bukkit.org/threads/resource-no-nms-make-custom-crafting-recipes-ingredients-obey-itemmeta-displayname-e-t-c.280482/