Best way to determine if a player steps over a specific block

Discussion in 'Plugin Development' started by kumpelblase2, Feb 16, 2012.

Thread Status:
Not open for further replies.
  1. Hey guys!

    I'm having a little question here: Which is the best way to check if a player steps over a specific block?
    The current situation is that i have a list of locations. If I check it in onplayermove and go through the list eveytime it's not really effective isn't it?

    So what I'm asking for, if there is a better way of doing this. I'm not asking for any code, just a way to do this.

    Thanks, kumpelblase2.
     
  2. Well the way I do it in my plugin is onPlayerMove it gets the block underneath the player. Simple :D
     
  3. like i said in my first post, i would do the same but it not very effective to go through a list everytime a player moves.
    let say if a server has lots of player and everytime ANY player moves i would need to go through a list with hundreds of entries. That will take (lots of) resources and might cause laggs which is not what you want.

    Because of that I'm trying to find a hopefully better solution.
     
  4. Although you are right and it could (, not will,) cause lag, depending on how resource intensive your methods are, this is the best way to do it. dumptruckman is working on PlayerCoarseMoveEvent I believe, which will only be called when the player's x, y, and/or z changes, which will be slightly more efficient. For now, use PlayerMoveEvent. It shouldn't lag (considerbly) if you remember to:
    • Cache all your variables. You should only have to call getPlayer(), getTo(), and getFrom() a maximum of once
    • Try to eliminate situations first: E.X. If I'm doing a plugin that's only active when the player is survival, before running all my checks make sure the player is in survival
    • Don't make unnecessary calls
    Good luck and hope this helped
    -tips
     
  5. Offline

    Father Of Time

    I find myself getting paranoid about looping through list allllll the time, but every time I end up doing it I find that it's just not that bad if done right. What I would do is make a list that stores a custom class, inside that class store a location and make a function that is called something like "IsSpecialLocation( Location loc )" and inside that function compare the Location stored in this custom class to the Location incoming arguement, something like this:

    Code:
        private Location SpecialLoc;
     
        public boolean LocationMatch( Location loc )
        {
            return ( loc.getBlockX() == SpecialLoc.getBlockX() && loc.getBlockY() == SpecialLoc.getBlockY() && loc.getBlockZ() == SpecialLoc.getBlockZ() );
        }
    Then you make a class that stores a list of these custom location classes, and every time you want to check to see if someone has walked onto one of your special spots you would loop through the special records checking the match location, something like this:

    Code:
        List<SpecialLocationRecord> locationrecords = new ArrayList<SpecialLocationRecord>();
     
     
        public boolean PlayerSteppedOnSpecialLoc( Player player, Location playerloc )
        {
            for( SpecialLocationRecord record : locationrecords )
            {
                if( record.LocationMatch( playerloc ) )
                    return true;
            }
            return false;
        }
    I like this method because you put the check that you perform inside each record you are trying to compare against, then you simply loop through the records and call that function to verify whether that record matches or not.

    Keep in mind, even if you had 100 locations, you are only doing a 3 integer comparison for each record, so assuming the record you are looking for is at the very end of the list you are only doing 300 integer comparisons, which is pretty much insignificant from a processing perspective; however, the key here is to only call this method when you leave one block and move to another, not every time you move, you can accomplish this by looking at WorldGuards player listener and at their player on move event for what triggers the greeting and farewell messages. If you trigger this function every time a player moves it will get called like 100 times when moving from one block to another, but if you make it only trigger when you leave one block and enter another it will only get called once, hence the true source of this projects efficiency focus (imo).

    Anyways, this is just some aimless rambling, another possible solution to your project... Good luck, I hope this helps!
     
  6. Father Of Time
    tips48

    thanks for you answers! I already implemented it and yeah with only one player on the server it works fine :D

    Checking if the x or z coord from 'from' to 'to' differ by 1 should work or doesn't it?
     
  7. Offline

    Father Of Time

    It's my pleasure, I am happy to assist. Yep it should work, I made another post about it somewhere... *looks*

    Found it, here is the other post with a great solution to identifying when a player moves from one block to another, just scroll down and look for my code example. :D

    http://forums.bukkit.org/threads/help-with-on_player_move-and-if-cords-changed.58751/#post-956682

    I wish I could take credit, but it was borrowed from WorldGuards onPlayerMove event (although modified from its original form).

    I hope this helps, good luck!
     
  8. Looks almost like my code, even though I don't need to check the z-coord for my needs.
     
Thread Status:
Not open for further replies.

Share This Page