package com.onarandombox.MultiverseScratchWorlds.commands;

import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionDefault;

import com.onarandombox.MultiverseScratchWorlds.MultiverseScratchWorlds;

public class ChunkRegenCommand extends ScratchWorldsCommand {
    public ChunkRegenCommand(MultiverseScratchWorlds plugin) {
        super(plugin);

        this.setName("Chunk regenerate");
        this.setCommandUsage("/mvsw chunk [world] [x z]");
        this.setArgRange(0, 3);
        this.addKey("mvsw chunk regen");
        this.setPermission("multiverse.scratchworlds.chunkregen", "Regenerate chunk.", PermissionDefault.OP);
    }

    @Override
    public void runCommand(CommandSender sender, List<String> args) {
        if(!(sender instanceof Player) && (args.size() != 3)) {
            sender.sendMessage(ChatColor.RED + "This command must be run from console.");
            return;
        }
        Player player = (Player)sender;

        Chunk chunk = null;
        World world = null;
        switch(args.size()) {
        case 0: world = player.getLocation().getWorld(); chunk = player.getLocation().getBlock().getChunk(); break;
        case 1: sender.sendMessage("unimplemented"); return;
        case 2: sender.sendMessage("unimplemented"); return;
        case 3: sender.sendMessage("unimplemented"); return;
        default: sender.sendMessage(ChatColor.RED + "Invalid argument count for chunk regen - this is a bug!"); return;
        }

        if(chunk == null || world == null) {
            sender.sendMessage(ChatColor.RED + "Couldn't locate chunk to regenerate - this may be a bug!");
            return;
        }

        boolean result = world.regenerateChunk(chunk.getX(), chunk.getZ());
        if(!result) {
            sender.sendMessage(ChatColor.RED + "Couldn't regenerate chunk. Don't know why.");
            return;
        }

        result = world.refreshChunk(chunk.getX(), chunk.getZ());
        if(!result) {
            sender.sendMessage(ChatColor.RED + "Couldn't refresh chunk. Don't know why.");
            return;
        }
    }
}

@@ -615,16 +615,22 @@
 w chunk 
+regen 
 %5Bworld%5D 
@@ -634,24 +634,24 @@
 d%5D %5Bx z%5D%22);%0A
-
         this
@@ -1238,16 +1238,28 @@
  case 0:
+%0A           
  world =
@@ -1291,16 +1291,28 @@
 World();
+%0A           
  chunk =
@@ -1355,16 +1355,28 @@
 Chunk();
+%0A           
  break;%0A
@@ -1394,174 +1394,697 @@
 e 1:
- sender.sendMessage(%22unimplemented%22); return;%0A        case 2: sender.sendMessage(%22unimplemented%22); return;%0A        case 3: sender.sendMessage(%22unimplemented%22); return
+%0A            world = this.getPlugin().getServer().getWorld(args.get(0));%0A            if(world == null) break;%0A            chunk = player.getLocation().getBlock().getChunk();%0A            chunk = world.getChunkAt(chunk.getX(), chunk.getZ());%0A            break;%0A        case 2:%0A            world = player.getLocation().getWorld();%0A            chunk = world.getChunkAt(Integer.parseInt(args.get(0)), Integer.parseInt(args.get(1)));%0A            break;%0A        case 3:%0A            world = this.getPlugin().getServer().getWorld(args.get(0));%0A            if(world == null) break;%0A            chunk = world.getChunkAt(Integer.parseInt(args.get(1)), Integer.parseInt(args.get(2)));%0A            break
 ;%0A  
@@ -2097,16 +2097,28 @@
 default:
+%0A           
  sender.
@@ -2204,16 +2204,28 @@
  bug!%22);
+%0A           
  return;

Implement better chunk selection