Search in sources :

Example 1 with Repository

use of de.Keyle.MyPet.api.repository.Repository 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)

Example 2 with Repository

use of de.Keyle.MyPet.api.repository.Repository in project MyPet by xXKeyleXx.

the class CommandTrade method onCommand.

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (sender instanceof Player) {
        Player player = (Player) sender;
        if (WorldGroup.getGroupByWorld(player.getWorld()).isDisabled()) {
            player.sendMessage(Translation.getString("Message.No.AllowedHere", player));
            return true;
        }
        if (args.length == 0) {
            return false;
        }
        if (args[0].equalsIgnoreCase("accept")) {
            if (offers.containsKey(player.getUniqueId())) {
                Offer offer = offers.get(player.getUniqueId());
                Player owner = Bukkit.getServer().getPlayer(offer.getOwner());
                if (owner == null || !owner.isOnline()) {
                    sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.PetUnavailable", player));
                    offers.remove(player.getUniqueId());
                    return true;
                }
                if (!Permissions.has(player, "MyPet.command.trade.receive." + offer.getPet().getPetType().name())) {
                    sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.NoPermission", player));
                    owner.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Reject", owner), player.getName(), offer.getPet().getPetName()));
                    offers.remove(player.getUniqueId());
                    return true;
                }
                if (MyPetApi.getPlayerManager().isMyPetPlayer(owner)) {
                    final MyPetPlayer oldOwner = MyPetApi.getPlayerManager().getMyPetPlayer(owner);
                    if (!oldOwner.hasMyPet() || oldOwner.getMyPet() != offer.getPet()) {
                        sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.PetUnavailable", player));
                        offers.remove(player.getUniqueId());
                        return true;
                    }
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player) && MyPetApi.getMyPetManager().hasActiveMyPet(player)) {
                        sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.HasPet", player));
                        return true;
                    }
                    if (!player.getWorld().equals(owner.getWorld()) || MyPetApi.getPlatformHelper().distanceSquared(player.getLocation(), owner.getLocation()) > 100) {
                        sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.Distance", player), owner.getName()));
                        return true;
                    }
                    if (offer.getPrice() > 0) {
                        if (!MyPetApi.getHookHelper().isEconomyEnabled()) {
                            player.sendMessage(Translation.getString("Message.No.Economy", player));
                            return true;
                        }
                        if (!MyPetApi.getHookHelper().getEconomy().transfer(player, owner, offer.getPrice())) {
                            sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.NotEnoughMoney", player), MyPetApi.getHookHelper().getEconomy().format(offer.getPrice())));
                            return true;
                        }
                    }
                    offers.remove(player.getUniqueId());
                    final MyPetPlayer newOwner = MyPetApi.getPlayerManager().isMyPetPlayer(player) ? MyPetApi.getPlayerManager().getMyPetPlayer(player) : MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                    final String worldGroup = offer.getPet().getWorldGroup();
                    MyPetApi.getMyPetManager().deactivateMyPet(oldOwner, false);
                    final StoredMyPet pet = MyPetApi.getMyPetManager().getInactiveMyPetFromMyPet(offer.getPet());
                    final Repository repo = MyPetApi.getRepository();
                    repo.removeMyPet(pet, new RepositoryCallback<Boolean>() {

                        @Override
                        public void callback(Boolean value) {
                            pet.setOwner(newOwner);
                            MyPetSaveEvent event = new MyPetSaveEvent(pet);
                            Bukkit.getServer().getPluginManager().callEvent(event);
                            repo.addMyPet(pet, null);
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(pet);
                            oldOwner.setMyPetForWorldGroup(worldGroup, null);
                            newOwner.setMyPetForWorldGroup(worldGroup, pet.getUUID());
                            repo.updateMyPetPlayer(oldOwner, null);
                            repo.updateMyPetPlayer(newOwner, null);
                            if (myPet.isPresent()) {
                                newOwner.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.Success", newOwner), oldOwner.getName(), myPet.get().getPetName()));
                                oldOwner.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Success", oldOwner), newOwner.getName(), myPet.get().getPetName()));
                                switch(myPet.get().createEntity()) {
                                    case Canceled:
                                        newOwner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", newOwner), myPet.get().getPetName()));
                                        break;
                                    case NoSpace:
                                        newOwner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", newOwner), myPet.get().getPetName()));
                                        break;
                                    case NotAllowed:
                                        newOwner.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", newOwner), myPet.get().getPetName()));
                                        break;
                                    case Dead:
                                        if (!Configuration.Respawn.DISABLE_AUTO_RESPAWN) {
                                            newOwner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", newOwner), myPet.get().getPetName(), myPet.get().getRespawnTime()));
                                        }
                                        break;
                                    case Spectator:
                                        newOwner.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Spectator", newOwner), myPet.get().getPetName()));
                                        break;
                                }
                            } else {
                                newOwner.sendMessage(Translation.getString("Message.Command.Trade.Receiver.Error", newOwner));
                            }
                        }
                    });
                } else {
                    sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.PetUnavailable", player));
                    offers.remove(player.getUniqueId());
                    return true;
                }
            } else {
                sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.NoOffer", player));
            }
            return true;
        } else if (args[0].equalsIgnoreCase("reject")) {
            if (offers.containsKey(player.getUniqueId())) {
                Offer offer = offers.get(player.getUniqueId());
                Player owner = Bukkit.getServer().getPlayer(offer.getOwner());
                if (owner != null && owner.isOnline()) {
                    owner.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Reject", owner), player.getName(), offer.getPet().getPetName()));
                }
                sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.Reject", player), offer.getOwnerName()));
                offers.remove(player.getUniqueId());
            } else {
                sender.sendMessage(Translation.getString("Message.Command.Trade.Receiver.NoOffer", player));
            }
            return true;
        } else if (args[0].equalsIgnoreCase("cancel")) {
            UUID ownerUUID = player.getUniqueId();
            for (Offer offer : offers.values()) {
                if (offer.getOwner().equals(ownerUUID)) {
                    offers.remove(offer.getReceiver());
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Cancel", player), offer.getReceiverName()));
                    Player receiver = Bukkit.getPlayer(offer.getReceiver());
                    if (receiver != null && receiver.isOnline()) {
                        receiver.sendMessage(Translation.getString("Message.Command.Trade.Receiver.PetUnavailable", player));
                    }
                    return true;
                }
            }
            sender.sendMessage(Translation.getString("Message.Command.Trade.Owner.NoOffer", player));
            return true;
        } else {
            if (MyPetApi.getMyPetManager().hasActiveMyPet(player)) {
                MyPet myPet = MyPetApi.getMyPetManager().getMyPet(player);
                if (!Permissions.has((Player) sender, "MyPet.command.trade.offer." + myPet.getPetType().name())) {
                    player.sendMessage(Translation.getString("Message.No.Allowed", player));
                    return true;
                }
                Player receiver = Bukkit.getPlayer(args[0]);
                if (receiver == null) {
                    sender.sendMessage(Translation.getString("Message.No.PlayerOnline", player));
                    return true;
                }
                if (offers.containsKey(receiver.getUniqueId())) {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.OpenOffer", player), receiver.getName()));
                    return true;
                }
                if (receiver.equals(player)) {
                    sender.sendMessage(Translation.getString("Message.Command.Trade.Owner.Yourself", player));
                    return true;
                }
                double price = 0;
                if (args.length >= 2) {
                    if (MyPetApi.getHookHelper().isEconomyEnabled()) {
                        if (Util.isDouble(args[1])) {
                            price = Double.parseDouble(args[1]);
                        } else {
                            receiver.sendMessage(Translation.getString("Message.Command.Trade.Owner.InvalidPrice", player));
                            return true;
                        }
                    } else {
                        sender.sendMessage(Translation.getString("Message.No.Economy", player));
                        return true;
                    }
                }
                Offer offer = new Offer(price, myPet, player.getUniqueId(), receiver.getUniqueId(), receiver.getName(), player.getName());
                offers.put(receiver.getUniqueId(), offer);
                if (price > 0) {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Offer.Price", player), myPet.getPetName(), receiver.getName(), MyPetApi.getHookHelper().getEconomy().format(price)));
                    receiver.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.Offer.Price", receiver), player.getName(), MyPetApi.getHookHelper().getEconomy().format(price)));
                } else {
                    sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Owner.Offer", player), myPet.getPetName(), receiver.getName()));
                    receiver.sendMessage(Util.formatText(Translation.getString("Message.Command.Trade.Receiver.Offer", receiver), player.getName()));
                }
                FancyMessage petMessage = new FancyMessage(" »» ").then(myPet.getPetName()).itemTooltip(Util.myPetToItemTooltip(myPet, MyPetApi.getPlatformHelper().getPlayerLanguage(receiver))).command("/pettrade accept");
                MyPetApi.getPlatformHelper().sendMessageRaw(receiver, petMessage.toJSONString());
                return true;
            } else {
                sender.sendMessage(Translation.getString("Message.No.HasPet", player));
                return true;
            }
        }
    }
    sender.sendMessage("You can't use this command from server console!");
    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) FancyMessage(de.Keyle.MyPet.api.util.chat.FancyMessage) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) Repository(de.Keyle.MyPet.api.repository.Repository) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Aggregations

StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)2 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)2 Repository (de.Keyle.MyPet.api.repository.Repository)2 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)1 RepositoryInitException (de.Keyle.MyPet.api.repository.RepositoryInitException)1 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)1 MongoDbRepository (de.Keyle.MyPet.repository.types.MongoDbRepository)1 MySqlRepository (de.Keyle.MyPet.repository.types.MySqlRepository)1 NbtRepository (de.Keyle.MyPet.repository.types.NbtRepository)1 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Player (org.bukkit.entity.Player)1