Proof of Concept - Chunk coords (Confirm this should be working?)

Discussion in 'Plugin Development' started by RegalMachine, May 19, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    RegalMachine

    I am creating a 'proof of concept' plugin for use in a much larger plugin i am creating involving regions. Here is the only class in the code:

    Code:java
    1. package me.RegalMachine.ChunkDetector;
    2.  
    3. imports...
    4.  
    5. public class Main extends JavaPlugin implements Listener{
    6. @Override
    7. public void onEnable(){
    8. getServer().getPluginManager().registerEvents(this,this);
    9. }
    10.  
    11. public static Map<Player, ChunkRef> chunks = new HashMap<Player, ChunkRef>();
    12. @EventHandler
    13. public void onMove(PlayerMoveEvent e){
    14. if(!chunks.contains(e.getPlayer())){
    15. chunks.put(e.getPlayer(), new ChunkRef(e.getPlayer().getLocation()));
    16. }
    17. if(!chunks.get(e.getPlayer()).equals(new ChunkRef(e.getPlayer().getLocation()))){
    18. chunks.remove(e.getPlayer());
    19. chunks.put(e.getPlayer(), new ChunkRef(e.getPlayer().getLocation()));
    20. e.getPlayer().sendMessage("You're in a new chunk! " + )
    21. }
    22. }
    23.  
    24. public static final class ChunkRef{
    25. int x, z;
    26. public ChunkRef(Location loc){
    27. x = loc.getBlockX() >> 4; //Same as deviding by 16?
    28. z = loc.getBlockZ() >> 4;
    29. }
    30.  
    31. public boolean equals(ChunkRef ref){
    32. return this.x == ref.getX() && this.z == ref.getZ();
    33. }
    34.  
    35. public int getX(){return x;}
    36. public int getZ(){return z;}
    37. }
    38. }
    39.  


    From what i can tell, this would work, correct? The '>>' operator is used correctly, and this SHOULD give me the coordinates of that chunk? Because as i recall its not. Do i need to possibly subtract x and z by one?

    This concept was introduced to me by BukkitDev StaffMember toothpick, but i cant tell if im doing it correctly. My gut tells me no, i should be deviding by 16 instead of bitshifting by 4. Thank you for reading, i know my question seems a bit unclear, but i am writing this on my phone and its difficult to re-write code from memory. :p
     
  2. RegalMachine I'm not sure what you're trying to do here. Location has a getChunk() method, and Chunk has a getX() and getZ() method.
     
  3. Offline

    RegalMachine

    I beleive I may have put the message print to the player befor i updated the map on the actual code in Eclipse, i'll check to see if i did it correctly later. But is this also the most efficent way to do this? I need the chunk coordinates as an Object.

    AdamQpzm HashMapping upwards of 1000 chunks would cause memory to fill up quickly. I am creating my own ChunkRef objects to represent/mimic chunks in order to improve performance :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    Garris0n

    So...what exactly is wrong? Is it not giving the coordinates? What is it giving?
     
  5. RegalMachine If you don't want to store the chunk itself, and you don't want to test/risk that it wouldn't get the correct chunk, you don't need to actually store the chunk - just set the value of your own class' x and z with the method available in chunk.
     
  6. Offline

    RegalMachine

    garrison I can't tell if anything is wrong, I need to know if this would work the way its intended befor I start to rely on it. I can't get to my computer untill tomarrow, and i'd like to continue planning my very large plugin.
     
  7. Offline

    Garris0n

    It should, but if all you're doing with it is seeing when a player moves to a new chunk, you don't need to store things in a map. Just compare the from and to locations. If you do need to store it, why? It's probably faster to just calculate the x/y every time than to search the map...

    It works, but it seems completely useless.
     
  8. Offline

    RegalMachine

    AdamQpzm Because this is both faster and requires less memory. Getting the chunk from a location and getting the x and z from that chunk would require a few extra steps. This is more direct, and i don't have to get a reference to the chunk in order to do it, making it less tasking on RAM. Keep in mind my larger plugin will be doing this around 80 times per second
     
  9. Offline

    Garris0n

    Technically you're not using up more memory by getting the chunk. Okay, maybe a bit to hold the variable, but that's negligible and less than you're using to store your special object. Regardless...why? What are you doing that needs these in a map?
     
  10. Offline

    RegalMachine

    garrison as of now it IS useless. Its a proof of concept to make sure it would work on a much larger scale. The reason im mapping it is because eventually i will be using the ChunkRef objects as the key to get a List<Region>.

    garrison you can see more details on why i am needing this in this post here http://forums.bukkit.org/threads/need-help-how-to-approach-region-change-events.268839/

    as long as this works the way i intended it to, i can label this thread solved. This works?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  11. Offline

    Zethariel

    RegalMachine 80 times a second? MC runs at 20 ticks per second. You're doing 4 runs on the same data that does not change during the length of a tick.
     
  12. Offline

    RegalMachine

    @Zetharial No, about 8 times for second for about 12 players on at a time.
     
  13. Offline

    hexagon

    The >> operator means your shitfing the bits to the right four times, it is not the same as dividing by 16.
    EDIT: Also, use location.getChunk().getX() and location.getChunk().getZ()
    EDIT2: Just to clarify the shift operator: 16 >> 4 = 1
     
  14. Offline

    RegalMachine

    hexagon So the way i have it now chunks with x 0-16 have a value of 1, 17-32 2, and so on?
     
  15. Offline

    hexagon

    Yes but the ranges are 0-15, 16-31 and so on...
     
  16. Offline

    Necrodoom

    Wouldn't / 16 produce same result as your case then, considering he is storing ints?
     
  17. Offline

    hexagon

    Based on a quick tought, yes it would but, maybe there are some odd cases where it wouln't return the correct loc and there is an API method there to be used that is 100% accurate and you're following the OOP concept.
     
Thread Status:
Not open for further replies.

Share This Page