package com.carpentersblocks.util.protection;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;

import com.mojang.authlib.Agent;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.ProfileLookupCallback;

public class ProtectedUtil {

    private static Map<String, UUID> cachedUUID = new HashMap<String, UUID>();

    /**
     * Returns whether player is owner of object.
     */
    public static boolean isOwner(IProtected object, EntityPlayer entityPlayer)
    {
        UUID uuid = updateOwnerUUID(object);

        if (uuid != null) {
            return object.getOwner().equals(entityPlayer.getUniqueID().toString());
        } else {
            boolean result = object.getOwner().equals(entityPlayer.getDisplayName());

            // Attempt to update owner format manually, which seems to happen
            // when it is not converted on world load (for reasons unknown).
            updateOwnerUUID(object);

            return result;
        }
    }

    /**
     * Reads owner from protected object and converts it to UUID if necessary.
     */
    public static UUID updateOwnerUUID(IProtected object)
    {
        String owner = object.getOwner();
        UUID uuid = null;

        try {
            uuid = UUID.fromString(owner);
        } catch (IllegalArgumentException e) {
            if (cachedUUID.containsKey(owner)) {
                if (cachedUUID.get(owner) != null) {
                    object.setOwner(cachedUUID.get(owner));
                }
            } else {
                uuid = getOwnerIdentity(owner);
                if (uuid != null) {
                    object.setOwner(uuid);
                }
                cachedUUID.put(owner, uuid);
            }
        }

        return uuid;
    }

    /**
     * Converts saved owner names to UUID and returns result.
     */
    private static UUID getOwnerIdentity(String name)
    {
        final GameProfile[] gameProfileResult = new GameProfile[1];
        ProfileLookupCallback profilelookupcallback = new ProfileLookupCallback() {
            @Override
            public void onProfileLookupSucceeded(GameProfile gameProfile) {
                gameProfileResult[0] = gameProfile;
            }
            @Override
            public void onProfileLookupFailed(GameProfile gameProfile, Exception exception) {
                gameProfileResult[0] = null;
            }
        };
        MinecraftServer.getServer().func_152359_aw().findProfilesByNames(new String[] { name }, Agent.MINECRAFT, profilelookupcallback);
        return gameProfileResult[0] != null ? gameProfileResult[0].getId() : null;
    }

}

@@ -636,25 +636,24 @@
 bject);%0A
-%0A
         
 if (uuid
@@ -648,183 +648,78 @@
     
-if (uuid != null) %7B%0A            return object.getOwner().equals(entityPlayer.getUniqueID().toString());%0A        %7D else %7B%0A            boolean result = object.getOwner().equals(
+String ident = uuid != null ? entityPlayer.getUniqueID().toString() : 
 enti
@@ -743,17 +743,16 @@
 ayName()
-)
 ;%0A%0A     
@@ -758,229 +758,47 @@
     
-    // Attempt to update owner format manually, which seems to happen%0A            // when it is not converted on world load (for reasons unknown).%0A            updateOwnerUUID(object);%0A%0A            return result;%0A        %7D
+return object.getOwner().equals(ident);
 %0A   

Remove redundant code.