Search in sources :

Example 6 with ConsoleCommandSender

use of org.bukkit.command.ConsoleCommandSender in project xAuth by CypherX.

the class xAuthCommand method locationCommand.

private boolean locationCommand(CommandSender sender, String[] args) {
    if (sender instanceof ConsoleCommandSender) {
        xAuthLog.info("This command cannot be executed from the console!");
        return true;
    }
    Player player = (Player) sender;
    if (!xPermissions.has(player, "xauth.admin.location")) {
        plugin.getMsgHndlr().sendMessage("admin.permission", player);
        return true;
    } else if (args.length < 2 || !(args[1].equals("set") || args[1].equals("remove"))) {
        plugin.getMsgHndlr().sendMessage("admin.location.usage", player);
        return true;
    }
    String action = args[1];
    boolean global = args.length > 2 && args[2].equals("global") ? true : false;
    String response;
    if (action.equals("set")) {
        if (!global && player.getWorld().getUID().equals(plugin.getLocMngr().getGlobalUID())) {
            plugin.getMsgHndlr().sendMessage("admin.location.set.error.global", player);
            return true;
        }
        boolean success = plugin.getLocMngr().setLocation(player.getLocation(), global);
        if (success)
            response = "admin.location.set.success." + (global ? "global" : "regular");
        else
            response = "admin.location.set.error.general";
    } else {
        if (global) {
            if (plugin.getLocMngr().getGlobalUID() == null) {
                plugin.getMsgHndlr().sendMessage("admin.location.remove.error.noglobal", player);
                return true;
            }
        } else {
            if (!plugin.getLocMngr().isLocationSet(player.getWorld())) {
                plugin.getMsgHndlr().sendMessage("admin.location.remove.error.notset", player);
                return true;
            } else if (player.getWorld().getUID().equals(plugin.getLocMngr().getGlobalUID())) {
                plugin.getMsgHndlr().sendMessage("admin.location.remove.error.global", player);
                return true;
            }
        }
        boolean success = plugin.getLocMngr().removeLocation(player.getWorld());
        if (success)
            response = "admin.location.remove.success." + (global ? "global" : "regular");
        else
            response = "admin.location.remove.error.general";
    }
    plugin.getMsgHndlr().sendMessage(response, player);
    return true;
}
Also used : Player(org.bukkit.entity.Player) com.cypherx.xauth.xAuthPlayer(com.cypherx.xauth.xAuthPlayer) RemoteConsoleCommandSender(org.bukkit.command.RemoteConsoleCommandSender) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender)

Example 7 with ConsoleCommandSender

use of org.bukkit.command.ConsoleCommandSender in project AuthMeReloaded by AuthMe.

the class PurgeTaskTest method shouldStopTaskAndInformConsoleUser.

@Test
public void shouldStopTaskAndInformConsoleUser() {
    // given
    Set<String> names = newHashSet("name1", "name2");
    PurgeTask task = new PurgeTask(purgeService, permissionsManager, null, names, new OfflinePlayer[0]);
    ReflectionTestUtils.setField(BukkitRunnable.class, task, "taskId", 10049);
    Server server = mock(Server.class);
    BukkitScheduler scheduler = mock(BukkitScheduler.class);
    given(server.getScheduler()).willReturn(scheduler);
    ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
    ConsoleCommandSender consoleSender = mock(ConsoleCommandSender.class);
    given(server.getConsoleSender()).willReturn(consoleSender);
    // Run for the first time -> results in empty names list
    task.run();
    // when
    task.run();
    // then
    verify(scheduler).cancelTask(task.getTaskId());
    verify(consoleSender).sendMessage(argThat(containsString("Database has been purged successfully")));
}
Also used : Server(org.bukkit.Server) BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Test(org.junit.Test)

Example 8 with ConsoleCommandSender

use of org.bukkit.command.ConsoleCommandSender in project EliteMobs by MagmaGuy.

the class SpawnMobCommandHandler method spawnMob.

public void spawnMob(CommandSender commandSender, String[] args) {
    World world = null;
    Location location = null;
    String entityInput = null;
    int mobLevel = 0;
    List<String> mobPower = new ArrayList<>();
    if (commandSender instanceof Player) {
        Player player = (Player) commandSender;
        if (args.length == 1) {
            player.sendMessage("Valid command syntax:");
            player.sendMessage("/elitemobs SpawnMob [mobType] [mobLevel] [mobPower] [mobPower(you can keep adding these mobPowers as many as you'd like)]");
        }
        world = player.getWorld();
        location = player.getTargetBlock((HashSet<Byte>) null, 30).getLocation().add(0, 1, 0);
        entityInput = args[1].toLowerCase();
        if (args.length > 2) {
            try {
                mobLevel = Integer.valueOf(args[2]);
            } catch (NumberFormatException ex) {
                player.sendMessage("Not a valid level.");
                player.sendMessage("Valid command syntax:");
                player.sendMessage("/elitemobs SpawnMob [mobType] [mobLevel] [mobPower] [mobPower(you can keep adding these mobPowers as many as you'd like)]");
            }
        }
        if (args.length > 3) {
            int index = 0;
            for (String arg : args) {
                //mob powers start after arg 2
                if (index > 2) {
                    mobPower.add(arg);
                }
                index++;
            }
        }
    } else if (commandSender instanceof ConsoleCommandSender || commandSender instanceof BlockCommandSender) {
        for (World worldIterator : worldList) {
            //find world
            if (worldIterator.getName().equals(args[1])) {
                world = worldIterator;
                //find x coord
                try {
                    double xCoord = Double.parseDouble(args[2]);
                    double yCoord = Double.parseDouble(args[3]);
                    double zCoord = Double.parseDouble(args[4]);
                    location = new Location(worldIterator, xCoord, yCoord, zCoord);
                    entityInput = args[5].toLowerCase();
                    break;
                } catch (NumberFormatException ex) {
                    getConsoleSender().sendMessage("At least one of the coordinates (x:" + args[2] + ", y:" + args[3] + ", z:" + args[4] + ") is not valid");
                    getConsoleSender().sendMessage("Valid command syntax: /elitemobs SpawnMob worldName xCoord yCoord " + "zCoord mobType mobLevel mobPower mobPower(you can keep adding these mobPowers as many as you'd like)");
                }
                if (args.length > 6) {
                    int index = 0;
                    for (String arg : args) {
                        //mob powers start after arg 2
                        if (index > 2) {
                            mobPower.add(arg);
                        }
                        index++;
                    }
                }
            }
        }
        if (world == null) {
            getConsoleSender().sendMessage("World " + args[1] + "not found. Valid command syntax: /elitemobs SpawnMob" + " [worldName] [xCoord] [yCoord] [zCoord] [mobType] [mobLevel] [mobPower] [mobPower(you can keep adding these " + "mobPowers as many as you'd like)]");
        }
    }
    EntityType entityType = null;
    switch(entityInput) {
        case "blaze":
            entityType = EntityType.BLAZE;
            break;
        case "cavespider":
            entityType = EntityType.CAVE_SPIDER;
            break;
        case "creeper":
            entityType = EntityType.CREEPER;
            break;
        case "enderman":
            entityType = EntityType.ENDERMAN;
            break;
        case "endermite":
            entityType = EntityType.ENDERMITE;
            break;
        case "husk":
            entityType = EntityType.HUSK;
            break;
        case "irongolem":
            entityType = EntityType.IRON_GOLEM;
            break;
        case "pigzombie":
            entityType = EntityType.PIG_ZOMBIE;
            break;
        case "polarbear":
            entityType = EntityType.POLAR_BEAR;
            break;
        case "silverfish":
            entityType = EntityType.SILVERFISH;
            break;
        case "skeleton":
            entityType = EntityType.SKELETON;
            break;
        case "spider":
            entityType = EntityType.SPIDER;
            break;
        case "stray":
            entityType = EntityType.STRAY;
            break;
        case "witch":
            entityType = EntityType.WITCH;
            break;
        case "witherskeleton":
            entityType = EntityType.WITHER_SKELETON;
            break;
        case "zombie":
            entityType = EntityType.ZOMBIE;
            break;
        case "chicken":
            entityType = EntityType.CHICKEN;
            break;
        case "cow":
            entityType = EntityType.COW;
            break;
        case "mushroomcow":
            entityType = EntityType.MUSHROOM_COW;
            break;
        case "pig":
            entityType = EntityType.PIG;
            break;
        case "sheep":
            entityType = EntityType.SHEEP;
            break;
        default:
            if (commandSender instanceof Player) {
                ((Player) commandSender).getPlayer().sendTitle("Could not spawn mob type " + entityInput, "If this is incorrect, please contact the dev.");
            } else if (commandSender instanceof ConsoleCommandSender) {
                getConsoleSender().sendMessage("Could not spawn mob type " + entityInput + ". If this is incorrect, " + "please contact the dev.");
            }
            break;
    }
    Entity entity = null;
    if (entityType != null) {
        entity = world.spawnEntity(location, entityType);
    }
    if (entityType == EntityType.CHICKEN || entityType == EntityType.COW || entityType == EntityType.MUSHROOM_COW || entityType == EntityType.PIG || entityType == EntityType.SHEEP) {
        HealthHandler.passiveHealthHandler(entity, ConfigValues.defaultConfig.getInt("Passive EliteMob stack amount"));
        NameHandler.customPassiveName(entity, plugin);
        return;
    }
    if (mobLevel > 0) {
        entity.setMetadata(MetadataHandler.ELITE_MOB_MD, new FixedMetadataValue(plugin, mobLevel));
    }
    if (mobPower.size() > 0) {
        boolean inputError = false;
        int powerCount = 0;
        MetadataHandler metadataHandler = new MetadataHandler();
        for (String string : mobPower) {
            switch(string) {
                //major powers
                case MetadataHandler.ZOMBIE_FRIENDS_H:
                    if (entity instanceof Zombie) {
                        entity.setMetadata(MetadataHandler.ZOMBIE_FRIENDS_MD, new FixedMetadataValue(plugin, true));
                        powerCount++;
                    }
                    break;
                case MetadataHandler.ZOMBIE_NECRONOMICON_H:
                    if (entity instanceof Zombie) {
                        entity.setMetadata(MetadataHandler.ZOMBIE_NECRONOMICON_MD, new FixedMetadataValue(plugin, true));
                        powerCount++;
                    }
                    break;
                case MetadataHandler.ZOMBIE_TEAM_ROCKET_H:
                    if (entity instanceof Zombie) {
                        entity.setMetadata(MetadataHandler.ZOMBIE_TEAM_ROCKET_MD, new FixedMetadataValue(plugin, true));
                        powerCount++;
                    }
                    break;
                case MetadataHandler.ZOMBIE_PARENTS_H:
                    if (entity instanceof Zombie) {
                        entity.setMetadata(MetadataHandler.ZOMBIE_PARENTS_MD, new FixedMetadataValue(plugin, true));
                        powerCount++;
                    }
                    break;
                //minor powers
                case MetadataHandler.ATTACK_ARROW_H:
                    entity.setMetadata(MetadataHandler.ATTACK_ARROW_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_BLINDING_H:
                    entity.setMetadata(MetadataHandler.ATTACK_BLINDING_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_CONFUSING_H:
                    entity.setMetadata(MetadataHandler.ATTACK_CONFUSING_MD, new FixedMetadataValue(plugin, true));
                    //                        minorPowerPowerStance.attackConfusing(entity);
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_FIRE_H:
                    entity.setMetadata(MetadataHandler.ATTACK_FIRE_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_FIREBALL_H:
                    entity.setMetadata(MetadataHandler.ATTACK_FIREBALL_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_FREEZE_H:
                    entity.setMetadata(MetadataHandler.ATTACK_FREEZE_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_GRAVITY_H:
                    entity.setMetadata(MetadataHandler.ATTACK_GRAVITY_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_POISON_H:
                    entity.setMetadata(MetadataHandler.ATTACK_POISON_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_PUSH_H:
                    entity.setMetadata(MetadataHandler.ATTACK_PUSH_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_WEAKNESS_H:
                    entity.setMetadata(MetadataHandler.ATTACK_WEAKNESS_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_WEB_H:
                    entity.setMetadata(MetadataHandler.ATTACK_WEB_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.ATTACK_WITHER_H:
                    entity.setMetadata(MetadataHandler.ATTACK_WITHER_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.BONUS_LOOT_H:
                    entity.setMetadata(MetadataHandler.BONUS_LOOT_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.DOUBLE_DAMAGE_H:
                    if (!(entity instanceof IronGolem)) {
                        entity.setMetadata(MetadataHandler.DOUBLE_DAMAGE_MD, new FixedMetadataValue(plugin, true));
                    }
                    powerCount++;
                    break;
                case MetadataHandler.DOUBLE_HEALTH_H:
                    if (!(entity instanceof IronGolem)) {
                        entity.setMetadata(MetadataHandler.DOUBLE_HEALTH_MD, new FixedMetadataValue(plugin, true));
                    }
                    powerCount++;
                    break;
                case MetadataHandler.INVISIBILITY_H:
                    entity.setMetadata(MetadataHandler.INVISIBILITY_MD, new FixedMetadataValue(plugin, true));
                    ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1));
                    powerCount++;
                    break;
                case MetadataHandler.INVULNERABILITY_ARROW_H:
                    entity.setMetadata(MetadataHandler.INVULNERABILITY_ARROW_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.INVULNERABILITY_FALL_DAMAGE_H:
                    entity.setMetadata(MetadataHandler.INVULNERABILITY_FALL_DAMAGE_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.INVULNERABILITY_FIRE_H:
                    entity.setMetadata(MetadataHandler.INVULNERABILITY_FIRE_MD, new FixedMetadataValue(plugin, true));
                    //                        minorPowerPowerStance.invulnerabilityFireEffect(entity);
                    powerCount++;
                    break;
                case MetadataHandler.INVULNERABILITY_KNOCKBACK_H:
                    entity.setMetadata(MetadataHandler.INVULNERABILITY_KNOCKBACK_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case MetadataHandler.MOVEMENT_SPEED_H:
                    entity.setMetadata(MetadataHandler.MOVEMENT_SPEED_MD, new FixedMetadataValue(plugin, true));
                    ((LivingEntity) entity).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 2));
                    powerCount++;
                    break;
                case MetadataHandler.TAUNT_H:
                    entity.setMetadata(MetadataHandler.TAUNT_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                case "custom":
                    entity.setMetadata(MetadataHandler.CUSTOM_POWERS_MD, new FixedMetadataValue(plugin, true));
                    powerCount++;
                    break;
                default:
                    if (commandSender instanceof Player) {
                        Player player = (Player) commandSender;
                        player.sendMessage(string + " is not a valid power.");
                    } else if (commandSender instanceof ConsoleCommandSender) {
                        getConsoleSender().sendMessage(string + " is not a valid power.");
                    }
                    inputError = true;
            }
        }
        entity.setMetadata(MetadataHandler.MINOR_POWER_AMOUNT_MD, new FixedMetadataValue(plugin, powerCount));
        minorPowerPowerStance.itemEffect(entity);
        majorPowerPowerStance.itemEffect(entity);
        if (inputError) {
            if (commandSender instanceof Player) {
                Player player = (Player) commandSender;
                player.sendMessage("Valid powers: " + MetadataHandler.powerListHumanFormat() + " custom");
            } else if (commandSender instanceof ConsoleCommandSender) {
                getConsoleSender().sendMessage("Valid powers: " + MetadataHandler.powerListHumanFormat() + MetadataHandler.majorPowerList() + " custom");
            }
        }
    }
}
Also used : PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MetadataHandler(com.magmaguy.elitemobs.MetadataHandler) World(org.bukkit.World) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Location(org.bukkit.Location) HashSet(java.util.HashSet) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 9 with ConsoleCommandSender

use of org.bukkit.command.ConsoleCommandSender in project EliteMobs by MagmaGuy.

the class CommandHandler method onCommand.

@Override
public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
    // /elitemobs SpawnMob with variable arg length
    if (args.length > 0 && args[0].equalsIgnoreCase("SpawnMob")) {
        if (commandSender instanceof ConsoleCommandSender || commandSender instanceof Player && commandSender.hasPermission("elitemobs.SpawnMob")) {
            SpawnMobCommandHandler spawnMob = new SpawnMobCommandHandler();
            spawnMob.spawnMob(commandSender, args);
            return true;
        } else if (commandSender instanceof Player && !commandSender.hasPermission("elitemobs.SpawnMob")) {
            Player player = (Player) commandSender;
            if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.SpawnMob");
            } else {
                player.sendMessage("You do not have the permission " + "elitemobs.SpawnMob");
            }
            return true;
        }
    }
    switch(args.length) {
        //just /elitemobs
        case 0:
            validCommands(commandSender);
            return true;
        // /elitemobs stats | /elitemobs getloot (for GUI menu)
        case 1:
            if (args[0].equalsIgnoreCase("stats") && commandSender instanceof Player && commandSender.hasPermission("elitemobs.stats") || args[0].equalsIgnoreCase("stats") && commandSender instanceof ConsoleCommandSender) {
                StatsCommandHandler stats = new StatsCommandHandler();
                stats.statsHandler(commandSender);
                return true;
            } else if (args[0].equalsIgnoreCase("stats") && commandSender instanceof Player && !commandSender.hasPermission("elitemobs.stats")) {
                Player player = (Player) commandSender;
                if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                    player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.stats");
                } else {
                    player.sendMessage("You do not have the permission " + "elitemobs.stats");
                }
                return true;
            } else if (args[0].equalsIgnoreCase("getloot") && commandSender instanceof Player && commandSender.hasPermission("elitemobs.getloot") || args[0].equalsIgnoreCase("gl") && commandSender instanceof Player && commandSender.hasPermission("elitemobs.getloot")) {
                LootGUI lootGUI = new LootGUI();
                lootGUI.lootGUI((Player) commandSender);
                return true;
            }
            validCommands(commandSender);
            return true;
        // /elitemobs killall aggressiveelites | /elitemobs killall passiveelites
        case 2:
            //valid /elitemobs reload config
            if (args[0].equalsIgnoreCase("reload") && commandSender instanceof Player && args[1].equalsIgnoreCase("configs") && commandSender.hasPermission("elitemobs.reload.configs")) {
                Player player = (Player) commandSender;
                ReloadConfigCommandHandler reloadConfigCommandHandler = new ReloadConfigCommandHandler();
                reloadConfigCommandHandler.reloadConfiguration();
                getLogger().info("EliteMobs configs reloaded!");
                player.sendTitle("EliteMobs config reloaded!", "Reloaded config, loot, mobPowers and translation");
                return true;
            //invalid /elitemobs reload config
            } else if (args[0].equalsIgnoreCase("reload") && commandSender instanceof Player && args[1].equalsIgnoreCase("config") && !commandSender.hasPermission("elitemobs.reload.config")) {
                Player player = (Player) commandSender;
                if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                    player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.reload.configs");
                } else {
                    player.sendMessage("You do not have the permission " + "elitemobs.reload.configs");
                }
                return true;
            //valid /elitemobs reload loot
            } else if (args[0].equalsIgnoreCase("reload") && commandSender instanceof Player && args[1].equalsIgnoreCase("loot") && commandSender.hasPermission("elitemobs.reload.loot")) {
                Player player = (Player) commandSender;
                LootCustomConfig lootCustomConfig = new LootCustomConfig();
                lootCustomConfig.reloadLootConfig();
                EliteDropsHandler eliteDropsHandler = new EliteDropsHandler();
                eliteDropsHandler.superDropParser();
                getLogger().info("EliteMobs loot reloaded!");
                player.sendTitle("EliteMobs loot reloaded!", "Reloaded loot.yml");
                return true;
            //invalid /elitemobs reload loot
            } else if (args[0].equalsIgnoreCase("reload") && commandSender instanceof Player && args[1].equalsIgnoreCase("loot") && !commandSender.hasPermission("elitemobs.reload.loot")) {
                Player player = (Player) commandSender;
                if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                    player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.reload.loot");
                } else {
                    player.sendMessage("You do not have the permission " + "elitemobs.reload.loot");
                }
                return true;
            //valid /elitemobs getloot | /elitemobs gl
            } else if (args[0].equalsIgnoreCase("getloot") && commandSender instanceof Player && commandSender.hasPermission("elitemobs.getloot") || args[0].equalsIgnoreCase("gl") && commandSender instanceof Player && commandSender.hasPermission("elitemobs.getloot")) {
                Player player = (Player) commandSender;
                GetLootCommandHandler getLootCommandHandler = new GetLootCommandHandler();
                if (getLootCommandHandler.getLootHandler(player, args[1])) {
                    return true;
                } else {
                    player.sendTitle("", "Could not find that item name.");
                    return true;
                }
            //invalid /elitemobs getloot | /elitemobs gl
            } else if (args[0].equalsIgnoreCase("getloot") && !commandSender.hasPermission("elitemobs.getloot") || args[0].equalsIgnoreCase("gl") && !commandSender.hasPermission("elitemobs.getloot")) {
                Player player = (Player) commandSender;
                if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                    player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.getloot");
                } else {
                    player.sendMessage("You do not have the permission " + "elitemobs.getloot");
                }
                return true;
            } else if (args[0].equalsIgnoreCase("killall") && args[1].equalsIgnoreCase("aggressiveelites")) {
                if (commandSender.hasPermission("elitemobs.killall.aggressiveelites")) {
                    int counter = 0;
                    for (World world : EliteMobs.worldList) {
                        for (LivingEntity livingEntity : world.getLivingEntities()) {
                            if (livingEntity.hasMetadata(MetadataHandler.ELITE_MOB_MD) && ValidAgressiveMobFilter.ValidAgressiveMobFilter(livingEntity)) {
                                livingEntity.remove();
                                counter++;
                            }
                        }
                    }
                    commandSender.sendMessage("Killed " + counter + " aggressive EliteMobs.");
                    return true;
                } else if (commandSender instanceof Player) {
                    Player player = ((Player) commandSender);
                    if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                        player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.killall.aggressiveelites");
                    } else {
                        player.sendMessage("You do not have the permission " + "elitemobs.killall.aggressiveelites");
                    }
                }
            } else if (args[0].equalsIgnoreCase("killall") && args[1].equalsIgnoreCase("passiveelites")) {
                if (commandSender.hasPermission("elitemobs.killall.passiveelites")) {
                    for (World world : EliteMobs.worldList) {
                        for (LivingEntity livingEntity : world.getLivingEntities()) {
                            if (livingEntity.hasMetadata(MetadataHandler.PASSIVE_ELITE_MOB_MD) && ValidPassiveMobFilter.ValidPassiveMobFilter(livingEntity)) {
                                livingEntity.remove();
                            }
                        }
                    }
                    return true;
                } else if (commandSender instanceof Player) {
                    Player player = ((Player) commandSender);
                    if (Bukkit.getPluginManager().getPlugin(MetadataHandler.ELITE_MOBS).getConfig().getBoolean("Use titles to warn players they are missing a permission")) {
                        player.sendTitle("I'm afraid I can't let you do that, " + player.getDisplayName() + ".", "You need the following permission: " + "elitemobs.killall.passiveelites");
                    } else {
                        player.sendMessage("You do not have the permission " + "elitemobs.killall.passiveelites");
                    }
                }
            }
            validCommands(commandSender);
            return true;
        // /elitemobs giveloot [player] [loot]
        case 3:
            if (commandSender instanceof ConsoleCommandSender || commandSender instanceof Player && commandSender.hasPermission("elitemobs.giveloot")) {
                if (args[0].equalsIgnoreCase("giveloot")) {
                    if (validPlayer(args[1])) {
                        Player receiver = Bukkit.getServer().getPlayer(args[1]);
                        GetLootCommandHandler getLootCommandHandler = new GetLootCommandHandler();
                        if (args[2].equalsIgnoreCase("random") || args[2].equalsIgnoreCase("r")) {
                            Random random = new Random();
                            int index = random.nextInt(lootList.size());
                            ItemStack itemStack = new ItemStack(lootList.get(index));
                            receiver.getInventory().addItem(itemStack);
                            return true;
                        } else if (getLootCommandHandler.getLootHandler(receiver, args[2])) {
                            return true;
                        } else if (!getLootCommandHandler.getLootHandler(receiver, args[2])) {
                            if (commandSender instanceof ConsoleCommandSender) {
                                getLogger().info("Can't give loot to player - loot not found.");
                                return true;
                            } else if (commandSender instanceof Player) {
                                Player player = (Player) commandSender;
                                player.sendTitle("Can't give loot to player - loot not found.", "");
                                return true;
                            }
                        }
                    } else {
                        if (commandSender instanceof ConsoleCommandSender) {
                            getLogger().info("Can't give loot to player - player not found.");
                            return true;
                        } else if (commandSender instanceof Player) {
                            Player player = (Player) commandSender;
                            player.sendTitle("Can't give loot to player - player not found.", "");
                            return true;
                        }
                    }
                }
            }
            validCommands(commandSender);
            return true;
        //invalid commands
        default:
            validCommands(commandSender);
            return true;
    }
}
Also used : Player(org.bukkit.entity.Player) EliteDropsHandler(com.magmaguy.elitemobs.elitedrops.EliteDropsHandler) World(org.bukkit.World) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) LivingEntity(org.bukkit.entity.LivingEntity) Random(java.util.Random) LootCustomConfig(com.magmaguy.elitemobs.config.LootCustomConfig) ItemStack(org.bukkit.inventory.ItemStack)

Example 10 with ConsoleCommandSender

use of org.bukkit.command.ConsoleCommandSender in project Glowstone by GlowstoneMC.

the class SummonCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!testPermission(sender))
        return true;
    if (sender instanceof ConsoleCommandSender) {
        sender.sendMessage("This command can only be executed by a player or via command blocks.");
        return true;
    }
    Location location = null;
    if (sender instanceof Player) {
        location = ((Player) sender).getLocation().clone();
    } else if (sender instanceof BlockCommandSender) {
        location = ((BlockCommandSender) sender).getBlock().getLocation().clone();
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    if (args.length >= 4) {
        location = CommandUtils.getLocation(location, args[1], args[2], args[3]);
    }
    if (location == null) {
        return false;
    }
    location.setYaw(0.0f);
    location.setPitch(0.0f);
    CompoundTag tag = null;
    if (args.length >= 5) {
        String data = String.join(" ", new ArrayList<>(Arrays.asList(args)).subList(4, args.length));
        try {
            tag = Mojangson.parseCompound(data);
        } catch (MojangsonParseException e) {
            sender.sendMessage(ChatColor.RED + "Invalid Data Tag: " + e.getMessage());
        }
    }
    String entityName = args[0];
    if (!checkSummon(sender, entityName)) {
        return true;
    }
    GlowEntity entity;
    if (EntityType.fromName(entityName) != null) {
        entity = (GlowEntity) location.getWorld().spawnEntity(location, EntityType.fromName(entityName));
    } else {
        Class<? extends GlowEntity> clazz = EntityRegistry.getCustomEntityDescriptor(entityName).getEntityClass();
        entity = ((GlowWorld) location.getWorld()).spawn(location, clazz, CreatureSpawnEvent.SpawnReason.CUSTOM);
    }
    if (tag != null) {
        EntityStorage.load(entity, tag);
    }
    sender.sendMessage("Object successfully summoned.");
    return true;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) MojangsonParseException(net.glowstone.util.mojangson.ex.MojangsonParseException) GlowEntity(net.glowstone.entity.GlowEntity) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Aggregations

ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)10 Player (org.bukkit.entity.Player)5 World (org.bukkit.World)3 ArrayList (java.util.ArrayList)2 Location (org.bukkit.Location)2 BlockCommandSender (org.bukkit.command.BlockCommandSender)2 LivingEntity (org.bukkit.entity.LivingEntity)2 Test (org.junit.Test)2 com.cypherx.xauth.xAuthPlayer (com.cypherx.xauth.xAuthPlayer)1 MetadataHandler (com.magmaguy.elitemobs.MetadataHandler)1 LootCustomConfig (com.magmaguy.elitemobs.config.LootCustomConfig)1 EliteDropsHandler (com.magmaguy.elitemobs.elitedrops.EliteDropsHandler)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 GlowEntity (net.glowstone.entity.GlowEntity)1 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)1