Limiting Blocks Placed

Discussion in 'Plugin Development' started by Pricetx, Sep 5, 2011.

Thread Status:
Not open for further replies.
  1. The scenario is: I want a limit placed on how many blocks of a certain type that a player can place. For arguements sake i'll say for now that this limit is 10 dirt blocks. This means that the player would place the first 10 blocks perfectly normally. However, when they try and place an 11th, instead of placing the block it would display a message in the chat explaining that they're over the limit.

    The slightly more complicated part comes in because I want to be able to make the counter go down again if you delete the dirt blocks that you previously placed. This would essentially allow a maximum of 10 of your dirt blocks to be placed at any one time. Also, making sure that you have to delete the blocks you placed and not any other dirt blocks.

    Any suggestions would be much appreciated. If possible, code examples would be preferred.


  2. Offline


    First of all, you need to store information of the blocks placed by the player.

    public static HashMap<String, HashMap<Material, Integer>> placedItems = new HashMap<String, HashMap<Material, Integer>>();
    Yes, it is a hashmap in a hashmap. Now to ease everything...
    public static HashMap<Material, Integer> getPlacedItems(String playername) {
        HashMap<Material, Integer> items = placedItems.get(playername);
        if (items == null) {
            items = new HashMap<Material, Integer>();
            placedItems.put(playername, items);
        return items;
    public static HashMap<Material, Integer> getPlacedItems(Player player) {
        return getPlacedItems(player.getName());
    This will return a HashMap containing the materials vs. the amount of placements for the player. It is never null. If the player is not mapped yet, it will put and return an empty Hashmap back.

    Now the magic. Get and set the items.
    public static int getPlacedCount(Player player, Material material) {
        return getPlacedItems(player).get(material);
    public static void setPlacedCount(Player player, Material material, int amount) {
        getPlacedItems(player).put(material, amount);
    Now you can use these functions to get and set materials on the fly. See also: onBlockBreak and onBlockPlace events in the BlockListener.
Thread Status:
Not open for further replies.

Share This Page