The*_*ood 1 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);
}
Run Code Online (Sandbox Code Playgroud)
一旦创建,我怎样才能打开库存?
如果您想为玩家打开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);
}
Run Code Online (Sandbox Code Playgroud)
将打开1x9库存,在第一个插槽中包含一颗钻石.如果您想添加更多项目,可以使用
inv.setItem(space, ItemStack);
Run Code Online (Sandbox Code Playgroud)
但请记住,计数从0开始,因此必须使用0来获取插槽1,并且必须使用1来获取插槽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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在你只需要注册在你的主文件的事件在你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);
}
Run Code Online (Sandbox Code Playgroud)
然后只需要inventoryClick在其中实现具有您的方法的类Listener
public class myClassNameHere implements Listener{
Run Code Online (Sandbox Code Playgroud)
现在你有一个功能齐全的GUI,当你打电话给openGUI(player)玩家想要打开GUI的玩家时,它将打开一个1x9的GUI,在0号插槽(插槽1)中有一个菱形,当点击消息播放器时"你点了钻石!" 祝好运!
| 归档时间: |
|
| 查看次数: |
16916 次 |
| 最近记录: |