/*
 * This file is part of SpongeSurvivalGamesPlugin, licensed under the MIT License (MIT).
 *
 * Copyright (c) Matthew Broomfield <m0pt0pmatt17@gmail.com>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package io.github.m0pt0pmatt.spongesurvivalgames.commands.game.stopped;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.command.CommandSender;

import io.github.m0pt0pmatt.spongesurvivalgames.BukkitSurvivalGamesPlugin;
import io.github.m0pt0pmatt.spongesurvivalgames.SurvivalGame;

/**
 * Command to scrape the entire bounds and get all the chests
 */
public class GetChestsCommand extends StoppedCommand {

    @Override
    public boolean execute(CommandSender sender, Map<String, String> arguments) {

        if (!super.execute(sender, arguments)) {
            return false;
        }
        
        SurvivalGame game = BukkitSurvivalGamesPlugin.survivalGameMap.get(id);
        
        game.getConfig().getChestLocations().clear();
        
        int x, z;
        x = game.getConfig().getXMax().get() - game.getConfig().getXMin().get();
        z = game.getConfig().getZMax().get() - game.getConfig().getZMin().get();
        
        x = x / 16;
        z = z / 16;
        
        sender.sendMessage("Bounds include about " + (x * z) + " chunks.");
        
        sender.sendMessage("Beginning scrape for chests. This may take a while...");
        List<Chest> chests = getChests(game, sender);
        sender.sendMessage("Done!");
        sender.sendMessage("Found " + chests.size() + " chests!");
        
        for (Chest chest : chests) {
        	game.getConfig().getChestLocations().add(
        			chest.getLocation().toVector()
        			);
        }

        return true;
    }
    
    private List<Chest> getChests(SurvivalGame game, CommandSender sender) {
    	String worldName = game.getWorldName().get();
        World world = Bukkit.getServer().getWorld(worldName);

        int xmin = game.getConfig().getXMin().get();
        int xmax = game.getConfig().getXMax().get();
        int ymin = game.getConfig().getYMin().get();
        int ymax = game.getConfig().getYMax().get();
        int zmin = game.getConfig().getZMin().get();
        int zmax = game.getConfig().getZMax().get();
        
        List<Chest> chests = new LinkedList<Chest>();
        
        int count = 0;
        
    	for (int x = game.getConfig().getXMin().get(); x < xmax; x += 16) {
    	        for (int z = game.getConfig().getZMin().get(); z < zmax; z += 16) {
    	                    
    	                    Chunk chunk = world.getChunkAt(x/16, z/16);
    	                    for (BlockState e : chunk.getTileEntities()) {
    	                    	if (e instanceof Chest) {
    	                    		chests.add((Chest) e);
    	                    	}
    	                    }
    	                   
    	                    
    	                    count++;
    	                	if (count % 1000 == 0) {
    	                		sender.sendMessage("Finished chunks 1 - " + count);
    	                	}
    	      }
    	}
    	
    	return chests;
    }
}

@@ -3569,24 +3569,65 @@
 0;%0A        %0A
+        Chunk lastChunk = null;%0A        %0A
     %09for (in
@@ -3854,21 +3854,250 @@
 kAt(
-x/
+Math.floorDiv(x, 
 16
+)
 , 
-z/
+Math.floorDiv(z, 
 16)
+)
 ;%0A
+    %09                    %0A    %09                    if (lastChunk != null && lastChunk.equals(chunk)) %7B%0A    %09                    %09System.out.println(%22Chunks are equal!%22);%0A    %09                    %7D%0A
     
@@ -4544,32 +4544,73 @@
              %09%7D%0A
+    %09                %09lastChunk = chunk;%0A
     %09      %7D%0A   

use floor instead of truncastion