java - 在 bukkit 中创建自定义 list

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

库存一旦创建,我该如何打开?

最佳答案

如果您想为玩家打开一个 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才能得到slot 1,必须用1才能得到slot 2

要使用以上代码打开 GUI,只需调用 openGUI(player),其中 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() 的 Main 文件中注册事件

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) player 作为你想要为其打开 GUI 的播放器时,它将打开一个 1x9 的 GUI,有一个菱形在插槽 0(插槽 1)中,当点击消息时玩家“你点击了钻石!”祝你好运!

关于java - 在 bukkit 中创建自定义 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985517/

相关文章:

java - 我想在两种不同类型的子 Activity 中重用父 Activity 的大部分可见部分

java - Bukkit - 为什么我的 Multimap 没有被通过?

java - 当我使用 break 时从 "for loop"获取字符串

java - 无法根据俯仰和偏航正确计算法线/方向 vector

java - 创建新的记录器

java - 使用 ObjectOutputStream 保存 HashMap

java - 必须在 Android 代码中的 Fabric.with() 异常中启动 TweetUi Kit?

java - 在 Netbeans GUI Builder 中开发现有 GUI 代码

java - JBoss 的单独登录配置?

java - 插口 | PersistantDataContainer() 表示无法解析