Search in sources :

Example 1 with RepositoryCallback

use of de.Keyle.MyPet.api.repository.RepositoryCallback 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), o.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:");
                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 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:
                                sender.sendMessage(Util.formatText(Translation.getString("Message.Call.Dead", 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.fanciful.FancyMessage) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) List(java.util.List) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 2 with RepositoryCallback

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

the class PlayerListener method on.

@EventHandler
public void on(final PlayerJoinEvent event) {
    long delay = MyPetApi.getRepository() instanceof SqLiteRepository ? 1L : Configuration.Repository.EXTERNAL_LOAD_DELAY;
    new BukkitRunnable() {

        @Override
        public void run() {
            MyPetApi.getRepository().getMyPetPlayer(event.getPlayer(), new RepositoryCallback<MyPetPlayer>() {

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

                            @Override
                            public void callback(StoredMyPet storedMyPet) {
                                MyPetApi.getMyPetManager().activateMyPet(storedMyPet);
                                if (joinedPlayer.hasMyPet()) {
                                    final MyPet myPet = joinedPlayer.getMyPet();
                                    if (myPet.wantsToRespawn()) {
                                        switch(myPet.createEntity()) {
                                            case Canceled:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case NotAllowed:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case Dead:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Respawn.In", joinedPlayer), myPet.getPetName(), myPet.getRespawnTime()));
                                                break;
                                            case Flying:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", joinedPlayer), myPet.getPetName()));
                                                break;
                                            case NoSpace:
                                                joinedPlayer.sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", joinedPlayer), myPet.getPetName()));
                                                break;
                                        }
                                    }
                                }
                            }
                        });
                    }
                    joinedPlayer.checkForDonation();
                    Bukkit.getServer().getPluginManager().callEvent(new MyPetPlayerJoinEvent(joinedPlayer));
                }
            });
        }
    }.runTaskLater(MyPetApi.getPlugin(), delay);
    if (event.getPlayer().isOp() && Updater.isUpdateAvailable()) {
        event.getPlayer().sendMessage(Util.formatText(Translation.getString("Message.Update.Available", event.getPlayer())) + " " + Updater.getLatest());
        event.getPlayer().sendMessage(ChatColor.DARK_GREEN + "    https://mypet-plugin.de/download");
    }
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) MyPetPlayerJoinEvent(de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent) SqLiteRepository(de.Keyle.MyPet.repository.types.SqLiteRepository) UUID(java.util.UUID) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Example 3 with RepositoryCallback

use of de.Keyle.MyPet.api.repository.RepositoryCallback 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 (!Permissions.hasLegacy(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:
                                        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)

Aggregations

WorldGroup (de.Keyle.MyPet.api.WorldGroup)3 MyPet (de.Keyle.MyPet.api.entity.MyPet)3 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)3 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)3 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)3 List (java.util.List)2 UUID (java.util.UUID)2 Player (org.bukkit.entity.Player)2 MyPetPlayerJoinEvent (de.Keyle.MyPet.api.event.MyPetPlayerJoinEvent)1 MyPetSelectionGui (de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui)1 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)1 MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)1 FancyMessage (de.keyle.fanciful.FancyMessage)1 ArrayList (java.util.ArrayList)1 EventHandler (org.bukkit.event.EventHandler)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1