Search in sources :

Example 1 with CustomMobConfig

use of com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig in project InfernalMobs by NyaaCat.

the class CommandHandler method onCommand.

@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
    if (!sender.hasPermission("infernal_mobs.commands")) {
        sender.sendMessage("You don't have permission to use this command!");
        return true;
    }
    Arguments arg = Arguments.parse(args);
    if (arg == null)
        return false;
    String subcommand = arg.next();
    if (subcommand == null)
        subcommand = "help";
    try {
        if ("help".equalsIgnoreCase(subcommand)) {
            printHelp(sender);
        } else if ("reload".equalsIgnoreCase(subcommand)) {
            plugin.reloadConfig();
            ConfigReader.reload();
            plugin.reloadLoot();
            InfernalMobs.instance.reloadMainLoopTask();
            EnumAbilities.reloadAbility();
            sender.sendMessage("§eConfig reloaded!");
        } else if ("mobList".equalsIgnoreCase(subcommand)) {
            sender.sendMessage("Mob List:");
            for (EntityType t : EntityType.values()) {
                sender.sendMessage(t.name());
            }
        } else if ("error".equalsIgnoreCase(subcommand)) {
            plugin.errorList.add(asPlayer(sender));
            sender.sendMessage("Click on a mob to send an error report about it.");
        } else if ("info".equalsIgnoreCase(subcommand)) {
            sender.sendMessage("Mounts: " + plugin.mobManager.mounteeMobs.size());
            sender.sendMessage("Infernals: " + plugin.mobManager.mobMap.size());
        } else if ("worldInfo".equalsIgnoreCase(subcommand)) {
            final World world = asPlayer(sender).getWorld();
            String enabled = ConfigReader.isEnabledWorld(world) ? "is" : "is not";
            sender.sendMessage("The world you are currently in, " + world + " " + enabled + " enabled.");
            sender.sendMessage("All the world that are enabled are: ");
            for (World w : Bukkit.getWorlds()) {
                if (ConfigReader.isEnabledWorld(w)) {
                    sender.sendMessage("- " + w.getName());
                }
            }
        } else if ("abilities".equalsIgnoreCase(subcommand)) {
            printAbilities(sender);
        } else if ("addloot".equalsIgnoreCase(subcommand)) {
            if (arg.top() == null) {
                sender.sendMessage("usage: /im addloot <name>");
                return true;
            }
            ItemStack item = asPlayer(sender).getInventory().getItemInMainHand();
            if (item == null || item.getType() == Material.AIR) {
                sender.sendMessage("Add fail. Please check if you are holding the item");
                return true;
            }
            String name = arg.nextString();
            LootItem li = new LootItem();
            li.item = item.clone();
            if (plugin.lootManager.cfg.lootItems.containsKey(name)) {
                sender.sendMessage("Fail. Duplicated name");
                return true;
            }
            plugin.lootManager.cfg.lootItems.put(name, li);
            plugin.lootManager.save();
            sender.sendMessage("Item Added.");
        } else if ("checkchance".equalsIgnoreCase(subcommand)) {
            if ("level".equalsIgnoreCase(arg.nextString())) {
                int level = arg.nextInt();
                Map<String, Double> m = plugin.lootManager.cfg.dropMap.get(level);
                Double sum = m.values().stream().mapToDouble(Double::doubleValue).sum();
                sender.sendMessage(String.format("Listing drop chance for level %d", level));
                m.entrySet().stream().sorted((a, b) -> a.getValue().compareTo(b.getValue())).forEach(e -> sender.sendMessage(String.format("  %3.03f%% %s", e.getValue() / sum * 100D, getLootDisplayName(e.getKey()))));
            } else {
                String itemName = arg.nextString();
                Map2D<Integer, String, Double> map = new Map2D<>();
                for (Map.Entry<Integer, Map<String, Double>> e : plugin.lootManager.cfg.dropMap.entrySet()) {
                    map.setRow(e.getKey(), normalize(e.getValue()));
                }
                sender.sendMessage(String.format("Listing drop chance for item \"%s\"", itemName));
                Map<Integer, Double> m = map.getColumn(itemName);
                if (m.size() == 0) {
                    sender.sendMessage("Item never dropped");
                } else {
                    normalize(m).entrySet().stream().sorted((a, b) -> a.getKey().compareTo(b.getKey())).forEach(e -> sender.sendMessage(String.format("  SpawnConfig%2d: %.03f%%", e.getKey(), e.getValue() * 100D)));
                }
            }
        } else if ("setdrop".equalsIgnoreCase(subcommand)) {
            int level = arg.nextInt();
            String name = arg.nextString();
            double w = arg.nextDouble();
            plugin.lootManager.cfg.setDropChance(level, name, w);
            plugin.lootManager.save();
            sender.sendMessage("Chance set.");
        } else if ("killall".equalsIgnoreCase(subcommand)) {
            String worldName = arg.nextString();
            World w = plugin.getServer().getWorld(worldName);
            if (w == null) {
                sender.sendMessage("World not found!");
                return true;
            }
            for (Entity e : w.getEntities()) {
                if (plugin.mobManager.mobMap.containsKey(e.getUniqueId())) {
                    plugin.mobManager.mobMap.remove(e.getUniqueId());
                    e.remove();
                }
            }
            sender.sendMessage("Killed all loaded infernal mobs in that world!");
        } else if ("kill".equalsIgnoreCase(subcommand)) {
            int radius = arg.nextInt();
            for (Entity e : asPlayer(sender).getNearbyEntities(radius, radius, radius)) {
                if (plugin.mobManager.mobMap.containsKey(e.getUniqueId())) {
                    plugin.mobManager.mobMap.remove(e.getUniqueId());
                    e.remove();
                }
            }
            sender.sendMessage("Killed all infernal mobs near you!");
        } else if ("getloot".equalsIgnoreCase(subcommand)) {
            if (arg.top() == null) {
                Player player = asPlayer(sender);
                final int powers = Helper.rand(ConfigReader.getMinimalLevel(), ConfigReader.getMaximumLevel());
                final ItemStack gottenLoot = plugin.lootManager.getRandomLoot(player, powers);
                if (gottenLoot != null && gottenLoot.getType() != Material.AIR) {
                    if (!InventoryUtils.addItem(player, gottenLoot)) {
                        Location location = player.getLocation();
                        player.getWorld().dropItem(location, gottenLoot);
                    }
                    if (sender.isOp()) {
                        sender.sendMessage("§eGave you some random loot!");
                    }
                }
            } else {
                String name = arg.nextString();
                ItemStack i = plugin.lootManager.getLootByName(asPlayer(sender), name);
                if (i != null && i.getType() != Material.AIR) {
                    Player player = asPlayer(sender);
                    if (!InventoryUtils.addItem(player, i)) {
                        Location location = player.getLocation();
                        player.getWorld().dropItem(location, i);
                    }
                    if (sender.isOp()) {
                        sender.sendMessage("§eGave you the loot: " + name);
                    }
                }
            }
        } else if ("spawn".equalsIgnoreCase(subcommand)) {
            EntityType type = arg.nextEnum(EntityType.class);
            Location farSpawnLoc = asPlayer(sender).getTargetBlock((Set<Material>) null, 200).getLocation();
            farSpawnLoc.setY(farSpawnLoc.getY() + 1.0);
            List<EnumAbilities> abilities = new ArrayList<>();
            if (arg.top() == null) {
                // random ability
                abilities = Helper.randomNItems(ConfigReader.getEnabledAbilities(), MobManager.getInfernalLevelForLocation(farSpawnLoc));
            } else {
                // ability list
                while (arg.top() != null) abilities.add(arg.nextEnum(EnumAbilities.class));
            }
            if (abilities.size() <= 0) {
                sender.sendMessage("No ability selected");
            } else {
                if (plugin.mobManager.spawnMob(type, farSpawnLoc, abilities, InfernalSpawnReason.COMMAND) != null) {
                    sender.sendMessage("Mob spawned");
                } else {
                    sender.sendMessage("Cannot spawn mob");
                }
            }
        } else if ("sm".equalsIgnoreCase(subcommand)) {
            String mobName = arg.nextString();
            if ("list".equalsIgnoreCase(mobName)) {
                Map<String, CustomMobConfig.CustomMob> customMobs = ConfigReader.getCustomMobConfig().getCustomMobs();
                Set<String> names = customMobs.keySet();
                if (names.isEmpty()) {
                    if (sender.isOp()) {
                        sender.sendMessage("no custom mobs found");
                    }
                } else {
                    if (sender.isOp()) {
                        String message = "&aHere " + (names.size() == 1 ? "is " : "are ") + names.size() + " mob" + (names.size() == 1 ? "" : "s");
                        message = ChatColor.translateAlternateColorCodes('&', message);
                        sender.sendMessage(message);
                        names.forEach(s -> {
                            sender.sendMessage(s);
                        });
                    }
                }
                return true;
            }
            Location farSpawnLoc = asPlayer(sender).getTargetBlock((Set<Material>) null, 200).getLocation();
            CustomMobConfig mbConf = ConfigReader.getCustomMobConfig();
            CustomMobConfig.CustomMob cm = mbConf.getByName(mobName);
            List<EnumAbilities> abilities = new ArrayList<>();
            mbConf.setAbilities(abilities, cm);
            Mob mob = mbConf.spawnCustomMob(plugin.mobManager, farSpawnLoc, abilities, cm);
            String top = arg.top();
            if (top != null) {
                cm.spawnLevel = Integer.parseInt(top);
            } else {
                cm.spawnLevel = cm.smSpawnLevel == -1 ? ConfigReader.getLevelConfig().getLevel(farSpawnLoc.distance(farSpawnLoc.getWorld().getSpawnLocation())) : cm.smSpawnLevel;
            }
            mbConf.addCustomAttr(mob, cm);
            LivingEntity entity = (LivingEntity) InfernalMobs.instance.getServer().getEntity(mob.entityId);
            InfernalMobSpawnEvent event = new InfernalMobSpawnEvent(entity, mob, null, InfernalSpawnReason.COMMAND);
            MobManager.setInfernalMobName(event);
            String spawned = "&aspawned &e" + cm.name + " &alevel &e" + cm.spawnLevel;
            spawned = ChatColor.translateAlternateColorCodes('&', spawned);
            sender.sendMessage(spawned);
        } else if ("csm".equalsIgnoreCase(subcommand)) {
            String mobName = arg.nextString();
            if ("list".equalsIgnoreCase(mobName)) {
                Map<String, CustomMobConfig.CustomMob> customMobs = ConfigReader.getCustomMobConfig().getCustomMobs();
                Set<String> names = customMobs.keySet();
                if (names.isEmpty()) {
                    if (sender.isOp()) {
                        sender.sendMessage("no custom mobs found");
                    }
                } else {
                    if (sender.isOp()) {
                        String message = "&aHere " + (names.size() == 1 ? "is " : "are ") + names.size() + " mob" + (names.size() == 1 ? "" : "s");
                        message = ChatColor.translateAlternateColorCodes('&', message);
                        sender.sendMessage(message);
                        names.forEach(s -> {
                            sender.sendMessage(s);
                        });
                    }
                }
                return true;
            }
            String worldName = arg.nextString();
            World w = plugin.getServer().getWorld(worldName);
            if (w == null) {
                sender.sendMessage("World not found!");
                return true;
            }
            double x = arg.nextDouble();
            double y = arg.nextDouble();
            double z = arg.nextDouble();
            Location loc = new Location(w, x, y, z);
            CustomMobConfig mbConf = ConfigReader.getCustomMobConfig();
            CustomMobConfig.CustomMob cm = mbConf.getByName(mobName);
            List<EnumAbilities> abilities = new ArrayList<>();
            mbConf.setAbilities(abilities, cm);
            Mob mob = mbConf.spawnCustomMob(plugin.mobManager, loc, abilities, cm);
            String top = arg.top();
            if (top != null) {
                cm.spawnLevel = Integer.parseInt(top);
            } else {
                cm.spawnLevel = cm.smSpawnLevel == -1 ? ConfigReader.getLevelConfig().getLevel(loc.distance(loc.getWorld().getSpawnLocation())) : cm.smSpawnLevel;
            }
            mbConf.addCustomAttr(mob, cm);
            LivingEntity entity = (LivingEntity) InfernalMobs.instance.getServer().getEntity(mob.entityId);
            InfernalMobSpawnEvent event = new InfernalMobSpawnEvent(entity, mob, null, InfernalSpawnReason.COMMAND);
            MobManager.setInfernalMobName(event);
            String spawned = "&aspawned &e" + cm.name + " &alevel &e" + cm.spawnLevel;
            spawned = ChatColor.translateAlternateColorCodes('&', spawned);
            sender.sendMessage(spawned);
        } else if ("cspawn".equalsIgnoreCase(subcommand)) {
            EntityType type = arg.nextEnum(EntityType.class);
            String worldName = arg.nextString();
            World w = plugin.getServer().getWorld(worldName);
            if (w == null) {
                sender.sendMessage("World not found!");
                return true;
            }
            double x = arg.nextDouble();
            double y = arg.nextDouble();
            double z = arg.nextDouble();
            Location loc = new Location(w, x, y, z);
            List<EnumAbilities> abilities = new ArrayList<>();
            if (arg.top() == null) {
                // random ability
                abilities = Helper.randomNItems(ConfigReader.getEnabledAbilities(), MobManager.getInfernalLevelForLocation(loc));
            } else {
                // ability list
                while (arg.top() != null) abilities.add(arg.nextEnum(EnumAbilities.class));
            }
            if (abilities.size() <= 0) {
                sender.sendMessage("No ability selected");
            } else {
                if (plugin.mobManager.spawnMob(type, loc, abilities, InfernalSpawnReason.COMMAND) != null) {
                    sender.sendMessage("Mob spawned");
                } else {
                    sender.sendMessage("Cannot spawn mob");
                }
            }
        } else if ("pspawn".equalsIgnoreCase(subcommand)) {
            EntityType type = arg.nextEnum(EntityType.class);
            String playerName = arg.nextString();
            Player p = Bukkit.getPlayer(playerName);
            if (p == null) {
                sender.sendMessage("Player not found: " + playerName);
                return true;
            }
            Location loc = p.getLocation();
            List<EnumAbilities> abilities = new ArrayList<>();
            if (arg.top() == null) {
                // random ability
                abilities = Helper.randomNItems(ConfigReader.getEnabledAbilities(), MobManager.getInfernalLevelForLocation(loc));
            } else {
                // ability list
                while (arg.top() != null) abilities.add(arg.nextEnum(EnumAbilities.class));
            }
            if (abilities.size() <= 0) {
                sender.sendMessage("No ability selected");
            } else {
                if (plugin.mobManager.spawnMob(type, loc, abilities, InfernalSpawnReason.COMMAND) != null) {
                    sender.sendMessage("Mob spawned");
                } else {
                    sender.sendMessage("Cannot spawn mob");
                }
            }
        }
    } catch (NotPlayerException ex) {
        sender.sendMessage("This command can only be run by a player!");
        return true;
    } catch (RuntimeException ex) {
        sender.sendMessage("Command fail: " + ex.getMessage());
        ex.printStackTrace();
        return true;
    }
    return true;
// TODO set infernal spawner
// if (args[0].equals("setInfernal") && args.length == 2) {
// if (player.getTargetBlock((Set<Material>) null, 25).getType().equals((Object) Material.MOB_SPAWNER)) {
// final int delay = Integer.parseInt(args[1]);
// Location loc = player.getTargetBlock((Set<Material>) null, 25).getLocation();
// plugin.persist.validInfernalSpawners.put(loc, delay);
// sender.sendMessage("Spawner set to infernal with a " + delay + " second delay!");
// } else {
// sender.sendMessage("You must be looking a spawner to make it infernal!");
// }
// }
}
Also used : CommandSender(org.bukkit.command.CommandSender) java.util(java.util) Entity(org.bukkit.entity.Entity) Mob(com.jacob_vejvoda.infernal_mobs.persist.Mob) InfernalSpawnReason(com.jacob_vejvoda.infernal_mobs.api.InfernalSpawnReason) InventoryUtils(cat.nyaa.nyaacore.utils.InventoryUtils) InfernalMobSpawnEvent(com.jacob_vejvoda.infernal_mobs.api.InfernalMobSpawnEvent) Player(org.bukkit.entity.Player) EntityType(org.bukkit.entity.EntityType) LivingEntity(org.bukkit.entity.LivingEntity) CommandExecutor(org.bukkit.command.CommandExecutor) CustomMobConfig(com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig) org.bukkit(org.bukkit) ItemStack(org.bukkit.inventory.ItemStack) EnumAbilities(com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities) Command(org.bukkit.command.Command) Arguments(cat.nyaa.nyaacore.CommandReceiver.Arguments) LootItem(com.jacob_vejvoda.infernal_mobs.loot.LootItem) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) CustomMobConfig(com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig) LivingEntity(org.bukkit.entity.LivingEntity) EnumAbilities(com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities) Mob(com.jacob_vejvoda.infernal_mobs.persist.Mob) Player(org.bukkit.entity.Player) LootItem(com.jacob_vejvoda.infernal_mobs.loot.LootItem) Arguments(cat.nyaa.nyaacore.CommandReceiver.Arguments) EntityType(org.bukkit.entity.EntityType) InfernalMobSpawnEvent(com.jacob_vejvoda.infernal_mobs.api.InfernalMobSpawnEvent) ItemStack(org.bukkit.inventory.ItemStack)

Example 2 with CustomMobConfig

use of com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig in project InfernalMobs by NyaaCat.

the class MobManager method infernalNaturalSpawn.

/**
 * Change the given entity into infernal mob
 * may need to be called delayed
 *
 * @param mobEntity the entity
 */
public void infernalNaturalSpawn(LivingEntity mobEntity) {
    if (mobEntity.isDead() || !mobEntity.isValid() || mobEntity.getCustomName() != null)
        return;
    if (mobEntity.hasMetadata("NPC") || mobEntity.hasMetadata("shopkeeper"))
        return;
    if (!isAcceptableBaby(mobEntity))
        return;
    final UUID id = mobEntity.getUniqueId();
    UUID parentId = mamaSpawned.getIfPresent(id);
    if (unnaturallySpawned.getIfPresent(id) != null)
        return;
    if (!Helper.possibility(ConfigReader.getInfernalNaturalSpawningPercentage()))
        return;
    if (!ConfigReader.getLevelConfig().isInRange(mobEntity.getLocation()))
        return;
    int level = getInfernalLevelForLocation(mobEntity.getLocation());
    List<EnumAbilities> abilities = getAbilities(level);
    CustomMobConfig customMobConfig = ConfigReader.getCustomMobConfig();
    CustomMobConfig.CustomMob customMob = customMobConfig.determineCustom(mobEntity, level, true);
    if (customMob != null) {
        customMobConfig.setAbilities(abilities, customMob);
    }
    if (abilities == null || abilities.size() <= 0)
        return;
    if (parentId != null) {
        if (!mobMap.containsKey(parentId) || mobMap.get(parentId).maxMamaInfernal <= 0) {
            return;
        }
        mobMap.get(parentId).maxMamaInfernal--;
        if (abilities.contains(EnumAbilities.MAMA)) {
            abilities.remove(EnumAbilities.MAMA);
            if (abilities.size() <= 0) {
                return;
            }
        }
    }
    // setup infernal mob
    int lives = abilities.contains(EnumAbilities.ONEUP) ? 2 : 1;
    Mob mob = new Mob(id, lives, ConfigReader.getRandomParticleEffect(), level, abilities);
    InfernalMobSpawnEvent spwanEvent;
    if (parentId != null) {
        spwanEvent = new InfernalMobSpawnEvent(mobEntity, mob, parentId, InfernalSpawnReason.MAMA);
    } else {
        spwanEvent = new InfernalMobSpawnEvent(mobEntity, mob, null, InfernalSpawnReason.NATURAL);
    }
    setInfernalHealth(spwanEvent);
    if (customMob != null) {
        customMobConfig.addCustomAttr(mob, customMob);
    }
    for (EnumAbilities ability : abilities) ability.onMobSpawn(spwanEvent);
    setInfernalMobName(spwanEvent);
    mobMap.put(id, mob);
    Bukkit.getPluginManager().callEvent(spwanEvent);
    // Show message
    if (ConfigReader.isSpwanMessageEnabled()) {
        String msg = Helper.randomItem(ConfigReader.getSpwanMessages());
        msg = msg.replace("{mob}", mobEntity.getCustomName() == null ? mobEntity.getType().name().toLowerCase() : mobEntity.getCustomName());
        msg = ChatColor.translateAlternateColorCodes('&', msg);
        if (ConfigReader.isBroadcastSpawnMessageServer()) {
            Bukkit.broadcastMessage(msg);
        } else if (ConfigReader.isBroadcastSpawnMessageWorld()) {
            for (Player p : mobEntity.getWorld().getPlayers()) {
                p.sendMessage(msg);
            }
        } else {
            int r = ConfigReader.getSpawnMessageBroadcaseRadius();
            for (Entity e : mobEntity.getNearbyEntities(r, r, r)) {
                if (e instanceof Player) {
                    e.sendMessage(msg);
                }
            }
        }
    }
}
Also used : Mob(com.jacob_vejvoda.infernal_mobs.persist.Mob) CustomMobConfig(com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig) InfernalMobSpawnEvent(com.jacob_vejvoda.infernal_mobs.api.InfernalMobSpawnEvent) EnumAbilities(com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities)

Example 3 with CustomMobConfig

use of com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig in project InfernalMobs by NyaaCat.

the class ConfigReader method loadCustomMob.

private static CustomMobConfig loadCustomMob() {
    customMobConfig = new CustomMobConfig();
    customMobConfig.load();
    return customMobConfig;
}
Also used : CustomMobConfig(com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig)

Aggregations

CustomMobConfig (com.jacob_vejvoda.infernal_mobs.config.CustomMobConfig)3 EnumAbilities (com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities)2 InfernalMobSpawnEvent (com.jacob_vejvoda.infernal_mobs.api.InfernalMobSpawnEvent)2 Mob (com.jacob_vejvoda.infernal_mobs.persist.Mob)2 Arguments (cat.nyaa.nyaacore.CommandReceiver.Arguments)1 InventoryUtils (cat.nyaa.nyaacore.utils.InventoryUtils)1 InfernalSpawnReason (com.jacob_vejvoda.infernal_mobs.api.InfernalSpawnReason)1 LootItem (com.jacob_vejvoda.infernal_mobs.loot.LootItem)1 java.util (java.util)1 org.bukkit (org.bukkit)1 Command (org.bukkit.command.Command)1 CommandExecutor (org.bukkit.command.CommandExecutor)1 CommandSender (org.bukkit.command.CommandSender)1 Entity (org.bukkit.entity.Entity)1 EntityType (org.bukkit.entity.EntityType)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1