2014-01-08 114 views
1

這是我在Bukkit中新庫存的代碼。在bukkit中創建自定義庫存

package com; 

import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftInventoryCustom; 
import org.bukkit.inventory.*; 

public class Server_Doc extends CraftInventoryCustom implements CraftingInventory, Inventory { 
    InventoryHolder IH; 

public Server_Doc(InventoryHolder owner, int size) { 
    super(owner, size); 
    ItemStack items = new ItemStack(278); 
    ((Inventory) owner).addItem(items); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public ItemStack[] getMatrix() { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public Recipe getRecipe() { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public ItemStack getResult() { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void setMatrix(ItemStack[] contents) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void setResult(ItemStack newResult) { 
    // TODO Auto-generated method stub 

} 
//Inventory inv = Server_Doc(IH,8); 
} 

我怎樣才能打開庫存,一旦創建?

回答

7

如果你想開一個3x3的手工藝表一球員,你可以簡單地撥打player.openWorkbench()。不過,創建一個自定義的GUI菜單有點難度。例如,使用

public Inventory inv; 

public void openGUI(Player p){ 
    //format: null, size of inventory (must be divisible by 9), "GUI name" 
    inv = Bukkit.createInventory(null, 9, "GUI Name"); 
    inv.setItem(0, new ItemStack(Material.DIAMOND); 
    p.openInventory(inv); 
} 

將打開一個1x9的廣告資源,其中包含第一個廣告位中的鑽石。如果你想添加更多的項目,你可以使用

inv.setItem(space, ItemStack); 

但要記住,從0開始計數,所以必須用來獲取插槽,並必須用來獲取插槽。

要使用上面的代碼打開界面,只需撥打openGUI(player),這裏的球員是要打開它的球員。

如果你想做些什麼,當玩家點擊某個項目,例如讓我們說,我們在上面的插槽0(插槽1)創建的鑽石,你只能這樣做

@EventHandler //MAKE SURE YOU HAVE THIS 
public void InventoryClick(InventoryClickEvent e){ 
    Player p = (Player) e.getWhoClicked(); 

    if(e.getInventory().getTitle().contains("put the name of the GUI here (CAsE SEnsITivE)")){ 
     //Cancel the event so they can't take items out of the GUI 
     e.setCancelled(true); 

     if(e.getCurrentItem() == null){ 
      return; 
     } 
     //gets called when the current item's type (The item the player clicked) is a diamond 
     else if(e.getCurrentItem().getType() == Material.DIAMOND){ 
      //send the player a message when they click it 
      p.sendMessage("You clicked the diamond!"); 

      //call this if you want to close the inventory when they click it 
      p.closeInventory(); 
     } 
    } 
} 

現在你必須登記在主文件事件在onEnable()像這樣

public void onEnable(){ 
    //if the code above is in your main file, use this: 
    this.getServer().getPluginManager().registerEvents(this, this); 

    //if it's in another class, use this: 
    this.getServer().getPluginManager().registerEvents(new myClassNameHere(), this); 
} 

接下來,只要有你inventoryClick方法的類在它實現Listener

public class myClassNameHere implements Listener{ 

現在你有一個全功能的圖形用戶界面,當你調用openGUI(player)球員是要打開的圖形用戶界面,它會打開一個GUI是1X9的球員,有在插槽0金剛石(插槽1)當點擊消息時,玩家「你點了鑽石!」祝你好運!

+0

它確實打開一個GUI,但各具特色貴!..請幫助 –

+0

@ user3171380在這種情況下,你需要的唯一代碼是'p.openWorkbench' – Jojodmo