Newer
Older
ChallengeSystem / src / main / java / de / fanta / challenges / Challenges.java
@fantahund fantahund on 27 Jul 2020 9 KB Add Event Mode
/*TODO
Backpack Reset
Gamerule Buttons
*/

package de.fanta.challenges;

import de.fanta.challenges.challenges.RandomDrops;
import de.fanta.challenges.scoreboard.ScoreBoardManager;
import de.fanta.challenges.commands.CommandRegistration;
import de.fanta.challenges.events.PlayerCountChangedEvent;
import de.fanta.challenges.events.ServerStatusChangedEvent;
import de.fanta.challenges.events.TimerChangedEvent;
import de.fanta.challenges.guis.BackpackGui;
import de.fanta.challenges.guis.ResetGui;
import de.fanta.challenges.guis.TimerGui;
import de.fanta.challenges.guis.settingsgui.MainGui;
import de.fanta.challenges.guis.settingsgui.OtherSettingsGui;
import de.fanta.challenges.guis.settingsgui.ServerSettingsGui;
import de.fanta.challenges.guis.settingsgui.SettingsGui;
import de.fanta.challenges.listeners.BlockCreateListener;
import de.fanta.challenges.listeners.BlockDestroyListener;
import de.fanta.challenges.listeners.DamageListener;
import de.fanta.challenges.listeners.DeathListener;
import de.fanta.challenges.listeners.InteractListener;
import de.fanta.challenges.listeners.InventoryClickListener;
import de.fanta.challenges.listeners.PlayerListener;
import de.fanta.challenges.listeners.QuitJoinListener;
import de.fanta.challenges.scoreboard.ScoreManager;
import de.fanta.challenges.utils.ChatUtil;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

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

public class Challenges extends JavaPlugin {
    private static Challenges plugin;

    private File backpackConfigFile;

    private FileConfiguration BackpackConfig;

    private File RndDropsConfigFile;

    private FileConfiguration RndDropsConfig;

    private Timer timer;

    private BackpackGui backpack;

    private ScoreBoardManager sbManager;
    private ScoreManager scoreManager;

    public RandomDrops rndDrops;

    public static Challenges getPlugin() {
        return plugin;
    }

    public static final String PREFIX = ChatColor.of("#455aad") + "[" + ChatUtil.NORMAL + "Challenge" + ChatColor.of("#455aad") + "]";
    public static final String GUIPREFIX = ChatColor.of("#2d8745") + "Challenge";

    private Player currentEditor;

    private boolean waitingForShutdown;

    @Override
    public void onEnable() {
        plugin = this;

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

        this.scoreManager = new ScoreManager(this);

        this.backpack = new BackpackGui(getConfig().getInt("backpack_size") * 9);

        new CommandRegistration(this).registerCommands();

        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);
        Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
        Bukkit.getPluginManager().registerEvents(new TimerGui(), this);
        Bukkit.getPluginManager().registerEvents(new ResetGui(), this);
        Bukkit.getPluginManager().registerEvents(new MainGui(), this);
        Bukkit.getPluginManager().registerEvents(new SettingsGui(), this);
        Bukkit.getPluginManager().registerEvents(new ServerSettingsGui(), this);
        Bukkit.getPluginManager().registerEvents(new OtherSettingsGui(), this);

        saveDefaultConfig();
        reloadConfig();
        createRndDropsConfig();
        createBackpackConfig();

        this.backpack.loadInventoryFromConfig();
        getLogger().info("Plugin loaded!");
        this.sbManager = new ScoreBoardManager(this);

        timer.setTime(getConfig().getInt("timertime"));

        Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
            World world = Bukkit.getWorld("world");
            world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
            checkMLGWorld();
        }, 1L);

        if (getConfig().getBoolean("World_Reset")) {
            File config = new File(plugin.getDataFolder(), "config.yml");
            config.delete();
            reloadConfig();
            saveConfig();
            this.backpack.clearConfig();
            this.rndDrops.shuffleItems();
            this.rndDrops.saveItems();
            this.rndDrops.loadItems();
        } else {
            this.rndDrops.loadItems();
            this.backpack.loadInventoryFromConfig();
        }

        Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
            Bukkit.getPluginManager().callEvent(new ServerStatusChangedEvent(true));
            Bukkit.getPluginManager().callEvent(new TimerChangedEvent(timer.isRunning()));
            Bukkit.getPluginManager().callEvent(new PlayerCountChangedEvent(Bukkit.getOnlinePlayers().size()));
        }, 200L);
    }

    @Override
    public void onDisable() {
        Bukkit.getBanList(BanList.Type.NAME).getBanEntries().forEach(b -> Bukkit.getBanList(BanList.Type.NAME).pardon(b.getTarget()));
        if (!getConfig().getBoolean("World_Reset")) {
            this.backpack.saveInventoryToConfig();getConfig().set("backpack_size", backpack.getSize() / 9);
        } else {
            this.backpack.clearConfig();
        }
        try {
            this.RndDropsConfig.save(this.RndDropsConfigFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        getConfig().set("timertime", timer.getTime());
        saveConfig();
        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 (InvalidConfigurationException | IOException 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 ScoreBoardManager getSBManager() {
        return this.sbManager;
    }

    public void checkMLGWorld() {
        World mlgWorld = Bukkit.getWorld("mlg_challenge");
        if (mlgWorld == null) {
            mlgWorld = Bukkit.createWorld((new WorldCreator("mlg_challenge")).type(WorldType.FLAT).generateStructures(false));
            mlgWorld.setAnimalSpawnLimit(0);
            mlgWorld.setGameRule(GameRule.DO_MOB_SPAWNING, false);
            mlgWorld.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
            mlgWorld.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
            mlgWorld.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) {
                metalore.addAll(Arrays.asList(lore));
            }
            meta.setLore(metalore);
            item.setItemMeta(meta);
        }
        return item;
    }

    public Player getCurrentEditor() {
        return currentEditor;
    }

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

    public boolean hasEditor() {
        return currentEditor != null;
    }

    public boolean isEditor(Player player) {
        return hasEditor() && currentEditor.getUniqueId().equals(player.getUniqueId());
    }

    public Timer getTimer() {
        return timer;
    }

    public boolean isWaitingForShutdown() {
        return waitingForShutdown;
    }

    public void setWaitingForShutdown(boolean waitingForShutdown) {
        this.waitingForShutdown = waitingForShutdown;
    }

    public BackpackGui getBackpack() {
        return backpack;
    }

    public File getBackpackConfigFile() {
        return backpackConfigFile;
    }

    public ScoreManager getScoreManager() {
        return scoreManager;
    }
}