2015-06-27 51 views
2

有沒有人有一個想法,爲什麼這個事件不起作用?PrepareItemCraftEvent問題 - 無法傳遞事件PrepareItemCraftEvent

OnEnable代碼:

Bukkit.getServer().getPluginManager().registerEvents(this, this); 

事件代碼:

@EventHandler(priority=EventPriority.HIGH) 
public void customCrafting(PrepareItemCraftEvent e){  
    ItemStack singleCompressed = new ItemStack(Material.COBBLESTONE, 1); 
    ItemStack nineCobblestone = new ItemStack(Material.COBBLESTONE, 9); 
    ItemMeta singleCompressedMeta = singleCompressed.getItemMeta(); 
    singleCompressedMeta.setDisplayName("Compressed Cobblestone"); 
    singleCompressedMeta.setLore(Arrays.asList("x9 Cobblestone")); 
    singleCompressed.setItemMeta(singleCompressedMeta); 

    if(e.getInventory() instanceof CraftingInventory){ 
     CraftingInventory inv = (CraftingInventory) e.getInventory(); 
     ItemStack[] itm = inv.getMatrix(); 
     boolean canCraft = false; 

     if (inv.getResult().getType() == Material.COBBLESTONE) { 
      for (int i = 0; i < inv.getSize(); i++) { 
       if(itm[i].getItemMeta().getLore().toString().toLowerCase().equals(singleCompressed.getItemMeta().getLore().toString().toLowerCase())) { 
        canCraft = true; 
       } 
      } 
      if (canCraft == true) { 
       inv.setResult(nineCobblestone); 
       canCraft = false; 
      } else { 
       inv.setResult(null); 
       canCraft = false; 
      } 
     } 
    } 
} 

我有這些配方在公共無效的設立是由onEnable稱爲:

ShapedRecipe singleCompressedRecipe = new ShapedRecipe(singleCompressed); 
    singleCompressedRecipe.shape("@@@","@@@","@@@"); 
    singleCompressedRecipe.setIngredient('@', Material.COBBLESTONE); 

    ItemStack nineCobblestone = new ItemStack(Material.COBBLESTONE, 9); 
    ShapelessRecipe decompressedRecipe = new ShapelessRecipe(nineCobblestone); 
    decompressedRecipe.addIngredient(Material.COBBLESTONE); 

    Bukkit.getServer().addRecipe(singleCompressedRecipe); 
    Bukkit.getServer().addRecipe(decompressedRecipe); 

出於某種原因每當我嘗試相應地製作時,我都會在控制檯中看到這些錯誤:

10:00:34 PM CONSOLE: ERROR]: Could not pass event PrepareItemCraftEvent to  CompressedBlocks v1.0 
10:00:35 PM CONSOLE: Source) [?:1.7.0_60] 
10:00:35 PM CONSOLE: Source) [?:1.7.0_60] 
10:00:35 PM CONSOLE: Source) [?:1.7.0_60] 
10:00:35 PM CONSOLE: Method) ~[?:1.7.0_60] 
10:00:35 PM CONSOLE: Source) ~[?:1.7.0_60] 
10:00:35 PM CONSOLE: Source) ~[?:1.7.0_60] 
10:00:35 PM CONSOLE: Source) ~[?:1.7.0_60] 

就遊戲中發生的事情而言,您可以從常規和單個壓縮鵝卵石中製作9塊鵝卵石,因此不會受到事件的影響。我試圖登錄到控制檯:itm [i] .getItemMeta()。getLore()。toString()。toLowerCase()它只是等於null。

+0

您能否包含整個錯誤消息?這可以幫助很多。 – Jojodmo

+0

這實際上是給出的全部錯誤消息,100%。 – Keydose

+0

這真的很奇怪。你有沒有在服務器上使用插件加載器? – Jojodmo

回答

1

該事件最有可能拋出一個NullPointerException異常,因爲:

  1. 一位在手工藝矩陣的插槽是空的
  2. 一種在基質中的項目並沒有傳說的文本或元數據
  3. 的工藝庫存的結果欄也是空的

我會添加空檢查以確保您不使用不存在的項目或元數據。

還有一些代碼的其他部分可以改進,以使壓縮鵝卵石無縫工作。

您遇到的第一個問題是使用成形配方(將9個鵝卵石變成壓縮物品的配方)。由於配料僅使用材料進行定義,玩家可以使用9個「壓縮」鵝卵石製作另一個鵝卵石,有效去除72顆鵝卵石。爲了解決這個問題,每當所有9個工藝槽都裝滿鵝卵石時,我們必須確保它們是「簡單」的鵝卵石(沒有元數據)。

我相信你試圖用你的代碼來解決的第二個問題是玩家將規則的鵝卵石而不是壓縮的鵝卵石變成9個鵝卵石,有效地複製塊並給玩家額外的8個鵝卵石。正如你可能已經注意到的那樣,這又是因爲無形配方的成分只是一種材料而不是ItemStack。下面是一些示例代碼來解決這兩個問題(測試,似乎工作得很好):

public void onEnable() { 
    getServer().getPluginManager().registerEvents(this, this); 

    ItemStack result = new ItemStack(Material.COBBLESTONE, 1); 
    ItemMeta meta = result.getItemMeta(); 
    meta.setDisplayName("Compressed Cobblestone"); 
    meta.setLore(Arrays.asList("9x Cobblestone")); 
    result.setItemMeta(meta); 

    //This recipe creates the problem that one can turn 9 compressed blocks into only one 
    ShapedRecipe shaped = new ShapedRecipe(result); 
    shaped.shape("@@@", "@@@", "@@@"); 
    shaped.setIngredient('@', Material.COBBLESTONE); 

    //This recipe creates the problem that we can turn a regular cobblestone into 9 
    ItemStack nine = new ItemStack(Material.COBBLESTONE, 9); 
    ShapelessRecipe shapeless = new ShapelessRecipe(nine); 
    shapeless.addIngredient(Material.COBBLESTONE); 

    Bukkit.getServer().addRecipe(shaped); 
    Bukkit.getServer().addRecipe(shapeless); 
} 

@EventHandler 
public void onPrepareCraft(PrepareItemCraftEvent event) { 
    ItemStack[] slots = event.getInventory().getMatrix(); 

    //The item list will only contain real itemstacks 
    List<ItemStack> items = new ArrayList<ItemStack>(); 
    for (ItemStack slot : slots) { 
     if (slot != null && slot.getType() != Material.AIR) { //We filter out air slots and null items 
      items.add(slot); 
     } 
    } 

    boolean canCraft = false; 
    if (items.size() == 1) { //If we are dealing with a single item 
     ItemStack item = items.get(0); //Get the first item 
     if (item.getType() == Material.COBBLESTONE && item.hasItemMeta()) { //If this block is cobblestone and has metadata 
      ItemMeta meta = item.getItemMeta(); 
      //Check if the metadata contains a display name and lore 
      //If so, compare them with what they should be 
      if (meta.hasDisplayName() && meta.hasLore() && meta.getDisplayName().equals("Compressed Cobblestone") && meta.getLore().toString().equals(Arrays.asList("9x Cobblestone").toString())) { 
       canCraft = true; 
      } 
     } 
    } else if (items.size() == 9) { //If we are dealing with 9 items 
     boolean allCobblestone = true; //We need to check whether they are all cobblestone, otherwise we would interfere with other recipes that use all 9 slots 
     for (ItemStack item : items) { 
      if (item.getType() != Material.COBBLESTONE) { 
       //If we find a non-cobblestone block, exit loop and let the player craft whatever he is trying to make 
       allCobblestone = false; 
       canCraft = true; 
       break; 
      } 
     } 
     if (allCobblestone) { //If we have 9 cobblestones in the matrix make sure none of them have metadata 
      canCraft = true; 
      for (ItemStack item : items) { 
       if (item.hasItemMeta()) { 
        canCraft = false; 
        break; 
       } 
      } 
     } 
    } 

    if (!canCraft) { 
     event.getInventory().setResult(null); 
    } 
}