package org.cyclops.cyclopscore.config.extendedconfig;

import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.IParticleFactory;
import net.minecraft.client.particle.ParticleManager;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ParticleType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import org.cyclops.cyclopscore.config.ConfigurableType;
import org.cyclops.cyclopscore.init.ModBase;

import javax.annotation.Nullable;
import java.util.function.Function;

/**
 * Config for recipe serializers.
 * @author rubensworks
 * @see ExtendedConfig
 */
public abstract class ParticleConfig<T extends IParticleData> extends ExtendedConfigForge<ParticleConfig<T>, ParticleType<T>> {

    /**
     * Create a new config
     *
     * @param mod                The mod instance.
     * @param namedId            A unique name id
     * @param elementConstructor The element constructor.
     */
    public ParticleConfig(ModBase mod, String namedId, Function<ParticleConfig<T>, ? extends ParticleType<T>> elementConstructor) {
        super(mod, namedId, elementConstructor);
    }

    @Override
    public String getTranslationKey() {
        return "gui." + getMod().getModId() + "." + getNamedId();
	}

    // Needed for config gui
    @Override
    public String getFullTranslationKey() {
        return getTranslationKey();
    }
    
    @Override
	public ConfigurableType getConfigurableType() {
		return ConfigurableType.PARTICLE;
	}

    @Override
    public IForgeRegistry<? super ParticleType<T>> getRegistry() {
        return ForgeRegistries.PARTICLE_TYPES;
    }

    @OnlyIn(Dist.CLIENT)
    @Nullable
    public abstract IParticleFactory<T> getParticleFactory();

    @OnlyIn(Dist.CLIENT)
    @Nullable
    public abstract ParticleManager.IParticleMetaFactory<T> getParticleMetaFactory();

    @OnlyIn(Dist.CLIENT)
    @Override
    public void onRegistered() {
        super.onRegistered();
        IParticleFactory<T> factory = getParticleFactory();
        if (factory != null) {
            Minecraft.getInstance().particles.registerFactory(getInstance(), factory);
        }
        ParticleManager.IParticleMetaFactory<T> metaFactory = getParticleMetaFactory();
        if (metaFactory != null) {
            Minecraft.getInstance().particles.registerFactory(getInstance(), metaFactory);
        }
    }

}

@@ -384,16 +384,249 @@
 OnlyIn;%0A
+import net.minecraftforge.client.event.ParticleFactoryRegisterEvent;%0Aimport net.minecraftforge.eventbus.api.SubscribeEvent;%0Aimport net.minecraftforge.fml.common.Mod;%0Aimport net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;%0A
 import n
@@ -671,16 +671,16 @@
 stries;%0A
-
 import n
@@ -1504,16 +1504,88 @@
 uctor);%0A
+        FMLJavaModLoadingContext.get().getModEventBus().register(this);%0A
     %7D%0A%0A 
@@ -2333,32 +2333,38 @@
 LIENT)%0A    @
-Override
+SubscribeEvent
 %0A    public 
@@ -2374,52 +2374,69 @@
 d on
-Registered() %7B%0A        super.onRegistered();
+ParticleFactoryRegister(ParticleFactoryRegisterEvent event) %7B
 %0A   

Fix particles not being registered correctly