/*
 * This file is part of UltimateCore, licensed under the MIT License (MIT).
 *
 * Copyright (c) Bammerbom
 *
 * 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 bammerbom.ultimatecore.sponge.modules.blood;

import bammerbom.ultimatecore.sponge.UltimateCore;
import bammerbom.ultimatecore.sponge.api.config.config.module.ModuleConfig;
import bammerbom.ultimatecore.sponge.api.config.config.module.RawModuleConfig;
import bammerbom.ultimatecore.sponge.api.module.Module;
import bammerbom.ultimatecore.sponge.modules.blood.api.BloodEffect;
import bammerbom.ultimatecore.sponge.modules.blood.api.BloodEffects;
import bammerbom.ultimatecore.sponge.modules.blood.listeners.BloodListener;
import bammerbom.ultimatecore.sponge.api.error.utils.ErrorLogger;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
import org.spongepowered.api.CatalogTypes;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.EntityType;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePostInitializationEvent;
import org.spongepowered.api.event.game.state.GameStoppingEvent;
import org.spongepowered.api.text.Text;

import java.util.Optional;

public class BloodModule implements Module {
    ModuleConfig config;

    @Override
    public String getIdentifier() {
        return "blood";
    }

    @Override
    public Text getDescription() {
        return Text.of("Custom 'blood' particles when a player gets damaged");
    }

    @Override
    public Optional<ModuleConfig> getConfig() {
        return Optional.of(config);
    }

    @Override
    public void onRegister() {

    }

    @Override
    public void onInit(GameInitializationEvent event) {
        //Config
        TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(BloodEffect.class), new BloodEffect.BloodEffectSerializer());
        config = new RawModuleConfig("blood");
        //Check if all entity types are in the config
        CommentedConfigurationNode node = config.get();
        boolean modified = false;
        //For every entitytype, if it doesnt exist in the config add it
        for (EntityType type : Sponge.getRegistry().getAllOf(CatalogTypes.ENTITY_TYPE)) {
            if (!Living.class.isAssignableFrom(type.getEntityClass())) {
                continue;
            }
            //If entitytype is not in config
            if (node.getNode("types", type.getId(), "enabled").getValue() == null) {
                modified = true;
                CommentedConfigurationNode typenode = node.getNode("types", type.getId());
                try {
                    typenode.setValue(TypeToken.of(BloodEffect.class), BloodEffects.DEFAULT);
                } catch (ObjectMappingException e) {
                    ErrorLogger.log(e, "Failed to set default blood effect.");
                }
            }
        }
        if (modified) {
            config.save(node);
        }
        //Load blood effects from config
        BloodEffects.reload();
        //Listeners
        Sponge.getEventManager().registerListeners(UltimateCore.get(), new BloodListener());
    }

    @Override
    public void onPostInit(GamePostInitializationEvent event) {

    }

    @Override
    public void onStop(GameStoppingEvent event) {

    }
}

@@ -1433,32 +1433,98 @@
 awModuleConfig;%0A
+import bammerbom.ultimatecore.sponge.api.error.utils.ErrorLogger;%0A
 import bammerbom
@@ -1563,16 +1563,16 @@
 Module;%0A
-
 import b
@@ -1780,74 +1780,8 @@
 er;%0A
-import bammerbom.ultimatecore.sponge.api.error.utils.ErrorLogger;%0A
 impo
@@ -2215,16 +2215,73 @@
 Living;%0A
+import org.spongepowered.api.event.game.GameReloadEvent;%0A
 import o
@@ -2347,148 +2347,8 @@
 nt;%0A
-import org.spongepowered.api.event.game.state.GamePostInitializationEvent;%0Aimport org.spongepowered.api.event.game.state.GameStoppingEvent;%0A
 impo
@@ -2384,16 +2384,50 @@
 .Text;%0A%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -2842,61 +2842,8 @@
  %7D%0A%0A
-    @Override%0A    public void onRegister() %7B%0A%0A    %7D%0A%0A
     
@@ -4364,39 +4364,35 @@
 d on
-PostInit(GamePostInitialization
+Reload(@Nullable GameReload
 Even
@@ -4406,80 +4406,38 @@
 ) %7B%0A
-%0A
     
-%7D%0A%0A
     
-@Override%0A    public void onStop(GameStoppingEvent event) %7B%0A
+BloodEffects.reload();
 %0A   

Fix blood with uc reload