在这个模组中,按下x键可以获得钻石
package com.example.mymod;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Items;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.FML.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent.KeyInputEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Keyboard;
@Mod(modid = "mymod", name = "My Mod", version = "1.0")
public class MyMod {
private boolean keyPressed = false;
@Mod.EventHandler
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if (keyPressed) {
Minecraft.getMinecraft().player.addItemStackToInventory(new ItemStack(Items.DIAMOND, 64));
Minecraft.getMinecraft().player.sendMessage(new TextComponentString("You have been granted 64 diamonds!"));
keyPressed = false;
}
}
@SubscribeEvent
public void onKeyPress(KeyInputEvent event) {
if (Keyboard.isKeyDown(Keyboard.KEY_X)) {
keyPressed = true;
}
}
}