Search in sources :

Example 1 with Achievement

use of org.bukkit.Achievement in project Bukkit by Bukkit.

the class AchievementCommand method execute.

@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender))
        return true;
    if (args.length < 2) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    if (!args[0].equalsIgnoreCase("give")) {
        sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
        return false;
    }
    String statisticString = args[1];
    Player player = null;
    if (args.length > 2) {
        player = Bukkit.getPlayer(args[1]);
    } else if (sender instanceof Player) {
        player = (Player) sender;
    }
    if (player == null) {
        sender.sendMessage("You must specify which player you wish to perform this action on.");
        return true;
    }
    if (statisticString.equals("*")) {
        for (Achievement achievement : Achievement.values()) {
            if (player.hasAchievement(achievement)) {
                continue;
            }
            PlayerAchievementAwardedEvent event = new PlayerAchievementAwardedEvent(player, achievement);
            Bukkit.getServer().getPluginManager().callEvent(event);
            if (!event.isCancelled()) {
                player.awardAchievement(achievement);
            }
        }
        Command.broadcastCommandMessage(sender, String.format("Successfully given all achievements to %s", player.getName()));
        return true;
    }
    Achievement achievement = Bukkit.getUnsafe().getAchievementFromInternalName(statisticString);
    Statistic statistic = Bukkit.getUnsafe().getStatisticFromInternalName(statisticString);
    if (achievement != null) {
        if (player.hasAchievement(achievement)) {
            sender.sendMessage(String.format("%s already has achievement %s", player.getName(), statisticString));
            return true;
        }
        PlayerAchievementAwardedEvent event = new PlayerAchievementAwardedEvent(player, achievement);
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            sender.sendMessage(String.format("Unable to award %s the achievement %s", player.getName(), statisticString));
            return true;
        }
        player.awardAchievement(achievement);
        Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
        return true;
    }
    if (statistic == null) {
        sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
        return true;
    }
    if (statistic.getType() == Type.UNTYPED) {
        PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1);
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
            return true;
        }
        player.incrementStatistic(statistic);
        Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
        return true;
    }
    if (statistic.getType() == Type.ENTITY) {
        EntityType entityType = EntityType.fromName(statisticString.substring(statisticString.lastIndexOf(".") + 1));
        if (entityType == null) {
            sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
            return true;
        }
        PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1, entityType);
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
            return true;
        }
        try {
            player.incrementStatistic(statistic, entityType);
        } catch (IllegalArgumentException e) {
            sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
            return true;
        }
    } else {
        int id;
        try {
            id = getInteger(sender, statisticString.substring(statisticString.lastIndexOf(".") + 1), 0, Integer.MAX_VALUE, true);
        } catch (NumberFormatException e) {
            sender.sendMessage(e.getMessage());
            return true;
        }
        Material material = Material.getMaterial(id);
        if (material == null) {
            sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
            return true;
        }
        PlayerStatisticIncrementEvent event = new PlayerStatisticIncrementEvent(player, statistic, player.getStatistic(statistic), player.getStatistic(statistic) + 1, material);
        Bukkit.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            sender.sendMessage(String.format("Unable to increment %s for %s", statisticString, player.getName()));
            return true;
        }
        try {
            player.incrementStatistic(statistic, material);
        } catch (IllegalArgumentException e) {
            sender.sendMessage(String.format("Unknown achievement or statistic '%s'", statisticString));
            return true;
        }
    }
    Command.broadcastCommandMessage(sender, String.format("Successfully given %s the stat %s", player.getName(), statisticString));
    return true;
}
Also used : EntityType(org.bukkit.entity.EntityType) PlayerStatisticIncrementEvent(org.bukkit.event.player.PlayerStatisticIncrementEvent) Player(org.bukkit.entity.Player) Statistic(org.bukkit.Statistic) Material(org.bukkit.Material) Achievement(org.bukkit.Achievement) PlayerAchievementAwardedEvent(org.bukkit.event.player.PlayerAchievementAwardedEvent)

Example 2 with Achievement

use of org.bukkit.Achievement in project Glowstone by GlowstoneMC.

the class GlowUnsafeValues method tabCompleteInternalStatisticOrAchievementName.

@Override
public List<String> tabCompleteInternalStatisticOrAchievementName(String token, List<String> completions) {
    Statistic[] stats = Statistic.values();
    Achievement[] achievements = Achievement.values();
    List<String> names = new ArrayList<>(stats.length + achievements.length);
    for (Statistic stat : stats) {
        names.add(stat.name());
    }
    for (Achievement achievement : achievements) {
        names.add(achievement.name());
    }
    return StringUtil.copyPartialMatches(token, names, completions);
}
Also used : Statistic(org.bukkit.Statistic) ArrayList(java.util.ArrayList) Achievement(org.bukkit.Achievement)

Aggregations

Achievement (org.bukkit.Achievement)2 Statistic (org.bukkit.Statistic)2 ArrayList (java.util.ArrayList)1 Material (org.bukkit.Material)1 EntityType (org.bukkit.entity.EntityType)1 Player (org.bukkit.entity.Player)1 PlayerAchievementAwardedEvent (org.bukkit.event.player.PlayerAchievementAwardedEvent)1 PlayerStatisticIncrementEvent (org.bukkit.event.player.PlayerStatisticIncrementEvent)1