# Solved Get Center of Chunk

Discussion in 'Spigot Plugin Development' started by BlahBerrys, May 16, 2017.

1. ### BlahBerrys

Attempting to get the center of a chunk when claimed by faction.. code works but location is wack.

Lets say I'm at 350, 64, -150 and I claim a chunk,
the location returned from getChunk() would be more like 14, 64, 20

// please note the events I'm hooking into are a part of faction's api
LandClaimEvent()

code:
Code (Java):

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
public void setCapital(LandClaimEvent event) {
Chunk c = event.getPlayer().getLocation().getChunk();
Location center = new Location(c.getWorld(), c.getX(), 64, c.getZ());
center.setY(center.getWorld().getHighestBlockYAt(center));

Location topLeft = center.clone().add(8, 0, 8);
Location topRight = center.clone().add(8, 0, -8);
Location bottomLeft = center.clone().add(-8, 0, -8);
Location bottomRight = center.clone().add(-8, 0, 8);

center.getBlock().setType(Material.DIAMOND_BLOCK);
topLeft.getBlock().setType(Material.DIAMOND_BLOCK);
topRight.getBlock().setType(Material.DIAMOND_BLOCK);
bottomLeft.getBlock().setType(Material.DIAMOND_BLOCK);
bottomRight.getBlock().setType(Material.DIAMOND_BLOCK);
}
}

2. ### RubbaBoy

Multiply the chunk coordinate for x and z by 16 to get the x and z position of the chunk's corner in normal world coordinates (I believe) and just add/subtract 8 from x and z, depending on if either axis is negative.

3. ### Synapz

Parameters:
x - 0-15
y - 0-127
z - 0-15

Get the chunk and supply the center of those parameters. For example
Code (Text):
Chunk chunk = location.getChunk()
Location center = chunk.getBlockAt(8,64,8):

#3
Last edited: May 17, 2017
4. ### BlahBerrys

okay somehow you both ignored the actual issue.... problem is not that I cannot find the center of the chunk! (getChunk.getX/getZ both return the center location. the problem is that the correct chunk is not being returned!!

5. ### CJP10

x >> 4 and z >> 4 will convert the weird cords back to world cords

6. ### RubbaBoy

Sorry, you said you were trying to get the center of the chunk several times I didn't assume it worked. Do something like what @CJP10 said and do x >> 4 and z >> 4 to go from location coords to chunk coords, which should return the chunk coordinate.

7. ### BlahBerrys

Can you give me example code?

Funny you mention this bc I found a similar piece of code within FLocation.class
Code (Text):

public static int blockToChunk(int blockVal) { // 1 chunk is 16x16 blocks
return blockVal >> 4; // ">> 4" == "/ 16"
}

What exactly does this do?

EDIT:

Tried this piece of code

[code
Location center = new Location(c.getWorld(), c.getX() >> 4, 64, c.getZ() >> 3);
[/code]

and it returned the location as 0, 66, 1

#7
Last edited: May 17, 2017
8. ### BlahBerrys

My fault, too many thoughts running through my head.

• Optimistic x 1
9. ### CJP10

to go from chunk to block just use << and you have block cords

10. ### RubbaBoy

That's fine, it's understandable. You will have to modify your current code a bit, but just do something like World#getChunkAt(x >> 4, z >> 4);, to get the chunk.

11. ### BlahBerrys

okay.. well, we're almost there. using this code

Code (Text):

Location center = new Location(c.getWorld(), c.getX() << 4, 64, c.getZ() << 4);

returns the general vesinity however it looks like its offset 4-8 blocks

12. ### RubbaBoy

You are trying to do the operation with the preexisting chunk coordinates, which probably sets things off. Try event.getPlayer().getLocation().getX() and do the same but with Z, instead of c.getX() and Z.

13. ### CJP10

add 8 to x and z after <<

14. ### RubbaBoy

I don't think that will work properly if the chunks are in the negatives, you would have to create more of a dynamic method for that.

15. ### CJP10

yeah it will work fine

16. ### BlahBerrys

Ended up solving myself.. I don't know what this code doesn't make any logical sense to me BUT it works.

Code (Text):

Chunk c = event.getPlayer().getLocation().getChunk();
Location center = new Location(c.getWorld(), c.getX() << 4, 64, c.getZ() << 4).add(7, 0, 7);
center.setY(center.getWorld().getHighestBlockYAt(center));

Location topLeft = center.clone().add(7, 0, 8);
Location topRight = center.clone().add(7, 0, -7);
Location bottomLeft = center.clone().add(-7, 0, -7);
Location bottomRight = center.clone().add(-7, 0, 8);

• Like x 1
• Winner x 1
17. ### SergiFerry

I've tested, and this code is more accurate.

Code (Java):
public static Location getCenterChunkLocation(Chunk c){
Location center = new Location(c.getWorld(), c.getX() << 4, 64, c.getZ() << 4).add(8, 0, 8);
center.setY(center.getWorld().getHighestBlockYAt(center) + 1);
return center;
}

public static Location getSouthEast(Chunk chunk){
Location center = getCenterChunkLocation(chunk);
Location topLeft = center.clone().add(7.5, 0, 7.5);
}

public static Location getNorthEast(Chunk chunk){
Location center = getCenterChunkLocation(chunk);
Location topRight = center.clone().add(7.5, 0, -7.5);
}

public static Location getNorthWest(Chunk chunk){
Location center = getCenterChunkLocation(chunk);
Location bottomLeft = center.clone().add(-7.5, 0, -7.5);
return bottomLeft;
}

public static Location getSouthWest(Chunk chunk){
Location center = getCenterChunkLocation(chunk);
Location bottomRight = center.clone().add(-7.5, 0, 7.5);
return bottomRight;
}

#17
Last edited: Jun 12, 2021
18. ### Goldentoenail

Most of you are confused here by co-ordinals.
The 'origin' in mathematics is 0, 0 therefore for conventional methods you would count the origin area cell as '1' ( in mathematics this cell is '0')
For an odd perimeter square or cube, then the center would be a cell, and it's center.
But for an even perimeter square or cube, it is the origin of the next center cell.
Consider drawing a cross at the center of an odd perimeter square. And then an even perimeter square.

For the even perimeter, you start counting at 0 to 7 (That is 8 cells) the next cell is 8, and it's origin is 8.0, 8.0

• Agree x 1