在bukkit中创建自定义清单

时间:2014-01-08 01:45:08

标签: java minecraft 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); 
}

创建后如何打开广告资源?

2 个答案:

答案 0 :(得分:8)

如果您想为玩家打开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开始,因此 0 必须用于获取插槽 1 ,并且 1 必须用于获取插槽的 2

要使用上述代码打开GUI,只需调用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{

现在你有一个功能齐全的GUI,当你打电话给openGUI(player)播放器作为你要打开GUI的播放器时,它会打开一个1x9的GUI,在0号插槽中有一个菱形(插槽1) ),当点击消息时播放器“你点击了钻石!”祝你好运!

答案 1 :(得分:0)

您不会将您的课程扩展为库存,而是使用此: http://jd.bukkit.org/rb/doxygen/d4/da9/interfaceorg_1_1bukkit_1_1Server.html#a509ae49c355653a3ac68c61a7b2c5194 例如:

Inventory(or something) myInventory = Bukkit.getServer().createInventory(player, size);

然后使用myInventory.open(player);