001package fr.aumgn.dac2;
002
003import org.bukkit.Bukkit;
004import org.bukkit.plugin.java.JavaPlugin;
005
006import com.google.gson.FieldNamingPolicy;
007import com.google.gson.Gson;
008import com.google.gson.GsonBuilder;
009
010import fr.aumgn.bukkitutils.command.CommandsRegistration;
011import fr.aumgn.bukkitutils.gson.GsonLoadException;
012import fr.aumgn.bukkitutils.gson.GsonLoader;
013import fr.aumgn.bukkitutils.gson.typeadapter.DirectionTypeAdapterFactory;
014import fr.aumgn.dac2.arena.regions.GsonRegionFactory;
015import fr.aumgn.dac2.commands.AdminCommands;
016import fr.aumgn.dac2.commands.ArenasCommands;
017import fr.aumgn.dac2.commands.FillCommands;
018import fr.aumgn.dac2.commands.InfoCommands;
019import fr.aumgn.dac2.commands.PlayerCommands;
020import fr.aumgn.dac2.commands.SetCommands;
021import fr.aumgn.dac2.commands.SpectatorCommands;
022import fr.aumgn.dac2.commands.StageCommands;
023import fr.aumgn.dac2.commands.worldedit.DisabledWorldEditCommands;
024import fr.aumgn.dac2.commands.worldedit.SelectCommands;
025import fr.aumgn.dac2.commands.worldedit.SetWECommands;
026import fr.aumgn.dac2.config.DACConfig;
027
028public class DACPlugin extends JavaPlugin {
029
030    private final static double GSON_VERSION = 1.0;
031
032    private DAC dac;
033
034    @Override
035    public void onEnable() {
036        dac = new DAC(this);
037
038        CommandsRegistration registration = new CommandsRegistration(
039                this, dac.getConfig().getLocale());
040        registration.register(new AdminCommands(dac));
041        registration.register(new ArenasCommands(dac));
042        registration.register(new SetCommands(dac));
043        if (isWorldEditEnabled()) {
044            registration.register(new SetWECommands(dac));
045            registration.register(new SelectCommands(dac));
046        } else {
047            registration.register(new DisabledWorldEditCommands(dac));
048        }
049        registration.register(new StageCommands(dac));
050        registration.register(new FillCommands(dac));
051        registration.register(new PlayerCommands(dac));
052        registration.register(new SpectatorCommands(dac));
053        registration.register(new InfoCommands(dac));
054
055        getLogger().info("Enabled.");
056    }
057
058    public boolean isWorldEditEnabled() {
059        return Bukkit.getPluginManager().isPluginEnabled("WorldEdit");
060    }
061
062    @Override
063    public void onDisable() {
064        getLogger().info("Disabled.");
065    }
066
067    public GsonLoader getGsonLoader() {
068        Gson gson = new GsonBuilder()
069            .registerTypeAdapterFactory(new DirectionTypeAdapterFactory())
070            .registerTypeAdapterFactory(new GsonRegionFactory())
071            .setVersion(GSON_VERSION)
072            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
073            .setPrettyPrinting()
074            .create();
075        return new GsonLoader(gson, this);
076    }
077
078    public DACConfig reloadDACConfig() {
079        try {
080            GsonLoader loader = getGsonLoader();
081            return loader.loadOrCreate("config.json", DACConfig.class);
082         } catch (GsonLoadException exc) {
083             getLogger().warning(
084                     "Unable to load configuration file.");
085             getLogger().warning("Using default values instead.");
086             return new DACConfig();
087         }
088    }
089}