Search in sources :

Example 1 with MyPetLogger

use of de.Keyle.MyPet.util.logger.MyPetLogger in project MyPet by xXKeyleXx.

the class MyPetPlugin method onEnable.

public void onEnable() {
    this.isReady = false;
    Updater updater = new Updater("MyPet");
    updater.update();
    if (compatUtil.getInternalVersion() == null || !MyPetVersion.isValidBukkitPacket(compatUtil.getInternalVersion())) {
        getLogger().warning(ChatColor.RED + "This version of MyPet is not compatible with \"" + compatUtil.getInternalVersion() + "\". Is MyPet up to date?");
        updater.waitForDownload();
        setEnabled(false);
        return;
    }
    serviceManager.activate(Load.State.OnEnable);
    pluginHookManager.enableHooks();
    serviceManager.activate(Load.State.AfterHooks);
    entityRegistry.registerEntityTypes();
    if (getLogger() instanceof MyPetLogger) {
        ((MyPetLogger) getLogger()).updateDebugLoggerLogLevel();
    }
    compatManager.enable();
    getLogger().info("Compat mode for " + compatUtil.getInternalVersion() + " loaded.");
    // register event listener
    PlayerListener playerListener = new PlayerListener();
    getServer().getPluginManager().registerEvents(playerListener, this);
    VehicleListener vehicleListener = new VehicleListener();
    getServer().getPluginManager().registerEvents(vehicleListener, this);
    EntityListener entityListener = new EntityListener();
    getServer().getPluginManager().registerEvents(entityListener, this);
    LevelUpListener levelupListener = new LevelUpListener();
    getServer().getPluginManager().registerEvents(levelupListener, this);
    WorldListener worldListener = new WorldListener();
    getServer().getPluginManager().registerEvents(worldListener, this);
    // register commands
    getCommand("petname").setExecutor(new CommandName());
    getCommand("petcall").setExecutor(new CommandCall());
    getCommand("petsendaway").setExecutor(new CommandSendAway());
    getCommand("petstop").setExecutor(new CommandStop());
    getCommand("petrelease").setExecutor(new CommandRelease());
    getCommand("mypet").setExecutor(new CommandHelp());
    getCommand("petinventory").setExecutor(new CommandInventory());
    getCommand("petpickup").setExecutor(new CommandPickup());
    getCommand("petbehavior").setExecutor(new CommandBehavior());
    getCommand("petinfo").setExecutor(new CommandInfo());
    getCommand("mypetadmin").setExecutor(new CommandAdmin());
    getCommand("petskill").setExecutor(new CommandSkill());
    getCommand("petskilltree").setExecutor(new CommandShowSkillTree());
    getCommand("petchooseskilltree").setExecutor(new CommandChooseSkilltree());
    getCommand("petbeacon").setExecutor(new CommandBeacon());
    getCommand("petrespawn").setExecutor(new CommandRespawn());
    getCommand("pettype").setExecutor(new CommandPetType());
    getCommand("petsettings").setExecutor(new CommandSettings());
    getCommand("petswitch").setExecutor(new CommandSwitch());
    getCommand("petstore").setExecutor(new CommandStore());
    getCommand("petlist").setExecutor(new CommandList());
    // register skills
    registerSkillsInfo();
    registerSkills();
    // create folders
    File skilltreeFolder = new File(getDataFolder().getPath(), "skilltrees");
    getDataFolder().mkdirs();
    boolean createDefaultSkilltree = skilltreeFolder.mkdirs();
    new File(getDataFolder(), "locale").mkdirs();
    new File(getDataFolder(), "logs").mkdirs();
    if (createDefaultSkilltree) {
        platformHelper.copyResource(this, "skilltrees/default.st", new File(skilltreeFolder, "default.st"));
        getLogger().info("Default skilltree file created (default.st).");
    }
    // load skilltrees
    List<String> petTypes = new LinkedList<>();
    petTypes.add("default");
    for (MyPetType type : MyPetType.all()) {
        petTypes.add(type.name());
    }
    SkillTreeMobType.clearMobTypes();
    SkillTreeLoaderNBT.getSkilltreeLoader().loadSkillTrees(getDataFolder().getPath() + File.separator + "skilltrees", petTypes);
    Set<String> skilltreeNames = new LinkedHashSet<>();
    for (MyPetType mobType : MyPetType.values()) {
        SkillTreeMobType skillTreeMobType = SkillTreeMobType.byPetType(mobType);
        SkillTreeLoader.addDefault(skillTreeMobType);
        SkillTreeLoader.manageInheritance(skillTreeMobType);
        skilltreeNames.addAll(skillTreeMobType.getSkillTreeNames());
    }
    // register skilltree permissions
    for (String skilltreeName : skilltreeNames) {
        try {
            Bukkit.getPluginManager().addPermission(new Permission("MyPet.skilltree." + skilltreeName));
        } catch (Exception ignored) {
        }
    }
    for (int i = 0; i <= Configuration.Misc.MAX_STORED_PET_COUNT; i++) {
        try {
            Bukkit.getPluginManager().addPermission(new Permission("MyPet.petstorage.limit." + i));
        } catch (Exception ignored) {
        }
    }
    File translationReadme = new File(getDataFolder(), "locale" + File.separator + "readme.txt");
    if (!translationReadme.exists()) {
        platformHelper.copyResource(this, "locale-readme.txt", translationReadme);
    }
    Translation.init();
    for (Team team : Bukkit.getScoreboardManager().getMainScoreboard().getTeams()) {
        if (team.getName().startsWith("MyPet-")) {
            team.unregister();
        }
    }
    // init repository
    repo = new SqLiteRepository();
    try {
        repo.init();
    } catch (RepositoryInitException ignored) {
        setEnabled(false);
        return;
    }
    File nbtFile = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "My.Pets");
    if (nbtFile.exists()) {
        Converter.convert();
    }
    if (repo instanceof Scheduler) {
        Timer.addTask((Scheduler) repo);
    }
    // load worldgroups
    loadGroups(new File(getDataFolder().getPath(), "worldgroups.yml"));
    Timer.startTimer();
    // init Metrics
    Metrics metrics = new Metrics(this);
    metrics.addCustomChart(new Metrics.SingleLineChart("active_pets", () -> myPetManager.countActiveMyPets()));
    metrics.addCustomChart(new Metrics.SimplePie("build", MyPetVersion::getBuild));
    metrics.addCustomChart(new Metrics.SimplePie("update_mode", () -> {
        String mode = "Disabled";
        if (Configuration.Update.CHECK) {
            mode = "Check";
            if (Configuration.Update.DOWNLOAD) {
                mode += " & Download";
            }
        }
        return mode;
    }));
    updater.waitForDownload();
    getLogger().info("version " + MyPetVersion.getVersion() + "-b" + MyPetVersion.getBuild() + ChatColor.GREEN + " ENABLED");
    this.isReady = true;
    serviceManager.activate(Load.State.OnReady);
    // load pets for online players
    new BukkitRunnable() {

        @Override
        public void run() {
            for (final Player player : getServer().getOnlinePlayers()) {
                repo.getMyPetPlayer(player, new RepositoryCallback<MyPetPlayer>() {

                    @Override
                    public void callback(final MyPetPlayer p) {
                        if (p != null) {
                            final MyPetPlayerImpl onlinePlayer = (MyPetPlayerImpl) p;
                            onlinePlayer.setLastKnownName(player.getName());
                            if (!player.getUniqueId().equals(onlinePlayer.getOfflineUUID())) {
                                if (onlinePlayer.getMojangUUID() == null) {
                                    onlinePlayer.setMojangUUID(player.getUniqueId());
                                }
                                onlinePlayer.setOnlineMode(true);
                            }
                            playerManager.setOnline(onlinePlayer);
                            final WorldGroup joinGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
                            if (onlinePlayer.hasMyPet()) {
                                MyPet myPet = onlinePlayer.getMyPet();
                                if (!myPet.getWorldGroup().equals(joinGroup.getName())) {
                                    myPetManager.deactivateMyPet(onlinePlayer, true);
                                }
                            }
                            if (joinGroup != null && !onlinePlayer.hasMyPet() && onlinePlayer.hasMyPetInWorldGroup(joinGroup.getName())) {
                                final UUID petUUID = onlinePlayer.getMyPetForWorldGroup(joinGroup.getName());
                                MyPetApi.getRepository().getMyPet(petUUID, new RepositoryCallback<StoredMyPet>() {

                                    @Override
                                    public void callback(StoredMyPet storedMyPet) {
                                        myPetManager.activateMyPet(storedMyPet);
                                        if (onlinePlayer.hasMyPet()) {
                                            final MyPet myPet = onlinePlayer.getMyPet();
                                            final MyPetPlayer myPetPlayer = myPet.getOwner();
                                            if (myPet.wantsToRespawn()) {
                                                if (myPetPlayer.hasMyPet()) {
                                                    MyPet runMyPet = myPetPlayer.getMyPet();
                                                    switch(runMyPet.createEntity()) {
                                                        case Canceled:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPet.getOwner()), runMyPet.getPetName()));
                                                            break;
                                                        case NoSpace:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPet.getOwner()), runMyPet.getPetName()));
                                                            break;
                                                        case NotAllowed:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", myPet.getOwner()), myPet.getPetName()));
                                                            break;
                                                        case Dead:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", myPet.getOwner()), myPet.getPetName(), myPet.getRespawnTime()));
                                                            break;
                                                        case Flying:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", myPet.getOwner()), myPet.getPetName()));
                                                            break;
                                                        case Success:
                                                            runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", myPet.getOwner()), runMyPet.getPetName()));
                                                            break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                });
                            }
                            onlinePlayer.checkForDonation();
                        }
                    }
                });
            }
        }
    }.runTaskLater(this, 0);
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) Metrics(org.bstats.bukkit.Metrics) Updater(de.Keyle.MyPet.util.Updater) SkillTreeMobType(de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType) Permission(org.bukkit.permissions.Permission) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetLogger(de.Keyle.MyPet.util.logger.MyPetLogger) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) File(java.io.File) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Team(org.bukkit.scoreboard.Team) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Example 2 with MyPetLogger

use of de.Keyle.MyPet.util.logger.MyPetLogger in project MyPet by xXKeyleXx.

the class CommandOptionReload method onCommandOption.

@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
    int oldMaxPetCount = Configuration.Misc.MAX_STORED_PET_COUNT;
    ConfigurationLoader.loadConfiguration();
    ConfigurationLoader.loadCompatConfiguration();
    if (MyPetApi.getLogger() instanceof MyPetLogger) {
        ((MyPetLogger) MyPetApi.getLogger()).updateDebugLoggerLogLevel();
    }
    Translation.init();
    if (Configuration.Misc.MAX_STORED_PET_COUNT > oldMaxPetCount) {
        for (int i = oldMaxPetCount + 1; i <= Configuration.Misc.MAX_STORED_PET_COUNT; i++) {
            try {
                Bukkit.getPluginManager().addPermission(new Permission("MyPet.petstorage.limit." + i));
            } catch (Exception ignored) {
            }
        }
    } else if (oldMaxPetCount > Configuration.Misc.MAX_STORED_PET_COUNT) {
        for (int i = oldMaxPetCount; i > Configuration.Misc.MAX_STORED_PET_COUNT; i--) {
            try {
                Bukkit.getPluginManager().removePermission("MyPet.petstorage.limit." + i);
            } catch (Exception ignored) {
            }
        }
    }
    sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] config loaded!");
    return true;
}
Also used : Permission(org.bukkit.permissions.Permission) MyPetLogger(de.Keyle.MyPet.util.logger.MyPetLogger)

Aggregations

MyPetLogger (de.Keyle.MyPet.util.logger.MyPetLogger)2 Permission (org.bukkit.permissions.Permission)2 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 SkillTreeMobType (de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType)1 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)1 Updater (de.Keyle.MyPet.util.Updater)1 MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)1 File (java.io.File)1 Metrics (org.bstats.bukkit.Metrics)1 Player (org.bukkit.entity.Player)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1 Team (org.bukkit.scoreboard.Team)1