Search in sources :

Example 1 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer 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 MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class CommandSettingHealthbar method onCommandOption.

@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
    if (sender instanceof Player && MyPetApi.getPlayerManager().isMyPetPlayer((Player) sender)) {
        MyPetPlayer myPetPlayer = MyPetApi.getPlayerManager().getMyPetPlayer((Player) sender);
        myPetPlayer.setHealthBarActive(!myPetPlayer.isHealthBarActive());
        sender.sendMessage(Translation.getString("Message.Command.Success", sender));
        return true;
    }
    sender.sendMessage(Translation.getString("Message.Command.Fail", sender));
    return true;
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer)

Example 3 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class EntityListener method onMyPet.

@EventHandler
public void onMyPet(final EntityDamageEvent event) {
    if (event.getEntity() instanceof MyPetBukkitEntity) {
        MyPetBukkitEntity bukkitEntity = (MyPetBukkitEntity) event.getEntity();
        if (event.getCause() == DamageCause.SUFFOCATION) {
            final MyPet myPet = bukkitEntity.getMyPet();
            final MyPetPlayer myPetPlayer = myPet.getOwner();
            myPet.removePet();
            myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Despawn", myPetPlayer.getLanguage()), myPet.getPetName()));
            MyPetApi.getPlugin().getServer().getScheduler().runTaskLater(MyPetApi.getPlugin(), new Runnable() {

                public void run() {
                    if (myPetPlayer.hasMyPet()) {
                        MyPet runMyPet = myPetPlayer.getMyPet();
                        switch(runMyPet.createEntity()) {
                            case Canceled:
                                runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Prevent", myPet.getOwner()), runMyPet.getPetName()));
                                break;
                            case NoSpace:
                                runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.NoSpace", myPet.getOwner()), runMyPet.getPetName()));
                                break;
                            case NotAllowed:
                                runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.No.AllowedHere", myPet.getOwner()), myPet.getPetName()));
                                break;
                            case Flying:
                                runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Spawn.Flying", myPet.getOwner()), myPet.getPetName()));
                                break;
                            case Success:
                                if (runMyPet != myPet) {
                                    runMyPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.Command.Call.Success", myPet.getOwner().getLanguage()), runMyPet.getPetName()));
                                }
                                break;
                        }
                    }
                }
            }, 10L);
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EventHandler(org.bukkit.event.EventHandler)

Example 4 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class EntityListener method on.

@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
    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() == MyPetApi.getPlugin()) {
                            leashItem = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItem == null) {
                        return;
                    }
                }
                if (projectile.hasMetadata("")) {
                    metaList = projectile.getMetadata("MyPetLeashItemArrow");
                    for (MetadataValue meta : metaList) {
                        if (meta.getOwningPlugin() == MyPetApi.getPlugin()) {
                            leashItemArrow = (ItemStack) meta.value();
                            break;
                        }
                    }
                    if (leashItemArrow == null) {
                        return;
                    }
                }
            } 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)) {
                LivingEntity leashTarget = (LivingEntity) event.getEntity();
                MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
                ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
                if (!Permissions.hasLegacy(player, "MyPet.leash.", petType.name())) {
                    return;
                }
                boolean usedArrow = false;
                if (!neededLeashItem.compare(leashItem)) {
                    if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
                        return;
                    } else {
                        usedArrow = true;
                    }
                }
                for (PlayerLeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(PlayerLeashEntityHook.class)) {
                    if (!hook.canLeash(player, leashTarget)) {
                        return;
                    }
                }
                boolean willBeLeashed = true;
                flagLoop: for (LeashFlag flag : MyPetApi.getMyPetInfo().getLeashFlags(petType)) {
                    switch(flag) {
                        case Adult:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = ((Ageable) leashTarget).isAdult();
                            } else if (leashTarget instanceof Zombie) {
                                willBeLeashed = !((Zombie) leashTarget).isBaby();
                            }
                            break;
                        case Baby:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = !((Ageable) leashTarget).isAdult();
                            } else if (leashTarget instanceof Zombie) {
                                willBeLeashed = ((Zombie) leashTarget).isBaby();
                            }
                            break;
                        case LowHp:
                            willBeLeashed = ((leashTarget.getHealth() - event.getDamage()) * 100) / leashTarget.getMaxHealth() <= 10;
                            break;
                        case UserCreated:
                            if (leashTarget instanceof IronGolem) {
                                willBeLeashed = ((IronGolem) leashTarget).isPlayerCreated();
                            }
                            break;
                        case Wild:
                            if (leashTarget instanceof IronGolem) {
                                willBeLeashed = !((IronGolem) leashTarget).isPlayerCreated();
                            } else if (leashTarget instanceof Tameable) {
                                willBeLeashed = !((Tameable) leashTarget).isTamed();
                            } else if (leashTarget instanceof Horse) {
                                willBeLeashed = !((Horse) leashTarget).isTamed();
                            }
                            break;
                        case Tamed:
                            if (leashTarget instanceof Tameable) {
                                willBeLeashed = ((Tameable) leashTarget).isTamed() && ((Tameable) leashTarget).getOwner() == player;
                            } else if (leashTarget instanceof Horse) {
                                willBeLeashed = ((Horse) leashTarget).isTamed() && ((Horse) leashTarget).getOwner() == player;
                            }
                            break;
                        case CanBreed:
                            if (leashTarget instanceof Ageable) {
                                willBeLeashed = ((Ageable) leashTarget).canBreed();
                            }
                            break;
                        case Angry:
                            if (leashTarget instanceof Wolf) {
                                willBeLeashed = ((Wolf) leashTarget).isAngry();
                            }
                            break;
                        case Impossible:
                            willBeLeashed = false;
                            break flagLoop;
                        case None:
                            willBeLeashed = true;
                            break flagLoop;
                    }
                    if (!willBeLeashed) {
                        break;
                    }
                }
                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().getLanguage()));
                    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);
                    if (converter.isPresent()) {
                        inactiveMyPet.setInfo(converter.get().convertEntity(leashTarget));
                    }
                    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);
                    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);
                            if (myPet.isPresent()) {
                                myPet.get().createEntity();
                            }
                        }
                    });
                }
            }
        }
    }
}
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) PlayerLeashEntityHook(de.Keyle.MyPet.api.util.hooks.types.PlayerLeashEntityHook) MetadataValue(org.bukkit.metadata.MetadataValue) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Optional(com.google.common.base.Optional) WorldGroup(de.Keyle.MyPet.api.WorldGroup) EntityMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Example 5 with MyPetPlayer

use of de.Keyle.MyPet.api.player.MyPetPlayer in project MyPet by xXKeyleXx.

the class PlayerListener method on.

@EventHandler(priority = EventPriority.LOWEST)
public void on(PlayerQuitEvent event) {
    if (MyPetApi.getPlayerManager().isMyPetPlayer(event.getPlayer())) {
        MyPetPlayer player = MyPetApi.getPlayerManager().getMyPetPlayer(event.getPlayer());
        if (player.hasMyPet()) {
            MyPet myPet = player.getMyPet();
            if (myPet.getStatus() == MyPet.PetState.Here) {
                myPet.removePet(true);
            }
            MyPetApi.getMyPetManager().deactivateMyPet(player, true);
        }
        MyPetApi.getPlayerManager().setOffline(player);
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) EventHandler(org.bukkit.event.EventHandler)

Aggregations

MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)46 EventHandler (org.bukkit.event.EventHandler)19 MyPet (de.Keyle.MyPet.api.entity.MyPet)17 Player (org.bukkit.entity.Player)16 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)14 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)8 WorldGroup (de.Keyle.MyPet.api.WorldGroup)7 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)6 TagCompound (de.keyle.knbt.TagCompound)5 List (java.util.List)5 UUID (java.util.UUID)4 Optional (com.google.common.base.Optional)3 RepositoryCallback (de.Keyle.MyPet.api.repository.RepositoryCallback)3 SkillTreeMobType (de.Keyle.MyPet.api.skill.skilltree.SkillTreeMobType)3 SqLiteRepository (de.Keyle.MyPet.repository.types.SqLiteRepository)3 MyPetPlayerImpl (de.Keyle.MyPet.util.player.MyPetPlayerImpl)3 FancyMessage (de.keyle.fanciful.FancyMessage)3 IOException (java.io.IOException)3 MyPetSaveEvent (de.Keyle.MyPet.api.event.MyPetSaveEvent)2 ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)2