Solved Getting specific blocks around a chunk

Discussion in 'Spigot Plugin Development' started by CodingCyClone, Jul 10, 2021.

  1. I am trying to make an event detection system which I accomplished by creating chunkID's for each chunk. Now the issue is I also have to determine if something occurs in the 3 surrounding blocks around a central chunk which is really tricky. So far what I decided to do was add all 9 chunks (the main chunk and 8 surroundings chunks) to a map. I then will be able to check if a specific event happens within those chunks very easily but like I said I only want a specific bit of code to execute if its 3 blocks or less away from that main chunk.

    I also am saving a center block of that center chunk in a map so that I can use it as a reference point. So when something happens within one of these chunks I can get its core chunk and see if occurs inside it and if not then I know it most be occurring in one of the 8 chunks surrounding it. My question is how can I use that knowledge and these coordinates (if I even need them) to get the 3 blocks surrounding that central chunk no matter if they are negative or positive?
    Code (Text):
     public String getCenterChunkLocation(Chunk c, Player p) {
            Location center = new Location(c.getWorld(), c.getX() << 4, 64, c.getZ() << 4).add(8, 0, 8);
            center.setY(p.getLocation().getY());
            int zValue = (int) center.getZ();
            int x = (int) center.getX();
            int y = (int) center.getY();
            int z;
            if(zValue < 0){
                z = zValue + 1;
            } else {
                z = zValue;
            }
            return x + " " + z + " " + y;
        }
    And just as some more info I wanted to include how I am adding the chunks to the map. In the following code the >> 4 allows me to turn an x,y or z location coordinate into its chunk form. Now the actual chunkID system is a lot more complicated and I did not figure it out although I did go through it all to try and understand it. For example I understand that 0xffffffffL is an identity which transforms the xCombined coordinate into a 32 bit long value. But like I said I may understand how it works I am not sure the thought processes behind it and how people knew it would create a unique chunkID for every Minecraft chunk.
    Code (Text):
     public void addOtherBaseClaims(String coordinates, String faction, int[] offset) {
            String[] cords = coordinates.split(" ");
            int baseX = Integer.parseInt(cords[0]);
            int baseZ = Integer.parseInt(cords[1]);

            for(int x : offset) {
                for(int z : offset) {
                    long xCombined = (baseX + x) >> 4;
                    long zCombined = (baseZ + z) >> 4;

                    long chunkID = (xCombined & 0xffffffffL) | (zCombined & 0xffffffffL) << 32;
                    baseClaimManager.setOtherBaseChunks(chunkID, faction);
                }
            }
        }
    Do you think it would be possible to do something similar to how I got the center of the chunk and if so how could I do it without needing a lot of if else (or switch) statements depending on if its values are negative or positive like I did above?
     
  2. Ok I had an idea is it possible to create a system like the ChunkID system but instead of it representing every 16 blocks it will represent every 2,3, or 4 blocks? I understand how to use bit operators but I am a little confused on how I would know if my outcome is a viable solution without testing a ton of random combinations, can anyone give me some insight into this so I can know if my idea is even possible before I mess with these operators?