Search in sources :

Example 1 with MyPetSelectionGui

use of de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui 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)1 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)1 MyPetSelectionGui (de.Keyle.MyPet.gui.selectionmenu.MyPetSelectionGui)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Player (org.bukkit.entity.Player)1