From 5dd1d28e047a7cd2e3c49a188fbf867af4292225 Mon Sep 17 00:00:00 2001 From: You_M <1055965862@qq.com> Date: Fri, 30 Dec 2022 22:39:51 +0800 Subject: [PATCH] finish timer task and use item event callback. update gui background. --- .../top/mpt/ingotfly/ingotfly/IngotFly.java | 80 ++++++++++++++++++- .../ingotfly/{gui => }/IngotFlyGuiScreen.java | 30 +++---- .../top/mpt/ingotfly/ingotfly/TimeTask.java | 30 +++++++ .../java/top/mpt/ingotfly/ingotfly/Util.java | 20 +++++ 4 files changed, 137 insertions(+), 23 deletions(-) rename src/main/java/top/mpt/ingotfly/ingotfly/{gui => }/IngotFlyGuiScreen.java (64%) create mode 100644 src/main/java/top/mpt/ingotfly/ingotfly/TimeTask.java create mode 100644 src/main/java/top/mpt/ingotfly/ingotfly/Util.java diff --git a/src/main/java/top/mpt/ingotfly/ingotfly/IngotFly.java b/src/main/java/top/mpt/ingotfly/ingotfly/IngotFly.java index 58ee230..97a1002 100644 --- a/src/main/java/top/mpt/ingotfly/ingotfly/IngotFly.java +++ b/src/main/java/top/mpt/ingotfly/ingotfly/IngotFly.java @@ -3,17 +3,43 @@ import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.fabricmc.fabric.api.event.player.UseItemCallback; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.PlayerListEntry; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.util.Hand; +import net.minecraft.util.TypedActionResult; +import net.minecraft.world.GameMode; import org.lwjgl.glfw.GLFW; -import top.mpt.ingotfly.ingotfly.gui.IngotFlyGuiScreen; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; +import java.util.Timer; /** * @author You_M */ + public class IngotFly implements ModInitializer { + public static Logger log = LoggerFactory.getLogger(IngotFly.class); + public static final Map itemMap = new HashMap<>(){ + { + put(Items.IRON_INGOT.getDefaultStack(),3L);//铁 + put(Items.GOLD_INGOT.getDefaultStack(),5L);//金 + put(Items.COPPER_INGOT.getDefaultStack(),5L);//铜 + put(Items.EMERALD.getDefaultStack(),12L);//绿宝石 + put(Items.DIAMOND.getDefaultStack(),30L);//钻石 + put(Items.NETHERITE_INGOT.getDefaultStack(),2*60L);//下界合金 + } + }; + //按键绑定 默认绑定在H键 可以自行更改 private final KeyBinding showGUI = KeyBindingHelper.registerKeyBinding( new KeyBinding( "key.ingot_fly.gui", @@ -22,12 +48,60 @@ public class IngotFly implements ModInitializer { KeyBinding.MISC_CATEGORY ) ); + + /** + * 模组初始化方法 + */ public void onInitialize() { - IngotFlyGuiScreen ingotFlyGuiScreen = new IngotFlyGuiScreen(); + registerKeyBinding(); + registerEvent(); + } + /** + * 注册按键 方法 + */ + public void registerKeyBinding(){ + log.info("按键绑定中..."); ClientTickEvents.END_CLIENT_TICK.register(client -> { while (showGUI.wasPressed()) { - MinecraftClient.getInstance().setScreen(ingotFlyGuiScreen); + MinecraftClient.getInstance().setScreen(new IngotFlyGuiScreen()); } }); } + /** + * 注册事件 方法 + */ + public void registerEvent(){ + /* + * 玩家使用物品事件,用来检测是否使用指定的物品 + */ + log.info("事件注册中..."); + UseItemCallback.EVENT.register((player, world, hand) -> + { + PlayerListEntry playerListEntry = MinecraftClient.getInstance().getNetworkHandler().getPlayerListEntry(player.getGameProfile().getId()); + //判断是否是客户端 是否是玩家主手在使用 玩家模式是否为生存模式 + if(hand == Hand.MAIN_HAND && playerListEntry.getGameMode() == GameMode.SURVIVAL && world.isClient) { + for (Map.Entry entry : itemMap.entrySet()) { + //主手的物品是否和遍历的物品一样 + if (player.getMainHandStack().getItem().toString().equals(entry.getKey().getItem().toString())) { + //玩家是否在飞行 + if (!player.getAbilities().flying && !player.getAbilities().allowFlying) { + //将玩家正在使用的物品数量减1 + player.getMainHandStack().setCount(player.getMainHandStack().getCount() - 1); + //播放升级声音 + world.playSound(null, player.getBlockPos(), SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.MUSIC, 1f, 1f); + //允许玩家飞行 + player.getAbilities().allowFlying = true; + //计时器开始计时 + TimeTask task = new TimeTask(entry.getValue(), player); + new Timer().schedule(task, 0, 1000); + } else { + Util.sendMessage("玩家已经在飞行或可以飞行,无法再次使用道具!"); + } + } + } + } + return TypedActionResult.pass(ItemStack.EMPTY); + }); + } + } diff --git a/src/main/java/top/mpt/ingotfly/ingotfly/gui/IngotFlyGuiScreen.java b/src/main/java/top/mpt/ingotfly/ingotfly/IngotFlyGuiScreen.java similarity index 64% rename from src/main/java/top/mpt/ingotfly/ingotfly/gui/IngotFlyGuiScreen.java rename to src/main/java/top/mpt/ingotfly/ingotfly/IngotFlyGuiScreen.java index 63ba60f..32276be 100644 --- a/src/main/java/top/mpt/ingotfly/ingotfly/gui/IngotFlyGuiScreen.java +++ b/src/main/java/top/mpt/ingotfly/ingotfly/IngotFlyGuiScreen.java @@ -1,16 +1,14 @@ -package top.mpt.ingotfly.ingotfly.gui; +package top.mpt.ingotfly.ingotfly; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import java.awt.*; -import java.util.HashMap; import java.util.Map; @@ -18,17 +16,8 @@ * @author You_M */ public class IngotFlyGuiScreen extends Screen { - private final Map itemMap = new HashMap<>(){ - { - put(Items.IRON_INGOT.getDefaultStack(),3L);//铁 - put(Items.GOLD_INGOT.getDefaultStack(),5L);//金 - put(Items.COPPER_INGOT.getDefaultStack(),5L);//铜 - put(Items.EMERALD.getDefaultStack(),12L);//绿宝石 - put(Items.DIAMOND.getDefaultStack(),30L);//钻石 - put(Items.NETHERITE_INGOT.getDefaultStack(),2*60L);//下界合金 - } - }; - private static final Identifier BACKGROUND_TEXTURE = new Identifier("textures/gui/demo_background.png"); + + private static final Identifier BACKGROUND_TEXTURE = new Identifier("textures/gui/advancements/window.png"); public IngotFlyGuiScreen() { super(Text.translatable("gui.ingot_fly.title")); @@ -46,14 +35,14 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { int yPos = (this.height - 166) / 2; int xOffset = 0; int yOffset = 0; - for(Map.Entry entry : itemMap.entrySet()){ - this.itemRenderer.renderGuiItemIcon(entry.getKey(), xPos + 10 + xOffset, yPos + 10 + yOffset); + for(Map.Entry entry : IngotFly.itemMap.entrySet()){ + this.itemRenderer.renderGuiItemIcon(entry.getKey(), xPos + 15 + xOffset, yPos + 15 + yOffset); MutableText text = Text.translatable("text.ingot_fly.message").append(" ["+entry.getValue().toString()+"] ").append(Text.translatable("text.ingot_fly.time")); - this.textRenderer.draw(matrices,entry.getKey().getName(), xPos + 35 + xOffset,yPos+15+yOffset,new Color(255,0,0).getRGB()); - this.textRenderer.draw(matrices,text, xPos + 10 + xOffset,yPos+30+yOffset,new Color(-1).getRGB()); - xOffset += 135; + this.textRenderer.draw(matrices,entry.getKey().getName(), xPos + 30 + xOffset,yPos+20+yOffset,new Color(255,0,0).getRGB()); + this.textRenderer.draw(matrices,text, xPos + 15 + xOffset,yPos+ 35 +yOffset,new Color(-1).getRGB()); + xOffset += 125; if(xOffset > 190){ - yOffset += 40; + yOffset += 35; xOffset = 0; } } @@ -63,6 +52,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { @Override public void renderBackground(MatrixStack matrices) { super.renderBackground(matrices); + //渲染图片 Mojang封装好后的OpenGL图片渲染方法 RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f); RenderSystem.setShaderTexture(0, BACKGROUND_TEXTURE); int i = (this.width - 248) / 2; diff --git a/src/main/java/top/mpt/ingotfly/ingotfly/TimeTask.java b/src/main/java/top/mpt/ingotfly/ingotfly/TimeTask.java new file mode 100644 index 0000000..f2095d0 --- /dev/null +++ b/src/main/java/top/mpt/ingotfly/ingotfly/TimeTask.java @@ -0,0 +1,30 @@ +package top.mpt.ingotfly.ingotfly; + +import net.minecraft.entity.player.PlayerEntity; + +import java.util.TimerTask; + +/** + * @author You_M + */ +public class TimeTask extends TimerTask { + + private Long time; + private final PlayerEntity player; + public TimeTask(Long time, PlayerEntity player) { + this.time = time; + this.player = player; + } + + @Override + public void run() { + if(time<=3L) Util.sendMessage("距离飞行时间结束还有"+time+"秒"); + if(time == 0L) { + Util.sendMessage("飞行时间已结束"); + player.getAbilities().allowFlying = false; + player.getAbilities().flying = false; + this.cancel(); + } + time--; + } +} diff --git a/src/main/java/top/mpt/ingotfly/ingotfly/Util.java b/src/main/java/top/mpt/ingotfly/ingotfly/Util.java new file mode 100644 index 0000000..42cb7b5 --- /dev/null +++ b/src/main/java/top/mpt/ingotfly/ingotfly/Util.java @@ -0,0 +1,20 @@ +package top.mpt.ingotfly.ingotfly; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; + +import java.util.Arrays; + +/** + * @author You_M + */ +public class Util { + public static void sendMessage(Object message){ + if (MinecraftClient.getInstance().player != null) { + MinecraftClient.getInstance().player.sendMessage(Text.of(message.toString())); + } + } + public static void sendMessage(Object ...message){ + Arrays.stream(message).forEach(Util::sendMessage); + } +}