Java 在bukkit中创建自定义库存
这是我在Java 在bukkit中创建自定义库存,java,minecraft,bukkit,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
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);
}
创建库存后,我如何打开库存?您不将类扩展为库存,而是使用以下内容: 例如:
然后使用
myInventory.open(播放器)代码>您不将类扩展为库存,而是使用以下内容:
例如:
然后使用myInventory.open(播放器)
如果您想为玩家打开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)
,其中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);
}
然后只需使包含清单Click
方法的类实现侦听器
public class myClassNameHere implements Listener{
现在您有了一个功能齐全的GUI,当您调用openGUI(player)
player作为您要打开GUI的播放器时,它将打开一个1x9的GUI,在插槽0(插槽1)中有一个菱形,单击时会向播放器发送消息“您单击了菱形!”祝您好运 如果您想为玩家打开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)
,其中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);
}
然后只需使包含清单Click
方法的类实现侦听器
public class myClassNameHere implements Listener{
现在您有了一个功能齐全的GUI,当您调用openGUI(player)
player作为您要打开GUI的播放器时,它将打开一个1x9的GUI,在插槽0(插槽1)中有一个菱形,单击时会向播放器发送消息“您单击了菱形!”祝您好运 它确实打开了一个gui,但是正在制作的gui!。。请help@user3171380在这种情况下,您需要的唯一代码是p.openWorkbench
它确实打开了gui,但却打开了手工制作的gui!。。请help@user3171380在这种情况下,您只需要p.openWorkbench