Search in sources :

Example 1 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet 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("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);
    entityRegistry.registerEntityTypes();
    if (getLogger() instanceof MyPetLogger) {
        ((MyPetLogger) getLogger()).updateDebugLoggerLogLevel();
    }
    compatManager.enable();
    getLogger().info("Compat mode for " + compatUtil.getInternalVersion() + " loaded.");
    ConfigurationLoader.loadCompatConfiguration();
    // register leash flags
    registerLeashFlags();
    // register skilltree requirementSettings
    registerSkilltreeRequirements();
    // register exp calculators
    if (!new File(getDataFolder(), "exp.js").exists()) {
        platformHelper.copyResource(this, "exp.js", new File(getDataFolder(), "exp.js"));
    }
    serviceManager.getService(ExperienceCalculatorManager.class).ifPresent(calculatorManager -> {
        calculatorManager.registerCalculator("JS", JavaScriptExperienceCalculator.class);
        calculatorManager.registerCalculator("JavaScript", JavaScriptExperienceCalculator.class);
        calculatorManager.switchCalculator(Configuration.LevelSystem.CALCULATION_MODE.toLowerCase());
    });
    // 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);
    MyPetEntityListener myPetEntityListener = new MyPetEntityListener();
    getServer().getPluginManager().registerEvents(myPetEntityListener, this);
    LevelListener levelupListener = new LevelListener();
    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("petchooseskilltree").setExecutor(new CommandChooseSkilltree());
    getCommand("petbeacon").setExecutor(new CommandBeacon());
    getCommand("petrespawn").setExecutor(new CommandRespawn());
    getCommand("petsettings").setExecutor(new CommandSettings());
    getCommand("petswitch").setExecutor(new CommandSwitch());
    getCommand("petstore").setExecutor(new CommandStore());
    getCommand("petlist").setExecutor(new CommandList());
    getCommand("petcapturehelper").setExecutor(new CommandCaptureHelper());
    getCommand("pettrade").setExecutor(new CommandTrade());
    getCommand("petshop").setExecutor(new CommandShop());
    // load worldgroups
    WorldGroup.loadGroups(new File(getDataFolder().getPath(), "worldgroups.yml"));
    // register skills
    registerSkills();
    // create folders
    File skilltreeFolder = new File(getDataFolder().getPath(), "skilltrees");
    getDataFolder().mkdirs();
    boolean createDefaultSkilltree = skilltreeFolder.mkdirs();
    boolean createLocaleReadme = new File(getDataFolder(), "locale").mkdirs();
    new File(getDataFolder(), "logs").mkdirs();
    if (!createDefaultSkilltree) {
        File legacyDefaultSkilltree = new File(skilltreeFolder, "default.st");
        if (legacyDefaultSkilltree.exists()) {
            if (Util.getSha256FromFile(legacyDefaultSkilltree) == -4323392001800132707L) {
                createDefaultSkilltree = true;
                legacyDefaultSkilltree.delete();
            }
        }
    }
    if (createDefaultSkilltree) {
        File skilltreeFile = new File(skilltreeFolder, "Combat.st.json");
        if (!skilltreeFile.exists()) {
            platformHelper.copyResource(this, "skilltrees/Combat.st.json", new File(skilltreeFolder, "Combat.st.json"));
        }
        skilltreeFile = new File(skilltreeFolder, "Farm.st.json");
        if (!skilltreeFile.exists()) {
            platformHelper.copyResource(this, "skilltrees/Farm.st.json", new File(skilltreeFolder, "Farm.st.json"));
        }
        skilltreeFile = new File(skilltreeFolder, "PvP.st.json");
        if (!skilltreeFile.exists()) {
            platformHelper.copyResource(this, "skilltrees/PvP.st.json", new File(skilltreeFolder, "PvP.st.json"));
        }
        skilltreeFile = new File(skilltreeFolder, "Ride.st.json");
        if (!skilltreeFile.exists()) {
            platformHelper.copyResource(this, "skilltrees/Ride.st.json", new File(skilltreeFolder, "Ride.st.json"));
        }
        skilltreeFile = new File(skilltreeFolder, "Utility.st.json");
        if (!skilltreeFile.exists()) {
            platformHelper.copyResource(this, "skilltrees/Utility.st.json", new File(skilltreeFolder, "Utility.st.json"));
        }
        MyPetApi.getLogger().info("Default skilltree files created.");
    }
    // load skilltrees
    MyPetApi.getSkilltreeManager().clearSkilltrees();
    SkillTreeLoaderJSON.loadSkilltrees(new File(getDataFolder(), "skilltrees"));
    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) {
        }
    }
    if (createLocaleReadme) {
        platformHelper.copyResource(this, "locale-readme.txt", new File(getDataFolder(), "locale" + File.separator + "readme.txt"));
    }
    Translation.init();
    for (Team team : Bukkit.getScoreboardManager().getMainScoreboard().getTeams()) {
        if (team.getName().startsWith("MyPet-")) {
            team.unregister();
        }
    }
    // init repository
    if (Configuration.Repository.REPOSITORY_TYPE.equalsIgnoreCase("NBT")) {
        Configuration.Repository.REPOSITORY_TYPE = "SQLite";
        Configuration.Repository.CONVERT_FROM = "NBT";
        repo = new SqLiteRepository();
        try {
            repo.init();
        } catch (RepositoryInitException e) {
            e.printStackTrace();
            repo = null;
        }
    } else if (Configuration.Repository.REPOSITORY_TYPE.equalsIgnoreCase("MySQL")) {
        MyPetApi.getLogger().info("Connect to MySQL database...");
        repo = new MySqlRepository();
        try {
            repo.init();
            MyPetApi.getLogger().info("MySQL connection successful.");
        } catch (RepositoryInitException e) {
            MyPetApi.getLogger().warning("MySQL connection failed!");
            e.printStackTrace();
            repo = null;
        }
    } else if (Configuration.Repository.REPOSITORY_TYPE.equalsIgnoreCase("MongoDB")) {
        MyPetApi.getLogger().info("Connect to MongoDB database...");
        repo = new MongoDbRepository();
        try {
            repo.init();
            MyPetApi.getLogger().info("MongoDB connection successful.");
        } catch (RepositoryInitException e) {
            MyPetApi.getLogger().warning("MongoDB connection failed!");
            e.printStackTrace();
            repo = null;
        }
    }
    if (repo == null) {
        MyPetApi.getLogger().info("Connect to SQLite database...");
        repo = new SqLiteRepository();
        try {
            repo.init();
            MyPetApi.getLogger().info("SQLite connection successful.");
        } catch (RepositoryInitException ignored) {
            MyPetApi.getLogger().warning("SQLite connection failed!");
            setEnabled(false);
            return;
        }
    }
    Converter.convert();
    if (repo instanceof Scheduler) {
        Timer.addTask((Scheduler) repo);
    }
    File shopConfig = new File(getDataFolder(), "pet-shops.yml");
    if (!shopConfig.exists()) {
        platformHelper.copyResource(this, "pet-shops.yml", shopConfig);
    }
    new ShopManager();
    Timer.startTimer();
    updater.waitForDownload();
    pluginHookManager.enableHooks();
    serviceManager.activate(Load.State.AfterHooks);
    // init Metrics
    try {
        Metrics metrics = new Metrics(this, 778);
        if (metrics.isEnabled()) {
            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;
            }));
            metrics.addCustomChart(new Metrics.AdvancedPie("hooks", () -> {
                Map<String, Integer> activatedHooks = new HashMap<>();
                for (PluginHook hook : MyPetApi.getPluginHookManager().getHooks()) {
                    activatedHooks.put(hook.getPluginName(), 1);
                }
                return activatedHooks;
            }));
        }
    } catch (Throwable e) {
        errorReporter.sendError(e, "Init Metrics failed");
    }
    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 (joinGroup.isDisabled()) {
                                return;
                            }
                            if (onlinePlayer.hasMyPet()) {
                                MyPet myPet = onlinePlayer.getMyPet();
                                if (!myPet.getWorldGroup().equals(joinGroup.getName())) {
                                    myPetManager.deactivateMyPet(onlinePlayer, true);
                                }
                            }
                            if (!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:
                                                            if (Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                                                runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", myPet.getOwner()), myPet.getPetName()));
                                                            } else {
                                                                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 : MySqlRepository(de.Keyle.MyPet.repository.types.MySqlRepository) MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) Metrics(org.bstats.bukkit.Metrics) Updater(de.Keyle.MyPet.util.Updater) Permission(org.bukkit.permissions.Permission) ShopManager(de.Keyle.MyPet.util.shop.ShopManager) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MongoDbRepository(de.Keyle.MyPet.repository.types.MongoDbRepository) MyPetLogger(de.Keyle.MyPet.api.util.logger.MyPetLogger) PluginHook(de.Keyle.MyPet.api.util.hooks.PluginHook) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) Team(org.bukkit.scoreboard.Team) ExperienceCalculatorManager(de.Keyle.MyPet.api.skill.experience.ExperienceCalculatorManager) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable)

Example 2 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class CommandSwitch method onCommand.

public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    if (!(sender instanceof Player)) {
        sender.sendMessage("You can't use this command from server console!");
        return true;
    }
    Player player = (Player) sender;
    if (WorldGroup.getGroupByWorld(player.getWorld()).isDisabled()) {
        player.sendMessage(Translation.getString("Message.No.AllowedHere", player));
        return true;
    }
    if (!Permissions.has(player, "MyPet.command.switch")) {
        player.sendMessage(Translation.getString("Message.No.Allowed", player));
        return true;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
        final MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

            @Override
            public void callback(List<StoredMyPet> pets) {
                if (pets.size() - (owner.hasMyPet() ? 1 : 0) == 0) {
                    owner.sendMessage(Translation.getString("Message.Command.Switch.NoStoredPets", owner));
                    return;
                }
                if (owner.isOnline()) {
                    String worldGroup = WorldGroup.getGroupByWorld(owner.getPlayer().getWorld().getName()).getName();
                    int inactivePetCount = getInactivePetCount(pets, worldGroup);
                    int maxPetCount = getMaxPetCount(owner.getPlayer());
                    String title;
                    if (owner.hasMyPet()) {
                        inactivePetCount--;
                        title = Translation.getString("Message.Npc.SwitchTitle", owner);
                    } else {
                        title = Translation.getString("Message.SelectMyPet", owner);
                    }
                    String stats = "(" + inactivePetCount + "/" + maxPetCount + ")";
                    final MyPetSelectionGui gui = new MyPetSelectionGui(owner, title + " " + stats);
                    gui.open(pets, new RepositoryCallback<StoredMyPet>() {

                        @Override
                        public void callback(StoredMyPet storedMyPet) {
                            Optional<MyPet> activePet = MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                            if (activePet.isPresent() && owner.isOnline()) {
                                Player player = owner.getPlayer();
                                activePet.get().getOwner().sendMessage(Util.formatText(Translation.getString("Message.Npc.ChosenPet", owner), activePet.get().getPetName()));
                                WorldGroup wg = WorldGroup.getGroupByWorld(player.getWorld().getName());
                                owner.setMyPetForWorldGroup(wg, activePet.get().getUUID());
                                switch(activePet.get().createEntity()) {
                                    case Canceled:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", owner), activePet.get().getPetName()));
                                        break;
                                    case NoSpace:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", owner), activePet.get().getPetName()));
                                        break;
                                    case NotAllowed:
                                        owner.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", owner), activePet.get().getPetName()));
                                        break;
                                    case Dead:
                                        if (Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                            owner.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", owner), activePet.get().getPetName()));
                                        } else {
                                            owner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", owner), activePet.get().getPetName(), activePet.get().getRespawnTime()));
                                        }
                                        break;
                                    case Spectator:
                                        sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", owner), activePet.get().getPetName()));
                                        break;
                                }
                            }
                        }
                    });
                }
            }
        });
    } else {
        sender.sendMessage(Translation.getString("Message.No.HasPet", player));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetSelectionGui(de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) ArrayList(java.util.ArrayList) List(java.util.List) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 3 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class CommandOptionSwitch method onCommandOption.

@Override
public boolean onCommandOption(final CommandSender sender, String[] parameter) {
    boolean show = true;
    MyPetPlayer o = null;
    UUID petUUID = null;
    final String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    if (parameter.length == 0) {
        if (sender instanceof Player) {
            Player petOwner = (Player) sender;
            if (MyPetApi.getPlayerManager().isMyPetPlayer(petOwner)) {
                o = MyPetApi.getPlayerManager().getMyPetPlayer(petOwner);
            }
        } else {
            sender.sendMessage("You can't use this command from server console!");
            return true;
        }
    } else if (parameter.length == 1) {
        Player player = Bukkit.getPlayer(parameter[0]);
        if (player == null || !player.isOnline()) {
            sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
            return true;
        }
        if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
            o = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        }
        if (o == null) {
            sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), player.getName()));
        }
    } else if (parameter.length == 2) {
        show = false;
        try {
            o = MyPetApi.getPlayerManager().getMyPetPlayer(UUID.fromString(parameter[0]));
            petUUID = UUID.fromString(parameter[1]);
        } catch (IllegalArgumentException ignored) {
        }
    }
    final MyPetPlayer owner = o;
    if (show && owner != null) {
        MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

            @Override
            public void callback(List<StoredMyPet> value) {
                sender.sendMessage("Select the MyPet you want the player to switch to:");
                if (sender instanceof Player) {
                    boolean doComma = false;
                    FancyMessage message = new FancyMessage("");
                    for (StoredMyPet mypet : value) {
                        if (doComma) {
                            message.then(", ");
                        }
                        message.then(mypet.getPetName()).color(ChatColor.AQUA).command("/petadmin switch " + owner.getInternalUUID() + " " + mypet.getUUID()).itemTooltip(Util.myPetToItemTooltip(mypet, lang));
                        if (!doComma) {
                            doComma = true;
                        }
                    }
                    MyPetApi.getPlatformHelper().sendMessageRaw((Player) sender, message.toJSONString());
                } else {
                    for (StoredMyPet mypet : value) {
                        sender.sendMessage(mypet.getPetName() + "(" + mypet.getPetType().name() + ") -> /petadmin switch " + owner.getInternalUUID() + " " + mypet.getUUID());
                    }
                }
            }
        });
    } else if (!show && owner != null && petUUID != null) {
        MyPetApi.getRepository().getMyPet(petUUID, new RepositoryCallback<StoredMyPet>() {

            @Override
            public void callback(StoredMyPet newPet) {
                if (newPet != null) {
                    if (owner.hasMyPet()) {
                        MyPetApi.getMyPetManager().deactivateMyPet(owner, true);
                    }
                    Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(newPet);
                    sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                    if (myPet.isPresent()) {
                        WorldGroup worldGroup = WorldGroup.getGroupByWorld(owner.getPlayer().getWorld().getName());
                        newPet.setWorldGroup(worldGroup.getName());
                        newPet.getOwner().setMyPetForWorldGroup(worldGroup, newPet.getUUID());
                        owner.sendMessage(Util.formatText(Translation.getString("Message.MultiWorld.NowActivePet", owner), myPet.get().getPetName()));
                        switch(myPet.get().createEntity()) {
                            case Success:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", owner), myPet.get().getPetName()));
                                break;
                            case Canceled:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", owner), myPet.get().getPetName()));
                                break;
                            case NoSpace:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", owner), myPet.get().getPetName()));
                                break;
                            case NotAllowed:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", owner), myPet.get().getPetName()));
                                break;
                            case Dead:
                                if (Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                    sender.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", owner), myPet.get().getPetName()));
                                } else {
                                    sender.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead.Respawn", owner), myPet.get().getPetName(), myPet.get().getRespawnTime()));
                                }
                                break;
                            case Flying:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", owner), myPet.get().getPetName()));
                                break;
                        }
                    }
                }
            }
        });
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) FancyMessage(de.Keyle.MyPet.api.util.chat.FancyMessage) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) List(java.util.List) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 4 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class CommandList method onCommand.

public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    final String lang;
    if (sender instanceof Player) {
        lang = MyPetApi.getPlatformHelper().getPlayerLanguage((Player) sender);
    } else {
        lang = "en";
    }
    final Player petOwner;
    if (args.length <= 0) {
        if (sender instanceof Player) {
            petOwner = (Player) sender;
        } else {
            sender.sendMessage("You can't use this command from server console!");
            return true;
        }
    } else {
        if (sender instanceof Player) {
            if (Permissions.has((Player) sender, "MyPet.admin", false)) {
                petOwner = Bukkit.getPlayer(args[0]);
            } else {
                petOwner = (Player) sender;
            }
        } else {
            sender.sendMessage("You can't use this command from server console!");
            return true;
        }
    }
    if (petOwner == null || !petOwner.isOnline()) {
        sender.sendMessage(Translation.getString("Message.No.PlayerOnline", lang));
        return true;
    }
    final MyPetPlayer owner;
    if (MyPetApi.getPlayerManager().isMyPetPlayer(petOwner)) {
        owner = MyPetApi.getPlayerManager().getMyPetPlayer(petOwner);
    } else {
        sender.sendMessage(Util.formatText(Translation.getString("Message.No.UserHavePet", lang), petOwner.getName()));
        return true;
    }
    if (owner != null) {
        MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

            @Override
            public void callback(List<StoredMyPet> value) {
                if (petOwner == sender) {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.List.Yours", lang), owner.getName()));
                } else {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.List.Player", lang), owner.getName()));
                }
                boolean doComma = false;
                FancyMessage message = new FancyMessage("");
                for (StoredMyPet mypet : value) {
                    if (doComma) {
                        message.then(", ");
                    }
                    message.then(mypet.getPetName()).color(ChatColor.AQUA).itemTooltip(Util.myPetToItemTooltip(mypet, lang));
                    if (!doComma) {
                        doComma = true;
                    }
                }
                MyPetApi.getPlatformHelper().sendMessageRaw((Player) sender, message.toJSONString());
            }
        });
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) List(java.util.List) FancyMessage(de.Keyle.MyPet.api.util.chat.FancyMessage) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 5 with StoredMyPet

use of de.Keyle.MyPet.api.entity.StoredMyPet in project MyPet by xXKeyleXx.

the class Converter method convert.

public static boolean convert() {
    if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase(Configuration.Repository.REPOSITORY_TYPE)) {
        return false;
    }
    Repository fromRepo;
    Repository toRepo;
    if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("NBT")) {
        fromRepo = new NbtRepository();
    } else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("MySQL")) {
        fromRepo = new MySqlRepository();
    } else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("MongoDB")) {
        fromRepo = new MongoDbRepository();
    } else if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("SQLite")) {
        fromRepo = new SqLiteRepository();
    } else {
        return false;
    }
    MyPetApi.getLogger().info("Converting from " + Configuration.Repository.CONVERT_FROM + " to " + Configuration.Repository.REPOSITORY_TYPE + "...");
    try {
        fromRepo.init();
    } catch (RepositoryInitException e) {
        return false;
    }
    toRepo = MyPetApi.getRepository();
    List<MyPetPlayer> playerList = fromRepo.getAllMyPetPlayers();
    if (toRepo instanceof NbtRepository) {
        return false;
    } else if (toRepo instanceof MySqlRepository) {
        ((MySqlRepository) toRepo).addMyPetPlayers(playerList);
    } else if (toRepo instanceof SqLiteRepository) {
        ((SqLiteRepository) toRepo).addMyPetPlayers(playerList);
    } else if (toRepo instanceof MongoDbRepository) {
        HashSet<String> playerNames = new HashSet<>();
        for (MyPetPlayer player : playerList) {
            String playerName = player.getName();
            if (playerNames.contains(playerName)) {
                MyPetApi.getLogger().info("Found duplicate Player: " + player.toString());
                continue;
            }
            playerNames.add(playerName);
            ((MongoDbRepository) toRepo).addMyPetPlayer(player);
        }
    }
    List<StoredMyPet> pets = fromRepo.getAllMyPets();
    if (toRepo instanceof MySqlRepository) {
        ((MySqlRepository) toRepo).addMyPets(pets);
    } else if (toRepo instanceof SqLiteRepository) {
        ((SqLiteRepository) toRepo).addMyPets(pets);
    } else if (toRepo instanceof MongoDbRepository) {
        for (StoredMyPet pet : pets) {
            ((MongoDbRepository) toRepo).addMyPet(pet);
        }
    }
    toRepo.save();
    fromRepo.disable();
    if (Configuration.Repository.CONVERT_FROM.equalsIgnoreCase("NBT")) {
        File nbtFile = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "My.Pets");
        File nbtFileOld = new File(MyPetApi.getPlugin().getDataFolder().getPath() + File.separator + "My.Pets.old");
        nbtFile.renameTo(nbtFileOld);
        MyPetApi.getPlugin().getConfig().set("MyPet.Repository.Type", Configuration.Repository.REPOSITORY_TYPE);
        MyPetApi.getPlugin().getConfig().set("MyPet.Repository.NBT", null);
    }
    MyPetApi.getPlugin().getConfig().set("MyPet.Repository.ConvertFrom", "-");
    MyPetApi.getPlugin().saveConfig();
    MyPetApi.getLogger().info("Conversion from " + Configuration.Repository.CONVERT_FROM + " to " + Configuration.Repository.REPOSITORY_TYPE + " complete!");
    return true;
}
Also used : MySqlRepository(de.Keyle.MyPet.repository.types.MySqlRepository) MongoDbRepository(de.Keyle.MyPet.repository.types.MongoDbRepository) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) NbtRepository(de.Keyle.MyPet.repository.types.NbtRepository) Repository(de.Keyle.MyPet.api.repository.Repository) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) MongoDbRepository(de.Keyle.MyPet.repository.types.MongoDbRepository) NbtRepository(de.Keyle.MyPet.repository.types.NbtRepository) MySqlRepository(de.Keyle.MyPet.repository.types.MySqlRepository) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) File(java.io.File) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) HashSet(java.util.HashSet)

Aggregations

StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)23 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)15 MyPet (de.Keyle.MyPet.api.entity.MyPet)10 Player (org.bukkit.entity.Player)9 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)8 WorldGroup (de.Keyle.MyPet.api.WorldGroup)7 IOException (java.io.IOException)7 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)7 List (java.util.List)6 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)5 Skilltree (de.Keyle.MyPet.api.skill.skilltree.Skilltree)5 UUID (java.util.UUID)5 IconMenu (de.Keyle.MyPet.api.gui.IconMenu)4 IconMenuItem (de.Keyle.MyPet.api.gui.IconMenuItem)4 RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)4 TagCompound (de.keyle.knbt.TagCompound)4 Material (org.bukkit.Material)4 Repository (de.Keyle.MyPet.api.repository.Repository)3 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)3 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)3