Jump to content

Recommended Posts

Posted

Subscribe to ClientTickEvent (be sure to check event.phase otherwise your code runs twice per tick). In the class with your event handler add a boolean field. Every tick you check if your KeyBinding is not pressed (getIsKeyPressed is false) and the boolean is true, if that is the case your key was released. After that set the boolean field to the current value of getIsKeyPressed.

 

I probably should have made this clear, but I need to know when the key is actually let go of, not just all the time it's not held down. Or maybe I did make this clear, and you've answered that?

 

Sorry for the stupid questions.

I have no idea what I'm doing.

Posted

You aren't paying attention to what he told you.

 

boolean last_value

 

if (last_value && !pressed) {do stuff}

last_value = pressed

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

You aren't paying attention to what he told you.

 

boolean last_value

 

if (last_value && !pressed) {do stuff}

last_value = pressed

 

I'll admit I did very quickly skim through the answer in between doing something else. Thanks.

I have no idea what I'm doing.

Posted

For some reason keyReleased is not set to true unless I release the key, press it and release it again. Here is the entire class:

 

public class TickEventHandler
{
public static boolean keyReleased = false;

@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event)
{
	if (event.phase == TickEvent.Phase.START)
	{
		if (keyReleased && !HotbarBag.bagHUD.getIsKeyPressed()) this.keyReleased = true;
		keyReleased = HotbarBag.bagHUD.getIsKeyPressed();
		System.out.println(this.keyReleased);
	}
}
}

 

This is also causing the HUD to only render for one frame. Here's the check for that:

if (TickEventHandler.keyReleased && !minecraft.inGameHasFocus) minecraft.setIngameFocus();

 

Sorry if I'm making really stupid basic Java mistakes, but it's the first week back at school and I'm tired  :-\

I have no idea what I'm doing.

Posted

Tell me, when key is not pressed, does it mean that it is released? Startig boolean (keyReleased) should be true.

1.7.10 is no longer supported by forge, you are on your own.

Posted

Tell me, when key is not pressed, does it mean that it is released? Startig boolean (keyReleased) should be true.

Yes, it renders for only one frame because that's what "release" means. There is just one frame of time where they has been released. Or do you actually mean "not pressed"? Then just invert the "pressed" boolean you get from the KeyBinding.

 

I want it to be true as the key is let go of, so as it is released (not true for as long as the key is not pressed, otherwise I could have just used !keyIsPressed() couldn't I?). The problem is it is only set to true if you release it twice for some reason.

I have no idea what I'm doing.

Posted

Your logic is flawed. It should be:

 

if (pressedLastTick && !binding.isPressed()) {
   // do whatever you wish
}

pressedLastTick = binding.isPressed();

 

Do not assign anything else to pressedLastTick inside the if-statement.

 

Using .getIsKeyPressed() I got the same result (HUD renders for 1 tick and game remains unfocussed first button press, second button press acts as expected). I then tried .isPressed() and now the HUD only renders for 1 tick and the game never re-focusses.

 

Here's my code, just in case I've done something stupid:

private boolean pressedLastTick = false;

@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event)
{
	if (event.phase == TickEvent.Phase.START)
	{
		if (pressedLastTick && !HotbarBag.bagHUD.isPressed())
		{
			Minecraft.getMinecraft().setIngameFocus();
		}
		pressedLastTick = HotbarBag.bagHUD.getIsKeyPressed();
	}
}

I have no idea what I'm doing.

Posted

Then I honestly have no idea why it needs two presses. It should work just fine.

 

Hmm, well it's not working. I'll upload the class in which the game is unfocussed and the HUD is drawn, because it must be something in there.

 

I hate logic.

 

 

public class RenderOverlayEventHandler extends Gui
{
private static final int RADIUS = 100;
private static final int ITEM_RADIUS = 85;

private static final int TOTAL_SECTORS = 20000; // Total number of small
												// sectors, essentially
												// circle definition

private Random random = new Random();
private static final Minecraft minecraft = Minecraft.getMinecraft();
private final RenderItem renderItem = new RenderItem();

private RenderBlocks renderBlocksRi = new RenderBlocks();
private boolean renderWithColor = true;
private static final ResourceLocation RES_ITEM_GLINT = new ResourceLocation("textures/misc/enchanted_item_glint.png");
// private int zLevel = 10000;

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event)
{
	Item holding;
	if (minecraft.thePlayer.getHeldItem() != null)
	{
		if (event.type == ElementType.ALL)
		{
			holding = minecraft.thePlayer.getHeldItem().getItem();
			if (holding instanceof ItemBag && HotbarBag.bagHUD.getIsKeyPressed()) //Check key down here
			{
				BagInventory inventory = new BagInventory(minecraft.thePlayer.getHeldItem());

				int items = 0;
				for(int i = 0; i < inventory.getSizeInventory(); i++)
				{
					if (inventory.getStackInSlot(i) != null) items++;
				}

				if (items != 0)
				{
					minecraft.setIngameNotInFocus();
					List<ItemStack> inventoryContentsList = new ArrayList<ItemStack>();

					for(int i = 0; i < inventory.getSizeInventory(); i++)
					{
						if (inventory.getStackInSlot(i) != null)
						{
							inventoryContentsList.add(inventory.getStackInSlot(i));
						}
					}

					ItemStack[] inventoryContents = inventoryContentsList.toArray(new ItemStack[inventoryContentsList.size()]);

					float x = event.resolution.getScaledWidth() / 2;
					float y = event.resolution.getScaledHeight() / 2 - 15;

					double multiplier = this.TOTAL_SECTORS / items;

					// Brace for ugly maths! TODO make maths look less ugly
					GL11.glPushMatrix();
					{
						GL11.glDisable(GL11.GL_TEXTURE_2D);
						GL11.glColor4f(0.5F, 0.5F, 0.5F, 0.6F);
						GL11.glBegin(GL11.GL_TRIANGLE_FAN);
						{
							GL11.glVertex2f(x, y);
							for(int i = 0; i < items; i++) // Draw circle
							{
								// GL11.glColor4f(random.nextFloat(),
								// random.nextFloat(), random.nextFloat(),
								// 0.6F); //This is not very nice to look
								// at, but nice to debug
								for(double j = 0 + (i * multiplier); j <= (this.TOTAL_SECTORS / items) + (i * multiplier); j += 1) // Draw
																																	// circle
																																	// sector
								{
									double t = 2 * Math.PI * (float) j / (float) this.TOTAL_SECTORS;
									GL11.glVertex2d(x + Math.sin(t) * this.RADIUS, y + Math.cos(t) * this.RADIUS);
								}
							}
						}
						GL11.glEnd();
						GL11.glEnable(GL11.GL_TEXTURE_2D);
					}
					GL11.glPopMatrix();

					double radiansPerSector = (2 * Math.PI) / items;
					double offset = 0;
					if (inventoryContents.length % 2 == 0) offset = radiansPerSector / 2;

					for(int i = 0; i < inventoryContents.length; i++)
					{
						double radians = offset + (radiansPerSector * i) - (Math.PI / 2);

						double itemX = this.ITEM_RADIUS * Math.cos(radians) + x - 8;
						double itemY = this.ITEM_RADIUS * Math.sin(radians) + y - 8;

						this.renderItemIntoGUI(minecraft.fontRenderer, minecraft.getTextureManager(), inventoryContents[i], itemX, itemY, true);
					}
				}
			}
		}
	}
}

 

I have no idea what I'm doing.

Posted

You need two presses because of this:

 

private boolean pressedLastTick = false;

 

Tell me, when key is not pressed, does it mean that it is released? Startig boolean (keyReleased) should be true.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

You need two presses because of this:

 

private boolean pressedLastTick = false;

 

Tell me, when key is not pressed, does it mean that it is released? Startig boolean (keyReleased) should be true.

 

Ok I have no idea when I removed that, because I remember it being there, but it's now not set to anything (just

private boolean pressedLastTick; 

). I still have to press the key twice for it to work.

I have no idea what I'm doing.

Posted

Try setting it to true and see if it changes?

 

Caused a nullpointerexception updating the inventory because no item had been select to update (an item is selected on key release). I'll try jiggling around some code to see if it will work.

 

Also, has anybody got a link to a tutorial on updating the inventory client-side (or would it be better to move the update code to a common class instead?)

 

EDIT: If I set it to true, I get exactly the same result.

I have no idea what I'm doing.

  • 2 months later...
Posted

Sorry to bump up an old thread, but I came back to this project after several months and I still can't solve the problem of having to press the button price. To re-iterate:

 

-Button press 1 renders nothing, the game remains unfocused when you let go

-Button press 2 renders correctly, the game goes back into focus (as intended) when you let go.

 

Here's the code:

 

 

@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event)
{
	if (event.phase == TickEvent.Phase.START)
	{
		if (this.pressedLastTick && !HotbarBag.bagHUD.getIsKeyPressed())
		{
			Minecraft.getMinecraft().setIngameFocus();
			HotbarBag.networkWrapper.sendToServer(new InventoryUpdate(this.renderOverlayEventHandler.getMouseSector(), this.renderOverlayEventHandler.getInventory()));
		}
		this.pressedLastTick = HotbarBag.bagHUD.getIsKeyPressed();
	}
}

 

 

 

EDIT: This seems to be due to the actual KeyBinding. Is/was this a known error (I'm on the recommended 1.7.10 src).

EDIT 2: The problem does not occur in any other keybindings, however I have no code which actually changes the state of the key.

I have no idea what I'm doing.

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

    • Reach Out To Rapid Digital: What sapp Info: +1 41 4 80 7 14 85 Email INFO: rap iddi gita lrecov ery @ exe cs. com Hello, my name is Jayson, and I’m 35 years old from the United Kingdom. My family and I recently endured an incredibly challenging experience that I wouldn’t wish on anyone. We became victims of a cryptocurrency investment fraud scheme that saw us lose a staggering $807,000 in USDT and Bitcoins. The fraudsters had created a convincing facade, and we were lured into investing, only to discover later that the platform was a complete scam. We were left devastated, not just financially, but emotionally, as we had trusted these people and believed in the legitimacy of the investment. After the initial shock wore off, we desperately searched for ways to recover the lost funds. It seemed like an impossible task, and we felt as though there was no hope. That’s when, by sheer luck, we stumbled across a post about Rapid Digital Recovery, a cryptocurrency and funds recovery organization with a proven track record in cybersecurity and fraud recovery. We decided to reach out to them, and from the first interaction, we were impressed with their professionalism and transparency. They explained the recovery process in detail and reassured us that they had the skills and expertise to track down the perpetrators and recover our funds. This gave us a renewed sense of hope, something we hadn’t felt in months. What truly stood out during our experience with Rapid Digital Recovery was their dedication to the recovery process. The team went above and beyond, using sophisticated tracking tools and cyber forensics to gather critical information. Within a matter of weeks, they had successfully located the funds and traced the scam back to the fraudsters responsible. They worked with the authorities to ensure the criminals were held accountable for their actions. To our relief, the team at Rapid Digital Recovery was able to recover every single penny we had lost. The funds were returned in full, and the sense of closure we felt was invaluable. We couldn’t have imagined such a positive outcome in the early stages of our recovery journey, and we are deeply grateful for the work they did. If you ever find yourself in a similar situation, I highly recommend contacting Rapid Digital Recovery. Their expertise, transparency, and dedication to their clients make them the go-to choice for anyone seeking to recover lost cryptocurrency or funds. They truly gave us back our financial future.  
    • This is my first time modding anything, so maybe just skill issue. I'm using Forge 54.0.12 and Temurin 21.0.5+11-LTS I wanted to create a custom keybind and to check whether it works I'd like to send a chat message. I tried using Minecraft.getInstance().player.sendSystemMessage(Component.literal("test")); but IntelliJ couldnt resolve sendSystemMessage(...). Since I saw people using it in earlier versions, I tried the same thing with 1.20.6(- 50.1.0), where it works fine, now I can't figure out if this is intentional and whether there are other options for sending chat messages. On that note, is there more documentation than https://docs.minecraftforge.net/en/1.21.x/? It seems very incomplete compared to something like the Oracle Java docs
    • Hi, i'm having this error and I wanna fix it. we try: -Reload drivers -Eliminate .minecraft -Eliminate Java -Restart launcher -Verify if minecraft is using gpu -Mods  in .minecraft is empty -Install the latest and recomended version of forge idk what i have to do, help me pls. the lastest log is: https://mclo.gs/WAMao8x  
    • Read the FAQ, Rule #2. (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/)  
  • Topics

×
×
  • Create New...

Important Information

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