Search in sources :

Example 1 with ConfigItem

use of de.Keyle.MyPet.api.util.ConfigItem 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 2 with ConfigItem

use of de.Keyle.MyPet.api.util.ConfigItem in project MyPet by xXKeyleXx.

the class ConfigurationLoader method loadCompatConfiguration.

public static void loadCompatConfiguration() {
    FileConfiguration config = MyPetApi.getPlugin().getConfig();
    Skilltree.Skill.CONTROL_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Skill.Control.Item", "" + Material.LEASH.getId()));
    Skilltree.Skill.Ride.RIDE_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Skill.Ride.Item", "" + Material.LEASH.getId()));
    File petConfigFile = new File(MyPetApi.getPlugin().getDataFolder().getPath(), "pet-config.yml");
    if (petConfigFile.exists()) {
        YamlConfiguration ymlcnf = new YamlConfiguration();
        try {
            ymlcnf.load(petConfigFile);
            config = ymlcnf;
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }
    MyPet.Chicken.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Chicken.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Cow.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Cow.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Horse.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Horse.GrowUpItem", "" + Material.BREAD.getId()));
    MyPet.Llama.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Llama.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Mooshroom.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Mooshroom.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Ocelot.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Ocelot.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Pig.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Pig.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Sheep.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Sheep.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Villager.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Villager.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Wolf.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Wolf.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Zombie.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Zombie.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.PigZombie.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.PigZombie.GrowUpItem", "" + Material.POTION.getId()));
    MyPet.Rabbit.GROW_UP_ITEM = ConfigItem.createConfigItem(config.getString("MyPet.Pets.Rabbit.GrowUpItem", "" + Material.POTION.getId()));
    for (MyPetType petType : MyPetType.values()) {
        DefaultInfo pi = petType.getMyPetClass().getAnnotation(DefaultInfo.class);
        MyPetApi.getMyPetInfo().setStartHP(petType, config.getDouble("MyPet.Pets." + petType.name() + ".HP", pi.hp()));
        MyPetApi.getMyPetInfo().setSpeed(petType, config.getDouble("MyPet.Pets." + petType.name() + ".Speed", pi.walkSpeed()));
        if (config.get("MyPet.Pets." + petType.name() + ".Food") instanceof ArrayList) {
            List<String> foodList = config.getStringList("MyPet.Pets." + petType.name() + ".Food");
            for (String foodString : foodList) {
                ConfigItem ci = ConfigItem.createConfigItem(foodString);
                if (ci.getItem() != null && ci.getItem().getType() != Material.AIR) {
                    MyPetApi.getMyPetInfo().setFood(petType, ci);
                } else {
                    MyPetApi.getLogger().warning(foodString + " is not a valid food item!");
                }
            }
        } else {
            seperateFood(petType, config.getString("MyPet.Pets." + petType.name() + ".Food", "0"));
        }
        seperateLeashFlags(petType, config.getString("MyPet.Pets." + petType + ".LeashFlags", linkLeashFlags(pi.leashFlags())));
        MyPetApi.getMyPetInfo().setCustomRespawnTimeFactor(petType, config.getInt("MyPet.Pets." + petType.name() + ".CustomRespawnTimeFactor", 0));
        MyPetApi.getMyPetInfo().setCustomRespawnTimeFixed(petType, config.getInt("MyPet.Pets." + petType.name() + ".CustomRespawnTimeFixed", 0));
        MyPetApi.getMyPetInfo().setLeashItem(petType, ConfigItem.createConfigItem(config.getString("MyPet.Pets." + petType.name() + ".LeashItem", "" + Material.LEASH.getId())));
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) DefaultInfo(de.Keyle.MyPet.api.entity.DefaultInfo) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) ArrayList(java.util.ArrayList) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) IOException(java.io.IOException) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) File(java.io.File) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException)

Example 3 with ConfigItem

use of de.Keyle.MyPet.api.util.ConfigItem in project MyPet by xXKeyleXx.

the class CommandInfo method onCommand.

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (sender instanceof Player) {
        Player player = (Player) sender;
        MyPetPlayer petOwner;
        if (args.length == 0) {
            if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
                petOwner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
            } else {
                sender.sendMessage(Translation.getString("Message.No.HasPet", player));
                return true;
            }
        } else if (Permissions.hasLegacy(player, "MyPet.command.info.other")) {
            Player p = Bukkit.getServer().getPlayer(args[0]);
            if (p == null || !p.isOnline()) {
                sender.sendMessage(Translation.getString("Message.No.PlayerOnline", player));
                return true;
            }
            if (MyPetApi.getPlayerManager().isMyPetPlayer(args[0])) {
                petOwner = MyPetApi.getPlayerManager().getMyPetPlayer(args[0]);
            } else {
                sender.sendMessage(Util.formatText(Translation.getString("Message.No.UserHavePet", player), args[0]));
                return true;
            }
        } else {
            sender.sendMessage(Translation.getString("Message.No.Allowed", player));
            return true;
        }
        if (petOwner.hasMyPet()) {
            boolean infoShown = false;
            MyPet myPet = petOwner.getMyPet();
            if (canSee(PetInfoDisplay.Name.adminOnly, player, myPet)) {
                player.sendMessage(ChatColor.AQUA + myPet.getPetName() + ChatColor.RESET + ":");
                infoShown = true;
            }
            if (!petOwner.equals(player) && canSee(!PetInfoDisplay.Owner.adminOnly, player, myPet)) {
                player.sendMessage("   " + Translation.getString("Name.Owner", player) + ": " + myPet.getOwner().getName());
                infoShown = true;
            }
            if (canSee(PetInfoDisplay.HP.adminOnly, player, myPet)) {
                String msg;
                if (myPet.getStatus() == PetState.Dead) {
                    msg = ChatColor.RED + Translation.getString("Name.Dead", player);
                } else {
                    if (myPet.getHealth() > myPet.getMaxHealth() / 3 * 2) {
                        msg = "" + ChatColor.GREEN;
                    } else if (myPet.getHealth() > myPet.getMaxHealth() / 3) {
                        msg = "" + ChatColor.YELLOW;
                    } else {
                        msg = "" + ChatColor.RED;
                    }
                    msg += String.format("%1.2f", myPet.getHealth()) + ChatColor.WHITE + "/" + String.format("%1.2f", myPet.getMaxHealth());
                }
                player.sendMessage("   " + Translation.getString("Name.HP", player) + ": " + msg);
                infoShown = true;
            }
            if (canSee(PetInfoDisplay.RespawnTime.adminOnly, player, myPet)) {
                if (myPet.getStatus() == PetState.Dead) {
                    player.sendMessage("   " + Translation.getString("Name.Respawntime", player) + ": " + myPet.getRespawnTime());
                    infoShown = true;
                }
            }
            if (!myPet.isPassiv() && canSee(PetInfoDisplay.Damage.adminOnly, player, myPet)) {
                double damage = (myPet.getSkills().isSkillActive(Damage.class) ? myPet.getSkills().getSkill(Damage.class).get().getDamage() : 0);
                player.sendMessage("   " + Translation.getString("Name.Damage", player) + ": " + String.format("%1.2f", damage));
                infoShown = true;
            }
            if (myPet.getRangedDamage() > 0 && CommandInfo.canSee(PetInfoDisplay.RangedDamage.adminOnly, player, myPet)) {
                double damage = myPet.getRangedDamage();
                player.sendMessage("   " + Translation.getString("Name.RangedDamage", player) + ": " + String.format("%1.2f", damage));
                infoShown = true;
            }
            if (Configuration.HungerSystem.USE_HUNGER_SYSTEM && canSee(PetInfoDisplay.Hunger.adminOnly, player, myPet)) {
                player.sendMessage("   " + Translation.getString("Name.Hunger", player) + ": " + Math.round(myPet.getSaturation()));
                FancyMessage m = new FancyMessage("   " + Translation.getString("Name.Food", player) + ": ");
                boolean comma = false;
                for (ConfigItem material : MyPetApi.getMyPetInfo().getFood(myPet.getPetType())) {
                    ItemStack is = material.getItem();
                    if (is == null || is.getType() == Material.AIR) {
                        continue;
                    }
                    if (comma) {
                        m.then(", ");
                    }
                    if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
                        m.then(is.getItemMeta().getDisplayName());
                    } else {
                        try {
                            m.thenTranslate(MyPetApi.getPlatformHelper().getVanillaName(is) + ".name");
                        } catch (Exception e) {
                            MyPetApi.getLogger().warning("A food item caused an error. If you think this is a bug please report it to the MyPet developer.");
                            MyPetApi.getLogger().warning("" + is);
                            e.printStackTrace();
                            continue;
                        }
                    }
                    m.color(ChatColor.GOLD);
                    ItemTooltip it = new ItemTooltip();
                    it.setMaterial(is.getType());
                    if (is.hasItemMeta()) {
                        if (is.getItemMeta().hasDisplayName()) {
                            it.setTitle(is.getItemMeta().getDisplayName());
                        }
                        if (is.getItemMeta().hasLore()) {
                            it.setLore(is.getItemMeta().getLore().toArray(new String[is.getItemMeta().getLore().size()]));
                        }
                    }
                    m.itemTooltip(it);
                    comma = true;
                }
                MyPetApi.getPlatformHelper().sendMessageRaw(player, m.toJSONString());
                infoShown = true;
            }
            if (canSee(PetInfoDisplay.Behavior.adminOnly, player, myPet)) {
                if (myPet.getSkills().hasSkill(Behavior.class)) {
                    Behavior behavior = myPet.getSkills().getSkill(Behavior.class).get();
                    player.sendMessage("   " + Translation.getString("Name.Skill.Behavior", player) + ": " + Translation.getString("Name." + behavior.getBehavior().name(), player));
                    infoShown = true;
                }
            }
            if (canSee(PetInfoDisplay.Skilltree.adminOnly, player, myPet) && myPet.getSkilltree() != null) {
                player.sendMessage("   " + Translation.getString("Name.Skilltree", player) + ": " + myPet.getSkilltree().getName());
                infoShown = true;
            }
            if (canSee(PetInfoDisplay.Level.adminOnly, player, myPet)) {
                int lvl = myPet.getExperience().getLevel();
                player.sendMessage("   " + Translation.getString("Name.Level", player) + ": " + lvl);
                infoShown = true;
            }
            int maxLevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : Configuration.LevelSystem.Experience.LEVEL_CAP;
            if (canSee(PetInfoDisplay.Exp.adminOnly, player, myPet) && myPet.getExperience().getLevel() < maxLevel) {
                double exp = myPet.getExperience().getCurrentExp();
                double reqEXP = myPet.getExperience().getRequiredExp();
                player.sendMessage("   " + Translation.getString("Name.Exp", player) + ": " + String.format("%1.2f", exp) + "/" + String.format("%1.2f", reqEXP));
                infoShown = true;
            }
            if (myPet.getOwner().getDonationRank() != DonateCheck.DonationRank.None) {
                infoShown = true;
                String donationMessage = "" + ChatColor.GOLD;
                donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                donationMessage += " " + Translation.getString("Name.Title." + myPet.getOwner().getDonationRank().name(), player) + " ";
                donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                sender.sendMessage("   " + donationMessage);
            }
            if (!infoShown) {
                sender.sendMessage(Translation.getString("Message.CantViewPetInfo", player));
            }
            return true;
        } else {
            if (args.length > 0) {
                sender.sendMessage(Util.formatText(Translation.getString("Message.No.UserHavePet", player), args[0]));
            } 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) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) FancyMessage(de.keyle.fanciful.FancyMessage) ItemTooltip(de.keyle.fanciful.ItemTooltip) Behavior(de.Keyle.MyPet.skill.skills.Behavior) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with ConfigItem

use of de.Keyle.MyPet.api.util.ConfigItem in project MyPet by xXKeyleXx.

the class CommandPetType method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
    if (args.length < 1) {
        return false;
    }
    String lang = "en";
    if (commandSender instanceof Player) {
        lang = MyPetApi.getPlatformHelper().getPlayerLanguage((Player) commandSender);
    }
    try {
        MyPetType myPetType = MyPetType.byName(args[0]);
        String leashFlagString = "";
        for (LeashFlag leashFlag : MyPetApi.getMyPetInfo().getLeashFlags(myPetType)) {
            leashFlagString += leashFlag.name() + ", ";
        }
        leashFlagString = leashFlagString.substring(0, leashFlagString.lastIndexOf(","));
        commandSender.sendMessage(Translation.getString("Name.LeashFlag", lang) + ": " + leashFlagString);
        String foodString = "";
        for (ConfigItem material : MyPetApi.getMyPetInfo().getFood(myPetType)) {
            foodString += WordUtils.capitalizeFully(MyPetApi.getPlatformHelper().getMaterialName(material.getItem().getTypeId()).replace("_", " ")) + ", ";
        }
        foodString = foodString.substring(0, foodString.lastIndexOf(","));
        commandSender.sendMessage(Translation.getString("Name.Food", lang) + ": " + foodString);
        commandSender.sendMessage(Translation.getString("Name.HP", lang) + ": " + MyPetApi.getMyPetInfo().getStartHP(myPetType));
    } catch (MyPetTypeNotFoundException e) {
        commandSender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) LeashFlag(de.Keyle.MyPet.api.entity.LeashFlag) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) MyPetTypeNotFoundException(de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)

Example 5 with ConfigItem

use of de.Keyle.MyPet.api.util.ConfigItem in project MyPet by xXKeyleXx.

the class EntityListener method onMyPet.

@EventHandler
public void onMyPet(final EntityDamageByEntityEvent event) {
    if (event.getEntity() instanceof MyPetBukkitEntity) {
        MyPetBukkitEntity craftMyPet = (MyPetBukkitEntity) event.getEntity();
        MyPet myPet = craftMyPet.getMyPet();
        if (event.getDamager() instanceof Player || (event.getDamager() instanceof Projectile && ((Projectile) event.getDamager()).getShooter() instanceof Player)) {
            Player damager;
            if (event.getDamager() instanceof Projectile) {
                damager = (Player) ((Projectile) event.getDamager()).getShooter();
            } else {
                damager = (Player) event.getDamager();
            }
            ItemStack leashItem;
            if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
                leashItem = damager.getEquipment().getItemInMainHand();
            } else {
                leashItem = damager.getItemInHand();
            }
            if (MyPetApi.getMyPetInfo().getLeashItem(myPet.getPetType()).compare(leashItem)) {
                boolean infoShown = false;
                if (CommandInfo.canSee(PetInfoDisplay.Name.adminOnly, damager, myPet)) {
                    damager.sendMessage(ChatColor.AQUA + myPet.getPetName() + ChatColor.RESET + ":");
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Owner.adminOnly, damager, myPet) && myPet.getOwner().getPlayer() != damager) {
                    damager.sendMessage("   " + Translation.getString("Name.Owner", damager) + ": " + myPet.getOwner().getName());
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.HP.adminOnly, damager, myPet)) {
                    String msg;
                    if (myPet.getHealth() > myPet.getMaxHealth() / 3 * 2) {
                        msg = "" + ChatColor.GREEN;
                    } else if (myPet.getHealth() > myPet.getMaxHealth() / 3) {
                        msg = "" + ChatColor.YELLOW;
                    } else {
                        msg = "" + ChatColor.RED;
                    }
                    msg += String.format("%1.2f", myPet.getHealth()) + ChatColor.WHITE + "/" + String.format("%1.2f", myPet.getMaxHealth());
                    damager.sendMessage("   " + Translation.getString("Name.HP", damager) + ": " + msg);
                    infoShown = true;
                }
                if (myPet.getStatus() == PetState.Dead && CommandInfo.canSee(PetInfoDisplay.RespawnTime.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.Respawntime", damager) + ": " + myPet.getRespawnTime());
                    infoShown = true;
                }
                if (!myPet.isPassiv() && CommandInfo.canSee(PetInfoDisplay.Damage.adminOnly, damager, myPet)) {
                    double damage = (myPet.getSkills().isSkillActive(Damage.class) ? myPet.getSkills().getSkill(Damage.class).get().getDamage() : 0);
                    damager.sendMessage("   " + Translation.getString("Name.Damage", damager) + ": " + String.format("%1.2f", damage));
                    infoShown = true;
                }
                if (myPet.getRangedDamage() > 0 && CommandInfo.canSee(PetInfoDisplay.RangedDamage.adminOnly, damager, myPet)) {
                    double damage = myPet.getRangedDamage();
                    damager.sendMessage("   " + Translation.getString("Name.RangedDamage", damager) + ": " + String.format("%1.2f", damage));
                    infoShown = true;
                }
                if (myPet.getSkills().hasSkill(Behavior.class) && CommandInfo.canSee(PetInfoDisplay.Behavior.adminOnly, damager, myPet)) {
                    Behavior behavior = myPet.getSkills().getSkill(Behavior.class).get();
                    damager.sendMessage("   " + Translation.getString("Name.Skill.Behavior", damager) + ": " + Translation.getString("Name." + behavior.getBehavior().name(), damager));
                    infoShown = true;
                }
                if (Configuration.HungerSystem.USE_HUNGER_SYSTEM && CommandInfo.canSee(PetInfoDisplay.Hunger.adminOnly, damager, myPet)) {
                    damager.sendMessage("   " + Translation.getString("Name.Hunger", damager) + ": " + Math.round(myPet.getSaturation()));
                    FancyMessage m = new FancyMessage("   " + Translation.getString("Name.Food", damager) + ": ");
                    boolean comma = false;
                    for (ConfigItem material : MyPetApi.getMyPetInfo().getFood(myPet.getPetType())) {
                        ItemStack is = material.getItem();
                        if (is == null) {
                            continue;
                        }
                        if (comma) {
                            m.then(", ");
                        }
                        if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
                            m.then(is.getItemMeta().getDisplayName());
                        } else {
                            try {
                                m.thenTranslate(MyPetApi.getPlatformHelper().getVanillaName(is) + ".name");
                            } catch (Exception e) {
                                MyPetApi.getLogger().warning("A food item for \"" + myPet.getPetType().name() + "\" caused an error. If you think this is a bug please report it to the MyPet developer.");
                                MyPetApi.getLogger().warning("" + is);
                                e.printStackTrace();
                                continue;
                            }
                        }
                        m.color(ChatColor.GOLD);
                        ItemTooltip it = new ItemTooltip();
                        it.setMaterial(is.getType());
                        if (is.hasItemMeta()) {
                            if (is.getItemMeta().hasDisplayName()) {
                                it.setTitle(is.getItemMeta().getDisplayName());
                            }
                            if (is.getItemMeta().hasLore()) {
                                it.setLore(is.getItemMeta().getLore().toArray(new String[is.getItemMeta().getLore().size()]));
                            }
                        }
                        m.itemTooltip(it);
                        comma = true;
                    }
                    MyPetApi.getPlatformHelper().sendMessageRaw(damager, m.toJSONString());
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Skilltree.adminOnly, damager, myPet) && myPet.getSkilltree() != null) {
                    damager.sendMessage("   " + Translation.getString("Name.Skilltree", damager) + ": " + myPet.getSkilltree().getName());
                    infoShown = true;
                }
                if (CommandInfo.canSee(PetInfoDisplay.Level.adminOnly, damager, myPet)) {
                    int lvl = myPet.getExperience().getLevel();
                    damager.sendMessage("   " + Translation.getString("Name.Level", damager) + ": " + lvl);
                    infoShown = true;
                }
                int maxLevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : Configuration.LevelSystem.Experience.LEVEL_CAP;
                if (CommandInfo.canSee(PetInfoDisplay.Exp.adminOnly, damager, myPet) && myPet.getExperience().getLevel() < maxLevel) {
                    double exp = myPet.getExperience().getCurrentExp();
                    double reqEXP = myPet.getExperience().getRequiredExp();
                    damager.sendMessage("   " + Translation.getString("Name.Exp", damager) + ": " + String.format("%1.2f", exp) + "/" + String.format("%1.2f", reqEXP));
                    infoShown = true;
                }
                if (myPet.getOwner().getDonationRank() != DonateCheck.DonationRank.None) {
                    infoShown = true;
                    String donationMessage = "" + ChatColor.GOLD;
                    donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                    donationMessage += " " + Translation.getString("Name.Title." + myPet.getOwner().getDonationRank().name(), damager) + " ";
                    donationMessage += myPet.getOwner().getDonationRank().getDefaultIcon();
                    damager.sendMessage("   " + donationMessage);
                }
                if (!infoShown) {
                    damager.sendMessage(Translation.getString("Message.No.NothingToSeeHere", myPet.getOwner().getLanguage()));
                }
                event.setCancelled(true);
            } else if (myPet.getOwner().equals(damager) && (!Configuration.Misc.OWNER_CAN_ATTACK_PET)) {
                event.setCancelled(true);
            } else if (!myPet.getOwner().equals(damager) && !MyPetApi.getHookHelper().canHurt(damager, myPet.getOwner().getPlayer(), true)) {
                event.setCancelled(true);
            }
        }
        if (event.getDamager() instanceof CraftMyPetProjectile) {
            EntityMyPetProjectile projectile = ((CraftMyPetProjectile) event.getDamager()).getMyPetProjectile();
            if (myPet == projectile.getShooter().getMyPet()) {
                event.setCancelled(true);
            }
            if (!MyPetApi.getHookHelper().canHurt(projectile.getShooter().getOwner().getPlayer(), myPet.getOwner().getPlayer(), true)) {
                event.setCancelled(true);
            }
        }
        if (!event.isCancelled() && event.getDamager() instanceof LivingEntity) {
            LivingEntity damager = (LivingEntity) event.getDamager();
            if (damager instanceof Player) {
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), (Player) damager, true)) {
                    return;
                }
            }
            if (myPet.getSkills().isSkillActive(Thorns.class)) {
                if (damager instanceof Creeper) {
                    return;
                }
                Thorns thornsSkill = myPet.getSkills().getSkill(Thorns.class).get();
                if (thornsSkill.activate()) {
                    isSkillActive = true;
                    thornsSkill.reflectDamage(damager, event.getDamage());
                    isSkillActive = false;
                }
            }
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) EntityMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) ConfigItem(de.Keyle.MyPet.api.util.ConfigItem) FancyMessage(de.keyle.fanciful.FancyMessage) EntityMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) ItemTooltip(de.keyle.fanciful.ItemTooltip) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ConfigItem (de.Keyle.MyPet.api.util.ConfigItem)5 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)3 ItemStack (org.bukkit.inventory.ItemStack)3 MyPetType (de.Keyle.MyPet.api.entity.MyPetType)2 CraftMyPetProjectile (de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile)2 EntityMyPetProjectile (de.Keyle.MyPet.api.skill.skills.ranged.EntityMyPetProjectile)2 InactiveMyPet (de.Keyle.MyPet.entity.InactiveMyPet)2 FancyMessage (de.keyle.fanciful.FancyMessage)2 ItemTooltip (de.keyle.fanciful.ItemTooltip)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 Optional (com.google.common.base.Optional)1 WorldGroup (de.Keyle.MyPet.api.WorldGroup)1 DefaultInfo (de.Keyle.MyPet.api.entity.DefaultInfo)1 LeashFlag (de.Keyle.MyPet.api.entity.LeashFlag)1 MyPet (de.Keyle.MyPet.api.entity.MyPet)1 StoredMyPet (de.Keyle.MyPet.api.entity.StoredMyPet)1 MyPetTypeNotFoundException (de.Keyle.MyPet.api.exceptions.MyPetTypeNotFoundException)1 PlayerLeashEntityHook (de.Keyle.MyPet.api.util.hooks.types.PlayerLeashEntityHook)1 EntityConverterService (de.Keyle.MyPet.api.util.service.types.EntityConverterService)1