分享个Minecraft 模组打开GUI的代码
#分享个Minecraft 模组打开GUI的代码
gui代码
sreen.java
package hskgzscz.client.gui;import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.Minecraft;import java.util.HashMap;import hskgzscz.world.inventory.H45Menu;import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.systems.RenderSystem;public class H45Screen extends AbstractContainerScreen<H45Menu> {private final static HashMap<String, Object> guistate = H45Menu.guistate;private final Level world;private final int x, y, z;private final Player entity;public H45Screen(H45Menu container, Inventory inventory, Component text) {super(container, inventory, text);this.world = container.world;this.x = container.x;this.y = container.y;this.z = container.z;this.entity = container.entity;this.imageWidth = 304;this.imageHeight = 178;}private static final ResourceLocation texture = new ResourceLocation("hskgzscz:textures/hsk_001.png");@Overridepublic void render(PoseStack ms, int mouseX, int mouseY, float partialTicks) {this.renderBackground(ms);super.render(ms, mouseX, mouseY, partialTicks);this.renderTooltip(ms, mouseX, mouseY);}@Overrideprotected void renderBg(PoseStack ms, float partialTicks, int gx, int gy) {RenderSystem.setShaderColor(1, 1, 1, 1);RenderSystem.enableBlend();RenderSystem.defaultBlendFunc();RenderSystem.setShaderTexture(0, texture);this.blit(ms, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight, this.imageWidth, this.imageHeight);RenderSystem.setShaderTexture(0, new ResourceLocation("hskgzscz:textures/fqsureqk3191br_qra2ajmij0aaf.png"));this.blit(ms, this.leftPos + 11, this.topPos + 40, 0, 0, 73, 59, 73, 59);RenderSystem.disableBlend();}@Overridepublic boolean keyPressed(int key, int b, int c) {if (key == 256) {this.minecraft.player.closeContainer();return true;}return super.keyPressed(key, b, c);}@Overridepublic void containerTick() {super.containerTick();}@Override \\文字信息\\protected void renderLabels(PoseStack poseStack, int mouseX, int mouseY) {this.font.draw(poseStack, "\u7248\u6743\u4FE1\u606F", 11, 10, -12829636);this.font.draw(poseStack, "\u7981\u6B62\u76D7\u7528\uFF01\uFF01\uFF01", 10, 24, -3407872);this.font.draw(poseStack, "\u7EA2\u77F3\u5757\u5DE5\u4F5C\u5BA4 \u00A9 2022 \u7248\u6743\u6240\u6709 ", 164, 157, -12829636);this.font.draw(poseStack, "\u5982\u679C\u53D1\u73B0\u76D7\u7248\u8BF7\u8054\u7CFB\u4F5C\u8005", 119, 15, -12829636);this.font.draw(poseStack, "\u4F5C\u8005\u90AE\u7BB1\uFF1A730899***@QQ.com", 119, 38, -12829636);this.font.draw(poseStack, "\u7559\u8A00\u6807\u9898\uFF1A\u6A21\u7EC4\u88AB\u76D7\u7248", 119, 55, -12829636);this.font.draw(poseStack, "\u5185\u5BB9\uFF1A\u88AB\u76D7\u6A21\u7EC4 ,\u76D7\u7248\u6A21\u7EC4\u540D\u5B57", 120, 70, -12829636);}@Overridepublic void onClose() {super.onClose();Minecraft.getInstance().keyboardHandler.setSendRepeatsToGui(false);}@Overridepublic void init() {super.init();this.minecraft.keyboardHandler.setSendRepeatsToGui(true);}
}
效果图
menu.java
package hskgzscz.world.inventory;import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.IItemHandler;import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;import java.util.function.Supplier;
import java.util.Map;
import java.util.HashMap;import hskgzscz.procedures.BQDangMingLingBeiZhiXingShiProcedure;import hskgzscz.init.HskgzsczModMenus;public class H45Menu extends AbstractContainerMenu implements Supplier<Map<Integer, Slot>> {public final static HashMap<String, Object> guistate = new HashMap<>();public final Level world;public final Player entity;public int x, y, z;private IItemHandler internal;private final Map<Integer, Slot> customSlots = new HashMap<>();private boolean bound = false;public H45Menu(int id, Inventory inv, FriendlyByteBuf extraData) {super(HskgzsczModMenus.HSK_001, id);this.entity = inv.player;this.world = inv.player.level;this.internal = new ItemStackHandler(0);BlockPos pos = null;if (extraData != null) {pos = extraData.readBlockPos();this.x = pos.getX();this.y = pos.getY();this.z = pos.getZ();}BQDangMingLingBeiZhiXingShiProcedure.execute(entity);}@Overridepublic boolean stillValid(Player player) {return true;}public Map<Integer, Slot> get() {return customSlots;}
}
打开GUI的代码
package hskgzscz.procedures;import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.CommandEvent;import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.MenuProvider;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;import javax.annotation.Nullable;import io.netty.buffer.Unpooled;import hskgzscz.world.inventory.H45Menu;@Mod.EventBusSubscriber
public class WqDangYouJianDianJiKongQiShiShiTiDeWeiZhiProcedure {@SubscribeEventpublic static void onCommand(CommandEvent event) {Entity entity = event.getParseResults().getContext().getSource().getEntity();if (entity != null) {execute(event, entity.level, entity.getX(), entity.getY(), entity.getZ(), entity);}}public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {execute(null, world, x, y, z, entity);}private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, Entity entity) {if (entity == null)return;{if (entity instanceof ServerPlayer _ent) {BlockPos _bpos = new BlockPos(x, y, z);NetworkHooks.openGui((ServerPlayer) _ent, new MenuProvider() {@Overridepublic Component getDisplayName() {return new TextComponent("你要打开的gui");}@Overridepublic AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {return new H45Menu(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(_bpos));}}, _bpos);}}}
}
需要考虑到文件路径
分享个Minecraft 模组打开GUI的代码相关推荐
- Minecraft模组开发——环境搭建
考虑到部分读者可能对环境搭建流程不熟,所以本章教程简单地过一遍环境搭建,并在项目中整合Mixin. 下载MDK 打开https://files.minecraftforge.net/net/minec ...
- 如何------使用Minecraft模组Mod开发?
这几天,土豆小子超级迷恋Minecraft的模组Mod模型开发.在看了达人迷 for KIDS 的书后,决定自己动手DIY属于自己的Mine craft. 由于,Mod组件是老外研发滴,所以我们现在不 ...
- 我的世界Minecraft模组开发(7)合成表
现在,我们的游戏中多出了许多无敌的装备,但当我们开生存试玩的时候,发现------怎么获得???? 所以我们今天就要让我们那无敌的装备可以合成! 1.首先,先建一个包: src\main\resour ...
- 【RTL8720CF】BW15模组 socke本地通信代码
RTL8720CF的WiFi蓝牙芯片,移植socket通信客户端代码. #include "FreeRTOS.h"#include "task.h"#inclu ...
- 我的世界服务器显示弹幕,我的世界直播弹幕模组
模组简介 这是一个辅助直播弹幕显示的 Minecraft 模组.只需要丢进模组文件夹.启动游戏.设定房间号即可连接直播弹幕服务器,实现在游戏内输出直播弹幕功能. 具体效果如下图所示: 目前支持 Bil ...
- 泰凌微ble mesh蓝牙模组天猫精灵学习之旅 ① 小白也痴迷,如何在 Linux环境搭建编译烧录 安信可TB02,点亮一盏LED灯;
本<泰凌微ble mesh蓝牙模组天猫精灵学习之旅>系列博客学习由非官方人员 半颗心脏 潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途.如有不对之处,请留言,本人及时更改. 1.小 ...
- QUECTEL上海移远4G通讯CAT4模组EC20CEFAG模块串口调试指南之03EC20模组基础串口指令说明
本系列博文将系统性讲解物联网模组系列的实际使用和调试指南,以移远4G模组EC20为例(其他厂家模组的调试方法大同小异),加快嵌入式软.硬件工程师对物联网模组调试和使用的上手速度. 一.准备工作 1.软 ...
- QUECTEL上海移远4G通讯CAT4模组EC20CEFAG模块串口调试指南之03【EC20模组基础串口指令说明】
QUECTEL上海移远4G通讯CAT4模组EC20CEFAG模块串口调试指南之03[EC20模组基础串口指令说明] 一.准备工作 二.硬件环境的搭建 三.基础AT指令的说明和测试 STM32F103学 ...
- 涂鸦CBU模组使用说明
大家好,我是程序员小哈. 幻彩灯带活动进行中 前几天tuya有一个幻彩灯带的活动,正好公众号的读者有兴趣,我们就建了个群,一起玩玩. 这个活动持续到6月底,感兴趣的可以随时私信我,我拉你进群,一起玩哈 ...
最新文章
- python使用matplotlib可视化线图(line plot)、自定义可视化图像的四个边框的色彩、可以分别设置矩形每一条边的色彩(change the axis color)
- 智能如何产生,这仍然是个问题
- 杨百万建议股民可以从以下几个方面进行
- 模拟退火求解TSP问题
- 使用Jedis源码生成Jedis.jar
- unittest单元测试笔记
- 小白用python处理excel文件-python处理excel文件
- Moq -.NET的Mocking库
- MySQL从查找数据库表到删除全过程
- 父与子python版本,父与子的编程之旅 与小卡特一起学Python(第3版)
- Kmeans算法实现
- matlab遥感图像分类
- 企业邮箱哪个最好用?企业邮箱哪个安全?
- 使用Google Colab运行项目
- 几个炫酷的3D旋转动态效果(附代码)
- 2022-2028年中国危化品运输行业市场深度分析及投资规模预测报告
- 运行elevation_mapping流程记录
- 计算机保存到桌面没有显示,电脑系统进不去桌面,没有图标怎么办?
- GPS北斗卫星授时服务器(NTP时钟)设计及方案应用
- Google新书:《构建安全可靠的系统》
热门文章
- 解决WEEX/phantomjs-prebuilt安装太慢 weex安装卡在phantomjs-prebuilt不动的问题
- c语言程序 电子钢琴按键,基于51单片机8按键电子琴-简易版系统设计(原理图+程序仿真+论文)...
- 物联网技术与应用【第一章测验答案】
- PTN与IPRAN承载LTE的比较
- 定类,定序,定距,定比
- Fingerprint Recognition
- MD5 base64 sha1 加密,解密
- VB如何使用计时器?
- OpenFace编译遇到的各种问题
- 【Ubuntu】ThinkPad T470p 安装Win10 Ubuntu 16.04 双系统