Search in sources :

Example 21 with StoredMyPet

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

the class NbtRepository method getAllMyPets.

// Pets ------------------------------------------------------------------------------------------------------------
@Override
public List<StoredMyPet> getAllMyPets() {
    List<MyPetPlayer> playerList = getAllMyPetPlayers();
    Map<UUID, MyPetPlayer> owners = new HashMap<>();
    for (MyPetPlayer player : playerList) {
        owners.put(player.getInternalUUID(), player);
    }
    List<StoredMyPet> pets = new ArrayList<>();
    for (UUID petUUID : petTags.keySet()) {
        TagCompound petTag = petTags.get(petUUID);
        if (petTag.containsKeyAs("Internal-Owner-UUID", TagString.class)) {
            UUID ownerUUID = UUID.fromString(petTag.getAs("Internal-Owner-UUID", TagString.class).getStringData());
            if (owners.containsKey(ownerUUID)) {
                InactiveMyPet myPet = new InactiveMyPet(owners.get(ownerUUID));
                myPet.load(petTag);
                pets.add(myPet);
            }
        }
    }
    return pets;
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) TagCompound(de.keyle.knbt.TagCompound) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 22 with StoredMyPet

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

the class CommandStore 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;
    }
    final 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.store")) {
        player.sendMessage(Translation.getString("Message.No.Allowed", player));
        return true;
    }
    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
        final MyPetPlayer owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
        final int maxPetCount = getMaxPetCount(owner.getPlayer());
        if (maxPetCount == 0) {
            player.sendMessage(Translation.getString("Message.No.Allowed", player));
            return true;
        }
        if (owner.hasMyPet()) {
            MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

                @Override
                public void callback(List<StoredMyPet> pets) {
                    if (owner.hasMyPet()) {
                        MyPet myPet = owner.getMyPet();
                        String worldGroup = myPet.getWorldGroup();
                        // -1 for active pet
                        int inactivePetCount = getInactivePetCount(pets, worldGroup) - 1;
                        if (inactivePetCount >= maxPetCount) {
                            sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Switch.Limit", player), maxPetCount));
                            return;
                        }
                        if (MyPetApi.getMyPetManager().deactivateMyPet(owner, true)) {
                            owner.setMyPetForWorldGroup(worldGroup, null);
                            sender.sendMessage(Util.formatText(Translation.getString("Message.Command.Switch.Success", player), myPet.getPetName()));
                        }
                    } else {
                        player.sendMessage(Translation.getString("Message.Command.Switch.NoPet", player));
                    }
                }
            });
            return true;
        }
    }
    player.sendMessage(Translation.getString("Message.Command.Switch.NoPet", 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) List(java.util.List) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 23 with StoredMyPet

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