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



×
×
  • Create New...

Important Information

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