Odd problem w/ array lists?

Discussion in 'Plugin Development' started by HyrulesLegend, Nov 3, 2013.

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

    HyrulesLegend

    This is supposed to stop a player from tping whenever they move:

    The event that checks if their moving:
    Code:java
    1. @EventHandler
    2. public void onPMove(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. if (e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) return;
    5. d.remove(p.getName());
    6. }
    7. }


    The teleport event that is supposed to happen only if they are in the array list.

    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. if(d.contains(p.getName())){
    5. p.teleport(new Location(w, x, y, z));
    6. }
    7. }
    8. }.runTaskLater(plugin, 60);


    The problem is, even when I move, I still get tped. Does anyone know why this might be happening?

    Well, I'm off to bed, if anyone could figure this out that would be great. :)

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

    Mathias Eklund

    You have to cancel the task, as it already started.
     
  3. Offline

    Coelho

    No, that is only required if he runs the task timer.

    You are activating the task multiple times,which is odd.
    Also, why are you listening for the player to be teleported? If you execute Entity.teleport(<>), It is safe to assume the teleport was successful.

    EDIT: By the way, use a Set, not a List. People seem to not know the difference.
     
  4. Offline

    Everdras

    Instead of listening for them to move to cancel the event, why not just do this:

    * player initiates teleport, store their location, andcountdown begins
    * When countdown ends, check current locations against the stored location. If same, teleport. If different, don't teleport.
     
    SupaHam likes this.
  5. Offline

    SupaHam

    Only downside is if the cooldown is long, players won't get notified until it is about to teleport them.
     
  6. Offline

    xize

    what happends if you print the values from the hashmap or arraylist?
    I'm very sure it is empty, a common mistake is when you use a ArrayList or HashMap inside a other class without making it static or using getters in the class the ArrayList object is.
    this means if you would instance it like in your event class with 'new' you create also a new ArrayList instance which is empty.

    also why are you using a ArrayList?
    an ArrayList can have duplicates thats very dangerous for memory leaks but if you use a HashMap or a HashSet, you can't have duplicates its alot safer.
     
Thread Status:
Not open for further replies.

Share This Page