Jump to content

[SOLVED]Check if player.posX ends with .99


TheCreepySheep

Recommended Posts

Hello, I'm looking for a way to check, if player's current X(or any axis really) ends with .99 and then fire an event.

My current code:

double playerX = Math.floor(Minecraft.getMinecraft().thePlayer.posX);
        double playerY = Math.floor(Minecraft.getMinecraft().thePlayer.posY);
        double playerZ = Math.floor(Minecraft.getMinecraft().thePlayer.posZ);
        double playerYaw = Minecraft.getMinecraft().thePlayer.rotationYaw;

if (String.format("%.2f", playerX) = endsWith(.99));
        {
            LogHelper.info("true");
        }

Link to comment
Share on other sites

First:

double playerX = Math.floor(Minecraft.getMinecraft().thePlayer.posX);

The floor function maps a number to its largest preceeding integer (e.g. 29.99 -> 29.00), so you will have a value ending with .00.

 

Second:

if (coordinate > 0) {
    float v = coordinate - .99;
    if (x = (int) x) {
        // the player's positive coordinate ends with .99
    }
} else {
    float v = coordinate + .99;
    if (x = (int) x) {
        // the player's negative coordinate ends with .99
    }
}

 

Note: if the coordinates have more than two digits after the decimal point, this code will mess up. If that is the case, be sure to truncate the floats.

Link to comment
Share on other sites

Hi

 

I think your sample code will work fine with a bit of tweaking

    
double playerXfloor = Math.floor(Minecraft.getMinecraft().thePlayer.posX);
double playerXremainder = Minecraft.getMinecraft().thePlayer.posX - playerXfloor;
    if (String.format("%.2f", playerXremainder ).contains(".99"));
    {
      LogHelper.info("true");
    }

For positive numbers, this will trigger for any numbers between x.985 and x.9949999999

For negative numbers, this will trigger at numbers ending in 0.01 instead of 0.99

 

Is that what you want?

 

I'm with DieSieben though, are you sure that's really the effect you're looking for?

 

-TGG

 

 

Link to comment
Share on other sites

Then I don't think you want to check "ends with .99". I think you want to check "first two decimal places are 99".

Then it's all just maths:

 

double absPos = Math.abs(player.posX);
boolean endsWith99 = (int) ((absPos - (int) absPos) * 100) == 99;

 

 

Alright, I had to make it check for just 9 and add booleans for 8 and 7, but it works and does exactly what I want.

Thank you very much!

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.



×
×
  • Create New...

Important Information

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