Remember player who placed block and remember the block

Discussion in 'Plugin Development' started by Soxra, Apr 5, 2012.

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

    Soxra

    Hey,
    i need help.
    I want to remember a block location with a player name. If the player logs out, the block under the head is set to a sponge. Now if another player leftclicks this block, the block should disapear and the player who leftclicked the block should get a message like: "You have clicked the block of " + GET THE PLAYER LOGGED OUT.
    Thats what i want :) I hope everyone understands this.
    Regards, Soxra
     
  2. Offline

    Ne0nx3r0

    This is doable, but possibly not as easy as you might expect.

    Here's one plausible way to do it:

    Create new class called PlayerSponge, or something:
    int blockType
    String playerName
    Location blockLocation

    Create a static HashMap<Location,PlayerSponge> in your plugin's main.

    Setup a PlayerQuitEvent listener, and have it set the block beneath him and save that block's location to the hash map.

    Setup a PlayerInteractEvent listener that checks if they clicked a block, and then if they clicked a sponge block. Then check if the location they clicked is in your hashmap. If it is, grab the PlayerSponge object from it and grab the playername from the PlayerSponge and display it to them. Then grab the block type from the PlayerSponge and set the block back to normal. Then remove the PlayerSponge from the hash map.

    The final cleanup step is to create a PlayerLoginEvent that goes through the whole list of locations whenever a player logs in checking each player variable in each PlayerSponge. If it finds the player logging in, it then uses the location to see if the block is still a sponge. If it is, then reset the block to what it was. Then remove the location from the hashmap.


    --

    The one thing I (purposely) left out is persisting this across server resets. As it is currently, if you shut down the server there will be a bunch of sponges laying around and the plugin wont know about them when the server starts back up.

    If you get the first part of the plugin working, I'm sure you'll be able to work out how to save the values to a file periodically, and when the server shuts down and then get them when the server comes back up.

    Additionally you might want to include a check in the PlayerInteractEvent that removes any sponge that a player clicks on even if it doesn't have a PlayerSponge object associated with it, and perhaps give them a message about it being a lost player. Just to clean up anything that somehow slips through the cracks.
     
  3. Offline

    avatarDr

    If it will be called only from leftclick you should use Block as key, not location.
     
  4. Offline

    Soxra

    Thats what i dont know how to do ... But thanks so far :)
     
  5. Offline

    Ne0nx3r0

    I think you misunderstand what I mean by that statement. I don't mean the sponge block they clicked on, I mean the static HashMap<Location,PlayerSponge> you created in step one.

    If by chance that is what you mean, you would do that from your listener typically by first creating the hash map (probably in your plugin's main):
    Code:
        public static Map<Location,PlayerSponge> playerSponges = new HashMap<Location,PlayerSponge>();
    
    Then set values to it in your listener by using:
    Code:
    plugin.playerSponges.put(location,new PlayerSponge(location,playerName,blockType));//Or however you setup your playersponge class
    
    Then grabbing it by using:
    Code:
    plugin.playerSponges.get(location);
    
    Again this is just one way to do it, I don't even know if it's the "best" way, but it would do the job just fine, and hashmaps are easy to work with.

    I suppose that would save a few getBlock() calls, though I'm more familiar with using locations. I don't know if storing the Block object has any gotchas.
     
Thread Status:
Not open for further replies.

Share This Page