package team.creative.creativecore.client;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;

import net.minecraft.client.Minecraft;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.gui.screens.MenuScreens.ScreenConstructor;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.client.ConfigGuiHandler.ConfigGuiFactory;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.RegisterClientCommandsEvent;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.event.TickEvent.ClientTickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import team.creative.creativecore.CreativeCore;
import team.creative.creativecore.Side;
import team.creative.creativecore.client.render.model.CreativeModelLoader;
import team.creative.creativecore.client.render.model.CreativeRenderBlock;
import team.creative.creativecore.client.render.model.CreativeRenderItem;
import team.creative.creativecore.common.config.gui.ConfigGuiLayer;
import team.creative.creativecore.common.config.holder.CreativeConfigRegistry;
import team.creative.creativecore.common.config.holder.ICreativeConfigHolder;
import team.creative.creativecore.common.gui.IScaleableGuiScreen;
import team.creative.creativecore.common.gui.integration.ContainerIntegration;
import team.creative.creativecore.common.gui.integration.ContainerScreenIntegration;
import team.creative.creativecore.common.gui.integration.GuiEventHandler;
import team.creative.creativecore.common.gui.integration.GuiScreenIntegration;
import team.creative.creativecore.common.gui.style.GuiStyle;
import team.creative.creativecore.common.util.registry.FilteredHandlerRegistry;

public class CreativeCoreClient {
    
    private static Minecraft mc = Minecraft.getInstance();
    
    public static final FilteredHandlerRegistry<Item, CreativeRenderItem> RENDERED_ITEMS = new FilteredHandlerRegistry<>(null);
    public static final FilteredHandlerRegistry<Block, CreativeRenderBlock> RENDERED_BLOCKS = new FilteredHandlerRegistry<>(null);
    
    private static final ItemColor ITEM_COLOR = (stack, tint) -> tint;
    
    public static void registerClientConfig(String modid) {
        ModLoadingContext.get().registerExtensionPoint(ConfigGuiFactory.class, () -> new ConfigGuiFactory((a, b) -> {
            ICreativeConfigHolder holder = CreativeConfigRegistry.ROOT.followPath(modid);
            if (holder != null && !holder.isEmpty(Side.CLIENT))
                return new GuiScreenIntegration(new ConfigGuiLayer(holder, Side.CLIENT));
            return null;
        }));
    }
    
    public static void registerBlocks(CreativeRenderBlock renderer, Block... blocks) {
        for (int i = 0; i < blocks.length; i++)
            RENDERED_BLOCKS.register(blocks[i], renderer);
    }
    
    public static void registerBlock(CreativeRenderBlock renderer, Block block) {
        RENDERED_BLOCKS.register(block, renderer);
    }
    
    public static void registerItem(CreativeRenderItem renderer, Item item) {
        RENDERED_ITEMS.register(item, renderer);
        mc.getItemColors().register(ITEM_COLOR, item);
    }
    
    public static float getDeltaFrameTime() {
        if (mc.isPaused())
            return 1.0F;
        return mc.getDeltaFrameTime();
    }
    
    @SubscribeEvent
    public static void commands(RegisterClientCommandsEvent event) {
        event.getDispatcher().register((LiteralArgumentBuilder<CommandSourceStack>) ((LiteralArgumentBuilder) LiteralArgumentBuilder.literal("cmdclientconfig")).executes((x) -> {
            try {
                GuiEventHandler.queueScreen(new GuiScreenIntegration(new ConfigGuiLayer(CreativeConfigRegistry.ROOT, Side.CLIENT)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }));
    }
    
    public static void init(FMLClientSetupEvent event) {
        GuiStyle.reload();
        Minecraft minecraft = Minecraft.getInstance();
        ReloadableResourceManager reloadableResourceManager = (ReloadableResourceManager) minecraft.getResourceManager();
        
        reloadableResourceManager.registerReloadListener(new PreparableReloadListener() {
            
            @Override
            public CompletableFuture<Void> reload(PreparationBarrier p_10638_, ResourceManager p_10639_, ProfilerFiller p_10640_, ProfilerFiller p_10641_, Executor p_10642_, Executor p_10643_) {
                return CompletableFuture.runAsync(() -> {
                    GuiStyle.reload();
                    for (CreativeRenderItem handler : RENDERED_ITEMS.handlers())
                        handler.reload();
                }, p_10643_);
            }
        });
        
        MenuScreens.register(CreativeCore.GUI_CONTAINER, new ScreenConstructor<ContainerIntegration, ContainerScreenIntegration>() {
            
            @Override
            public ContainerScreenIntegration create(ContainerIntegration container, Inventory inventory, Component p_create_3_) {
                return new ContainerScreenIntegration(container, inventory);
            }
        });
    }
    
    public static void modelEvent(ModelRegistryEvent event) {
        ModelLoaderRegistry.registerLoader(new ResourceLocation(CreativeCore.MODID, "rendered"), new CreativeModelLoader());
    }
    
    @SubscribeEvent
    public static void clientTick(ClientTickEvent event) {
        if (event.phase == Phase.START && Minecraft.getInstance().screen instanceof IScaleableGuiScreen)
            ((IScaleableGuiScreen) Minecraft.getInstance().screen).clientTick();
    }
}

@@ -41,94 +41,8 @@
 t;%0A%0A
-import java.util.concurrent.CompletableFuture;%0Aimport java.util.concurrent.Executor;%0A%0A
 impo
@@ -459,78 +459,8 @@
 on;%0A
-import net.minecraft.server.packs.resources.PreparableReloadListener;%0A
 impo
@@ -579,32 +579,108 @@
 esourceManager;%0A
+import net.minecraft.server.packs.resources.SimplePreparableReloadListener;%0A
 import net.minec
@@ -4934,16 +4934,22 @@
 ner(new 
+Simple
 Preparab
@@ -5021,74 +5021,32 @@
    p
-ublic CompletableFuture%3CVoid%3E reload(PreparationBarrier p_10638_, 
+rotected Object prepare(
 Reso
@@ -5065,11 +5065,11 @@
 p_10
-639
+796
 _, P
@@ -5090,141 +5090,218 @@
 p_10
-640_, ProfilerFiller p_10641_, Executor p_10642_, Executor p_10643_) %7B%0A                return CompletableFuture.runAsync(() -%3E %7B%0A    
+797_) %7B%0A                return GuiStyle.class; // No idea%0A            %7D%0A            %0A            @Override%0A            protected void apply(Object p_10793_, ResourceManager p_10794_, ProfilerFiller p_10795_) %7B%0A
     
@@ -5323,36 +5323,32 @@
 Style.reload();%0A
-    
                 
@@ -5428,20 +5428,16 @@
         
-    
 handler.
@@ -5450,38 +5450,8 @@
 ();%0A
-                %7D, p_10643_);%0A
     

Revert "Fixed resource reloader"