Get the centre of a block?

Discussion in 'Plugin Development' started by KingFaris11, Aug 11, 2014.

Thread Status:
Not open for further replies.
  1. Hi, I want to get the Location of the centre of a Block.
    I've tried:
    Code:
    Location centre = block.getLocation().add(0.5D, 0D, 0.5D);
    
    But that only works depending on the Block location (e.g. negative or positive). I can't figure the logic behind when to add 0.5D, etc. as I'm having a mind blank.

    Can someone please explain exactly how this'd work? It'd be greatly appreciated if you already had a snippet or so, because I already understand the basics and so I can instantly pick up why.
     
  2. Offline

    Garris0n

    It would work if the block was at a negative position...
     
  3. I've tested it - it returned the incorrect location. :/

    Though DenialMC gave me this code:
    Code:
    Location original = block.getLocation();
    double x = original.getX();
    double z = original.getZ();
    Location new = new Location(original.getWorld(), x > 0 ? x + 0.5 : x - 0.5, original.getY(), z > 0 ? z + 0.5 : z - 0.5, original.getYaw(), original.getPitch());
    
    I'll test this (without making a new Location - he said he could've but he was too lazy).

    Edit: He made it:
    Code:
    location.add(x > 0 ? 0.5 : -0.5, 0.0, z > 0 ? 0.5 : -0.5);
     
  4. Offline

    stormneo7

    No I don't think so.... If it was negative, it'd be -0.5.

    Code:java
    1. Location centre = block.getLocation().add(block.getLocation().getX() < 0 ? -0.5D : 0.5D, 0D, block.getLocation().getZ() < 0 ? -0.5D : 0.5D);
     
  5. Offline

    Garris0n

    Why?
     
  6. Offline

    stormneo7

    nvm u were right

    If I were to do the command /tp 100 100 100, it tps me to 100.5.
    [​IMG]

    Where as if I were to tp to -100 100 -100, it tps me to -99.5.
    [​IMG]
    This is using Vanilla Minecraft. Can't argue with that.
     
  7. Hmm.. I'll do some testing again and print.

    This is really weird... It's not working with both. Oh well, I guess I don't need this anymore. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. KingFaris11 Awe... Come on... Real bukkit devs never give up when they face a problem they can't explain.
    Until you actually solve this issue. The thread is not solved. By marking it as solved, you lie and trick any future fourms.bukkit user into thinking they've found the answer to THEIR same problem.(only to be deeply disappointed)
     
  9. Very true but I have 3 other plugin tasks in my queue and I've got to finish this current plugin by tonight (already) reached. Though anyway, I don't need it anymore, it's too much anyway (I was spawning a hologram ontop of a block and I wanted the hologram at the centre).
     
  10. Offline

    1Rogue

    Code:java
    1. Location loc = /* your location corner */;
    2. Location center = getCenter(loc);
    3.  
    4. public Location getCenter(Location loc) {
    5. return new Location(loc.getWorld(),
    6. getRelativeCoord(loc.getBlockX()),
    7. getRelativeCoord(loc.getBlockY()),
    8. getRelativeCoord(loc.getBlockZ()));
    9. }
    10.  
    11. private double getRelativeCoord(int i) {
    12. double d = i;
    13. d = d < 0 ? d - .5 : d + .5;
    14. return d;
    15. }


    You can also improve upon my answer by not instantiating a new Location object and instead setting it to the BlockX, BlockY, and BlockZ first.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    KingFaris11 likes this.
  11. Ly :] I'll test it when I get home in 12hrs.
     
Thread Status:
Not open for further replies.

Share This Page