player.getLocation Problem

Discussion in 'Plugin Development' started by surtic, Dec 27, 2011.

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

    surtic

    Hi @all,

    I want to make a Teleportation Command but when a the Player Move in 5 sec to abort the Teleportation. My Problem is that i don't get the right Location from the Player.

    There is some of my Code
    Code:
    
    
    long startet = System.currentTimeMillis();
    long now = System.currentTimeMillis();
    long end = startet + 5000;
    
    Integer x = player.getLocation().getBlockX();
    Location startLocation = player.getLocation();
    
    while ( now < end ) {
    	now = System.currentTimeMillis();
    }
    
    if ( startLocation.getBlockX() == player.getLocation().getBlockX() &&
    	 startLocation.getBlockY() == player.getLocation().getBlockY() &&
    	 startLocation.getBlockZ() == player.getLocation().getBlockZ() ) {
    
    	if ( clan.getClanSpawnX() != 0 && clan.getClanSpawnZ() != 0 ) {
    		player.teleport( clan.getClanSpawnLocation() );
    	} else {
    		player.teleport( new Location( plugin.getServer().getWorld("world") , clan.getBaseX() -2, clan.getBaseY(), clan.getBaseZ() ) );
    	}
    
    } else {
    	player.sendMessage(ChatColor.GOLD + "Teleporierung abgebrochen, du hast dich bewegt.");
    }
    
    How i can get the right Location from the Player now? With plugin.getServer().getPlayer( player.getName() ).getLocation() i become the same Location. But not the New Location.

    Any Idea? Thanks
     
  2. You are not allowed to freeze the main thread!
    Use a syncDelayedTask to wait 20*5 ticks ( = 5 secs). More information: http://wiki.bukkit.org/Scheduler_Programming

    In this task itself you grab the location of the player like you told:
     
  3. Offline

    surtic

    Ah thanks, i look how i can do this. First Time to work with Task. How i can give this Task some Variables?
     
  4. If you have a new class (implementing Runnable) you can give references in the constructor, like this:

    Code:java
    1. public class MyTask implements Runnable
    2. {
    3. private final String playerName;
    4. private final Server server;
    5.  
    6. public MyTask(String playerName, Server server)
    7. {
    8. this.playerName = playerName;
    9. this.server = server;
    10. }
    11.  
    12. public void run()
    13. {
    14. Player player = server.getPlayerExact(playerName);
    15. //Do stuff with the player here...
    16. }
    17. }
     
  5. Offline

    surtic

    Thanks, the Timer Works, but i become the wrong Location from the Server.

    Code:
    plugin.getServer().getScheduler().scheduleSyncDelayedTask( plugin, new ClanSpawnTask(plugin, clan, player), 100L);
    Code:
    package ch.nonameweb.bukkit.plugins.minepvp.tasks;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    import ch.nonameweb.bukkit.plugins.minepvp.Clan;
    import ch.nonameweb.bukkit.plugins.minepvp.MinePvP;
    
    public class ClanSpawnTask implements Runnable {
    	private MinePvP plugin;
    	private Clan clan;
    	private Player player;
    	public ClanSpawnTask( MinePvP plugin, Clan clan, Player player ) {
    		this.plugin = plugin;
    		this.clan = clan;
    		this.player = player;
    	}
    	@Override
    	public void run() {
    		player.sendMessage("X : " + player.getLocation().getBlockX() + " Y : " + player.getLocation().getBlockY() + " Z : " + player.getLocation().getBlockZ() );
    		player.sendMessage("X : " + plugin.getServer().getPlayer(player.getName()).getLocation().getBlockX() + " Y : " + plugin.getServer().getPlayer(player.getName()).getLocation().getBlockY() + " Z : " + plugin.getServer().getPlayer(player.getName()).getLocation().getBlockZ() );
    		if ( player.getLocation().equals( plugin.getServer().getPlayer( player.getName() ).getLocation() ) ) {
    			if ( clan.getClanSpawnX() != 0 && clan.getClanSpawnZ() != 0 ) {
    				player.teleport( clan.getClanSpawnLocation() );
    			} else {
    				player.teleport( new Location( plugin.getServer().getWorld("world") , clan.getBaseX() -2, clan.getBaseY(), clan.getBaseZ() ) );
    			}
    		} else {
    			player.sendMessage(ChatColor.GOLD + "Teleporierung abgebrochen, du hast dich bewegt.");
    		}
    	}
    
    }
    


    player.getLocation().getBlockX() and plugin.getServer().getPlayer(player.getName()).getLocation().getBlockX() get the same X Coordinate. And this is the Location wo i Start the Command.

    Any idea?

    How you make this nice Black Code Style?

    Sorry, it works :) now i save the Last Position in X Y Z.

    Thank you. Thread Close :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  6. //EDIT: Old post:
    Show Spoiler
    Code:java
    1. package ch.nonameweb.bukkit.plugins.minepvp.tasks;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6.  
    7. import ch.nonameweb.bukkit.plugins.minepvp.Clan;
    8. import ch.nonameweb.bukkit.plugins.minepvp.MinePvP;
    9.  
    10. public class ClanSpawnTask implements Runnable {
    11. private MinePvP plugin;
    12. private Clan clan;
    13. private String playerName; // A lot of things can happen in 100 ticks: The user can log off for example, so we only store it's name!
    14. private Location loc; // Here we store the location the player is at when executing the command.
    15. public ClanSpawnTask( MinePvP plugin, Clan clan, String playerName, Location loc ) { // Don't forget that the constructor changed!
    16. this.plugin = plugin;
    17. this.clan = clan;
    18. this.playerName = playerName;
    19. this.loc = loc;
    20. }
    21. public void run() {
    22. Player player = plugin.getServer().getPlayerExact(playerName);
    23. if(player == null) // That's the case if the player is offline now.
    24. return;
    25. player.sendMessage("X : " + loc.getBlockX() + " Y : " + loc.getBlockY() + " Z : " + loc.getBlockZ() );
    26. player.sendMessage("X : " + player.getLocation().getBlockX() + " Y : " + player.getLocation().getBlockY() + " Z : " + player..getLocation().getBlockZ() );
    27. if ( player.getLocation().equals( loc ) ) {
    28. if ( clan.getClanSpawnX() != 0 && clan.getClanSpawnZ() != 0 ) {
    29. player.teleport( clan.getClanSpawnLocation() );
    30. } else {
    31. player.teleport( new Location( loc.getWorld() , clan.getBaseX() -2, clan.getBaseY(), clan.getBaseZ() ) );
    32. }
    33. } else {
    34. player.sendMessage(ChatColor.GOLD + "Teleporierung abgebrochen, du hast dich bewegt.");
    35. }
    36. }
    37.  
    38. }
    39.  

    It could also be that loc.equals(anotherLoc) always returns true, in that case you'll have to check the worlds name, x, y and z manually:
    Code:java
    1. package ch.nonameweb.bukkit.plugins.minepvp.tasks;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6.  
    7. import ch.nonameweb.bukkit.plugins.minepvp.Clan;
    8. import ch.nonameweb.bukkit.plugins.minepvp.MinePvP;
    9.  
    10. public class ClanSpawnTask implements Runnable {
    11. private final MinePvP plugin;
    12. private final Clan clan;
    13. private final String playerName; // A lot of things can happen in 100 ticks: The user can log off for example, so we only store it's name!
    14. private final String worldName;
    15. private final int x;
    16. private final int y;
    17. private final int z;
    18. public ClanSpawnTask( MinePvP plugin, Clan clan, String playerName, Location loc ) { // Don't forget that the constructor changed!
    19. this.plugin = plugin;
    20. this.clan = clan;
    21. this.playerName = playerName;
    22. this.worldName =loc.getWorld().getName();
    23. this.x = loc.getBlockX();
    24. this.y = loc.getBlockY();
    25. this.z = loc.getBlockZ();
    26. }
    27. public void run() {
    28. Player player = plugin.getServer().getPlayerExact(playerName);
    29. if(player == null) // That's the case if the player is offline now.
    30. return;
    31. player.sendMessage("X : " + x + " Y : " + y + " Z : " + z );
    32. player.sendMessage("X : " + player.getLocation().getBlockX() + " Y : " + player.getLocation().getBlockY() + " Z : " + player..getLocation().getBlockZ() );
    33. Location loc = player.getLocation();
    34. if ( player.getWorld().getName().equals(worldName) && x == loc.getBlockX() && y == loc.getBlockY() && z == loc.getBlockZ()) {
    35. if ( clan.getClanSpawnX() != 0 && clan.getClanSpawnZ() != 0 ) {
    36. player.teleport( clan.getClanSpawnLocation() );
    37. } else {
    38. player.teleport( new Location( loc.getWorld() , clan.getBaseX() -2, clan.getBaseY(), clan.getBaseZ() ) );
    39. }
    40. } else {
    41. player.sendMessage(ChatColor.GOLD + "Teleporierung abgebrochen, du hast dich bewegt.");
    42. }
    43. }
    44.  
    45. }
    46.  


    BTW: For java syntax highlighting:
    Code:
    [syntax=java]public void foo() {};[/syntax]

    Fein das es nun werkelt / Good that it works now! :)
     
  7. Offline

    surtic

    Hi Danke :) Hi Thanks....

    How can i add a generatet Delay Time for the Task?

    Code:java
    1.  
    2. Integer time = plugin.getConfig().getInt("Global.Settings.Attack.Time");
    3. Integer delay = plugin.getConfig().getInt("Global.Settings.Attack.Delay");
    4.  
    5. time = ( time * 60 ) * 20;
    6. delay = ( delay * 60 ) * 20;
    7. delay = delay + time;
    8.  
    9. plugin.getServer().getScheduler().scheduleSyncDelayedTask( plugin, new StartAttackTask(plugin, clan.getName(), clan2.getName() ), delay + L);
    10.  
    11. plugin.getServer().getScheduler().scheduleSyncDelayedTask( plugin, new StopAttackTask(plugin, clan.getName(), clan2.getName() ), 20L);
    12.  


    And how can i Stop the Task after the Run?

    Thank you.
     
  8. First off: Sorry, I drunk a bit... I'll ignore the first question for now... ^^
    You can't stop it after cause it has already executed and doesn't exist anymore... But if you start a task it returns a int - this int is a pid (process ID) - If you want to stop the task before it executes:
    getServer().getScheduler().cancelTask(pid);
    of course you have to store the pid somewhere... ;)

    BTW: I don't mean that in a bad way, but your grammar shows that you speak german (and your code shows that it's "schwitzerdütsch"?)... ;)
     
  9. Offline

    surtic

    @V10lator : Thans, and yes im from Switzerland :) And my English is really bad :D
     
  10. As long as you don't had the nick "kill0r" (or something similar, don't remember atm) everything is okay. :D BTW: I'm from Bavaria.
    But let's go back to topic... ;)
     
  11. Offline

    surtic

    Yes back to Topic :)

    Okey, than i make a Scheduler close from Time to Time the old Tasks.

    Back to first Question.

    Now i make this to Start a Scheduler....


    Code:java
    1. plugin.getServer().getScheduler().scheduleSyncDelayedTask( plugin, new StartAttackTask(plugin, clan.getName(), clan2.getName() ), 1000L);


    but i want to replace the 1000L to a Variable.
     
  12. Offline

    tkausl

    Then do this:

    Code:java
    1.  
    2. long time = 500L;
    3. plugin.getServer().getScheduler().scheduleSyncDelayedTask( plugin, new StartAttackTask(plugin, clan.getName(), clan2.getName() ), time);
     
  13. Offline

    surtic

    Thanks it works.
     
Thread Status:
Not open for further replies.

Share This Page