客户端附属开发教程
首先假设读者已具有Bukkit插件开发的基础知识。
本教程以客户端附属例子为基础对天狼星api进行基本的介绍。
客户端附属简介:天狼星客户端附属会直接运行在玩家的电脑上,纯调用天狼星api的客户端附属可以直接在各个客户端版本中直接使用,无需移植。
一 插件基类
所有使用天狼星插件系统的软件必须继承SiriusPlugin,并且实现onLoad,onEnable,onDisable这三个方法。
- public class TestPlugin extends SiriusPlugin
复制代码- @Override
- public void onLoad() {
- getLogger().info("onLoad");
- }
- @Override
- public void onEnable() {
- getLogger().info("onEnable");
- logger = CommonServices.getLogger().getModuleLogger("SiriusPluginExample");//获取Logger
- CommonServices.getEventBusManager().getDefaultEventBus().register(getPluginInfo().getName(), this);//注册事件监听器
- channel = ClientServices.getNetworkManager().getChannel(domain, name);
- getLogger().info("注册事件监听器完成");
- }
- @Override
- public void onDisable() {
- getLogger().info("onDisable");
- getLogger().info("on %s %s", "onDisable", "onDisable");
- }
复制代码 |
二.事件
本例子将在客户端加入服务器后向服务器发送一条为“你好天狼星”的消息
天狼星内建独立的事件系统,其与Bukkit的事件有9成相似,使用事件系统的步骤为
1.获取事件总线
- val eventbus=CommonServices.getEventBusManager().getDefaultEventBus();//获取默认事件总线
复制代码 2.注册事件监听器
- eventbus.register(getPluginInfo().getName(), this);
复制代码 3.监听想要监听的事件,并且做一些想做的事
- @EventHandler
- public void onJoinServer(JoinServerEvent event) {
- CommonServices.getScheduler().addTaskLater(() -> {
- String str = new String("你好天狼星");
- val bytes = str.getBytes(StandardCharsets.UTF_8);
- channel.send(bytes, 0, bytes.length);
- }, 60);
- }//当客户端加入服务器,向服务器发送你好天狼星的消息
复制代码 |
三.ImGui库的使用
本例子将拦截背包打开事件,并且打开一个自定义的界面
1.监听Gui打开事件
- @EventHandler
- public void onVanillaGuiOpenEvent(VanillaGuiOpenEvent vanillaGuiOpenEvent)
复制代码 2.检查打开的Gui是否为背包
- if (vanillaGuiOpenEvent.getType() == GuiType.INVENTORY)
复制代码 3.阻止原版的打开事件
- vanillaGuiOpenEvent.setCanceled(true);
复制代码 4.在2个时间单位后打开一个Sceen
- CommonServices.getScheduler().addTaskLater(() -> {
- val imGuiAPI = ImGuiServices.getImGuiAPI();//获取API
- imGuiAPI.openScreen("sirius", "plugin example", new ImGuiRender() {//打开Screen
- float value = 0.0f;//进度值
- {
- new Timer().scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- value = value > 100.0f ? 0 : value + 1.0f;
- }
- }, 100, 100);
- }
- @Override
- public void render(ImGuiRenderEntry imGuiRenderEntry) {
- val resource = ClientVanillaServices.getVanillaResourceManager().getResource("sirius", "textures/gui/sirius.png").get();//获取MC资源管理器的资源
- val textureUtil = ClientServices.getTextureUtil();
- val texture = textureUtil.loadTexture(resource.getInputStream()).get();//载入纹理
- val textureUVMap = textureUtil.newTextureUVMap(texture, 0, 0, texture.getSize().x, texture.getSize().y);//新建贴图
- imGuiRenderEntry.begin(new Consumer<ImGui>() {
- @Override
- public void accept(ImGui imGui) {
- imGui.text(100, 200, "你好天狼星");//画一串字
- imGui.image(400, 500, 200, 200, textureUVMap);//画图片
- //画4个不同方向的进度条
- imGui.progressBar(200, 200, 100, 50, value, 0xff882288, ImDirection.LEFT_TO_RIGHT);
- imGui.progressBar(300, 200, 100, 50, value, 0xff882288, ImDirection.RIGHT_TO_LEFT);
- imGui.progressBar(400, 200, 100, 50, value, 0xff882288, ImDirection.TOP_TO_BOTTOM);
- imGui.progressBar(500, 200, 100, 50, value, 0xff882288, ImDirection.BOTTOM_TO_TOP);
- }
- });
- }
- @Override
- public void onClose() {
- logger.info("自定义Inventory关闭");
- }
- });
- }, 2);
复制代码 |
四 天狼星多媒体库
五 与服务端通信
1.注册通道
- channel = ClientServices.getNetworkManager().getChannel(domain, name);
复制代码 2.发送数据
向服务器发送”你好天狼星“
- String str = new String("你好天狼星");
- val bytes = str.getBytes(StandardCharsets.UTF_8);
- channel.send(bytes, 0, bytes.length);
复制代码 3.接收数据
接收服务器发来的消息并且通过Logger输出
- channel.registerListener((bytes) -> {
- String recv = new String(bytes, StandardCharsets.UTF_8);
- getLogger().info("接收到服务端消息:%s%n", recv);
- });
复制代码 |
完整代码- package com.lonacloud.sirius.plugin.test;
- import com.lonacloud.sirius.client.api.ClientServices;
- import com.lonacloud.sirius.client.api.imgui.*;
- import com.lonacloud.sirius.client.api.vanilla.ClientVanillaServices;
- import com.lonacloud.sirius.client.api.vanilla.event.JoinServerEvent;
- import com.lonacloud.sirius.client.api.vanilla.event.gui.GuiType;
- import com.lonacloud.sirius.client.api.vanilla.event.gui.VanillaGuiOpenEvent;
- import com.lonacloud.sirius.common.api.CommonServices;
- import com.lonacloud.sirius.common.api.eventbus.EventHandler;
- import com.lonacloud.sirius.common.api.log.Logger;
- import com.lonacloud.sirius.common.api.network.Channel;
- import com.lonacloud.sirius.common.api.plugin.SiriusPlugin;
- import lombok.val;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.function.Consumer;
- /**
- * @author lona, [email protected]
- * @version 1.0
- **/
- public class TestPlugin extends SiriusPlugin {
- private static final String domain = "ExamplePlugin";
- private static final String name = "main";
- private Logger logger;
- private Channel channel;
- @Override
- public void onLoad() {
- getLogger().info("onLoad");
- }
- @Override
- public void onEnable() {
- getLogger().info("onEnable");
- logger = CommonServices.getLogger().getModuleLogger("SiriusPluginExample");//获取Logger
- CommonServices.getEventBusManager().getDefaultEventBus().register(getPluginInfo().getName(), this);//注册事件监听器
- channel = ClientServices.getNetworkManager().getChannel(domain, name);
- getLogger().info("注册事件监听器完成");
- }
- @EventHandler
- public void onJoinServer(JoinServerEvent event) {
- CommonServices.getScheduler().addTaskLater(() -> {
- String str = new String("你好天狼星");
- val bytes = str.getBytes(StandardCharsets.UTF_8);
- channel.send(bytes, 0, bytes.length);
- }, 60);
- }
- @EventHandler
- public void onVanillaGuiOpenEvent(VanillaGuiOpenEvent vanillaGuiOpenEvent) throws IOException {
- if (vanillaGuiOpenEvent.getType() == GuiType.INVENTORY) {//检测游戏内是否背包被打开
- vanillaGuiOpenEvent.setCanceled(true);//阻止原版背包打开
- CommonServices.getScheduler().addTaskLater(() -> {
- val imGuiAPI = ImGuiServices.getImGuiAPI();//获取API
- imGuiAPI.openScreen("sirius", "plugin example", new ImGuiRender() {//打开Screen
- float value = 0.0f;//进度值
- {
- new Timer().scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- value = value > 100.0f ? 0 : value + 1.0f;
- }
- }, 100, 100);
- }
- @Override
- public void render(ImGuiRenderEntry imGuiRenderEntry) {
- val resource = ClientVanillaServices.getVanillaResourceManager().getResource("sirius", "textures/gui/sirius.png").get();//获取MC资源管理器的资源
- val textureUtil = ClientServices.getTextureUtil();
- val texture = textureUtil.loadTexture(resource.getInputStream()).get();//载入纹理
- val textureUVMap = textureUtil.newTextureUVMap(texture, 0, 0, texture.getSize().x, texture.getSize().y);//新建贴图
- imGuiRenderEntry.begin(new Consumer<ImGui>() {
- @Override
- public void accept(ImGui imGui) {
- imGui.text(100, 200, "你好天狼星");//画一串字
- imGui.image(400, 500, 200, 200, textureUVMap);//画图片
- //画4个不同方向的进度条
- imGui.progressBar(200, 200, 100, 50, value, 0xff882288, ImDirection.LEFT_TO_RIGHT);
- imGui.progressBar(300, 200, 100, 50, value, 0xff882288, ImDirection.RIGHT_TO_LEFT);
- imGui.progressBar(400, 200, 100, 50, value, 0xff882288, ImDirection.TOP_TO_BOTTOM);
- imGui.progressBar(500, 200, 100, 50, value, 0xff882288, ImDirection.BOTTOM_TO_TOP);
- }
- });
- }
- @Override
- public void onClose() {
- logger.info("自定义Inventory关闭");
- }
- });
- }, 2);
- //以下被注释的部分为另一个名为Widgets的UI库
- // val container = new SFixed();
- // val resource = ClientVanillaServices.getVanillaResourceManager().getResource("sirius", "textures/gui/sirius.png").get();
- // try (val inputStream = resource.getInputStream()) {
- // BufferedImage image = ImageIO.read(inputStream);
- // val texture = ClientServices.getTextureUtil().loadTexture(image);
- // val textureUtil = ClientServices.getTextureUtil();
- // val tuvMap = textureUtil.newTextureUVMap(texture, 0, 0, texture.getSize().x, texture.getSize().y);
- // val sImage = new SImage(new SVec2f(384, 216), tuvMap);
- // val videoResource = ClientVanillaServices.getVanillaResourceManager().getResource("sirius", "video/test.flv");
- // val videoAttach = new VideoAttach(sImage.getImage().getTexture(), videoResource.get());
- // CommonServices.getScheduler().addTaskAsync(videoAttach::attachVideoBuffer);
- // val sLabel = new SLabel("你好天狼星", 9999, 0xffffffff, 18, "dk");
- // container.addComponent(new SLabel("天狼星Label", 9999, 0xffffffff, 18, "dk"), new SVec2f(300, 200));
- // container.addComponent(new SPushButton(sImage, sLabel), new SVec2f(600, 300));
- // container.addComponent(new SHoverText(new SVec2f(200, 400), Arrays.asList(new String[]{"你好", "hovertext"})), new SVec2f(200, 300));
- // container.setCloseCallback(() -> videoAttach.setClosed(true));
- // }
- // vanillaGuiOpenEvent.setGui(new SWidgetGui() {
- // private Container<?> iContainer = container;
- //
- // @Override
- // public Container<?> getContainer() {
- // return iContainer;
- // }
- //
- // @Override
- // public void setContainer(Container<?> container) {
- // this.iContainer = container;
- // }
- //
- // @Override
- // public void draw() {
- // try {
- // IContainerRendererManager containerRendererManager = SWidgetServices.getContainerRendererManager();
- // containerRendererManager.render(this.iContainer, 0.0F, 0.0F);
- // } catch (Exception var2) {
- // }
- // }
- // });
- }
- }
- @Override
- public void onDisable() {
- getLogger().info("onDisable");
- getLogger().info("on %s %s", "onDisable", "onDisable");
- }
- }
复制代码 |
|
|
|