/*
 * This file is part of Hammer, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2015 Daniel Naylor
 * 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 uk.co.drnaylor.minecraft.hammer.core.commands.parsers;

import uk.co.drnaylor.minecraft.hammer.core.HammerCore;
import uk.co.drnaylor.minecraft.hammer.core.data.HammerPlayerInfo;
import uk.co.drnaylor.minecraft.hammer.core.handlers.DatabaseConnection;

import java.util.List;
import java.util.ListIterator;
import java.util.Optional;

public class HammerPlayerParser implements IParser<List<HammerPlayerInfo>> {

    private final HammerCore core;

    public HammerPlayerParser(HammerCore core) {
        this.core = core;
    }

    @Override
    public Optional<List<HammerPlayerInfo>> parseArgument(ListIterator<String> stringIterator) throws ArgumentParseException {
        if (!stringIterator.hasNext()) {
            throw new ArgumentParseException("No player was specified");
        }

        try (DatabaseConnection dg = core.getDatabaseConnection()) {
            String name = stringIterator.next();
            List<HammerPlayerInfo> hpi = dg.getPlayerHandler().getPlayersByName(name);
            if (hpi == null || hpi.isEmpty()) {
                throw new ArgumentParseException("The player " + name + " does not exist in Hammer.");
            }

            return Optional.of(hpi);
        } catch (ArgumentParseException e) {
            // Special case, re-throw this.
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            return Optional.empty();
        }
    }

    @Override
    public void onFailedButOptional(ListIterator<String> stringListIterator) {
        stringListIterator.previous();
    }
}

@@ -1278,16 +1278,56 @@
 rsers;%0A%0A
+import com.google.common.collect.Lists;%0A
 import u
@@ -1374,16 +1374,16 @@
 erCore;%0A
-
 import u
@@ -1595,16 +1595,71 @@
 ptional;
+%0Aimport java.util.UUID;%0Aimport java.util.regex.Pattern;
 %0A%0Apublic
@@ -1764,16 +1764,147 @@
 re core;
+%0A    private final Pattern uuid = Pattern.compile(%22%5E%5Ba-fA-F0-9%5D%7B8%7D-%5Ba-fA-F0-9%5D%7B4%7D-%5Ba-fA-F0-9%5D%7B4%7D-%5Ba-fA-F0-9%5D%7B4%7D-%5Ba-fA-F0-9%5D%7B12%7D$%22);
 %0A%0A    pu
@@ -2359,24 +2359,24 @@
 tor.next();%0A
-
             
@@ -2397,16 +2397,410 @@
 yerInfo%3E
+ hpi;%0A%0A            // We have a UUID%0A            if (uuid.matcher(name).matches()) %7B%0A                UUID uuid = UUID.fromString(name);%0A                HammerPlayerInfo h = dg.getPlayerHandler().getPlayer(uuid);%0A                if (h != null) %7B%0A                    return Optional.of(Lists.newArrayList(h));%0A                %7D%0A%0A                return Optional.empty();%0A            %7D%0A%0A           
  hpi = d

Add UUID support for a player parser argument.