Only the first time you step on the block!

Discussion in 'Plugin Development' started by zDubsCrazy, Apr 25, 2014.

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

    zDubsCrazy

    I created a PlayerMoveEvent for when the player stepping on the x, y, z defined happen one delay and teleports him. But how is a PlayerMoveEvent, every time he moves the same thing happens several times. Wanted only the first time he set foot on the block, this delay happens. I hope you can help me!

    Thanks.
     
  2. Code:java
    1. public Map<String, Boolean> hasStepped = new HashMap<String, Boolean>();


    Now when he steps on it:


    Code:java
    1. Player p = event.getPlayer();
    2. if (!hasStepped.containskey(p.getName())) hasStepped.put(p.getName(), false);
    3. if (hasStepped.get(p.getName()) == false){
    4. //Do everything like normal
    5. hasStepped.put(p.getName(), true);
    6. else //Do stuff for when its NOT the first time.
     
    zDubsCrazy likes this.
  3. Offline

    Maurdekye

    zDubsCrazy You would have to keep track of the fact that he'd stepped on a block. Save an arraylist of players who stepped on it, and ignore them if they are already in the list.
     
  4. Offline

    TryB4

    zDubsCrazy
    if (e.getTo().getX() != e.getFrom().getX() || e.getTo().getZ() != e.getFrom().getZ()) // moved a block.
     
    BillyGalbreath likes this.
  5. Offline

    amhokies

    A map with a boolean as a value can be simply changed to a Set. If the player is in the Set, they have stepped on the block, if they aren't, they haven't. It's simpler, more concise, and saves memory.
     
  6. Offline

    zDubsCrazy

    WeeziMonkey
    Thanks :). But all it in PlayerMoveEvent?
     
  7. Everything except for the part defining the HashMap (or HashSet as the person above you said).
     
  8. Offline

    BillyGalbreath

    TryB4's example is the most efficient for this scenario (although it's incomplete). No need to store data or sift through lists of crap.

    They key here is that the player is being teleported away when they step into this block. This doesnt need to fire 100 times from the player moving as it processes the teleport. Once the player is teleported away they are again no longer in the block, and no need to reset/modify any lists or sets for the player to become eligible for the teleport again.

    Code:java
    1.  
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. if (event.getTo().getX() == event.getFrom().getX() &&
    4. event.getTo().getY() == event.getFrom().getY() &&
    5. event.getTo().getZ() == event.getFrom().getZ()) {
    6. return; // Stop. Player did not move between blocks (no need to check the location)
    7. }
    8. // The rest of this is just to finish the example of a hardcoded teleport location. Your existing code will go here instead.
    9. int x = event.getTo().getBlockX();
    10. int y = event.getTo().getBlockY();
    11. int z = event.getTo().getBlockZ();
    12. if (x == 10 && y == 64 && z == 10)
    13. event.getPlayer().teleport(new Location(someWorld, 20, 64, 10));
    14. }
    15.  
     
  9. Offline

    zDubsCrazy

    BillyGalbreath
    Thanks.
    But it will work with the delay? This delay is 3 seconds before the teleporter!
     
Thread Status:
Not open for further replies.

Share This Page