Getting 2 blocks locations and saving them ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Oct 10, 2013.

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

    ThunderWaffeMC

    Hi. How can I save 2 blocks locations? For example:

    Code:java
    1.  
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Action action = event.getAction();
    4. Player player = event.getPlayer();
    5. Material defineItem = Material.matchMaterial(this.plugin.getConfig().getString("Define Block Item"));
    6. if(action == Action.RIGHT_CLICK_BLOCK) {
    7. int x1 = event.getClickedBlock().getX();
    8. int y1 = event.getClickedBlock().getY();
    9. int z1 = event.getClickedBlock().getZ();
    10. String world1 = event.getClickedBlock().getWorld().getName();
    11. if(player.getItemInHand().getType() == defineItem) {
    12. List<String> block1List = Arrays.asList("" + x1, "" + y1, "" + z1, "" + world1);
    13. //save block1List and then in another event I can use it
    14. }
    15. }
    16. if(action == Action.LEFT_CLICK_BLOCK) {
    17. int x2 = event.getClickedBlock().getX();
    18. int y2 = event.getClickedBlock().getY();
    19. int z2 = event.getClickedBlock().getZ();
    20. String world2 = event.getClickedBlock().getWorld().getName();
    21. if(player.getItemInHand().getType() == defineItem) {
    22. List<String> block2List = Arrays.asList("" + x2, "" + y2, "" + z2, "" + world2);
    23. //save block2List and then in another event I can use it
    24. }
    25. }
    26. if(action == Action.RIGHT_CLICK_BLOCK) {
    27. if(event.getClickedBlock() == block2List { //if the block that was clicked equals the block2List location
    28. player.teleport(block1List); //teleport them to block1List location
    29. }
    30. if(event.getClickedBlock() == block1List { //if the block that was clicked equals the block1List location
    31. player.teleport(block2List); //teleport them to block2List location
    32. }
    33. }
    34. }
    35.  


    I need to be able to save the block1List and block2List and then when a player right clicks one of the blocks it teleports them to the other.

    Thanks!
     
  2. In your case, I would use a HashMap instead of a list. So you can map the locations to each other. And to make it easier to get the destination, you shouldn't convert the Location to a String, but save it in an instance of a class like Vector, as that is quite much easier to convert back to a Location afterwards. So you can use a HashMap<Vector,Vector> to store and to retrieve the locations. You can add each location pair twice, so the lookup works in both directions. If you want to permanently save the HashMap to a config, you will need to write your one de-/serialization code for it.
     
    ThunderWaffeMC likes this.
Thread Status:
Not open for further replies.

Share This Page