Java 在 bukkit 中创建自定义库存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20985517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a custom inventory in bukkit
提问by The XGood
This is my code for a new Inventory in 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);
}
How could I open the inventory, once created?
创建后如何打开库存?
采纳答案by Jojodmo
If you would like to open a 3x3 crafting table for a player, you can simply call player.openWorkbench()
. Creating a custom GUI menu is a little more difficult, though. For example, using
如果您想为玩家打开一个 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);
}
would open a 1x9 inventory, containing a diamond in the first slot. If you wanted to add more items, you could use
将打开一个 1x9 的库存,在第一个插槽中包含一个钻石。如果你想添加更多的项目,你可以使用
inv.setItem(space, ItemStack);
but remember, counting starts from 0, so 0must be used to get slot 1, and 1must be used to get slot 2.
但请记住,计数从 0 开始,因此必须使用0来获取插槽1,并且必须使用1来获取插槽2。
To open the GUI using the above code, simply call openGUI(player)
, where player is the player that you want to open it for.
要使用上述代码打开 GUI,只需调用openGUI(player)
,其中 player 是您要为其打开的播放器。
If you would like to do something when a player clicks an item, for example let's say the diamond that we created in slot 0 (Slot 1) above, you could do this
如果你想在玩家点击一个物品时做一些事情,例如假设我们在上面的插槽 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();
}
}
}
Now you only have to register events in your Main file in your onEnable()
like so
现在你只需要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);
}
then just make the class that has your inventoryClick
method in it implement Listener
然后只需使包含您的inventoryClick
方法的类实现Listener
public class myClassNameHere implements Listener{
now you have a fully functioning GUI, that when you call openGUI(player)
player being the player you want to open the GUI for, it will open a GUI that is 1x9, having a diamond in slot 0 (Slot 1), that when clicked messages the player "You clicked the diamond!" Good luck!
现在你有一个功能齐全的 GUI,当你调用openGUI(player)
玩家是你想要打开 GUI 的玩家时,它会打开一个 1x9 的 GUI,在插槽 0(插槽 1)中有一个菱形,当点击消息时播放器“你点击了钻石!” 祝你好运!
回答by fox_news
you do not extend your class as an Inventory but rather use this: http://jd.bukkit.org/rb/doxygen/d4/da9/interfaceorg_1_1bukkit_1_1Server.html#a509ae49c355653a3ac68c61a7b2c5194Example:
您没有将您的课程扩展为库存,而是使用它:http: //jd.bukkit.org/rb/doxygen/d4/da9/interfaceorg_1_1bukkit_1_1Server.html#a509ae49c355653a3ac68c61a7b2c5194示例:
Inventory(or something) myInventory = Bukkit.getServer().createInventory(player, size);
and then use myInventory.open(player);
然后使用 myInventory.open(player);