Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / Challenges.java
@fantahund fantahund on 24 Jun 2020 11 KB Hier tolle Commit Nachricht einfügen!
/*TODO
Backpack
MLG-Challenges machen*/

package de.fanta.challenges;

import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import de.fanta.challenges.commands.BackpackCommand;
import de.fanta.challenges.challenges.RandomDrops;
import de.fanta.challenges.challenges.SBManager;
import de.fanta.challenges.commands.ChallengesCommand;
import de.fanta.challenges.commands.coords.CoordsCommand;
import de.fanta.challenges.commands.coords.CoordsDeleteCommand;
import de.fanta.challenges.commands.coords.CoordsGetCommand;
import de.fanta.challenges.commands.coords.CoordsSaveCommand;
import de.fanta.challenges.commands.coords.CoordsShareCommand;
import de.fanta.challenges.commands.ResetCommand;
import de.fanta.challenges.commands.revive.ReviveCommandTODO;
import de.fanta.challenges.commands.settings.SettingsCommandTODO;
import de.fanta.challenges.commands.timer.TimerForcemlgCommand;
import de.fanta.challenges.commands.timer.TimerPauseCommand;
import de.fanta.challenges.commands.timer.TimerResetCommand;
import de.fanta.challenges.commands.timer.TimerReverseCommand;
import de.fanta.challenges.commands.timer.TimerSetCommand;
import de.fanta.challenges.commands.timer.TimerStartCommand;
import de.fanta.challenges.commands.timer.TimerUtil;
import de.fanta.challenges.commands.hp.HPAddCommand;
import de.fanta.challenges.commands.hp.HPGetCommand;
import de.fanta.challenges.commands.hp.HPMaxCommand;
import de.fanta.challenges.commands.hp.HPRemoveCommand;
import de.fanta.challenges.commands.hp.HPSetCommand;
import de.fanta.challenges.events.BlockCreateListener;
import de.fanta.challenges.events.BlockDestroyListener;
import de.fanta.challenges.events.DamageListener;
import de.fanta.challenges.events.DeathListener;
import de.fanta.challenges.events.InteractListener;
import de.fanta.challenges.events.InventoryClickListener;
import de.fanta.challenges.events.QuitJoinListener;
import de.fanta.challenges.gui.InventoryHandler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import de.iani.cubesideutils.bukkit.commands.CommandRouter;
import me.wolfyscript.utilities.api.WolfyUtilities;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class Challenges extends JavaPlugin {
    private static Challenges plugin;

    private File BackpackConfigFile;

    private FileConfiguration BackpackConfig;

    private File RndDropsConfigFile;

    private FileConfiguration RndDropsConfig;

    private TimerUtil Timer;

    private BackpackCommand backpack;

    private SBManager sbManager;

    private RandomDrops rndDrops;

    public static Inventory SettingsGUI = Bukkit.createInventory(null, 45, "ยง6Settings");

    public static String GUIName = "ยง6Settings";

    private WolfyUtilities wolfyUtilities;
    private InventoryHandler inventoryHandler;

    public static Challenges getplugin() {
        return plugin;
    }

    public static final String PREFIX = ChatColor.BLUE + "[" + ChatColor.GREEN + "Challenge" + ChatColor.BLUE + "]";

    private Player currentEditor;

    @Override
    public void onEnable() {
        plugin = this;
        wolfyUtilities = WolfyUtilities.getOrCreateAPI(this);
        wolfyUtilities.setCHAT_PREFIX(PREFIX);
        wolfyUtilities.setCONSOLE_PREFIX(PREFIX);

        this.Timer = new TimerUtil(this);
        this.rndDrops = new RandomDrops();

        this.inventoryHandler = new InventoryHandler(this);
        this.inventoryHandler.init();

        this.backpack = new BackpackCommand(this);

        getCommand("settings").setExecutor(new SettingsCommandTODO(this));
        getCommand("revive").setExecutor(new ReviveCommandTODO(this));

        CommandRouter challengesRouter = new CommandRouter(getCommand("challenges"));
        challengesRouter.addCommandMapping(new ChallengesCommand(this));

        CommandRouter resetRouter = new CommandRouter(getCommand("reset"));
        resetRouter.addCommandMapping(new ResetCommand(this));

        CommandRouter backpackRouter = new CommandRouter(getCommand("backpack"));
        backpackRouter.addCommandMapping(backpack);


        CommandRouter coordsRouter = new CommandRouter(getCommand("coords"));
        coordsRouter.addCommandMapping(new CoordsCommand(this));
        coordsRouter.addCommandMapping(new CoordsShareCommand(this), "share");
        coordsRouter.addCommandMapping(new CoordsGetCommand(this), "get");
        coordsRouter.addCommandMapping(new CoordsSaveCommand(this), "save");
        coordsRouter.addCommandMapping(new CoordsDeleteCommand(this), "delete");

        CommandRouter hpRouter = new CommandRouter(getCommand("hp"));
        hpRouter.addCommandMapping(new HPAddCommand(this), "add");
        hpRouter.addCommandMapping(new HPGetCommand(this), "get");
        hpRouter.addCommandMapping(new HPMaxCommand(this), "max");
        hpRouter.addCommandMapping(new HPRemoveCommand(this), "remove");
        hpRouter.addCommandMapping(new HPSetCommand(this), "set");

        CommandRouter timerRouter = new CommandRouter(getCommand("timer"));
        timerRouter.addCommandMapping(new TimerForcemlgCommand(this), "forcemlg");
        timerRouter.addCommandMapping(new TimerPauseCommand(this), "pause");
        timerRouter.addCommandMapping(new TimerResetCommand(this), "reset");
        timerRouter.addCommandMapping(new TimerReverseCommand(this), "reverse");
        timerRouter.addCommandMapping(new TimerSetCommand(this), "set");
        timerRouter.addCommandMapping(new TimerStartCommand(this), "start");

        Bukkit.getPluginManager().registerEvents(new InventoryClickListener(), this);
        Bukkit.getPluginManager().registerEvents(new QuitJoinListener(), this);
        Bukkit.getPluginManager().registerEvents(new BlockDestroyListener(), this);
        Bukkit.getPluginManager().registerEvents(new BlockCreateListener(), this);
        Bukkit.getPluginManager().registerEvents(new InteractListener(), this);
        Bukkit.getPluginManager().registerEvents(new DamageListener(), this);
        Bukkit.getPluginManager().registerEvents(new DeathListener(), this);

        saveDefaultConfig();
        reloadConfig();
        createRndDropsConfig();
        createBackpackConfig();
        if (getConfig().getBoolean("World_Reset")) {
            getLogger().info("/Reset Command was executed!");
            getLogger().info("Preparing world reset!");
            String worldname = getConfig().getString("World_Name");
            String nethername = worldname + "_nether";
            String endname = worldname + "_the_end";
            File worldfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + worldname);
            File netherfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + nethername);
            File endfolder = new File(Bukkit.getWorldContainer().getPath() + "/" + endname);
            File mlgfolder = new File(Bukkit.getWorldContainer().getPath() + "/mlg_challenge");

            try {
                MoreFiles.deleteRecursively(worldfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
                MoreFiles.deleteRecursively(netherfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
                MoreFiles.deleteRecursively(endfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
                MoreFiles.deleteRecursively(mlgfolder.toPath(), new RecursiveDeleteOption[] { RecursiveDeleteOption.ALLOW_INSECURE });
                getLogger().info("World reset successful!");
            } catch (IOException e) {
                getLogger().info("World reset failed!");
                e.printStackTrace();
            }
            getConfig().set("World_Reset", Boolean.valueOf(false));
            getConfig().set("timer.enabled", Boolean.valueOf(false));
            getConfig().set("timer.time", Integer.valueOf(0));
            getConfig().set("timer.sek", "00");
            getConfig().set("timer.min", "00");
            getConfig().set("timer.hrs", Integer.valueOf(0));
            saveConfig();
            this.backpack.clearConfig();
            this.rndDrops.shuffleItems();
            this.rndDrops.saveItems();
            this.rndDrops.loadItems();
        } else {
            this.rndDrops.loadItems();
        }
        this.backpack.loadInventoryFromConfig();
        this.Timer.LoadTimer();
        getLogger().info("Plugin loaded!");
        this.sbManager = new SBManager();
    }

    @Override
    public void onDisable() {
        this.backpack.saveInventoryToConfig();
        try {
            this.BackpackConfig.save(this.BackpackConfigFile);
            this.RndDropsConfig.save(this.RndDropsConfigFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        getLogger().info("Plugin unloaded");
    }

    public RandomDrops getRandomDropsManager() {
        return this.rndDrops;
    }

    public FileConfiguration getBackpackConfig() {
        return this.BackpackConfig;
    }

    private void createBackpackConfig() {
        this.BackpackConfigFile = new File(getDataFolder(), "backpack.yml");
        if (!this.BackpackConfigFile.exists()) {
            this.BackpackConfigFile.getParentFile().mkdirs();
            saveResource("backpack.yml", false);
        }
        this.BackpackConfig = new YamlConfiguration();
        try {
            this.BackpackConfig.load(this.BackpackConfigFile);
        } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }

    public FileConfiguration getRndDropsConfig() {
        return this.RndDropsConfig;
    }

    private void createRndDropsConfig() {
        this.RndDropsConfigFile = new File(getDataFolder(), "rnddrops.yml");
        if (!this.RndDropsConfigFile.exists()) {
            this.RndDropsConfigFile.getParentFile().mkdirs();
            saveResource("rnddrops.yml", false);
        }
        this.RndDropsConfig = new YamlConfiguration();
        try {
            this.RndDropsConfig.load(this.RndDropsConfigFile);
        } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }

    public SBManager getSBManager() {
        return this.sbManager;
    }

    @SuppressWarnings("deprecation")
    public void CheckMLGWorld() {
        if (Bukkit.getWorld("mlg_challenge") == null) {
            Bukkit.createWorld((new WorldCreator("mlg_challenge")).type(WorldType.FLAT).generateStructures(false));
            Bukkit.getWorld("mlg_challenge").setAnimalSpawnLimit(0);
            Bukkit.getWorld("mlg_challenge").setGameRuleValue("doMobSpawning", "false");
            Bukkit.getWorld("mlg_challenge").setGameRuleValue("doDaylightCycle", "false");
            Bukkit.getWorld("mlg_challenge").setGameRuleValue("doWeatherCycle", "false");
            Bukkit.getWorld("mlg_challenge").setTime(6000L);
        }
    }

    public ItemStack addGUIItem(Material mat, String name, String... lore) {
        ItemStack item = new ItemStack(mat, 1);
        ItemMeta meta = item.getItemMeta();
        if (mat != Material.AIR) {
            meta.setDisplayName(name);
            ArrayList<String> metalore = new ArrayList<>();
            if (lore != null) {
                for (String lorecomments : lore) {
                    metalore.add(lorecomments);
                }
            }
            meta.setLore(metalore);
            item.setItemMeta(meta);
        }
        return item;
    }

    public Player getCurrentEditor() {
        return currentEditor;
    }

    public void setCurrentEditor(Player currentEditor) {
        this.currentEditor = currentEditor;
    }
}