Jump to content

Starting GUI on key Input


Jeldrik

Recommended Posts

  

Hey,

i am new to minecraft modding and to this forum and have to get used to a lot of things.

I was trying to make a custom GUI that is opened when I press a key.

In my ClientEvents file I want to read the Key input and start the GUI

@SubscribeEvent
	public void onKeyInput(final KeyboardKeyPressedEvent event) {
		if(event.getKeyCode() == 75) {
		Minecraft.getInstance().displayGuiScreen(new DiceScreen());
		}

But when i press "k", (supposed to be the Key with number 75) nothing happens.

This is my Screen class if needed:

@OnlyIn(Dist.CLIENT)
public class DiceScreen extends Screen {

	private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation(DnD.MOD_ID,
			"textures/gui/DiceScreen");

	public DiceScreen() {
		super(new TranslationTextComponent("jeldriks_dnd_mod.dice_screen"));
		this.height = 81;
		this.width = 175;
	}
	
	@Override
	public void render(MatrixStack matrixStack, int mouseX, int mouseY, final float partialTicks) {
		this.renderBackground(matrixStack);
		super.render(matrixStack, mouseX, mouseY, partialTicks);
		this.renderComponentHoverEffect(matrixStack, null, mouseX, mouseY);
		
	}

	@Override
	public void renderBackground(MatrixStack matrixStack) {
		super.renderBackground(matrixStack);
		this.font.drawString(matrixStack, "Choose your dice to roll!", 8.0f, 6.0f, 4210752); // posx posy color
		RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
		this.minecraft.getTextureManager().bindTexture(BACKGROUND_TEXTURE);
		int x = (this.width) / 2;
		int y = (this.height) / 2;
		this.blit(matrixStack, x, y, 0, 0, this.width, this.height);
	}
	
}

 

Link to comment
Share on other sites

Found an old thread about this topic and tried to apply the Code as you said. Good to know that the KeyPressedEvent only works on GUIs

The GUI Class works, i tried opening it in another way.

But I still can't get this to run:

private static Field KEYBIND_ARRAY = null;

	@SubscribeEvent(priority = EventPriority.LOWEST)
	public void onClientTick(final ClientTickEvent event) throws Exception {
		if (KEYBIND_ARRAY == null) {
			KEYBIND_ARRAY = KeyBinding.class.getDeclaredField("KEYBIND_ARRAY");
			KEYBIND_ARRAY.setAccessible(true);
		}
		if (event.phase.equals(Phase.END)) {
			@SuppressWarnings("unchecked")
			Map<String, KeyBinding> binds = (Map<String, KeyBinding>) KEYBIND_ARRAY.get(null);
			for (String bind : binds.keySet()) {
				if (binds.get(bind).isKeyDown()) {
					if (binds.get(bind).getKey()
							.getKeyCode() == (ModClientEvents.openDiceScreen.getKey().getKeyCode())) {
						Minecraft.getInstance().displayGuiScreen(new DiceScreen());
					}
					break;
				}	
			}
		}
	}

In ModClientEvents I Registered a KeyBind. It shows up in the Controls Settings.

Edited by Jeldrik
Link to comment
Share on other sites

34 minutes ago, Jeldrik said:

As I mentioned, I am new and still learning a lot.

I deleted the reflect code and have 2 simple checks. one for the eventphase, the other for the isKeyDown Method for my KeyBinding.

It still refuses to work :/


public void onClientTick(final ClientTickEvent event){
		if (event.phase.equals(Phase.END)) {
			if (ModClientEvents.openDiceScreen.isKeyDown()) {
				Minecraft.getInstance().displayGuiScreen(new DiceScreen());
			}

		}
	}

 

 

Edited by Jeldrik
Link to comment
Share on other sites

1.
@EventBusSubscriber(modid = DnD.MOD_ID, bus = Bus.MOD, value = Dist.CLIENT)
public class ModClientEvents {
	
public final static KeyBinding openDiceScreen = new KeyBinding("key.open_dice_screen", 75, "key.categories.miscellaneous");
	@SubscribeEvent
	public static void keyBindings(FMLClientSetupEvent event) {
		ClientRegistry.registerKeyBinding(openDiceScreen);
	}

}

2.
@EventBusSubscriber(modid = DnD.MOD_ID, bus = Bus.FORGE, value = Dist.CLIENT)
public class ClientEvents {
//here is my ClientTickEvent
}

 

Edited by Jeldrik
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.