基于Java的Minecraft游戏后端自定义插件 05事件监听器

事件监听器

    • 内容简介
    • 事件监听器

内容简介

基于Java的Minecraft游戏后端自定义插件 05事件监听器_第1张图片

事件监听器

基于Java的Minecraft游戏后端自定义插件 05事件监听器_第2张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第3张图片
文档中org.bukkit.event都是事件包
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第4张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第5张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第6张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第7张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第8张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第9张图片
https://www.youtube.com/watch?v=PWQNsqwD-AY.
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第10张图片
在这里插入图片描述
public void registerEvents(Listener listener, Plugin plugin) {
以上省略…
for (Map.Entry, Set> entry : plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) {
getEventListeners(getRegistrationClass((Class)entry.getKey())).registerAll((Collection)entry.getValue());
}
}

那么我们可以从上边的代码发现, BukkitAPI 先对传入的 Listener 实现类进行检查,
之后反射遍历传入的 Listener 实现类的所有方法, 之后再做些必要的事情后就注册好了一个事件了

优先级
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第11张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第12张图片
详细教程http://mineplugin.org/Event_API_Reference#EventHandler.E7.9A.84.E5.8F.82.E6.95.B0

插件中我编写的事件监听包括游戏进入游戏后的操作提示以及图鉴界面上的按钮点击操作跳转:
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第13张图片


package com.project.minecraft.event;

import FzmXer.VexFxTitle.Gui.FxTitleGui;
import FzmXer.VexFxTitle.Gui.PlayerTags;
import FzmXer.VexFxTitle.SQL.DataManager;
import FzmXer.VexFxTitle.Utils.Utils;
import com.connorlinfoot.titleapi.TitleAPI;
import com.project.minecraft.main;
import com.project.minecraft.vexview.Jiangli_Effect1;
import com.project.minecraft.vexview.Jiangli_Effect2;
import com.project.minecraft.vexview.MyGui_tujian;
import lk.vexview.api.VexViewAPI;
import lk.vexview.event.ButtonClickEvent;
import lk.vexview.event.VerificationFinishEvent;
import lk.vexview.event.gui.VexGuiOpenEvent;
import lk.vexview.gui.components.VexComponents;
import lk.vexview.hud.VexImageShow;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.List;


public class VexGuiEvent implements Listener {

    @EventHandler
    public void PlayerOpenGui(VexGuiOpenEvent event) {
        List<VexComponents> components = event.getGui().getComponents();
        for (VexComponents vex : components) {
            main.plugin.getLogger().info(vex.getType());
        }
    }
//监听图鉴界面上的按钮点击操作跳转
    @EventHandler
    public void PlayerClickButton(ButtonClickEvent event) {
        Player player = event.getPlayer();
        int buttonID = (int) event.getButtonID();

        if (buttonID == 1) {
            VexViewAPI.openGui(player, MyGui_tujian.zhiwuGui());
            player.sendMessage("§d打开植物图鉴界面§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§1Minecraft图鉴", "§2植物图鉴");
        } else if (buttonID == 2) {
            VexViewAPI.openGui(player, MyGui_tujian.shengwuGui());
            player.sendMessage("§d打开生物图鉴界面§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§1Minecraft图鉴", "§2生物图鉴");
        } else if (buttonID == 3) {
            VexViewAPI.openGui(player, MyGui_tujian.zhuangbeiGui());
            player.sendMessage("§d打开装备图鉴界面§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§1Minecraft图鉴", "§2装备图鉴");
        } else if (buttonID == 4) {
            VexViewAPI.openGui(player, MyGui_tujian.chengjiuGui());
            player.sendMessage("§d打开悬赏任务界面§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§1Minecraft图鉴", "§2悬赏任务");
        } else if (buttonID == 5) {
            VexViewAPI.openGui(player, MyGui_tujian.tujianGui());
            player.sendMessage("§d返回图鉴列表§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§1Minecraft图鉴", "§2返回");
        } else if (buttonID == 6) {
            player.closeInventory();
            player.sendMessage("§d关闭图鉴界面§e成功");
        } else if (buttonID == 7) {
            VexViewAPI.openGui(player, MyGui_tujian.jiangliGui(player));
            player.sendMessage("§d打开奖励图鉴界面§e成功");
        }else  if (buttonID == 8) {
            //装备 特效1
            Jiangli_Effect1 effect1 = new Jiangli_Effect1(player);
            effect1.startEffect();
            player.sendMessage("§d装备特效1“闪电风暴”§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2闪电风暴", "特效1");
        }else if (buttonID == 9) {
            //返回 主菜单
            VexViewAPI.openGui(player, MyGui_tujian.tujianGui());
            player.sendMessage("§d返回图鉴界面§e成功");
        }else if (buttonID == 10) {
            //装备 特效2
            Jiangli_Effect2 effect2 = new Jiangli_Effect2(player);
            effect2.startEffect();
            player.sendMessage("§d装备特效2“龙之吐息”§e成功");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2龙之吐息", "特效2");
        } else if (buttonID == 12) {
            //装备 称号1
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            TitleAPI.sendTitle(player, 5, 4, 5, "§2羿若惊羽", "称号1");
            player.sendMessage("§a称号显示界面§e已打开");
        }else if (buttonID == 13) {
           //取消装备 称号1
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }else if (buttonID == 14) {
            //装备 称号2
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            player.sendMessage("§a称号显示界面§e已打开");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2魅影无双", "称号2");
        }else if (buttonID == 15) {
            //取消装备 称号2
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }else if (buttonID == 16) {
            //装备 称号3
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            player.sendMessage("§a称号显示界面§e已打开");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2仙神蕴灵", "称号3");
        }else if (buttonID == 17) {
            //取消装备 称号3
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }else if (buttonID == 18) {
            //装备 称号4
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            player.sendMessage("§a称号显示界面§e已打开");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2傲视天下", "称号4");
        }else if (buttonID == 19) {
            //取消装备 称号4
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }else if (buttonID == 20) {
            //装备 称号5
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            player.sendMessage("§a称号显示界面§e已打开");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2御神役鬼", "称号5");
        }else if (buttonID == 21) {
            //取消装备 称号5
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }else if (buttonID == 22) {
            //装备 称号6
            DataManager.SetTitleState(player.getUniqueId().toString(), 1);
            VexViewAPI.openGui(player, new FxTitleGui(player, Utils.Scale(player)));
            PlayerTags.ResPlayerTags(player);
            player.sendMessage("§a称号显示界面§e已打开");
            TitleAPI.sendTitle(player, 5, 4, 5, "§2八荒雷动·傲视群雄", "称号6");
        }else if (buttonID == 23) {
            //取消装备 称号6
            DataManager.SetTitleState(player.getUniqueId().toString(), 0);
            PlayerTags.cencelRun(player);
            player.sendMessage("§a称号显示: 已关闭");
        }
    }

// 监听玩家进入游戏后显示操作提示

    @EventHandler
    public void PlayerJoinFinishEvent(VerificationFinishEvent event){
        Player player = event.getPlayer();
        VexImageShow vis = new VexImageShow("0","[local]欢迎.jpg",-1,-1,240,160,240,160,10);
        VexViewAPI.sendHUD(player,vis);

        int num = main.plugin.getConfig().getInt(event.getPlayer().getName());
        if(num==0){
            num++;
            main.plugin.getConfig().set(event.getPlayer().getName(),1);
            main.plugin.saveConfig();
        }else{
            num++;
            main.plugin.getConfig().set(event.getPlayer().getName(),num);
            main.plugin.saveConfig();
        }
        event.getPlayer().sendMessage("你是第"+num+"次进入游戏");

        main.plugin.getLogger().info("触发了加入服务器时间:" + player.getName());
        player.sendMessage("§4欢迎至尊vip进入游戏:§c【" + player.getName() + "】");

        player.sendMessage("§d打开主菜单具体指令操作 :§e【 /handbook  menu 】");
        player.sendMessage("§d打开图鉴界面请输入指令:§e【 /open  以查看帮助 】");
        player.sendMessage("§d图鉴界面具体指令操作 :§e【 /open  】");
        player.sendMessage("§d成就奖励具体指令操作 :§e【 /vftxi  < Order > 】");
        player.sendMessage("§d每日签到具体指令操作 :§e【 /signin  < Order > 】");
        player.sendMessage("§d成就具体指令操作 :   §e【 /chengjiu < Order> 】");

    }
}

如何实现VexView绘制的Gui界面中VexButton的点击完成相应的操作如界面跳转?
通过赋予VexView绘制的Gui界面中相同功能的VexButton以相同的ButtonID,通过ButtonClickEvent中的getButtonID();方法获取每一个按钮的ID并赋予其所需要的方法,当监听到玩家点击了某个Button时,获取其ID并调用其方法如VexViewAPI.openGui()方法以实现界面跳转或其他功能。
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第14张图片
基于Java的Minecraft游戏后端自定义插件 05事件监听器_第15张图片

你可能感兴趣的:(#)