Skip to content

Commit

Permalink
finish timer task and use item event callback. update gui background.
Browse files Browse the repository at this point in the history
  • Loading branch information
YOM667 committed Dec 30, 2022
1 parent 6389567 commit 5dd1d28
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 23 deletions.
80 changes: 77 additions & 3 deletions src/main/java/top/mpt/ingotfly/ingotfly/IngotFly.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack,Long> 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",
Expand All @@ -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<ItemStack, Long> 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);
});
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
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;


/**
* @author You_M
*/
public class IngotFlyGuiScreen extends Screen {
private final Map<ItemStack,Long> 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"));
Expand All @@ -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<ItemStack,Long> entry : itemMap.entrySet()){
this.itemRenderer.renderGuiItemIcon(entry.getKey(), xPos + 10 + xOffset, yPos + 10 + yOffset);
for(Map.Entry<ItemStack,Long> 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;
}
}
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/top/mpt/ingotfly/ingotfly/TimeTask.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
20 changes: 20 additions & 0 deletions src/main/java/top/mpt/ingotfly/ingotfly/Util.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5dd1d28

Please sign in to comment.