Solved Clear blocks placed by player

Discussion in 'Spigot Plugin Help' started by DeivisMac, May 30, 2017.

  1. Hi im making a minigame and i want to remove blocks placed by a certain player at the end of the game . I've tried everything nothing worked. Please post suggestions below :)
     
  2. can you redirect it to plugin development XD
     
  3. Not another BuildUHC please
     
  4. Mh. I know you marked it as solved but i would add each placed block to an array and at the end of the round/match what ever you could just change each block to Material.AIR :)
     
  5. yea but you see i needed to clear just certain players blocks not everyones
     
  6. Mh. Mh... then...

    There is one problem with this code if you use it without the second code below. If the player destroyes a block and places a block there... than this block gets set to Material.AIR. If you want to prevent that than you should also use the second code below the first.

    Code (Text):


      HashMap<Player, Set<Block>> placed_blocks = new HashMap<>();

       @EventHandler
       public void onBlockPlace(BlockPlaceEvent event){
         Set<Block> temp_list = new HashSet<Block>();
         // HashSet is like Array. HashSet is a unordered list that allows not duplicates. I think that this is a bit faster and better for performance and uses less ram than a arraylist
         Player player = event.getPlayer();
     
         if(placed_blocks.containsKey(player)){
           temp_list = placed_blocks.get(player);
           temp_list.add(event.getBlock());
           placed_blocks.put(player, temp_list);
         }else{
           temp_list.add(event.getBlock());
           placed_blocks.put(player, temp_list);
         }
       }

       public void removePlacedBlocks(Player player){
         if(!placed_blocks.containsKey(player))
           return;
     
         for(Block block : placed_blocks.get(player)){
           block.setType(Material.AIR);
         }
         placed_blocks.remove(player);
       }
     


    So ahm.. this code checks if the block that the player trys to break got placed by a player. If not than the event gets cancelled :)
    Code (Text):

      @EventHandler
       public void onDestroy(BlockBreakEvent event){
         for(Player p : placed_blocks.keySet()){
           if(!placed_blocks.get(p).contains(event.getBlock())){
               event.setCancelled(true);
               return;
           }
         }
       }
     
     
    #6 VinceGhi, May 30, 2017
    Last edited: May 30, 2017
    • Useful Useful x 1