package org.cyclops.cyclopscore.helper;

import com.google.common.collect.Sets;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementManager;
import net.minecraft.advancements.ICriterionTrigger;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.FMLCommonHandler;
import org.cyclops.cyclopscore.CyclopsCore;
import org.cyclops.cyclopscore.helper.obfuscation.ObfuscationHelpers;
import org.cyclops.cyclopscore.network.packet.RequestPlayerAdvancementUnlockedPacket;

import java.util.Set;

/**
 * Helpers related to advancements
 * @author rubensworks
 */
public class AdvancementHelpers {

    public static final Set<ResourceLocation> ACHIEVED_ADVANCEMENTS = Sets.newHashSet();

    public static boolean hasAdvancementUnlocked(EntityPlayer player, Advancement advancement) {
        return player instanceof EntityPlayerMP
                && FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList()
                .getPlayerAdvancements((EntityPlayerMP) player).getProgress(advancement).isDone();
    }

    public static boolean hasAdvancementUnlocked(EntityPlayer player, ResourceLocation advancementId) {
        return ACHIEVED_ADVANCEMENTS.contains(advancementId);
    }

    public static void requestAdvancementUnlockInfo(ResourceLocation advancementId) {
        CyclopsCore._instance.getPacketHandler().sendToServer(new RequestPlayerAdvancementUnlockedPacket(advancementId.toString()));
    }

    public static Advancement getAdvancement(ResourceLocation resourceLocation) {
        return getAdvancementManager().getAdvancement(resourceLocation);
    }

    public static AdvancementManager getAdvancementManager() {
        return FMLCommonHandler.instance().getMinecraftServerInstance().getAdvancementManager();
    }

    public static <T extends ICriterionTrigger> T registerCriteriaTrigger(T criterion) {
        return ObfuscationHelpers.registerCriteriaTrigger(criterion);
    }

}

@@ -223,24 +223,129 @@
 ionTrigger;%0A
+import net.minecraft.client.Minecraft;%0Aimport net.minecraft.client.multiplayer.ClientAdvancementManager;%0A
 import net.m
@@ -1782,34 +1782,201 @@
     
-return getAdvancementManag
+if (MinecraftHelpers.isClientSide()) %7B%0A            return getAdvancementManagerClient().getAdvancementList().getAdvancement(resourceLocation);%0A        %7D%0A        return getAdvancementManagerServ
 er()
@@ -2075,16 +2075,22 @@
 tManager
+Server
 () %7B%0A   
@@ -2182,32 +2182,197 @@
 nager();%0A    %7D%0A%0A
+    public static ClientAdvancementManager getAdvancementManagerClient() %7B%0A        return Minecraft.getMinecraft().player.connection.getAdvancementManager();%0A    %7D%0A%0A
     public stati

Fix NPE on showing advancements in infobooks