Search in sources :

Example 1 with MyPetSaveEvent

use of de.Keyle.MyPet.api.event.MyPetSaveEvent in project MyPet by xXKeyleXx.

the class MyPetManager method deactivateMyPet.

public boolean deactivateMyPet(MyPetPlayer owner, boolean update) {
    if (mActivePlayerPets.containsKey(owner)) {
        final MyPet myPet = owner.getMyPet();
        MyPetSaveEvent event = new MyPetSaveEvent(myPet);
        Bukkit.getServer().getPluginManager().callEvent(event);
        myPet.removePet();
        if (update) {
            MyPetApi.getRepository().updateMyPet(myPet, null);
        }
        mActivePetsPlayer.remove(myPet);
        return true;
    }
    return false;
}
Also used : MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent)

Example 2 with MyPetSaveEvent

use of de.Keyle.MyPet.api.event.MyPetSaveEvent in project MyPet by xXKeyleXx.

the class CommandOptionCreate method onCommandOption.

@Override
public boolean onCommandOption(final CommandSender sender, String[] args) {
    if (args.length == 0) {
        sender.sendMessage(Translation.getString("Message.Command.Help.MissingParameter", sender));
        sender.sendMessage(" -> " + ChatColor.DARK_AQUA + "/petadmin create " + ChatColor.RED + "<a player name>");
        return false;
    }
    int forceOffset = 0;
    if (args[0].equalsIgnoreCase("-f")) {
        forceOffset = 1;
    }
    if (args.length < 2 + forceOffset) {
        sender.sendMessage(Translation.getString("Message.Command.Help.MissingParameter", sender));
        sender.sendMessage(" -> " + ChatColor.DARK_AQUA + "/petadmin create " + (forceOffset > 0 ? " -f " : "") + args[0] + " " + ChatColor.RED + "<a pet-type>");
        return false;
    }
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    try {
        MyPetType myPetType = MyPetType.byName(args[1 + forceOffset]);
        if (myPetType.checkMinecraftVersion() && MyPetApi.getMyPetInfo().isLeashableEntityType(EntityType.valueOf(myPetType.getBukkitName()))) {
            Player owner = Bukkit.getPlayer(args[forceOffset]);
            if (owner == null || !owner.isOnline()) {
                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
                return true;
            }
            if (WorldGroup.getGroupByWorld(owner.getWorld()).isDisabled()) {
                sender.sendMessage("Pets are not allowed in " + ChatColor.GOLD + owner.getWorld().getName());
                return true;
            }
            final MyPetPlayer newOwner;
            if (MyPetApi.getPlayerManager().isMyPetPlayer(owner)) {
                newOwner = MyPetApi.getPlayerManager().getMyPetPlayer(owner);
                if (newOwner.hasMyPet() && forceOffset == 1) {
                    MyPetApi.getMyPetManager().deactivateMyPet(newOwner, true);
                }
            } else {
                newOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(owner);
            }
            final InactiveMyPet inactiveMyPet = new InactiveMyPet(newOwner);
            inactiveMyPet.setPetType(myPetType);
            inactiveMyPet.setPetName(Translation.getString("Name." + inactiveMyPet.getPetType().name(), inactiveMyPet.getOwner()));
            TagCompound compound = inactiveMyPet.getInfo();
            createInfo(myPetType, Arrays.copyOfRange(args, 2 + forceOffset, args.length), compound);
            updateData(inactiveMyPet, Arrays.copyOfRange(args, 2 + forceOffset, args.length));
            final WorldGroup wg = WorldGroup.getGroupByWorld(owner.getWorld().getName());
            inactiveMyPet.setWorldGroup(wg.getName());
            List<RepositoryMyPetConverterService> converters = MyPetApi.getServiceManager().getServices(RepositoryMyPetConverterService.class);
            for (RepositoryMyPetConverterService converter : converters) {
                converter.convert(inactiveMyPet);
            }
            MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.AdminCommand);
            Bukkit.getServer().getPluginManager().callEvent(createEvent);
            MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
            Bukkit.getServer().getPluginManager().callEvent(saveEvent);
            MyPetApi.getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                @Override
                public void callback(Boolean added) {
                    if (added) {
                        if (!newOwner.hasMyPet()) {
                            inactiveMyPet.getOwner().setMyPetForWorldGroup(wg, inactiveMyPet.getUUID());
                            MyPetApi.getRepository().updateMyPetPlayer(inactiveMyPet.getOwner(), null);
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            if (myPet.isPresent()) {
                                myPet.get().createEntity();
                                sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                            } else {
                                sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] Can't create MyPet for " + newOwner.getName() + ". Is this player online?");
                            }
                        } else {
                            sender.sendMessage(Translation.getString("Message.Command.Success", sender));
                        }
                    }
                }
            });
        }
    } catch (MyPetTypeNotFoundException e) {
        sender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) TagString(de.keyle.knbt.TagString) TagCompound(de.keyle.knbt.TagCompound) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) MyPetTypeNotFoundException(de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)

Example 3 with MyPetSaveEvent

use of de.Keyle.MyPet.api.event.MyPetSaveEvent in project MyPet by xXKeyleXx.

the class CommandOptionClone method onCommandOption.

@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
    if (args.length < 2) {
        return false;
    }
    String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
    Player oldOwner = Bukkit.getPlayer(args[0]);
    if (oldOwner == null || !oldOwner.isOnline()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
        return true;
    }
    final Player newOwner = Bukkit.getPlayer(args[1]);
    if (newOwner == null || !newOwner.isOnline()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
        return true;
    }
    if (!MyPetApi.getPlayerManager().isMyPetPlayer(oldOwner)) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
        return true;
    }
    MyPetPlayer oldPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(oldOwner);
    if (!oldPetOwner.hasMyPet()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
        return true;
    }
    final MyPetPlayer newPetOwner;
    if (MyPetApi.getPlayerManager().isMyPetPlayer(newOwner)) {
        newPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(newOwner);
    } else {
        newPetOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(newOwner);
    }
    if (newPetOwner.hasMyPet()) {
        sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + newOwner.getName() + " has already an active MyPet!");
        return true;
    }
    MyPet oldPet = oldPetOwner.getMyPet();
    final InactiveMyPet newPet = new InactiveMyPet(newPetOwner);
    newPet.setPetName(oldPet.getPetName());
    newPet.setWorldGroup(oldPet.getWorldGroup());
    newPet.setExp(oldPet.getExperience().getExp());
    newPet.setHealth(oldPet.getHealth());
    newPet.setSaturation(oldPet.getSaturation());
    newPet.setRespawnTime(oldPet.getRespawnTime());
    newPet.setInfo(oldPet.getInfo());
    newPet.setPetType(oldPet.getPetType());
    newPet.setSkilltree(oldPet.getSkilltree());
    newPet.setSkills(oldPet.getSkillInfo());
    MyPetSaveEvent event = new MyPetSaveEvent(newPet);
    Bukkit.getServer().getPluginManager().callEvent(event);
    MyPetApi.getRepository().addMyPet(newPet, new RepositoryCallback<Boolean>() {

        @Override
        public void callback(Boolean value) {
            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(newPet);
            if (myPet.isPresent()) {
                WorldGroup worldGroup = WorldGroup.getGroupByWorld(newPet.getOwner().getPlayer().getWorld().getName());
                newPet.setWorldGroup(worldGroup.getName());
                newPet.getOwner().setMyPetForWorldGroup(worldGroup, newPet.getUUID());
                MyPetApi.getRepository().updateMyPetPlayer(newPetOwner, null);
                newOwner.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] MyPet owned by " + newPetOwner.getName() + " successfully cloned!");
            }
        }
    });
    return true;
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) Optional(java.util.Optional) MyPet(de.Keyle.MyPet.api.entity.MyPet) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent)

Example 4 with MyPetSaveEvent

use of de.Keyle.MyPet.api.event.MyPetSaveEvent in project MyPet by xXKeyleXx.

the class EntityListener method on.

@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
    // noinspection ConstantConditions
    if (event.getEntity() == null) {
        // catch invalid events (i.e. EnchantmentAPI)
        return;
    }
    if (WorldGroup.getGroupByWorld(event.getEntity().getWorld()).isDisabled()) {
        return;
    }
    if (!event.getEntity().isDead() && !(event.getEntity() instanceof MyPetBukkitEntity)) {
        if (MyPetApi.getMyPetInfo().isLeashableEntityType(event.getEntity().getType())) {
            ItemStack leashItem = null;
            ItemStack leashItemArrow = null;
            Player player;
            if (Configuration.Misc.ALLOW_RANGED_LEASHING && event.getDamager() instanceof Projectile) {
                Projectile projectile = (Projectile) event.getDamager();
                if (!(projectile.getShooter() instanceof Player)) {
                    return;
                }
                player = (Player) projectile.getShooter();
                List<MetadataValue> metaList;
                if (projectile.hasMetadata("MyPetLeashItem")) {
                    metaList = projectile.getMetadata("MyPetLeashItem");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin().getName().equals("MyPet")) {
                            leashItem = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItem == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItem", MyPetApi.getPlugin());
                }
                if (projectile.hasMetadata("MyPetLeashItemArrow")) {
                    metaList = projectile.getMetadata("MyPetLeashItemArrow");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin().getName().equals("MyPet")) {
                            leashItemArrow = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItemArrow == null) {
                        return;
                    }
                    projectile.removeMetadata("MyPetLeashItemArrow", MyPetApi.getPlugin());
                }
            } else if (event.getDamager() instanceof Player) {
                player = (Player) event.getDamager();
                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                    leashItem = player.getEquipment().getItemInMainHand();
                } else {
                    leashItem = player.getItemInHand();
                }
            } else {
                return;
            }
            if (!MyPetApi.getMyPetManager().hasActiveMyPet(player) && !justLeashed.contains(player.getUniqueId())) {
                LivingEntity leashTarget = (LivingEntity) event.getEntity();
                MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
                ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
                if (!Permissions.has(player, "MyPet.leash." + petType.name())) {
                    return;
                }
                boolean usedArrow = false;
                if (!neededLeashItem.compare(leashItem)) {
                    if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
                        return;
                    } else {
                        usedArrow = true;
                    }
                }
                for (LeashHook hook : MyPetApi.getPluginHookManager().getHooks(LeashHook.class)) {
                    if (!hook.canLeash(player, leashTarget)) {
                        return;
                    }
                }
                boolean willBeLeashed = true;
                for (Settings flagSettings : MyPetApi.getMyPetInfo().getLeashFlagSettings(petType)) {
                    String flagName = flagSettings.getName();
                    LeashFlag flag = MyPetApi.getLeashFlagManager().getLeashFlag(flagName);
                    if (flag == null) {
                        MyPetApi.getLogger().warning("\"" + flagName + "\" is not a valid leash requirement!");
                        continue;
                    }
                    MyPetPlayer myPetPlayer = null;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    }
                    if (!flag.check(player, leashTarget, event.getDamage(), flagSettings)) {
                        willBeLeashed = false;
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(false) + message, 10000);
                                }
                            }
                        }
                    } else {
                        if (myPetPlayer != null) {
                            if (myPetPlayer.isCaptureHelperActive()) {
                                String message = flag.getMissingMessage(player, leashTarget, event.getDamage(), flagSettings);
                                if (message != null) {
                                    myPetPlayer.sendMessage(LeashFlag.getMessagePrefix(true) + message, 10000);
                                }
                            }
                        }
                    }
                }
                if (willBeLeashed) {
                    event.setCancelled(true);
                    final MyPetPlayer owner;
                    if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                        owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    } else {
                        owner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                    }
                    final InactiveMyPet inactiveMyPet = new InactiveMyPet(owner);
                    inactiveMyPet.setPetType(petType);
                    inactiveMyPet.setPetName(Translation.getString("Name." + petType.name(), inactiveMyPet.getOwner()));
                    WorldGroup worldGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
                    inactiveMyPet.setWorldGroup(worldGroup.getName());
                    inactiveMyPet.getOwner().setMyPetForWorldGroup(worldGroup, inactiveMyPet.getUUID());
                    /*
                        if(leashTarget.getCustomName() != null)
                        {
                            inactiveMyPet.setPetName(leashTarget.getCustomName());
                        }
                        */
                    Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
                    converter.ifPresent(service -> inactiveMyPet.setInfo(service.convertEntity(leashTarget)));
                    boolean remove = true;
                    for (LeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(LeashEntityHook.class)) {
                        if (!hook.prepare(leashTarget)) {
                            remove = false;
                        }
                    }
                    if (remove) {
                        leashTarget.remove();
                    }
                    if (!usedArrow) {
                        if (Configuration.Misc.CONSUME_LEASH_ITEM && player.getGameMode() != GameMode.CREATIVE && leashItem != null) {
                            if (leashItem.getAmount() > 1) {
                                leashItem.setAmount(leashItem.getAmount() - 1);
                            } else {
                                if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                                    player.getEquipment().setItemInMainHand(null);
                                } else {
                                    player.setItemInHand(null);
                                }
                            }
                        }
                    }
                    MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.Leash);
                    Bukkit.getServer().getPluginManager().callEvent(createEvent);
                    MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
                    Bukkit.getServer().getPluginManager().callEvent(saveEvent);
                    justLeashed.add(player.getUniqueId());
                    MyPetApi.getPlugin().getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {

                        @Override
                        public void callback(Boolean value) {
                            owner.sendMessage(Translation.getString("Message.Leash.Add", owner));
                            Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
                            myPet.ifPresent(MyPet::createEntity);
                            if (owner.isCaptureHelperActive()) {
                                owner.setCaptureHelperActive(false);
                                owner.sendMessage(Util.formatText(Translation.getString("Message.Command.CaptureHelper.Mode", owner), Translation.getString("Name.Disabled", owner)));
                            }
                            justLeashed.remove(player.getUniqueId());
                        }
                    });
                }
            }
        }
    }
}
Also used : EntityConverterService(de.Keyle.MyPet.api.util.service.types.EntityConverterService) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) MyPetSaveEvent(de.Keyle.MyPet.api.event.MyPetSaveEvent) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) Settings(de.Keyle.MyPet.api.util.configuration.settings.Settings) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MetadataValue(org.bukkit.metadata.MetadataValue) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) LeashEntityHook(de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook) WorldGroup(de.Keyle.MyPet.api.WorldGroup) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) LeashFlag(de.Keyle.MyPet.api.entity.leashing.LeashFlag) ItemStack(org.bukkit.inventory.ItemStack) LeashHook(de.Keyle.MyPet.api.util.hooks.types.LeashHook) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) EventHandler(org.bukkit.event.EventHandler)

Example 5 with MyPetSaveEvent

use of de.Keyle.MyPet.api.event.MyPetSaveEvent 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

MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)5 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)4 WorldGroup (de.Keyle.MyPet.api.WorldGroup)3 MyPet (de.Keyle.MyPet.api.entity.MyPet)3 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)3 Player (org.bukkit.entity.Player)3 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)2 MyPetCreateEvent (de.Keyle.MyPet.api.event.MyPetCreateEvent)2 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)1 LeashFlag (de.Keyle.MyPet.api.entity.leashing.LeashFlag)1 MyPetTypeNotFoundException (de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)1 Repository (de.Keyle.MyPet.api.repository.Repository)1 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)1 FancyMessage (de.Keyle.MyPet.api.util.chat.FancyMessage)1 Settings (de.Keyle.MyPet.api.util.configuration.settings.Settings)1 LeashEntityHook (de.Keyle.MyPet.api.util.hooks.types.LeashEntityHook)1 LeashHook (de.Keyle.MyPet.api.util.hooks.types.LeashHook)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1 RepositoryMyPetConverterService (de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService)1