本帖最后由 byxiaobai 于 2021-7-29 16:55 编辑
ToolAPI-master.zip
(388.83 KB, 下载次数: 38)
1.12及以下的源码取自ToolAPI
sources.zip
(9.77 KB, 下载次数: 16, 售价: 5 粒金粒)
复制代码复制代码复制代码复制代码复制代码
来自群组: Unknown Domain
前言
本教程致力于解决1.13更新删除Inventory.getTitle()导致的一堆问题,
并同时帮助大家更容易制作好用的GUI,而不是被各种特性烦到死
本教程代码取自PlotHotel[1.15-1.16],使用MIT协议(就是可以随便抄袭)
如果你的版本在1.12及以下,请使用这一份代码中的gui部分(com.toolapi.api.gui),工具包(toolapi.utils)

使用方法
首先,下载附件,里面有用到的源代码


API架构
GuiManager:在这里注册你的gui
Gui:一个gui的抽象类
GuiHandler:用来注册gui中的按钮

GuiSlot:用于代表gui中的一个位置
GuiListener:用来监听gui相关事件,并通知注册的gui进行处理
使用示例
这里以上面的GUI为例,介绍制作这个GUI的过程

在所有工作开始之前,使用键盘上的Ctrl+C键和Ctrl+V键将api代码导入到自己的项目中,并修改包名和import语句
在自己插件的onEnable()方法中调用GuiManager.INSTANCE.registerGuiManager(this);
- @Override
- public void onEnable() {
- plugin = this;
- GuiManager.INSTANCE.registerGuiManager(this);
- SellerGuiManager.INSTANCE.init();
- Bukkit.getLogger().info("[Seller] 插件加载完毕");
- }
首先,创建一个自己的GUI管理器(SellerGuiManager)
其中canPutSlots是能够放物品的格子,SellGui是我们自定义的GUI
SellHandler为玩家点击"点击出售"按钮是插件进行的处理
SellSlot为更新用Slot,如果你的物品可能需要在某些时候自动更新,就需要实现这个类
- public class SellerGuiManager {
- public static final SellerGuiManager INSTANCE=new SellerGuiManager();
- private SellGui sellGui;
- private int[] canPutSlots=new int[]{9,10,11,12,13,14,15,
- 19,20,21,22,23,24,25,
- 29,30,31,32,33,34,35,
- 39,40,41,42,43,44,45};
- public void init(){
- sellGui=new SellGui("§a出售菜单",canPutSlots);
- //点击出售
- ItemStack sellItem=ItemUtil.createItem(Material.ACACIA_LOG,"点击出售","QAQ");
- SellSlot sellSlot=new SellSlot(53);
- sellSlot.setSlotItem(sellItem);
- InventoryUtil.setInventoryItem(sellGui.getInventory(),sellItem,53);
- //玻璃板
- ItemStack glassPane=ItemUtil.createItem(Material.GLASS_PANE," ","");
- InventoryUtil.setInventoryItem(sellGui.getInventory(),glassPane,MathUtil.getNumberToNumber(0,8));
- InventoryUtil.setInventoryItem(sellGui.getInventory(),glassPane,9,18,27,36,45,
- 17,26,35,44);
- InventoryUtil.setInventoryItem(sellGui.getInventory(),glassPane,MathUtil.getNumberToNumber(46,52));
- sellGui.addGuiSlot(sellSlot);
- SellHandler sellHandler=new SellHandler(sellGui, GuiHandler.GuiHandlerType.SINGLE_CLICK,53);
- sellGui.addGuiHandlers(sellHandler);
- }
- public SellGui getSellGui() {
- return sellGui;
- }
- public void setSellGui(SellGui sellGui) {
- this.sellGui = sellGui;
- }
- }
SellGui
继承即可
- public class SellGui extends Gui {
- public SellGui(String title, int[] canPutSlots) {
- super(title,canPutSlots);
- }
- }
SellHandler
- public class SellHandler extends GuiHandler {
- /**
- * 实例化
- *
- * @param gui
- * @param type 处理器类型
- * @param handleSlots 需要处理的GUI slot
- */
- public SellHandler(Gui gui, GuiHandlerType type, int... handleSlots) {
- super(gui, type, handleSlots);
- }
- @Override
- public void handle(InventoryClickEvent evt) throws Exception {
- System.out.println("handle");
- }
- }
SellSlot
- /**
- * 出售物品Slot
- */
- public class SellSlot extends GuiSlot {
- public SellSlot(int inventorySlot) {
- super(inventorySlot);
- }
- @Override
- public void updateItem() {
- //System.out.println("updateItem");
- return;
- }
- }
为什么要使用这个API?
为了不在一个破Gui上消耗一大堆垃圾时间
来自群组: Unknown Domain
我只是来混帖子的,别管我
white_fele 发表于 2021-7-17 16:05
我只是来混帖子的,别管我
不能不管
有什么建议可以在帖子下回复
感谢楼主的解说和解释
看不懂很难受
那1.12以上呢

111111111111111
安全性怎么样,不会被刷物品吧
6666666666666
对于新人蛮不错的
不过我还是喜欢在holder里自己捣鼓
不过我还是喜欢在holder里自己捣鼓
谢谢楼主分享、教程
其实做Gui理解了就很简单了,照着源文件的顺序和结构就行了
看不懂啊怎么办
真的很有用!!!
脑子会了,手不会
真的好用,感谢教程贴作者!
一楼加一!!!!!!!!!
Java是世界上最好的编程语言
不错 学废了

请问有没有铁砧的相关api