use of org.bukkit.Statistic in project Denizen-For-Bukkit by DenizenScript.
the class StatisticCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element action = scriptEntry.getElement("action");
dList players = scriptEntry.getdObject("players");
Element statistic = scriptEntry.getElement("statistic");
Element amount = scriptEntry.getElement("amount");
dMaterial material = scriptEntry.getdObject("material");
dEntity entity = scriptEntry.getdObject("entity");
dB.report(scriptEntry, getName(), action.debug() + statistic.debug() + amount.debug() + players.debug() + (material != null ? material.debug() : entity != null ? entity.debug() : ""));
Action act = Action.valueOf(action.asString().toUpperCase());
Statistic stat = Statistic.valueOf(statistic.asString().toUpperCase());
int amt = amount.asInt();
switch(stat.getType()) {
case BLOCK:
case ITEM:
Material mat = material.getMaterial();
switch(act) {
case ADD:
for (dPlayer player : players.filter(dPlayer.class)) {
player.incrementStatistic(stat, mat, amt);
}
break;
case TAKE:
for (dPlayer player : players.filter(dPlayer.class)) {
player.decrementStatistic(stat, mat, amt);
}
break;
case SET:
for (dPlayer player : players.filter(dPlayer.class)) {
player.setStatistic(stat, mat, amt);
}
break;
}
break;
case ENTITY:
EntityType ent = entity.getBukkitEntityType();
switch(act) {
case ADD:
for (dPlayer player : players.filter(dPlayer.class)) {
player.incrementStatistic(stat, ent, amt);
}
break;
case TAKE:
for (dPlayer player : players.filter(dPlayer.class)) {
player.decrementStatistic(stat, ent, amt);
}
break;
case SET:
for (dPlayer player : players.filter(dPlayer.class)) {
player.setStatistic(stat, ent, amt);
}
break;
}
break;
}
}
use of org.bukkit.Statistic in project Denizen-For-Bukkit by DenizenScript.
the class StatisticCommand method parseArgs.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean specified_players = false;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action") && arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
} else if (arg.matchesPrefix("players") && !scriptEntry.hasObject("players") && arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class));
specified_players = true;
} else if (!scriptEntry.hasObject("statistic") && arg.matchesEnum(Statistic.values())) {
scriptEntry.addObject("statistic", arg.asElement());
} else if (!scriptEntry.hasObject("amount") && arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("amount", arg.asElement());
} else if (arg.matchesPrefix("qualifier", "q") && !scriptEntry.hasObject("material") && !scriptEntry.hasObject("entity")) {
if (arg.matchesArgumentType(dMaterial.class)) {
scriptEntry.addObject("material", arg.asType(dMaterial.class));
} else if (arg.matchesArgumentType(dEntity.class)) {
scriptEntry.addObject("entity", arg.asType(dEntity.class));
}
}
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify a valid action!");
}
if (!scriptEntry.hasObject("statistic")) {
throw new InvalidArgumentsException("Must specify a valid Statistic!");
}
if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", new Element(1));
}
Statistic.Type type = Statistic.valueOf(scriptEntry.getElement("statistic").asString().toUpperCase()).getType();
if (type != Statistic.Type.UNTYPED) {
if ((type == Statistic.Type.BLOCK || type == Statistic.Type.ITEM) && !scriptEntry.hasObject("material")) {
throw new InvalidArgumentsException("Must specify a valid " + type.name() + " MATERIAL!");
} else if (type == Statistic.Type.ENTITY && !scriptEntry.hasObject("entity")) {
throw new InvalidArgumentsException("Must specify a valid ENTITY!");
}
}
if (!scriptEntry.hasObject("players") && ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() && !specified_players) {
scriptEntry.addObject("players", new dList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().identify()));
}
if (!scriptEntry.hasObject("players")) {
throw new InvalidArgumentsException("Must specify valid players!");
}
}
use of org.bukkit.Statistic 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;
}
use of org.bukkit.Statistic 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);
}
Aggregations